// pages/mine/mine.js
import international from '../../international/appointment_scence/index'
const {
	desensitizationPhone,
	desensitizationIdNumber,
} = require('../../utils/util')
const app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		//国际化语言包
		international: international,
		//中英文配置
		language: app.data.language,
		//胶囊位置
		statusBarCont: app.data.statusBarCont,
		//1常客 2访客
		type: 2,
		//用户基础信息
		userInfo: null,
		//可供使用的账号信息
		usedAdminInfo: null,
		// 功能列表
		component_list: [{
			id: 1,
			name_ch: '邀请人员',
			name_en: 'Invite',
			icon: '../../static/mine/my_appointment.png',
			url: ''
		},
		{
			id: 2,
			name_ch: '申请审核',
			name_en: 'Audit',
			icon: '../../static/mine/appointment_records.png',
			url: ''
		},
		{
			id: 6,
			name_ch: '访客记录',
			name_en: 'VisitorRecords',
			icon: '../../static/mine/visitors.png',
			url: ''
		},
		{
			id: 4,
			name_ch: '企业信息',
			name_en: 'Enterprise',
			icon: '../../static/mine/change_msg.png',
			url: ''
		}
		],

	},
	// 跳转
	goto(e) {
		let id = e.currentTarget.dataset.id
		let obj = {
			1: {
				url: '/pages/regular_invite/regular_invite',
				needUserInfo: true,
				needAdminInfo: true,
				tmplIds: ['261lS0M-ugfPHTOFeTFS6IN8WQshNOCbrPEC51OONy8'],
				ch: "要先注册为内部员工才可以使用该功能哦，是否现在去注册？",
				en: "Please do certification first，regist now？",
				jumpLink: '/pages/regular_register/regular_register',
				jumpLinkTmplIds:['v_ue7JEmz5AYeRz690ymJO1ImTOiM96FyeHPYny7ihw', 'sYVZwmwu3gGNF4Ywjo0qgx-Gu7MEj59EDJvmOaQPVA0','sYVZwmwu3gGNF4Ywjo0qgx-Gu7MEj59EDJvmOaQPVA0']
			},
			2: {
				url: '/pages/regular_visit_records/regular_visit_records',
				needUserInfo: true,
				needAdminInfo: true,
				ch: "要先注册为内部员工才可以使用该功能哦，是否现在去注册？",
				en: "Please do certification first，regist now？",
				jumpLink: '/pages/regular_register/regular_register',
				jumpLinkTmplIds:['v_ue7JEmz5AYeRz690ymJO1ImTOiM96FyeHPYny7ihw', 'sYVZwmwu3gGNF4Ywjo0qgx-Gu7MEj59EDJvmOaQPVA0','sYVZwmwu3gGNF4Ywjo0qgx-Gu7MEj59EDJvmOaQPVA0']
			},
			4: {
				url: '/pages/regular_company_manage/regular_company_manage',
				needvisitorUserInfo: true,
				ch: "要先进行认证才可以看到您的记录呢，是否现在去认证？",
				en: "Please do certification first，apply now？",
				jumpLink: '/pages/visitor_make/visitor_make?justCertification=true',
				jumpLinkTmplIds:['fXvvf63kLk-koo2Rnt0igsVnaPNAoO9bMRqaKtFOqJg', 'sYVZwmwu3gGNF4Ywjo0qg2hJMHqpTr2yh_x-qr710T8','sYVZwmwu3gGNF4Ywjo0qg3ED_h5wsEZW2j7AyU1zhWA']
			},
			6: {
				url: '/pages/visit_records/visit_records',
				needvisitorUserInfo: true,
				ch: "要先进行认证才可以看到您的记录呢，是否现在去认证？",
				en: "Please do certification first，apply now？",
				jumpLink: '/pages/visitor_make/visitor_make?justCertification=true',
				jumpLinkTmplIds:['fXvvf63kLk-koo2Rnt0igsVnaPNAoO9bMRqaKtFOqJg', 'sYVZwmwu3gGNF4Ywjo0qg2hJMHqpTr2yh_x-qr710T8','sYVZwmwu3gGNF4Ywjo0qg3ED_h5wsEZW2j7AyU1zhWA']
			}
		}
		let config = obj[id];
		if((config.needUserInfo && !app.data.userInfo) || (config.needvisitorUserInfo && !app.data.visitorUserInfo) || (config.needAdminInfo && !app.data.adminInfo)) {
			wx.showModal({
				content: config[this.data.language],
				confirmText:this.data.language =="ch"?"是":"YES" ,
				cancelText:this.data.language =="ch"?"否":"NO",
				complete: (res) => {
					if (!res.confirm) {
						return;
					}
					if(config.jumpLinkTmplIds) {
						wx.requestSubscribeMessage({
							tmplIds: config.jumpLinkTmplIds,
							success(res) {}
						});
					}
					wx.navigateTo({
						url: config.jumpLink,
					})
				}
			})
			return;
		}
		if(config.tmplIds) {
			wx.requestSubscribeMessage({
				tmplIds: config.tmplIds,
				success(res) {}
			});
		}
		wx.navigateTo({
			url: config.url
		})
	},
	// 认证
	goCertification(e) {
		let type = e.currentTarget.dataset.type
		if(type == 1) {
			wx.navigateTo({
				url: '/pages/visitor_make/visitor_make?justCertification=true'
			})
			return;
		}
		if(type == 2) {
			wx.navigateTo({
				url: '/pages/regular_register/regular_register?justCertification=true'
			})
			return;
		}
	},
	//图片预览
	preview(e) {
		let url = e.currentTarget.dataset.src;
		wx.previewImage({
			current: url, // 当前显示图片的http链接
			urls: [url] // 需要预览的图片http链接列表
		})
	},
	getUserInfo() {
		let { userInfo } = this.data;
		userInfo = app.data.userInfo || app.data.visitorUserInfo
		if (userInfo && userInfo.idNumber) {
			userInfo.hideIdNumber = desensitizationIdNumber(userInfo.idNumber)
		}
		if (userInfo && userInfo.phone) {
			userInfo.hidePhone = desensitizationPhone(userInfo.phone)
		}
		this.setData({
			userInfo,
			type: (app.data.adminInfo && app.data.adminInfo.official)? 1 : 2,
			language: app.data.language,
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},

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

	},

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

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

	},

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

	},

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

	},

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

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})