// pages/resitor_scence/resitor_scence.js
import international from '../../international/appointment_scence/index'
import {
  getBuildingAndCompanyInfo,
  getBuildingList
} from '../../utils/api/api';
import { formatAdminInfo } from '../../utils/util/util';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
		//国际化语言包
		international: international,
		//中英文配置
		language: app.data.language,
		//内宾外宾选择 1-内宾 2-外宾
		type: app.data.language === 'ch' ? '1' : '2',
		//胶囊位置
		statusBarCont: app.data.statusBarCont,
		//用户基础信息-常客
		userInfo: app.data.userInfo,
		//用户基础信息-访客
		visitorUserInfo: app.data.visitorUserInfo,
		//账号信息-常客
		adminInfo: app.data.adminInfo,
		//账号信息-访客
		visitorAdminInfo: app.data.visitorAdminInfo,
		
    building_adminId: null,
    // 功能列表
    component_list: [{
        id: 1,
        name_ch: '邀请人员',
        name_en: 'Invite',
        icon: '../../static/home/my_appointment.png',
        url: ''
      },
      {
        id: 2,
        name_ch: '申请审核',
        name_en: 'Audit',
        icon: '../../static/home/appointment_records.png',
        url: ''
      },
      {
        id: 6,
        name_ch: '访客记录',
        name_en: 'VisitorRecords',
        icon: '../../static/home/visitors.png',
        url: ''
      },
      {
        id: 4,
        name_ch: '企业信息',
        name_en: 'Enterprise',
        icon: '../../static/home/change_msg.png',
        url: ''
      },
    ],
    
  },
  // 内外宾
  changeRadio(e) {
		e.detail == 1 ? app.data.language = 'ch' : app.data.language = 'en'
		wx.setStorageSync('language', app.data.language);
    this.setData({
      type: e.detail + "",
      language: app.data.language,
    });
  },
  // 显示提示 1-访客提示 | 2-常客提示
  showTip(e) {
    const type = e.currentTarget.dataset.type;
    let msg = this.data.international[type][this.data.language]
    wx.showModal({
      title: '提示',
      content: msg,
      showCancel: false,
      success: res => {}
    })
  },
  // 访客申请
  visitors() {
		wx.requestSubscribeMessage({
      tmplIds: ['fXvvf63kLk-koo2Rnt0igsVnaPNAoO9bMRqaKtFOqJg', 'sYVZwmwu3gGNF4Ywjo0qg2hJMHqpTr2yh_x-qr710T8','sYVZwmwu3gGNF4Ywjo0qg3ED_h5wsEZW2j7AyU1zhWA'],
      success(res) {
			}
    })
    wx.navigateTo({
      url: '/pages/visitor_make/visitor_make'
    })
  },
  // 内部员工
  regular() {
    wx.requestSubscribeMessage({
      tmplIds: ['v_ue7JEmz5AYeRz690ymJO1ImTOiM96FyeHPYny7ihw', 'sYVZwmwu3gGNF4Ywjo0qgx-Gu7MEj59EDJvmOaQPVA0','sYVZwmwu3gGNF4Ywjo0qg3ED_h5wsEZW2j7AyU1zhWA'],
      success(res) {
			}
    })
		wx.navigateTo({
			url: '/pages/regular_register/regular_register'
		})
  },
  // 根据扫码获取的企业id查询企业信息和楼宇信息
  getBuildingAndCompanyInfo(adminId) {
    getBuildingAndCompanyInfo(adminId).then(res => {
      if (res.code == 400) {
        this.getBuildingList(adminId)
      } else {			
        app.data.temporaryAdminInfo = formatAdminInfo(res.data)
      }
    })
  },
  // 获取楼宇信息
  getBuildingList(adminId) {
    getBuildingList().then(res => {
      res.data.forEach(item => {
        if (item.adminId == adminId) {
					//组合成统一格式
					let obj = formatAdminInfo(item)
					app.data.temporaryAdminInfo = obj
          return
        }
			})
			if(!app.data.temporaryAdminInfo) {
				wx.showModal({
					content: '二维码不正确或已失效，请联系管理员获取最新二维码再试一次',
					showCancel: false
				})
			}
    })
  },
	//功能区
  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','sYVZwmwu3gGNF4Ywjo0qg3ED_h5wsEZW2j7AyU1zhWA']
			},
			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','sYVZwmwu3gGNF4Ywjo0qg3ED_h5wsEZW2j7AyU1zhWA']
			},
			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
		})    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // options = {
    //   // adminId: 108,
    //   adminId: 103,
    //   type: 1,//1常客0通用
    // }
    if ('adminId' in options) {
			app.data.qrcodeInfo = options
      this.getBuildingAndCompanyInfo(parseInt(options.adminId))
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
			adminInfo: app.data.adminInfo,
			visitorAdminInfo: app.data.visitorAdminInfo,
			userInfo: app.data.userInfo,
			visitorUserInfo: app.data.visitorUserInfo
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})