<template>
	<page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta> 
	<view class="pwd-retrieve-container">
		<Navbar title="证件信息" :showRightText="false" :isBack="false" :goTo="'/pages/certificateManage/certificateInfo/index'"/>
		<uni-forms ref="form" :value="certificate" labelWidth="80px"> 
			<uni-forms-item name="name" label="姓名" required="true">
				<uni-easyinput type="text" v-model="certificate.name" maxlength="20" placeholder="请输入" />
			</uni-forms-item>
			<uni-forms-item name="idNumber" label="身份证号" required="true">
				<uni-easyinput type="text" v-model="certificate.idNumber"  maxlength="18" placeholder="请输入" />
			</uni-forms-item>
			<uni-forms-item name="birthday" label="出生日期" required="true">
				<uni-datetime-picker type="date" :clear-icon="false" format="YYYY-MM-DD" v-model="certificate.birthday"/>
			</uni-forms-item>
			<uni-forms-item name="birthAddr" label="出生地点" required="true">
				<uni-easyinput type="text" v-model="certificate.birthAddr"  maxlength="30" placeholder="请输入" />
			</uni-forms-item>
			<uni-forms-item name="certificateType" label="证件类型" required="true">
				<view style="width: 100%;height: 100%;border: 1px solid #dcdfe6;padding: 16rpx 10rpx;border-radius: 8rpx;" @click="show=true">{{certificate.certificateTypeName || '填选择证件类型'}}</view>
			</uni-forms-item>
			<uni-forms-item name="certificateNo" label="证件编号" required="true">
				<uni-easyinput type="text" v-model="certificate.certificateNo"  maxlength="30" placeholder="请输入" />
			</uni-forms-item>
			<uni-forms-item name="issueAddr" label=" 签发地" required="true">
				<uni-easyinput type="text" v-model="certificate.issueAddr"  maxlength="30" placeholder="请输入" />
			</uni-forms-item>
			<uni-forms-item name="issueDay" label="签发日期" required="true"> 
				<uni-datetime-picker type="date" :clear-icon="false" format="YYYY-MM-DD" v-model="certificate.issueDay"/>
			</uni-forms-item>
			<uni-forms-item name="issueLifespan" label="有效期至" required="true">
				<uni-datetime-picker type="date" :clear-icon="false" format="YYYY-MM-DD" v-model="certificate.issueLifespan"/>
			</uni-forms-item>
			<uni-forms-item name="isEle" label="是否电子标签" required="true">
				<uni-data-checkbox v-model="certificate.isEle" :localdata="sexs" />
			</uni-forms-item>
		  
			<view @click="submit" style="width: 90%;height: 80rpx;margin: 10px auto;background: #3888FF;color: #fff;border-radius: 10rpx;text-align: center;line-height: 80rpx;" >
				提交
			</view>
			<view v-show="certificate.certificateNo!=''" @click="delAddress" style="width: 90%;height: 80rpx;margin: 10px auto;background: #fff;border-radius: 10rpx;text-align: center;line-height: 80rpx;border: 1px solid #ccc;" >
				删除
			</view> 
		</uni-forms>
	
		<u-picker :show="show" :columns="columns" @confirm="confirm" @close="show=false" :closeOnClickOverlay="true"></u-picker>
	</view>
</template>

<script> 
import { addCertificateInfoApi,deleteCertificateInfoApi,updateCertificateInfoApi } from '@/api/certificateManage/index.js';
export default {
    data() {
      return { 
		fontValue:uni.getStorageSync('fontSize') || 8,  
		columns: [[
			{text:"往来国外",value:'1'},
			{text:"往来台湾通行证",value:'2'},
			{text:"往来香港通行证",value:'3'},
		]],
		show:false,
		// 单选数据源
		sexs: [{
			text: '是',
			value: 1
		}, {
			text: '否',
			value: 0
		}],
		isSumbit:false,
		certificate:{
			name:"",
			idNumber:"",
			birthday:"",
			birthAddr:"",
			certificateType:"",
			certificateNo:"",
			issueAddr:"",
			issueDay:"",
			issueLifespan:"",
			isEle:0
		}
      }
    }, 
	onLoad(options) {  
		this.certificate = JSON.parse(options.certificate)
		console.log(this.certificate)
	},
    methods: {  
		//地址选中
		confirm(e) {
			console.log('confirm', e)
			this.certificate.certificateType=e.value[0].value;
			this.certificate.certificateTypeName = e.value[0].text
			this.show = false
		},
		submit() {
			const regex = /(^\d{15}$)|(^\d{17}([0-9]|X)$)/;
			console.log(this.certificate)
			if(this.certificate.name==""){
				uni.$u.toast(`姓名不能为空`) 
			}else if(this.certificate.idNumber==""){
				uni.$u.toast(`身份证号不能为空`) 
			}else if(!regex.test(this.certificate.idNumber)){
				uni.$u.toast(`请输入正确身份证号`) 
			}else if(this.certificate.birthday==""){
				uni.$u.toast(`出生日期不能为空`)   
			}else if(this.certificate.birthAddr==""){	
				uni.$u.toast(`出生地点不能为空`) 
			}else if(this.certificate.certificateType==""){
				uni.$u.toast(`证件类型不能为空`) 
			}else if(this.certificate.certificateNo==""){
				uni.$u.toast(`证件编号不能为空`)        
			}else if(this.certificate.issueAddr==""){
				uni.$u.toast(`签发地不能为空`) 
			}else if(this.certificate.issueDay==""){
				uni.$u.toast(`签发日期不能为空`) 
			}else if(this.certificate.issueLifespan==""){
				uni.$u.toast(`有效期至不能为空`) 
			}else{
				if(!this.isSumbit){
					const certificateNo=this.certificate.certificateNo;
					if(certificateNo!=undefined&&certificateNo!=""){
						this.updatedata();
					}else{
						this.insertdata();
					}
				}
			}
		}, 
		async updatedata(){
			this.isSumbit=true
			let param = { 
				"certificateNo":this.certificate.certificateNo, 
				"name": this.certificate.name, 
				"idNumber": this.certificate.idNumber,
				"birthday": this.certificate.birthday, 
				"birthAddr": this.certificate.birthAddr, 
				"certificateType": this.certificate.certificateType, 
				"issueAddr": this.certificate.issueAddr, 
				"issueDay": this.certificate.issueDay, 
				"issueLifespan": this.certificate.issueLifespan, 
				"isEle": this.certificate.isEle,  
			}
			const res = await updateCertificateInfoApi(param); 
			console.log('?? ~ getList ~ res:', res) 
			if(res.code==200){
				uni.$u.toast(`操作成功！`)
				setTimeout(()=>{
					uni.navigateBack()
					this.isSumbit=false
				},500)
			}else{
				this.isSumbit=false
				uni.$u.toast(`操作失败！`)
			}
			
		},
		async insertdata(){ 
			this.isSumbit=true
			let param = {
				"custId":uni.getStorageSync('custId'),
				"name": this.certificate.name, 
				"idNumber": this.certificate.idNumber,
				"addrFullName": this.certificate.addrFullName, 
				"birthAddr": this.certificate.birthAddr, 
				"areaCode":1,
				"openid": uni.getStorageSync('openId'),
				"sourceType": "7",
				"placeId":this.certificate.addressIndex,
			}
			const res = await addCertificateInfoApi(param); 
			console.log('?? ~ getList ~ res:', res) 
			if(res.code==200){
				uni.$u.toast(`新增成功！`)
				setTimeout(()=>{
					this.isSumbit=false
					uni.navigateBack()
				},500)
			}else{
				this.isSumbit=false
				uni.$u.toast(res.msg)
			} 
		}, 
		async delAddress(){
			let param = {
				"custId":uni.getStorageSync('custId'),
				"openid": uni.getStorageSync('openId'),
				"sourceType": "7",
				"certificateNo":this.certificate.certificateNo,
			}
			const res = await deleteCertificateInfoApi(param); 
			if(res.code==200){
				uni.$u.toast(`删除成功！`)
				setTimeout(()=>{
					uni.navigateBack()
				},500)
			}else{
				uni.$u.toast(res.msg)
			}
			
		}
	}
}
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .pwd-retrieve-container {
    padding-top: 36rpx;
    padding: 15px;
  }
</style>
