<template>
	<view class="total-page page-box task-bg">
		<Nav :title="productInfo.product_name" :genre="1" is_fixed></Nav>
		<view class="task-tabs" :style="{top:`${$tools.topHeight()}px`}">
			<view class="m-t20">
				<view class="creation_item row-justify-sb center sys-background-fff m-lr30 m-b20 p-30 r-20"
					v-for="(item,index) in informationList" :key="index" v-show="item.is_show"
					@click="onAuthentication(item)">
					<view class="row-c">
						<image class="wh-80 m-r20 r-100" :src="item.icon" mode="aspectFill"></image>
						<view>
							<text class="size-28 text-color-12 sys-weight-600">{{item.name}}</text>
							<view class="size-24 text-color-999 m-t10">{{item.explain}}</view>
						</view>
					</view>
					<view class="">
						<button class="button-background info_button sys-weight-500 text-color-fff size-26 r-100"
							v-if="item.is_finish">已完成</button>
						<button class="sys-bg-CBCBCB info_button sys-weight-500 text-color-fff size-26 r-100"
							v-else>待上传</button>
					</view>
				</view>
			</view>
		</view>
		<EnButton :is_both="1" :leftText="'保存草稿'" :rightText="'提交资料'" @onLeftSubmit="addDraft" @onSubmit="addTask">
		</EnButton>
	</view>
</template>

<script>
	import TaskImgTab from "@/common/task/task_ima_tab.vue"
	import tools from "@/service/tools";
	import {
		addDraft,
		addTask,
		delBusinessCache,
		getBusinessCache,
		getClientInfo,
		getDraftItem,
		getTaskDetail,
		getTaskTypeInfo,
		setBusinessCache,
		verifyBusinessModuleKey
	} from "@/api/task";

	export default {
		components: {
			TaskImgTab,
		},
		data() {
			return {
				title: '',
				informationList: [{
					name: '身份信息',
					explain: '身份证、婚姻、职业、社保信息',
					icon: 'https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/new-xcx/task/task-identity.png',
					path: '/page_task/identity/identity',
					type: 1,
					is_finish: false,
					is_show: false,
					type_keys: []
				}, {
					name: '资产信息',
					explain: '房产、车辆等资产',
					icon: 'https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/new-xcx/task/task-property.png',
					path: '/page_task/property/property',
					type: 2,
					is_finish: false,
					is_show: false,
					type_keys: []
				}, {
					name: '申请信息',
					explain: '贷款额度信息',
					icon: 'https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/new-xcx/task/task-apply.png',
					path: '/page_task/apply/apply',
					type: 3,
					is_finish: false,
					is_show: false,
					type_keys: []
				}, {
					name: '第三方信息',
					explain: '第三方企业/人员相关信息',
					icon: 'https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/new-xcx/task/task-three.png',
					path: '/page_task/tripartite_info/tripartite_info',
					type: 4,
					is_finish: false,
					is_show: false,
					type_keys: []
				}, {
					name: '联系人信息',
					explain: '其他联系人',
					icon: 'https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/new-xcx/task/task-contacts.png',
					type: 5,
					path: '/page_task/linkman/linkman',
					is_finish: false,
					is_show: false,
					type_keys: []
				}, {
					name: '征信信息',
					explain: '银行征信信息',
					icon: 'https://wealfavor-1257406827.cos.ap-beijing.myqcloud.com/new-xcx/task/task-credit.png',
					path: '/page_task/credit_upload/credit_upload',
					type: 6,
					is_finish: false,
					is_show: false,
					type_keys: []
				}, ],
				stepsNum: 0,
				stepsKey: 0,
				productInfo: {
					id: 1,
					product_name: "",
					product_types: '',
					field_ids: [],
					type_data: [],
				},
				taskData: {
					'productId': '',
					"clientInfo": {
						"name": "",
						"birthday": "",
						"sex": 1,
						"id_number": "",
						"identity_one": "",
						"identity_two": "",
						"phone": "",
						"marriage_type": 1,
						"education_type": '',
						"education_name": '',
						"employ_type": 1,
						"firm_name": "",
						"firm_address": "",
						"resident_city": "",
						"resident_address": "",
						"mate_name": "",
						"mate_phone": "",
						"mate_id_number": "",
						"mate_firm": "",
						"m_code": "",
						"code": "",
						"m_identity_one": "",
						"m_identity_two": "",
						"firm_city": "",
						"social_num": "",
						"reserved_money": "",
						"marriage_img": [],
						"credit_img": []
					},
					"property": [],
					"applies": {
						"is_farming": '1',
						"loan_form": '',
						"loan_type": '',
						"loan_industry": [],
						"enterprise_type": "",
						"use_date": "",
						"pay_status": '1',
						"quota": "",
						"apply_data": []
					},
					"tripartite": [],
					"linkman": [],
					"credit": {
						'credit_img': [],
						'deal_data': [],
						'overdue_data': [],
						'query_data': [],
					},
					"post_loan": {
						'bank_id': '',
						'loans_money': '',
						'interest_rate': '',
						'repayment_money': '',
						'refund_num': '',
						'repayment_type': '',
						'repayment_date': '',
					},
				},
				isCache: true,
				isNew: true
			}
		},
		watch: {
			'taskData': {
				handler() {
					this.setBusinessCache()
				},
				deep: true
			},
			'productInfo.product_types': function() {
				let productTypes = this.productInfo.product_types
				if (typeof productTypes === 'string') {
					productTypes = productTypes.split(',')
				}
				this.informationList.forEach(item => {
					item.is_show = productTypes.indexOf(item.type + '') >= 0;
					this.productInfo.type_data.forEach(val => {
						if (val.type === item.type + '') {
							item.type_keys = val.type_keys
						}
					})
				})
			}
		},
		onLoad(data) {
			if (data.draftId) {
				this.getDraftItem(data.draftId)
				this.isNew = false
			} else if (data.businessId) {
				this.getTaskDetail(data.businessId)
				this.isNew = false
			} else {
				if (data.productId === undefined) {
					tools.leftClick()
				} else {
					this.taskData.productId = data.productId
					this.getTaskTypeInfo(data.productId)
				}
			}
			setTimeout(() => {
				//5秒后开启缓记录
				this.isCache = false;
			}, 8000)
			uni.$on('putClientInfo', clientInfo => {
				if (this.taskData.clientInfo.id_number !== clientInfo.id_number) {
					this.taskData.clientInfo = clientInfo
					this.getClientInfo()
				} else {
					this.taskData.clientInfo = clientInfo
					this.verifyBusinessModuleKey(1)
				}
			})
			//更新资产信息
			uni.$on('putPropertyInfo', propertyInfo => {
				this.taskData.property = propertyInfo
				this.verifyBusinessModuleKey(2)
			})
			//更新申请信息
			uni.$on('putAppliesInfo', appliesInfo => {
				this.taskData.applies = appliesInfo
				this.verifyBusinessModuleKey(3)
			})
			//更新第三方信息
			uni.$on('putTripartiteInfo', tripartiteInfo => {
				this.taskData.tripartite = tripartiteInfo
				this.verifyBusinessModuleKey(4)
			})
			//更新联系人信息
			uni.$on('putLinkmenInfo', linkmenInfo => {
				this.taskData.linkman = linkmenInfo
				this.verifyBusinessModuleKey(5)
			})
      //putCreditData
      uni.$on('putCreditData', creditInfo => {
        this.taskData.credit = creditInfo
        this.verifyBusinessModuleKey(6)
      })
		},
		methods: {
			addTask() {
				console.log('addTask----start')
				// let isEnd=false;
				// this.productInfo.type_data.forEach((typeItem,typeKey)=>{
				//   if(!typeItem.is_end && !isEnd && typeKey!==this.stepsNum){
				//     isEnd=true
				//     tools.error('完善'+typeItem.type_name)
				//   }
				// })
				// if(isEnd){
				//   return;
				// }
				if (this.isAjax) {
					return;
				}
				this.isAjax = true;
				addTask(this.taskData).then((res) => {
					if (res.code === 1) {
						tools.success(res.msg)
						// uni.$emit('goToTaskList', 0)
						setTimeout(() => {
							uni.redirectTo({
								url: 'page_task/task_details/task_details?taskId=' + res.data.id
							});
						}, 1500)
					} else {
						this.isAjax = false;
						tools.error(res.msg)
					}
				})
			},
			addDraft() {
				addDraft(this.taskData).then((res) => {
					if (res.code === 1) {
						tools.success(res.msg)
						delBusinessCache({
							'productId': this.productInfo.id
						}).then((res) => {
							uni.$emit('setNewDraft')
							setTimeout(() => {
								tools.leftClick()
							}, 1000)
						})
					} else {
						tools.error(res.msg)
					}
				})
			},

			verifyBusinessModuleKey(verifyKey) {
				let data = {}
				if (verifyKey === 1) {
					data = this.taskData.clientInfo
				} else if (verifyKey === 2) {
					data = this.taskData.property
				} else if (verifyKey === 3) {
					data = this.taskData.applies
				} else if (verifyKey === 4) {
					data = this.taskData.tripartite
				} else if (verifyKey === 5) {
					data = this.taskData.linkman
				} else if (verifyKey === 6) {
					data = this.taskData.credit
				} else if (verifyKey === 7) {
					data = this.taskData.post_loan
				}
				this.setBusinessCache()
				verifyBusinessModuleKey({
					'productId': this.productInfo.id,
					'data': data,
					'type': verifyKey
				}).then((res) => {
					this.informationList.forEach(item => {
						if (item.type === verifyKey) {
							item.is_finish = res.code === 1;
						}
					})
				})
			},
			getClientInfo() {
				//获取客户原始数据
				if (this.taskData.clientInfo.id_number === '') {
					return;
				}
				getClientInfo({
					'id_number': this.taskData.clientInfo.id_number,
					'productId': this.taskData.productId
				}).then((res) => {
					if (res.code === 1) {
						this.isNew = false
						if (res.data.business) {
							res.data.business.id = ''
							this.taskData.clientInfo = res.data.business
							uni.$emit('setClientData', this.taskData.clientInfo)
						}
						if (res.data.propertyList) {
							this.taskData.property = res.data.propertyList
							if (this.taskData.property) {
								this.taskData.property.forEach((item) => {
									item.id = ''
								})
							}
						}
						if (res.data.applyFor) {
							res.data.applyFor.id = ''
							this.taskData.applies = res.data.applyFor
							if (this.taskData.applies.apply_data) {
								this.taskData.applies.apply_data.forEach((item) => {
									item.id = ''
								})
							}
						}
						if (res.data.tripartite) {
							this.taskData.tripartite = res.data.tripartite
							this.taskData.tripartite.forEach((item) => {
								item.id = ''
							})
						}
						if (res.data.linkman) {
							res.data.linkman.forEach((item) => {
								item.id = ''
							})
							this.taskData.linkman = res.data.linkman
						}
						if (res.data.credit) {
							this.taskData.credit = res.data.credit
							this.taskData.credit.deal_data.forEach((item) => {
								item.id = ''
							})
							this.taskData.credit.overdue_data.forEach((item) => {
								item.id = ''
							})
							this.taskData.credit.query_data.forEach((item) => {
								item.id = ''
							})
						}
						if (res.data.postLoan) {
							this.taskData.post_loan = res.data.postLoan
						}
					}
				})
			},
			setBusinessCache() {
				//设置缓存
				if (this.isCache) {
					return false
				}
				this.isCache = true
				setBusinessCache(this.taskData).then((res) => {
					setTimeout(() => {
						this.isCache = false;
					}, 2000)
				})
			},
			getBusinessCache(productId) {
				//获取缓存
				getBusinessCache({
					'productId': productId
				}).then((res) => {
					if (res.code === 1) {
						if (res.data.productId !== 0) {
							this.isNew = false
							this.taskData = res.data
						}
					}

				})
			},

			getTaskTypeInfo(id) {
				getTaskTypeInfo({
					'id': id
				}).then((res) => {
					if (res.code === 1) {
						this.productInfo = res.data;
						if (this.isNew) this.getBusinessCache(id)
					} else {
						tools.error(res.msg)
						setTimeout(() => {
							this.closePage();
						}, 1000)
					}
				})
			},
			getDraftItem(draftId) {
				getDraftItem({
					'draftId': draftId
				}).then((res) => {
					if (res.code === 1) {
						this.productInfo = res.data.productInfo
						this.taskData = res.data.taskData
						this.getTaskTypeInfo(this.taskData.productId)
					} else {
						this.closePage();
					}
				})
			},
			async getTaskDetail(businessId) {
				const res = await getTaskDetail({
					id: businessId
				})
				if (res.code === 1) {
					if (res.data.product) {
						this.productInfo = res.data.product
						this.taskData.productId = res.data.product.id
					}
					if (res.data.business) {
						this.taskData.clientInfo = res.data.business
					}
					if (res.data.propertyList) {
						this.taskData.property = res.data.propertyList
					}
					if (res.data.applyFor) {
						this.taskData.applies = res.data.applyFor
					}
					if (res.data.tripartite) {
						this.taskData.tripartite = res.data.tripartite
					}
					if (res.data.linkman) {
						this.taskData.linkman = res.data.linkman
						console.log('this.taskData.linkman:', this.taskData.linkman)
					}
					if (res.data.credit) {
						this.taskData.credit = res.data.credit
					}
					if (res.data.postLoan) {
						this.taskData.post_loan = res.data.postLoan
					}
					this.getTaskTypeInfo(this.taskData.productId)
				} else {
					this.closePage();
				}
			},
			delBusinessCache() {
				uni.showModal({
					title: '警告',
					content: '是否退出当前任务，并清除缓存！',
					success: (res) => {
						if (res.confirm) {
							delBusinessCache({
								'productId': this.productInfo.id
							}).then((res) => {
								tools.leftClick()
							})
							uni.removeStorageSync('creditImg');
						} else {
							tools.leftClick()
						}
					}
				});

			},
			onAuthentication(item) {
				if (item.type === 1) {
					uni.setStorageSync('clientInfoKey', item.type_keys)
					uni.setStorageSync('clientInfoData', this.taskData.clientInfo)
				} else if (item.type === 2) {
					uni.setStorageSync('propertyKey', item.type_keys)
					uni.setStorageSync('propertyData', this.taskData.property)
				} else if (item.type === 3) {
					uni.setStorageSync('propertyData', this.taskData.property)
					uni.setStorageSync('appliesKey', item.type_keys)
					uni.setStorageSync('appliesData', this.taskData.applies)
				} else if (item.type === 4) {
					uni.setStorageSync('tripartiteKey', item.type_keys)
					uni.setStorageSync('tripartiteData', this.taskData.tripartite)
				} else if (item.type === 5) {
					uni.setStorageSync('linkmanKey', item.type_keys)
					uni.setStorageSync('linkmanData', this.taskData.linkman)
				} else if (item.type === 6) {
					uni.setStorageSync('creditKey', item.type_keys)
					uni.setStorageSync('creditData', this.taskData.credit)
				} else if (item.type === 7) {
					uni.setStorageSync('postLoanKey', item.type_keys)
					uni.setStorageSync('postLoanData', this.taskData.post_loan)
				}
				uni.navigateTo({
					url: `${item.path}?title=${item.name}`
				})
			},
			closePage() {
				tools.leftClick()
			},

		}
	}
</script>

<style lang="scss" scoped>
	.info_button {
		width: 130rpx;
		height: 60rpx;
		border: none;
		line-height: 60rpx;
	}

	button::after {
		border: none;
	}
</style>