<template>
	<view class="welcome">
		<view class="welT">基本信息</view>
		<view class="welBox">
			<!-- <input type="hidden" name="ID" value="" id="ID" /> -->
			<l-input title="学号" placeholder="请填写" :value="StuInfoFreshEntity.StuNo" disabled right />
			<l-input title="准考证号" placeholder="请填写" :value="StuInfoFreshEntity.zkzh" disabled right />
			<l-input title="考生号" placeholder="请填写" :value="StuInfoFreshEntity.ksh" disabled right />
			<l-input title="姓名" placeholder="请填写" :value="StuInfoFreshEntity.StuName" disabled right />
			<l-input title="性别" placeholder="请填写" :value="displayListItem(StuInfoFreshEntity, 'GenderNo')" disabled right />
			<l-input title="系别" placeholder="请填写" :value="displayListItem(StuInfoFreshEntity, 'DeptNo')" disabled right />
			<l-input title="专业" placeholder="请填写" :value="StuInfoFreshEntity.MajorNo" disabled right />
			<l-input title="班级" placeholder="请填写" :value="StuInfoFreshEntity.ClassNo" disabled right />
			<l-input title="身份证号" placeholder="请填写" :value="StuInfoFreshEntity.IdentityCardNo" disabled right />
			<l-input title="出生日期" placeholder="请填写" :value="StuInfoFreshEntity.Birthday" disabled right />
			<!-- <l-input title="户口类别" placeholder="请填写" :value="StuInfoFreshEntity.ResidenceNo" disabled right /> -->
			<l-select v-model="StuInfoFreshEntity.ResidenceNo" disabled :range="dataSource.ResidenceNo" title="户口类别" placeholder="请选择" />
			<l-input title="政治面貌" placeholder="请填写" :value="displayListItem(StuInfoFreshEntity, 'PartyFaceNo')" disabled right />
		</view>
		<view class="welT" style="padding-top: 10px;">采集照片信息</view>
		<l-upload
		  v-if="uploadVisiable"
		  ref="upload"
		  :number="1"
		  :size="100"
		  type="image/jpeg,image/jpg"
		  :value.sync="photo"
		/>
		<view class="passwordDes">
			<text>照片要求：1 . 单色背景，正面，免冠近期证件照   2 . 文件格式JPG    3 . 文件大小165320KB以下</text>
		</view>
		<view class="welLine"></view>
		<view class="welT" style="padding-top: 10px;">自然信息</view>
		<view class="welLine"></view>
		<view class="welBox"><l-input v-model="queryData.RegionNo" title="籍贯" placeholder="请填写" right /></view>
		<view class="welT" style="padding-top: 10px;">通讯信息</view>
		<view class="welLine"></view>
		<view class="welBox">
			<l-input v-model="queryData.FamilyAddress" title="家庭住址" placeholder="请填写" right />
			<l-input v-model="queryData.telephone" title="联系电话" placeholder="请填写" right />
		</view>
		<view class="welT" style="padding-top: 10px;">
			家庭成员(直系家属)
			<text class="text-xxl cuIcon cuIcon-add" @click="add('StuInfoFreshFamilyEntities')"></text>
		</view>
		<view class="welBox" id="StuInfoFreshFamily" v-show="queryData.StuInfoFreshFamilyEntities.length != 0">
			<view class="welCon" v-for="(item, i) in queryData.StuInfoFreshFamilyEntities" :key='i'>
				<l-input v-model="item.Name" title="姓名" placeholder="请填写" right />
				<!-- <l-input v-model="item.Relation" title="" placeholder="请填写" right /> -->
				<l-select v-model="item.Relation" :range="dataSource.FamilyRelation" title="与本人关系" placeholder="请选择" />
				<l-select v-model="item.Sex" :range="dataSource.GenderNo" title="性别" placeholder="请选择" />
				<l-input v-model="item.Age" title="年龄" placeholder="请填写" right />
				<!-- <l-input v-model="item.Politicy" title="政治面貌" placeholder="请填写" right /> -->
				<l-select v-model="item.Politicy" :range="dataSource.PartyFaceNo" title="政治面貌" placeholder="请选择" />
				<view class="welDel" @click="del('StuInfoFreshFamilyEntities', i)"><text class="text-xxl cuIcon cuIcon-move"></text></view>
			</view>
		</view>
		<view class="welT" style="padding-top: 10px;">
			紧急联系人
			<text class="text-xxl cuIcon cuIcon-add" @click="add('StuInfoFreshEmergePeopleEntities')"></text>
		</view>
		<view class="welBox" id="StuInfoFreshEmergePeople" v-show="queryData.StuInfoFreshEmergePeopleEntities.length != 0">
			<view class="welCon" v-for="(item, i) in queryData.StuInfoFreshEmergePeopleEntities" :key='i'>
				<l-input v-model="item.Name" title="姓名" placeholder="请填写" right />
				<l-input v-model="item.Relation" title="与本人关系" placeholder="请填写" right />
				<l-input v-model="item.Telephone" title="手机号码" placeholder="请填写" right />
				<l-input v-model="item.Mobile" title="座机号码" placeholder="请填写" right />
				<l-input v-model="item.Address" title="联系地址" placeholder="请填写" right />
				<view class="welDel" @click="del('StuInfoFreshEmergePeopleEntities', i)"><text class="text-xxl cuIcon cuIcon-move"></text></view>
			</view>
		</view>
		<!-- <view class="welT" style="padding-top: 10px;">附件上传</view>
		<uploadFile v-if="uploadVisiable" :number="10" :folderId="queryData.ID" :value="fileList"></uploadFile> -->
		<view class="btn" @click="submit">保存</view>
	</view>
</template>

<script>
import moment from 'moment';
import get from 'lodash/get';
import set from 'lodash/set';
import uploadFile from '@/components/upload-file.vue'
export default {
	components:{
		uploadFile,
	},
	data() {
		return {
			photo:[],
			// fileList:[],
			uploadVisiable:false,
			imgSrc: '',
			scheme: {
				PartyFaceNo: {
					type: 'sourceData',
					dataSource: '1',
					dataSourceId: 'BCdPartyFace,partyface,partyfaceno'
				},
				GenderNo:{
					type: 'dataItem',
					dataType: 'dataDictionary'
				},
				DeptNo:{
					type: 'dataItem',
					dataType: 'dataDictionary'
				},
			},
			userInfo: {},
			dataSource:{
				DeptNo:[],
				GenderNo: Object.values(this.GET_GLOBAL('dataDictionary').usersex).map(t => ({ value: t.value, text: t.text })),
				FamilyRelation: Object.values(this.GET_GLOBAL('dataDictionary').FamilyRelation).map(t => ({ value: t.value, text: t.text })),
				PartyFaceNo:[],
				ResidenceNo:Object.values(this.GET_GLOBAL('dataDictionary').ResidenceNo).map(t => ({ value: t.value, text: t.text })),
			},
			StuInfoFreshFamilyEntities:{
				Name: '',
				Age: '',
				Sex: '1',
				Politicy: '01',
				Relation: '01'
			},
			StuInfoFreshEmergePeopleEntities:{
				Name: '',
				Relation: '',
				Telephone: '',
				Mobile: '',
				Address: ''
			},
			queryData:{
				ID: '',
				RegionNo: '',
				FamilyAddress: '',
				telephone: '',
				Photo: '',
				StuInfoFreshFamilyEntities:[],
				StuInfoFreshEmergePeopleEntities: []
			},
			StuInfoFreshEntity:{}
		};
	},
	methods: {
		async init() {
			// console.log(this.API)
			let _this = this;
			Promise.all([
				this.FETCH_DATASOURCE('BCdPartyFace').then(data => {
					// console.log(data)
					this.dataSource.PartyFaceNo = data.data.map(t => ({
						text: t.partyface,
						value: t.partyfaceno
					}));
				}),
				this.FETCH_DATASOURCE('CdDeptInfo').then(data => {
					this.dataSource.DeptNo = data.data.map(t => ({
						text: t.deptname,
						value: t.deptno
					}));
				}),
				() => {}
			]);
			_this.LOADING('加载数据…');
			_this.HTTP_GET('StuInfoFresh/stuInfoFreshEntity', _this.userInfo.account, '加载数据时出错').then(async(res)=> {
				this.HIDE_LOADING();
				_this.StuInfoFreshEntity = _this.COPY(res.StuInfoFreshEntity)||{};
				_this.queryData.StuInfoFreshFamilyEntities = _this.COPY(res.StuInfoFreshFamilyList);
				_this.queryData.StuInfoFreshEmergePeopleEntities = _this.COPY(res.StuInfoFreshEmergePeopleList);
				_this.queryData.ID = res.StuInfoFreshEntity.ID;
				
				_this.queryData.Photo = res.StuInfoFreshEntity.Photo;
				let resFile = await this.FETCH_FILEINFO(res.StuInfoFreshEntity.Photo)
				let url = ""
				if(resFile&&resFile.F_FolderId){
					url = this.API + '/learun/adms/annexes/wxdown?' + this.URL_QUERY(resFile.F_FolderId, true)
				}
				_this.photo = (res.StuInfoFreshEntity.Photo&&url)?[{url,id:res.StuInfoFreshEntity.Photo}]:[];
				_this.refreshComponent()
				_this.queryData.telephone = res.StuInfoFreshEntity.telephone;
				_this.queryData.FamilyAddress = res.StuInfoFreshEntity.FamilyAddress;
				_this.queryData.RegionNo = res.StuInfoFreshEntity.RegionNo;
				// _this.imgSrc = this.API.slice(0,-1) + res.Url;
			});
		},
		async submit() {
			let validateRes = this.validate()
			if(validateRes.length>0){
				this.CONFIRM('表单验证失败', validateRes.join('\n'))
				return
			}
			
			this.LOADING('正在提交数据…');
			let res = await this.$refs["upload"].uploadImage()
			if(res&&res.length){
				this.queryData.Photo = res[0]["id"];
			}else{
				this.queryData.Photo = ""
			}
			this.HTTP_POST('StuInfoFresh/saveStuInfoFresh', this.queryData).then(res => {
				this.HIDE_LOADING();
				if (res) {
					// this.NAV_TO("/pages/my/newpassword")
					setTimeout(()=>{
						this.TOAST('保存成功');
					},100)
					this.NAV_BACK()
				}
			});
		},
		add(str) {
			let jsons = this.COPY(this[str]);
			this.queryData[str].push(jsons)
			// console.log(this.queryData[str])
		},
		del(str, num) {
			this.queryData[str].splice(num, 1)
		},
		validate(){
			let array = []
			if(!this.photo.length)array.push("[采集照片信息]：不能为空")
			// if(!this.queryData.RegionNo)array.push("[籍贯]：不能为空")
			// if(!this.queryData.FamilyAddress)array.push("[家庭住址]：不能为空")
			if(!this.queryData.telephone||!/^(\+\d{2,3}\-)?\d{11}$/.test(this.queryData.telephone)){
				let str = this.queryData.telephone?"格式错误":"不能为空"
				array.push("[联系电话]：" + str)
			}
			if(!this.queryData.StuInfoFreshFamilyEntities.length||
				!this.queryData.StuInfoFreshFamilyEntities.every((item)=>{
					return item.Name&&(item.Relation||item.Relation===0)&&(item.Sex||item.Sex===0)&&/^[1-9]\d*$/.test(item.Age)&&(item.Politicy||item.Politicy===0)
				})
			)array.push("[家庭成员]：未完善")
			// if(!this.queryData.StuInfoFreshEmergePeopleEntities.length||
			// 	!this.queryData.StuInfoFreshEmergePeopleEntities.every((item)=>{
			// 		return item.Name&&(item.Relation||item.Relation===0)&&item.Telephone&&item.Mobile&&item.Address
			// 	})
			// )array.push("[紧急联系人]：未完善")
			return array
		},
		refreshComponent(){
			// this.uploadVisiable = false
			this.$nextTick(()=>{
				this.uploadVisiable = true
			})
		},
		
		// 显示列表中的标题项
		displayListItem(item, field) {
			const fieldItem = this.scheme[field];
			const value = item[field];
			// console.log(Object.values(this.GET_GLOBAL('dataDictionary').usersex));
			
			switch (fieldItem.type) {
				case 'currentInfo':
				case 'organize':
					return fieldItem.dataType === 'time' ? value : get(this.GET_GLOBAL(fieldItem.dataType), `${value}.name`, '');
					
				case 'dataItem':
					const sex = this.dataSource[field].find(t => t.value === String(value));
					return get(sex, 'text', '');
				
				case 'sourceData':
					const BCdPartyFace = this.dataSource[field].find(t => t.value === String(value));
					return get(BCdPartyFace, 'text', '');
				
				case 'radio':
				case 'select':
					const selectItem = this.dataSource[field].find(t => t.value === String(value));
					return get(selectItem, 'text', '');

				case 'checkbox':
					if (!value || value.split(',').length <= 0) {
						return '';
					}
					const checkboxItems = value.split(',');
					return this.dataSource[field]
						.filter(t => checkboxItems.includes(t.value))
						.map(t => t.text)
						.join('，');

				case 'datetime':
					if (!value) {
						return '';
					}
					return moment(value).format(Number(fieldItem.dateformat) === 0 ? 'YYYY年 M月 D日' : 'YYYY-MM-DD HH:mm');

				default:
					return value === null || value === undefined ? '' : value;
			}
		}
	},
	created() {
		this.userInfo = this.GET_GLOBAL('loginUser'); //获取登录信息
		this.init();
	}
};
</script>

<style lang="less" scoped>
/deep/ .cu-form-group {
	min-height: 40px;
}
.welcome {
	padding: 1px 0px 20px;
	background: #fff;
}

.welT {
	font-size: 16px;
	margin-bottom: 10px;
	line-height: 26px;
	padding: 0 12px;
}

.welT text {
	width: 26px;
	height: 26px;
	line-height: 24px;
	border: 1px solid #efefef;
	border-radius: 4px;
	float: right;
	text-align: center;
	color: #999;
}

.welLine {
	height: 1px;
	background: #efefef;
	// margin: 10px -12px;
}

.welInput {
	overflow: hidden;
	font-size: 14px;
	margin-top: 10px;
}

.welInput span {
	float: left;
	width: 26%;
	text-align: right;
	line-height: 30px;
}

.welInput input {
	display: block;
	margin-left: 28%;
	width: 72%;
	border: 1px solid #efefef;
	border-radius: 4px;
	height: 30px;
	line-height: 28px;
	padding: 0 10px;
	margin-bottom: 0;
}

.welCon {
	padding: 10px;
	border: 1px solid #efefef;
	margin-top: 18px;
	border-radius: 4px;
	position: relative;
}

.welDel {
	position: absolute;
	top: -8px;
	left: -8px;
	background: #fff;
}

.welDel text {
	text-align: center;
	width: 26px;
	height: 26px;
	display: block;
	line-height: 24px;
	border: 1px solid #efefef;
	border-radius: 50%;
}

.welImgAdd {
	text-align: center;
	line-height: 0;
}

.passwordDes{
	color: #606266;
	font-size: 14px;
	padding: 8px;
	text-indent:2em;
}

#files {
	opacity: 0;
	-webkit-opacity: 0;
	z-index: 9;
	display: none;
}

#welImgBtn {
	display: block;
	width: 110px;
	height: 30px;
	text-align: center;
	line-height: 26px;
	border: 2px solid #efefef;
	border-radius: 4px;
	font-size: 14px;
	color: #999;
	margin: 10px auto;
	cursor: pointer;
}
.welImgAdd {
	min-height: 110px;
}
#PhotoImg {
	width: 110px;
	height: 110px;
}

.btnBox {
	margin-top: 20px;
	text-align: center;
}
#saveBtn {
	margin: auto;
	margin-top: 25px;
	width: 92%;
}
#StuInfoFreshFamily,#StuInfoFreshEmergePeople{
	padding: 0 15px;
}
</style>
