// pages/IDCardWithNFC/index.js
const app = getApp();
import { baseUrl } from '../../utils/request.js';

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		activeIndex: 0,
		drawerShow: false,
		currentInfo: {},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {},

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

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {},

	/**
	 * 点击的推荐方式
	 *
	 *
	 */
	setActive(e) {
		let that = this;
		console.log(e);
		this.setData({
			activeIndex: e.currentTarget.dataset.index,
		});
		if (this.data.activeIndex == 1) {
			wx.navigateTo({
				url: "/pages/inputIDInfo/index?type=1",
			});
		} else if (this.data.activeIndex == 2) {
			wx.chooseImage({
				count: 1,
				sizeType: ["original", "compressed"],
				sourceType: ["album", "camera"],
				success(res) {
					console.log(res);
					// debugger;
					that.uploadIdcardImg(res);
				},
			});
		} else if (this.data.activeIndex == 3) {
			wx.navigateTo({
				url: "/pages/inputIDInfo/index?type=3",
			});
		}
	},
	/**
	 * ocr识别身份证
	 *
	 *
	 */
	uploadIdcardImg(imgPath) {
		let that = this;
		// tempFilePath可以作为img标签的src属性显示图片
		let filePath;
		console.log(imgPath);
		if (imgPath.tempFilePaths) {
			filePath = imgPath.tempFilePaths[0];
		} else {
			filePath = imgPath;
		}

		console.log(filePath);
		wx.showLoading({
			title: "识别中...",
		});
		wx.uploadFile({
			// url: app.globalData.ip + '/commons/idCardOcrRecognize',
			url: baseUrl + "/commons/idCardOcrRecognizeAndUploadToAliyunOss",
			filePath,
			name: "uploadFile",
			header: app.globalData.headers,
			success: function (res) {
				console.log(res);
				let idInfo = JSON.parse(res.data);
				if (idInfo.status == 200) {
					console.log(idInfo.data);
					that.data.currentInfo = idInfo.data;
					that.setData({
						drawerShow: true,
						currentInfo: that.data.currentInfo,
					});
				} else {
					wx.showToast({
						duration: 2000,
						title: "身份证未识别",
						icon: "none",
						mask: true,
					});
					// console.log(1);
					//app.showTips(that, res.data.msg);
				}
				wx.hideLoading();
			},
			fail: function (res) {
				console.log(res);
				setTimeout(() => {
					wx.hideLoading();
				}, 2000);
			},
		});
	},
	NFCread() {
		wx.navigateTo({
			url: "/pages/filterPage/index",
		});
	},
	hideDrawer() {
		this.setData({
			drawerShow: false,
		});
	},
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {},

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

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

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

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