// pages/makeInfo/index.js
import { baseUrl } from '../../utils/request.js';
const app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		btnDisabled: true,
		user: {},
		which: 1,
		isLogin: false,
		switch1Checked: true,
	},
	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,
		});

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

		// }
	},

	TT: function (e) {
		var that = this;
		if (that.data.user.idauth == 1) {
			app.dialog("已实名认证", "实名认证后不能修改身份证照片", "知道了");
			return;
		}

		console.log(e.currentTarget.dataset.id); //判断点击的第几个

		this.setData({
			which: e.currentTarget.dataset.id,
		});
		var type;
		if (this.data.which == 1) {
			type = 2;
		} else {
			type = 3;
		}

		that.uploadImageOperator(type);
	},

	uploadImageOperator: function (type) {
		var that = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ["album", "camera"], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				wx.showLoading({
					title: "上传中",
					mask: true,
				});
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths;
				var formData = { type: type, userId: app.globalData.userId };
				wx.uploadFile({
					url: baseUrl + "/user/auth/uploadImage",
					filePath: res.tempFilePaths[0],
					name: "uploadFile",
					header: app.globalData.headers2, // 设置请求的 header
					formData: formData, // HTTP 请求中其他额外的 form data
					success: function (res1) {
						console.log(res1);
						if (res1.statusCode == 200) {
							var result = JSON.parse(res1.data);
							if (result.status == 200) {
								if (type == 2) {
									that.data.user["idcardImgFrontm"] = result.data.miniUrl;
									that.data.user["idcardImgFront"] = result.data.url;
									that.data.user["IDCard"] = result.data.IDCard;
									that.data.user["realName"] = result.data.realName;
								} else if (type == 3) {
									that.data.user["idcardImgBackm"] = result.data.miniUrl;
									that.data.user["idcardImgBack"] = result.data.url;
								}
								that.setData({
									user: that.data.user,
								});
							} else {
								that.setData({
									popErrorMsg: result.msg,
									pop: 1,
								});
								setTimeout(() => {
									_that.setData({
										popErrorMsg: "",
										pop: 0,
									});
									return;
								}, 2000);
							}
						}
						//判断是否可以进入下一步
						if (!that.isEmptyCheck(that.data.user.idcardImgFront) && !that.isEmptyCheck(that.data.user.idcardImgBack)) {
							that.setData({
								btnDisabled: false,
							});
						}
						wx.hideLoading();
					},
					fail: function (res2) {
						wx.hideLoading();
					},
				});
			},
		});
	},
	isEmptyCheck: function (str) {
		if (str == null || str == "" || str == "null" || str == "undefined" || typeof str == "undefined") {
			return true;
		}
		return false;
	},
	isNotEmptyCheck: function (str) {
		return !this.isEmptyCheck(str);
	},
	chooseImg: function () {
		var that = this;
		wx.chooseImage({
			count: 1,
			sizeType: ["original", "compressed"],
			sourceType: ["album", "camera"],
			success(res) {
				// tempFilePath可以作为img标签的src属性显示图片
				const tempFilePaths = res.tempFilePaths;
				that.setData({
					btnDisabled: false,
				});
			},
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		//登录=================================start
		if (app.globalData.isLogin) {
			this.setData({
				isLogin: app.globalData.isLogin,
				user: app.globalData.user,
			});
			if (this.data.user.idcardImgBack && this.data.user.idcardImgFront) {
				this.setData({
					btnDisabled: false,
				});
			}
			this.data.user["IDCard"] = this.data.user["idcard"];
		} else {
			// 由于 userLogin 是网络请求，可能会在 Page.onLoad 之后才返回
			// 所以此处加入 callback 以防止这种情况
			app.userLoginCallback = (res) => {
				this.setData({
					isLogin: app.globalData.isLogin,
					user: app.globalData.user,
				});
				if (this.data.user.idcardImgBack && this.data.user.idcardImgFront) {
					this.setData({
						btnDisabled: false,
					});
				}
				this.data.user["IDCard"] = this.data.user["idcard"];
			};
		}
		console.log(this.data.user);
	},
	makeInfoSure: function () {
		var that = this;
		wx.setStorage({
			key: "userInformation",
			data: that.data.user,
		});
		wx.redirectTo({
			url: "/pages/makeInfoSure/index",
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

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

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

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

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

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