<template>
	<view>
		<!-- 公共组件-每个页面必须引入 -->
		<public-module></public-module>
		<u-form :model="form" ref="uForm" class="addStaffManage">
			<u-form-item label="姓名" prop="param1"><u-input v-model="form.param1" /></u-form-item>
			<!-- <u-form-item label="性别">
				<u-input type="select" :select-open="showSex" v-model="form.sex"
						placeholder="请选择性别" @click="showSex = true"
						placeholder-style="color:#808080"></u-input>
				<u-select v-model="showSex" mode="single-column" :list="actions"
				 @confirm="sexSelect"></u-select>
					
			</u-form-item> -->
			<u-form-item label="身份证号" prop="param2"><u-input v-model="form.param2" /></u-form-item>
			<u-button hover-class="none" :custom-style="submitBtnStyle" type="success" @click="submit">提交</u-button>
		</u-form>


	</view>
</template>

<script>
	import store from '@/store';
	import {
		mapState
	} from "vuex"
	export default {
		data() {
			//身份证验证
			function validateSfz(rule, value, callback) {
				if (!value) {
					return callback(new Error('身份证号码不能为空'));
				}

				function validataCredentials(num) {
					var msg, boo; //  msg身份证验证友情提示,boo返回值
					var num = num.toUpperCase(); // 身份证为末尾可能是X
					//   身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，
					//   最后一位是校验位，可能为数字或字符X。
					const arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
					const arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
					var nTemp = 0,
						i;
					if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
						//  输入的身份证号长度不对，或者号码不符合规定;
						return [boo = false, msg = "身份证号码不符合规定"];
					}
					//   校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
					//   下面分别分析出生日期和校验位
					var len, re;
					len = num.length;
					if (len == 15) {
						re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
						var arrSplit = num.match(re);
						//   检查生日日期是否正确
						var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
						var bGoodDay;
						bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(
							arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
						if (!bGoodDay) {
							//   输入的身份证号里出生日期不对！
							return [boo = false, msg = "输入的身份证号里出生日期不对！"];

						} else {
							//   将15位身份证转成18位
							//   校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。   
							num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);
							for (i = 0; i < 17; i++) {
								nTemp += num.substr(i, 1) * arrInt[i];
							}
							num += arrCh[nTemp % 11];
							return [boo = true, msg = num];
						}
					}
					if (len == 18) {
						re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
						var arrSplit = num.match(re);
						//  检查生日日期是否正确
						var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
						var bGoodDay;
						bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(
							arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
						if (!bGoodDay) {
							//  输入的身份证号里出生日期不对！ 

							return [boo = false, msg = " 输入的身份证号里出生日期不对"];
						} else {
							//  检验18位身份证的校验码是否正确。
							//  校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
							var valnum;
							for (i = 0; i < 17; i++) {
								nTemp += num.substr(i, 1) * arrInt[i];
							}
							valnum = arrCh[nTemp % 11];
							if (valnum != num.substr(17, 1)) {
								//  18位身份证的校验码不正确！
								return [boo = false, msg = "身份证的校验码不正确！"];
							}
							return [boo = true, msg = "验证成功"]
						}
					}
					return [boo = false, msg = "身份证的长度不正确！"];
				}
				setTimeout(() => {
					var res = validataCredentials(value);
					if (!res[0]) {
						callback(new Error(res[1]));
					} else {
						callback();
					}
				}, 200);
			}
			return {
				showSex: false,
				submitBtnStyle: {
					margin: "30rpx 0rpx 0rpx",
					color: "#ffffff",
					fontWeight: "550",
					letterSpacing: "10rpx",
					backgroundColor: "#ea552d",
				},
				form: {
					param1: '',
					param2: '',
				},
				actions: [{
						value: '1',
						label: '男'
					},
					{
						value: '2',
						label: '女'
					},
					{
						value: '3',
						label: '未知'
					}
				],
				rules: {
					param1: [{
						required: true,
						message: '请输入姓名',
						trigger: ['blur', 'change']
					}],
					param2: [
						// {
						// 	required: true,
						// 	message: '请输入身份证号',
						// 	trigger: ['blur', 'change']
						// }
						{
							required: true,
							message: "请填写身份证号"
						},
						{
							validator: validateSfz
						}
					]
				}
			}
		},
		computed: {
			...mapState(['userInfo'])
		},

		methods: {
			sexSelect(e) {
				this.form.sex = e[0].label
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$http.post('/user/realNameAuth', this.form).then(res => {
							if (res.code == 200) {
								// 更新个人信息
								this.$http.get('/user/loginUser').then(res => {
									store.commit('setUserModules', {
										title: 'userInfo',
										data: {
											sysUser: {
												...res.data
											}
										}
									})
									uni.showToast({
										icon: "none",
										title: '提交成功',
										duration: 1500
									})
									uni.reLaunch({
										url: "/pages/index/index"
									})
								})
							} else {
								uni.showToast({
									icon: "none",
									title: res.msg,
									duration: 1500
								})
							}
						})
						// console.log('验证通过');
						// console.log(this.userInfo.sysUser,11111111)
					} else {
						console.log('验证失败');
					}
				});


			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style>
	.addStaffManage {
		height: 220upx;
		margin: 0 40upx;
	}
</style>