// pages/inputIDInfo/index.js
import { getAgeByBirthday } from "../../utils/commonUtil";
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		type: 1,
		switch1Checked: false,
		userInfo: {
			name: "",
			sex: "",
			age: "",
			nation: "",
			IDCard: "",
			nativePlace: "",
		},
		users: [],
		isGetCardInfo: 0,
		nationList: [{ "01": "汉" }, { 15: "土家" }, { 29: "柯尔克孜" }, { 43: "乌孜别克" }, { "02": "蒙古" }, { 16: "哈尼" }, { 30: "土" }, { 44: "俄罗斯" }, { "03": "回" }, { 17: "哈萨克" }, { 31: "达斡尔" }, { 45: "鄂温克" }, { "04": "藏" }, { 18: "傣" }, { 32: "仫佬" }, { 46: "德昂" }, { "05": "维吾尔" }, { 19: "黎" }, { 33: "羌" }, { 47: "保安" }, { "06": "苗" }, { 20: "傈僳" }, { 34: "布朗" }, { 48: "裕固" }, { "07": "彝" }, { 21: "畲" }, { 35: "撒拉" }, { 49: "京" }, { "08": "壮" }, { 22: "畲" }, { 36: "毛南" }, { 50: "塔塔尔" }, { "09": "布依" }, { 23: "高山" }, { 37: "仡佬" }, { 51: "独龙" }, { 10: "朝鲜" }, { 24: "拉祜" }, { 38: "锡伯" }, { 52: "鄂伦春" }, { 11: "满" }, { 25: "水" }, { 39: "阿昌" }, { 53: "赫哲" }, { 12: "侗" }, { 26: "东乡" }, { 40: "普米" }, { 54: "门巴" }, { 13: "瑶" }, { 27: "纳西" }, { 41: "塔吉克" }, { 55: "珞巴" }, { 14: "白" }, { 28: "景颇" }, { 42: "怒" }, { 56: "基诺" }, { 59: "穿青人" }, { 95: "摩梭人" }, { 97: "其他" }, { 98: "外国血统中国籍人士" }],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		console.log(options);
		if (options.type) {
			if (options.type == 1) {
				wx.setNavigationBarTitle({
					title: "刷身份证",
				});
			} else {
				wx.setNavigationBarTitle({
					title: "手动输入",
				});
			}
			this.setData({
				type: options.type,
			});
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {},
	/**
	 * 显示nfc扫描弹窗
	 *
	 *
	 */
	showNFC() {
		if (!this.data.switch1Checked) {
			wx.showToast({
				title: "请先阅读并同意《服务协议》及《隐私政策》",
				icon: "none",
				duration: 2000,
			});
			return;
		}
		this.setData({
			drawerShow: true,
		});
	},
	hideDrawer() {
		this.setData({
			drawerShow: false,
		});
	},
	/**
	 * NFC获取身份证信息
	 *
	 *
	 */
	NFCread() {
		wx.navigateTo({
			url: "/pages/filterPage/index",
		});
	},
	switch1Change: function (e) {
		console.log("radio发生change事件，携带value值为：", e.detail.value);
		console.log(e.detail.value);

		var check = this.data.switch1Checked;
		if (check) {
			this.data.switch1Checked = false;
			console.log("已取消选中");
			// wx.showToast({
			// 	title: "请先阅读并同意《服务协议》及《隐私政策》",
			// 	icon: "none",
			// 	duration: 2000,
			// });
		} else {
			this.data.switch1Checked = true;
			console.log("已选中");
		}
		this.setData({
			switch1Checked: this.data.switch1Checked,
			drawerShow: false,
		});

		// if(!e.detail.value){

		// }
	},

	/**
	 * 手动输入页面中的事件-----------------------------------------------------------------------------
	 *
	 *
	 */

	/**
	 *  表单提交前置校验
	 *
	 *
	 */
	beforeFormSubmit(e) {
		console.log(e);
		console.log(this.data.userInfo);
		this.toFilter();
	},
	/**
	 * 跳转筛选页面
	 *
	 *
	 */
	toFilter() {
		wx.navigateTo({
			url: "/pages/filterPage/index",
		});
	},
	/**
	 * 性别选择
	 *
	 *
	 */
	changeSex(e) {
		this.data.userInfo.sex = e.currentTarget.dataset.type;
		console.log(this.data.userInfo);
		this.setData({
			userInfo: this.data.userInfo,
		});
	},
	/**
	 * 模拟双向绑定
	 *
	 *
	 */
	bothwayBind(e) {
		console.log(e);
		let type = `userInfo.${e.currentTarget.dataset.type}`;
		this.setData({
			[type]: e.detail.value,
		});
	},
	handlerReading(data) {
		var that = this;
		const { success, error, status, percent, cardInfo } = data.detail;
			console.log(error);
      if (error) {
			wx.showToast({
				title: error.errMsg,
				duration: 2000,
			});
			wx.hideLoading();
			return;
		}
		if (success) {
			// 读卡状态和进度
			console.log("-reading process-", status, percent);
			if (status === 1) {
				wx.showLoading({
					title: "识别中...",
				});
			}
			if (status === 3) {
				// 读卡成功
				wx.hideLoading();
				console.log("-reading success-", cardInfo);
				console.log(cardInfo.birth.slice(0, 4) + "-" + cardInfo.birth.slice(4, 6) + "-" + cardInfo.birth.slice(6));
				let birth = cardInfo.birth.slice(0, 4) + "-" + cardInfo.birth.slice(4, 6) + "-" + cardInfo.birth.slice(6);
				cardInfo.age = getAgeByBirthday(birth);
				cardInfo.nationStr = this.data.nationList.find((item) => {
					return item[cardInfo.nation];
				})[cardInfo.nation];

				console.log(cardInfo);
				that.setData({
					users: cardInfo,
					isGetCardInfo: true,
				});
			}
		} else {
			// 读卡失败
			console.log("-reading error-", error);
		}
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage1() {},
});
