<template>
	<view class="admin-jiazheng-page">
		<view class="icon-title-navBar-box">
			<view @click="goUpPage" class="nav-bar-icon"></view>
			<text class="nav-bar-title">家政人员</text>
		</view>
		<v-tabs v-model="current" :tabs="tabs" :scroll="false" @change="changeTab" field="name"
			class="admin-tab-box"></v-tabs>
		<gerenZiliao ref="gerenZiliao" :status='status' @finishDom='finishDom' @editSave="editSaveGerenziliao"
			@goNext="gerenziliaoNext" v-show="current ==0"></gerenZiliao>
		<qiuzhiXinxi ref="qiuzhiXinxi" :status='status' @editSave="editSaveQiuzhixinxi" @goNext="goNextQiuzhixinxi"
			v-show="current ==1">
		</qiuzhiXinxi>
		<zhaopianZiliao ref="zhaopianZiliao" :status='status' @editSave="editSaveZhaopianziliao"
			@goNext="goNextZhaopianZiliao" v-show="current ==2"></zhaopianZiliao>
		<banzhengXinxi ref="banzhengXinxi" :status='status' @addSave="addSave" @editSave="editSave"
			v-show="current ==3">
		</banzhengXinxi>
		<banzhengXinxiList :list="editInfo.kaozhengList" v-show="banzhengXinxiFlag" @editBanzheng="editBanzheng"
			@addBanzheng="addBanzheng" @deleteBanzheng="deleteBanzheng"
			ref="banzhengXinxiList"></banzhengXinxiList>
	</view>
</template>

<script>
	import {
		ref
	} from "vue";
	import banzhengXinxi from './banzhengXinxi.vue';
	import banzhengXinxiList from './banzhengXinxiList.vue';
	import qiuzhiXinxi from './qiuzhiXinxi.vue';
	import zhaopianZiliao from './zhaopianZiliao.vue';
	import gerenZiliao from './gerenZiliao.vue';
	import {
		getJiazhengInfo,
		jiazhengAdd,
		jiazhengUpdate,
		jiazhengKaozhengAdd,
		jiazhengKaozhengDelete
	} from "@/api/jiazheng.js";
	export default {
		data() {
			return {
				current: 0,
				//	tabs: ['个人资料', '求职信息', '照片资料', '办证信息']
				tabs: [],
				allData: {
					"birthday": "",
					"gender": '',
					"hujiDizhi": "",
					"hunyin": '',
					"icon": "",
					"idcard": "",
					"idcardUrl1": "",
					"idcardUrl2": "",
					"idtype": '',
					"jiankangDate": "",
					"jibiexinzi": "",
					"jiguanShengId": '',
					"jiguanShiId": '',
					"jineng": "",
					"jingli": "",
					"jingyan": "",
					"juzhuDizhi": "",
					"kaozhengList": [],
					"leixing": "",
					"minzu": '',
					"qiwangxinzi": "",
					"realName": "",
					"shenghuoUrls": "",
					"shuxiang": '',
					"tijianDate": "",
					"tijianUrls": "",
					"userId": '',
					"userName": "",
					"xingzuo": '',
					"xingzuoFlag": false,
					"xueli": '',
					"zhengshuUrls": "",
					"zhuangtai": ''
				},
				status: '',
				id: '',
				userId: '',
				banzhengXinxiFlag: false,
				editInfo: {}
			}
		},
		components: {
			banzhengXinxi,
			banzhengXinxiList,
			qiuzhiXinxi,
			gerenZiliao,
			zhaopianZiliao,
		},
		onLoad(options) {
			let pageInfo = JSON.parse(options.pageInfo)
			this.status = pageInfo.status
			this.dataHandle(pageInfo)
		},
		methods: {

			editBanzheng(data, index) {
				this.current = 3
				this.banzhengXinxiFlag = false
				this.$refs.banzhengXinxi.editinfo(data, index)
			},
			addBanzheng(data, index) {
				this.banzhengXinxiFlag = false
				this.current = 3
				this.$refs.banzhengXinxi.resetFun()
			},
			deleteBanzheng(data, index) {
				console.log('data', data);
				console.log('index', index);
				jiazhengKaozhengDelete({
					ids: [data.id]
				}).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: "成功",
							icon: 'none',
							duration: 2000
						})
						this.editInfo.kaozhengList.splice(index, 1);
						if (this.editInfo.kaozhengList.length == 0) {
							this.banzhengXinxiFlag = false
							this.current = 3
						}
					}
				})

			},
		
			dataHandle(pageInfo) {
				if (this.status == 'add') {
					this.tabs = [{
							id: 1,
							name: '个人资料',
							disabled: false
						},
						{
							id: 2,
							name: '求职信息',
							disabled: true
						},
						{
							id: 3,
							name: '照片资料',
							disabled: true
						},
						{
							id: 4,
							name: '办证信息',
							disabled: true
						},
					]
				} else {
					this.id = pageInfo.id
					this.userId = pageInfo.userId
					this.tabs = [{
							id: 1,
							name: '个人资料',
							disabled: false
						},
						{
							id: 2,
							name: '求职信息',
							disabled: false
						},
						{
							id: 3,
							name: '照片资料',
							disabled: false
						},
						{
							id: 4,
							name: '办证信息',
							disabled: false
						},
					]

				}
			},
			finishDom() {
				let req = {
					id: this.id,
					userId: this.userId
				}
				getJiazhengInfo(req).then(res => {
					if (res.code == 0) {
						this.$refs.gerenZiliao.editinfo(res.data)
						this.editInfo = JSON.parse(JSON.stringify(res.data));
						this.allData = JSON.parse(JSON.stringify(res.data));
						this.allData.id = this.id
						console.log('this.allData this.allData ', this.allData);
					} else {
						uni.showToast({
							icon: 'none',
							title: '获取详情失败'
						})
						return false
					}
				})
			},
			changeTab(data) {
				console.log('data', data);
				if (this.status == 'edit' && data == 3 && this.editInfo.kaozhengList.length > 0) {
					this.banzhengXinxiFlag = true
					return false
				} else {
					this.banzhengXinxiFlag = false
				}

				if (this.status == 'add') {
					if (this.current >= 0) {
						this.current--;
						this.updateTabs();
					}
				}
				this.current = data
				if (this.status != 'edit') {
					return false
				}
				switch (Number(data)) {
					case 0:
						this.$refs.gerenZiliao.editinfo(this.editInfo)
						break;
					case 1:
						this.$refs.qiuzhiXinxi.editinfo(this.editInfo)
						break;
					case 2:
						this.$refs.zhaopianZiliao.editinfo(this.editInfo)
						break;
					case 3:
						this.$refs.banzhengXinxi.editinfo()
						break;
					default:
						break;
				}


			},
			gerenZiliaoDataHandle(data, flag) {
				console.log('gerenZiliaoDataHandle', data);
				console.log('gerenZiliaoDataHandle', data.hunyin.id);
				this.allData.userName = data.userName
				this.allData.realName = data.realName
				this.allData.idcard = data.idcard
				this.allData.idtype = data.idtype
				this.allData.jiguanShengId = data.jiguanObj.jiguanShengId
				this.allData.jiguanShiId = data.jiguanObj.jiguanShiId
				this.allData.juzhuDizhi = data.juzhuDizhi
				this.allData.birthday = data.birthday
				this.allData.minzu = data.minzu.id
				this.allData.gender = data.gender
				this.allData.xueli = data.xueli.id
				this.allData.hunyin = data.hunyin.id
				this.allData.hujiDizhi = data.hujiDizhi
				this.allData.xingzuo = data.xingzuo.id
				this.allData.shuxiang = data.shuxiang.id
				this.allData.xingzuoFlag = data.xingzuoFlag
				if (flag == 'edit') {
					jiazhengUpdate(this.allData).then(res => {
						uni.showToast({
							title: "保存成功！",
							icon: 'none',
							duration: 2000
						})
					})
				}
			},
			gerenziliaoNext(data, num) {
				this.gerenZiliaoDataHandle(data)
				// this.current = num + 1
				// this.tabs = this.tabs.map((item, i) => {
				// 	if (i == this.current) item.disabled = false
				// 	return item
				// })
				if (this.current < this.tabs.length - 1) {
					this.current++;
					this.updateTabs();
				}

			},
			editSaveGerenziliao(data) {
				this.gerenZiliaoDataHandle(data, 'edit')
			},
			qiuzhiXinxiDataHandle(data, flag) {
				this.allData.leixing = data.leixing
				this.allData.jingyan = data.jingyan
				this.allData.jineng = data.jineng
				this.allData.zhuangtai = data.zhuangtai
				this.allData.jibiexinzi = data.jibiexinzi
				this.allData.qiwangxinzi = data.qiwangxinzi
				this.allData.jingli = data.jingli
				if (flag == 'edit') {
					jiazhengUpdate(this.allData).then(res => {
						uni.showToast({
							title: "保存成功！",
							icon: 'none',
							duration: 2000
						})
					})
				}
			},
			goNextQiuzhixinxi(data, num) {
				this.qiuzhiXinxiDataHandle(data)

				if (this.current < this.tabs.length - 1) {
					this.current++;
					this.updateTabs();
				}



				// this.current = num + 1
				// this.tabs = this.tabs.map((item, i) => {
				// 	if (i == this.current) item.disabled = false
				// 	return item
				// })
			},
			updateTabs() {
				console.log('this.tabsthis.tabs', this.tabs);
				console.log('this.current', this.tabs);

				this.tabs = this.tabs.map((item, i) => {
					// 当前页签及之前的页签 disabled 为 false，之后的页签 disabled 为 true
					item.disabled = i >= this.current;
					return item;
				});
			},
			editSaveQiuzhixinxi(data) {
				this.qiuzhiXinxiDataHandle(data, 'edit')
			},

			goNextZhaopianZiliao(data, num) {
				this.zhaopianziliaoDataHandle(data)
				// this.current = num + 1
				// this.tabs = this.tabs.map((item, i) => {
				// 	if (i == this.current) item.disabled = false
				// 	return item
				// })
				if (this.current < this.tabs.length - 1) {
					this.current++;
					this.updateTabs();
				}

			},
			zhaopianziliaoDataHandle(data, flag) {
				this.allData.icon = data.icon
				this.allData.idcardUrl1 = data.idcardUrl1
				this.allData.idcardUrl2 = data.idcardUrl2
				this.allData.shenghuoUrls = data.shenghuoUrls
				this.allData.zhengshuUrls = data.zhengshuUrls
				this.allData.tijianDate = data.tijianDate
				this.allData.jiankangDate = data.jiankangDate
				this.allData.tijianUrls = data.tijianUrls
				if (flag == 'edit') {
					jiazhengUpdate(this.allData).then(res => {
						uni.showToast({
							title: "保存成功！",
							icon: 'none',
							duration: 2000
						})
					})
				}
			},
			editSaveZhaopianziliao(data) {
				this.zhaopianziliaoDataHandle(data, 'edit')
			},
			addSave(data) {
				if (data != '') {
					this.allData.kaozhengList.push(data)
				}
				if (this.status == 'add') {
					this.submitJiazheng()
				} else {

				}


			},
			submitJiazheng() {
				console.log('this.allData', this.allData);
				if (this.allData.kaozhengList.length > 0 && this.allData.icon == '') {
					uni.showToast({
						title: "当录入办证信息时候，头像必填",
						icon: 'none',
						duration: 2000
					})
					return false
				}
				jiazhengAdd(this.allData).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: "成功",
							icon: 'none',
							duration: 2000
						})
						uni.navigateTo({
							url: `/pages/admin/Jiazheng/index`
						})
					} else {
						uni.showToast({
							title: "失败",
							icon: 'none',
							duration: 2000
						})
						return false
					}

				})

			},
			editSave(data) {
				debugger
				console.log('this.allData', this.allData);
				console.log('this.editInfo', this.editInfo);
				if (data == '' && this.status == 'edit') {
					jiazhengUpdate(this.allData).then(res => {
						uni.showToast({
							title: "保存成功！",
							icon: 'none',
							duration: 2000
						})
					})
				} else if (data && this.status == 'edit') {
					data.userId = this.userId
					if (this.allData.icon == '') {
						uni.showToast({
							title: "当录入办证信息时候，头像必填",
							icon: 'none',
							duration: 2000
						})
						return false
					}
					jiazhengKaozhengAdd(data).then(res => {
						if (res.code == 0) {
							data.id = res.data
							this.allData.kaozhengList.push(data)
							this.editInfo.kaozhengList.push(data)
							console.log('this.editInfo.kaozhengList', this.editInfo.kaozhengList);
							this.banzhengXinxiFlag = true
						}
					})

				}
				// if (data != '') {
				// 	this.allData.kaozhengList.push(data)
				// }
				// if(this.status =='add'){
				// 	this.submitJiazheng()
				// }else{

				// }
			},
			goUpPage() {
				uni.navigateTo({
					url: `/pages/admin/Jiazheng/index`
				})
			}

		}
	}
</script>