<template>
	<view class="box">
		<u-navbar :autoBack="true" title="志愿报名" bgColor="rgba(255,255,255,0)" :placeholder="true" titleStyle="font-weight:bold;color:#000000"></u-navbar>
		<view class="ban_box">
			<view class="ban_item2">
				<image class="top_img" src="https://huli-app.wenlvti.net/app_static/WenWuGuanJia/image/bm_top.png"></image>
			</view>
			<view class="ban_item1">
				<image class="top_img" src="https://huli-app.wenlvti.net/app_static/WenWuGuanJia/image/dt_ban2.png"></image>
			</view>
		</view>



		<!-- 表单 -->
		<view class="example">
			<uni-forms style="padding: 0 20rpx 0 20rpx" label-position="top" ref="valiForm" :rules="rules" :modelValue="valiFormData">
				<uni-forms-item label="您的姓名" label-width="80px" required name="name">
					<uni-easyinput v-model="valiFormData.name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="您的联系电话" label-width="100px" required name="mobile">
					<uni-easyinput type="number" v-model="valiFormData.mobile" placeholder="请输入联系电话" />
				</uni-forms-item>

				<uni-forms-item label="您的身份证" label-width="100px" required name="id_card">
					<uni-easyinput type="idcard" v-model="valiFormData.id_card" placeholder="请输入身份证号" />
				</uni-forms-item>
				<uni-forms-item label="您的区域" label-width="100px" required name="id_card">
					<uni-data-picker :localdata="regionList" popup-title="请选择班级" @change="onchange" @nodeclick="onnodeclick"></uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="住址" label-width="100px" required name="address">
					<uni-easyinput v-model="valiFormData.address" placeholder="请输入住址" />
				</uni-forms-item>
				<uni-forms-item label="单位" label-width="80px" required name="unit_name">
					<uni-easyinput v-model="valiFormData.unit_name" placeholder="请输入单位名称" />
				</uni-forms-item>
				<!-- 		<uni-forms-item label="备注" label-width="80px" required name="notesVal">
					<uni-easyinput v-model="valiFormData.notesVal" placeholder="请输入申请类型(个人、家庭、机构)" />
				</uni-forms-item> -->

				<uni-forms-item label="认领原因" label-width="80px" required name="intro">
					<uni-easyinput type="textarea" v-model="valiFormData.intro" placeholder="请输入认领原因或个人介绍(可上传个人优秀证明、奖项)" />
					<view @click="chooseFile" class="scfj">
						<view>上传文件</view>
						<view>
							<uni-icons type="wallet" size="18"></uni-icons>
						</view>
					</view>
				</uni-forms-item>
			</uni-forms>
		</view>
		<view v-if="valiFormData.status == '-1'" class="text-wrapper_3" @click="modifySubmit('valiForm')">
			<view class="bm_tit">确认修改</view>
		</view>
		<view v-else class="text-wrapper_3" @click="submit('valiForm')">
			<view class="bm_tit">报名</view>
		</view>
	</view>
</template>

<script>
let that;
import { mapGetters } from 'vuex'
export default {
	data() {
		return {
			regionList: [],
			region_id: '',
			cr_id: '' /* 文物id */,
			modifyId: '' /* 修改资料的文物id */,
			volunteer_id: '' /* 志愿者id */,
			/* 表单数据 */
			valiFormData: {
				name: '',
				mobile: '',
				address: '',
				id_card: '',
				unit_name: '',
				fullurl: '',
				intro: ''
			},

			/* 校验规则 */
			rules: {
				// 对name字段进行必填验证
				name: {
					// name 字段的校验规则
					rules: [
						// 校验 name 不能为空
						{
							required: true,
							errorMessage: '请输入姓名'
						},
						// 对name字段进行长度验证
						{
							minLength: 1,
							maxLength: 6,
							errorMessage: '{label}长度在 {minLength} 到 {maxLength} 个字符'
						}
					],
					// 当前表单域的字段中文名，可不输入
					label: '姓名',
					validateTrigger: 'submit'
				},
				/* 手机号校验 */
				mobile: {
					// mobile 字段的校验规则
					rules: [
						// 校验 mobile 不能为空
						{
							required: true,
							errorMessage: '请输入联系电话'
						},
						// 对mobile字段进行长度验证
						{
							minLength: 11,
							maxLength: 11,
							errorMessage: '{label}长度为 {minLength}  个字符'
						}
					],
					// 当前表单域的字段中文名，可不输入
					label: '手机号',
					validateTrigger: 'submit'
				},
				/* 身份证校验 */
				id_card: {
					// idCard 字段的校验规则
					rules: [
						// 校验 idCard 不能为空
						{
							required: true,
							errorMessage: '请输入身份证号'
						}
						// 对idCard字段进行长度验证
						// {
						// 	minLength: 18,
						// 	maxLength: 18,
						// 	errorMessage: '{label}长度为 {minLength}  个字符'
						// }
					],
					// 当前表单域的字段中文名，可不输入
					label: '身份证号',
					validateTrigger: 'submit'
				},

				/* 单位 */
				unit_name: {
					rules: [
						// 校验 unit 不能为空
						{
							required: true,
							errorMessage: '请输入单位名称'
						}
					]
				},

				address: {
					rules: [
						// 校验 住址 不能为空
						{
							required: true,
							errorMessage: '请输入单位名称'
						}
					]
				},
				// notesVal: {
				// 	rules: [
				// 		// 校验 申请类型 不能为空
				// 		{
				// 			required: true,
				// 			errorMessage: '请输入申请类型'
				// 		}
				// 	]
				// },
				intro: {
					rules: [
						{
							required: true,
							errorMessage: '请输入认领原因或个人介绍'
						}
					]
				}
			}
		};
	},
	onLoad(o) {
		that = this;
		this.cr_id = o.id;
		this.volunteer_id = o.volunteer_id;
		this.modifyId = o.modifyId;
		this.details();
		this.getCategoryCascadeList();
    if (this.isLogin) {
      console.log('111');
      
    }
	},
  computed: {
    ...mapGetters(['isLogin'])
  },
	methods: {
		onchange(e) {
			const value = e.detail.value;
		},
		onnodeclick(node) {
			this.region_id = node.value;
			console.log(node);
		},
		// 区域
		getCategoryCascadeList() {
			this.$api.getCategoryCascadeList({ main_body_id: 1, pid: 5, level: 2 }, function (res) {
				that.regionList = res.data;
			});
		},

		// 测试上传文件
		chooseFile() {
			let userToken = '';
			let auth = this.$db.get('auth');
			userToken = auth.token;
			wx.chooseMessageFile({
				count: 1,
				type: 'file', // 修改为支持的文档类型
				// 配置后导致读取不到聊天文件
				// extension: ['.doc', '.xlsx', '.docx', '.ppt'],
				success(res) {
					const tempFilePaths = res.tempFiles;
					uni.uploadFile({
						url: that.$config.baseUrl + 'api/common/upload?token=' + userToken,
						filePath: tempFilePaths[0].path,
						name: 'file',
						header: {
							'content-type': 'multipart/form-data' // 根据实际情况设置请求头
						},
						formData: {}, // 如果需要，添加额外的form数据
						success: (res) => {
							console.log('上传成功，服务器响应数据:', res.data);

							let url = JSON.parse(res.data);
							that.valiFormData.fullurl = url.data.fullurl;
							// console.log('生成的链接', that.valiFormData.fullurl);
							that.$common.errorToShow('上传成功');
						},
						fail: (err) => {
							console.error('文件上传失败:', err);
						}
					});
				}
			});
		},

		submit() {
			this.$refs.valiForm.validate(['id'], async (err, valiFormData) => {
				if (!err) {
					console.log('校验成功');
					/* 检验成功 */
					const { name, mobile, address, id_card, unit_name, intro, fullurl } = this.valiFormData;
					this.$api.applyVolunteer(
						{
							main_body_id: 1,
							name: name,
							mobile: mobile,
							region_id: this.region_id,
							address: address,
							unit_name: unit_name,
							id_card: id_card,
							intro: intro,
							cr_id: this.cr_id,
							file: fullurl
						},
						function (res) {
							if (res.code === 1) {
								that.$common.errorToShow(res.msg);
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/shouhu/shouhu'
									});
								}, 1000);
							} else {
								that.$common.errorToShow(res.msg);
							}
							// console.log(res);
						}
					);
				} else {
					that.$common.errorToShow('请完善信息');
				}
			});
		},
		// 志愿者详情判断是否为被驳回
		details() {
			this.$api.details({ main_body_id: 1 }, function (res) {
				// 被驳回
				if (res.data != null && res.data.status == '-1') {
					// console.log(res);
					that.valiFormData = res.data;

					// console.log(that.valiFormData, '被驳回');
				}
				if (res.data != null && res.data.status == '2') {
					that.$common.errorToShow('您已经是志愿者，正在前往守护');
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/shouhu/shouhu'
						});
					}, 3000);
				}
				if (res.data != null && res.data.status == '0') {
					that.$common.errorToShow('您的资料正在审核中');
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/shouhu/shouhu'
						});
					}, 3000);
				}
			});
		},
		// 修改资料
		modifySubmit() {
			this.$refs.valiForm.validate(['id'], async (err, valiFormData) => {
				if (!err) {
					console.log('校验成功');
					/* 检验成功 */
					const { name, mobile, address, id_card, unit_name, intro } = this.valiFormData;
					this.$api.editApplyVolunteer(
						{
							main_body_id: 1,
							name: name,
							mobile: mobile,
							address: address,
							unit_name: unit_name,
							id_card: id_card,
							intro: intro,
							volunteer_id: this.volunteer_id || this.valiFormData.id,
							cr_id: this.cr_id || this.valiFormData.cr_list[0].id
						},
						function (res) {
							if (res.code === 1) {
								that.$common.errorToShow(res.msg);
							} else {
								that.$common.errorToShow(res.msg);
							}
							// console.log(res);
						}
					);
				} else {
					that.$common.errorToShow('请完善信息');
				}
			});
		}
	}
};
</script>

<style>
.box {
	width: 100%;
	padding-bottom: 50rpx;
	background-image: url('https://huli-app.wenlvti.net/app_static/WenWuGuanJia/image/xy_bgt.png');
	background-size: 100% 100%;
	background-repeat: repeat-y;
	height: auto;
}
.ban_box {
	position: relative;
	margin-top: 40rpx;
}

.top_img {
	width: 100%;
	height: 100%;
}
.ban_item1 {
	position: absolute;
	top: -20rpx;
	left: 233rpx;
	width: 290rpx;
	height: 290rpx;
	margin: auto;
}
.ban_item2 {
	width: 250rpx;
	height: 250rpx;
	margin: auto;
}
/deep/.uni-forms-item__error {
	color: red !important;
}
.is-input-border {
	background-color: #f7dfc0 !important;
}
.uni-forms-item__label {
	color: #000000 !important;
}

.text-wrapper_3 {
	margin: auto;
	margin-top: 50rpx;
	height: 80rpx;
	flex-direction: column;
	width: 240rpx;
	background: url('/static/img/dt_bg2.png') no-repeat;
	background-size: 100% 100%;
}
.bm_tit {
	text-align: center;
	font-size: 36rpx;
	line-height: 80rpx;
	letter-spacing: 6rpx;
	text-align: center;
	font-weight: 700;
	background: linear-gradient(180deg, #af7e44 0%, #934b36 100%);
	-webkit-background-clip: text;
	-webkit-text-fill-color: transparent;
}
.example {
	height: 87%;
	margin: 20rpx 32rpx 0 32rpx;
}
.uni-forms {
	padding: 0 20rpx 0 20rpx;
}
.uni-forms-item {
	margin-bottom: 30rpx !important;
}
.active {
	border: 6rpx solid #efb57a;
	border-radius: 10rpx;
}
.img {
	width: 230rpx;
	height: 250rpx;
	border: 6rpx solid #efb57a;
	border-radius: 10rpx;
}
.map_tit {
	display: flex;
	align-items: center;
	margin-left: 60rpx;
	margin-top: 40rpx;
	font-size: 40rpx;
	font-family: Songti SC, Songti SC;
	font-weight: 900;
	line-height: 52rpx;
	color: #444444;
}
.scarch_box {
	width: 430rpx;
	height: 82rpx;
	padding: 15rpx 0 0 30rpx;
	/* background-image: url('/static/img/search_bg1.png');
	background-size: 100% 100%; */
}
.rl_box {
	width: 90%;
	margin: auto;
	margin-top: 40rpx;
	display: flex;
	justify-content: space-between;
}
.rl_item {
	width: 188rpx;
	height: 102rpx;
	background-image: url('/static/img/rl_bg.png');
	background-size: 100% 100%;
}
.tit {
	text-align: center;
	line-height: 102rpx;
	font-weight: 700;
	font-size: 36rpx;
	letter-spacing: 6rpx;
	background: linear-gradient(180deg, #af7e44 0%, #934b36 100%);
	-webkit-background-clip: text;
	-webkit-text-fill-color: transparent;
}

.scfj {
	width: 170rpx;
	height: 50rpc;
	line-height: 50rpx;
	align-items: center;
	display: flex;
	background-color: #f7dfc0;
	position: absolute;
	top: -60rpx;
	right: 2rpx;
	border-radius: 5rpx;
	justify-content: space-around;
}
.tit2 {
	text-align: center;
	line-height: 66rpx;
	font-weight: 700;
	font-size: 32rpx;
	letter-spacing: 6rpx;
	background: linear-gradient(180deg, #af7e44 0%, #934b36 100%);
	-webkit-background-clip: text;
	-webkit-text-fill-color: transparent;
}
.is-input-error-border {
	border: none !important;
}

.xx_box {
	padding: 20rpx;
	background-color: #e1bf9a;
	width: 660rpx;
	height: 560rpx;
	padding-top: 80rpx;
}
.xx_tit {
	height: 360rpx;
	font-size: 30rpx;
	padding: 20rpx;
	text-indent: 2em;
	background-color: #f3e3d3;
	overflow: scroll;
}
</style>