import {
  updateConfig,
  snConfig
} from '../../../utils/api/api'
import { getWaterDrop } from '../../../utils/index/index'
const app = getApp()
Page({
  data: {
    chooseObj: {
      usePersonCard: {
        chooseItem: [{
          name: '是',
          checked: true,
          value: true,
        },
        {
          name: '否',
          checked: false,
          value: false,
        },
        ],
      },
      noIdcardInputPhone: {
        chooseItem: [{
          name: '是',
          checked: true,
          value: true,
        },
        {
          name: '否',
          checked: false,
          value: false,
        },
        ],
      }
    },
    form: {},
    // 获取受访人列表地址
    isShowInterviewee: false,
    IntervieweeList: ['https://tx.hz-hanghui.com:8088/yx-fyzd/api/v1/fkj/visitee/list', 'https://noise.hz-hanghui.com:8088/yx-fyzd/api/v1/fkj/visitee/list'],
    // 访客记录推送地址
    isShowVisitorLog: false,
    VisitorLogList: ['https://tx.hz-hanghui.com:8088/yx-fyzd/VisitorRecords', 'https://noise.hz-hanghui.com:8088/yx-fyzd/VisitorRecords'],
    // 微信二维码地址
    isShowWx: false,
    WXList: ['https://tx.hz-hanghui.com:8088/yx-fyzd/faceCheckVisitor?type=2', 'https://noise.hz-hanghui.com:8088/yx-fyzd/faceCheckVisitor?type=2'],
    // 支付宝二维码地址
    isShowZfb: false,
    ZFBList: ['https://tx.hz-hanghui.com:8088/yx-fyzd/faceCheckVisitor?type=2', 'https://noise.hz-hanghui.com:8088/yx-fyzd/faceCheckVisitor?type=2'],
    isPortraitScreen: false,// 是否为竖屏：true竖屏 false横屏
    screenIntervalId: null, //定时器
  },
  onLoad() {
    my.hideBackHome();
    const _this = this
    this.initScreenType().then(isPortraitScreenRes => {
      console.log('isPortraitScreenRes', isPortraitScreenRes)
      _this.setData({
        isPortraitScreen: isPortraitScreenRes
      })
    })
    this.getForm()
  },
  initScreenType() {
    let that = this
    return new Promise((resolve, reject) => {
      if (app.globalData.isPortraitScreen) {
        if (app.globalData.isPortraitScreen != null) {
          resolve(app.globalData.isPortraitScreen)
        }
      } else {
        that.data.screenIntervalId = setInterval(function () {
          console.log('asdasdsaddasdad', app.globalData)
          if (app.globalData.isPortraitScreen != null) {
            resolve(app.globalData.isPortraitScreen)
            clearInterval(that.data.screenIntervalId);
          }
        }, 1000);
      }
    })
  },
  // 赋值
  async getForm(e) {
    my.showLoading()
    try {
      let dto = {
        sn: app.globalData.sn,
        token: app.globalData.FKJConfiguration,
      }
      let res = await snConfig(dto)
      let resultData = res.data || {}
      this.setData({
        form: resultData
      })

      let newObj = {
        usePersonCard: {
          chooseItem: []
        },
        noIdcardInputPhone: {
          chooseItem: []
        }
      }
      Object.keys(this.data.chooseObj).forEach((chooseKey) => {
        let chooseItemArr = this.data.chooseObj[chooseKey].chooseItem || []
        if (typeof (resultData[chooseKey]) !== 'undefined') {
          let checkValue = !!resultData[chooseKey]
          chooseItemArr[0].checked = checkValue
          chooseItemArr[1].checked = !checkValue
        }
        newObj[chooseKey].chooseItem = chooseItemArr
      });
      this.setData({
        chooseObj: newObj
      })
    } catch (error) { } finally {
      my.hideLoading()
    }
  },
  // 单选选中
  onRadioChange(e) {
    let currentItem = e.currentTarget.dataset.item;
    let currentIndex = e.currentTarget.dataset.index;
    let fieldKey = e.currentTarget.dataset.fieldKey;
    console.log('onRadioChange', currentItem, currentIndex, fieldKey)
    let checkValue = false;
    let chooseItemArr = this.data.chooseObj[fieldKey].chooseItem || []
    chooseItemArr.forEach((cItem, cIndex) => {
      if (cIndex == currentIndex) {
        cItem.checked = true
        checkValue = cItem.value
      } else {
        cItem.checked = false
      }
    })
    console.log(currentItem, chooseItemArr)
    this.setData({
      [`form.${fieldKey}`]: checkValue,
      [`chooseObj.${fieldKey}.chooseItem`]: chooseItemArr
    })
  },
  onSwitchChange(e) {
    let key = e.currentTarget.dataset.fieldKey
    let value = e.currentTarget.dataset.checked
    let checkValue = !value
    let forKey = `form[${key}]`
    switch (key) {
      case 'useAlipayFace': // 启用支付宝刷脸登记
      case 'useIdcard': // 启用刷身份证登记
      case 'noIdcardRegister': // 启用无证登记
      case 'wxScanRegister': // 启用微信扫码登记
      case 'zfbScanRegister': // 启用支付宝扫码登记
        this.setData({
          [forKey]: checkValue
        })
        break;
      case 'ethQrcode': // 启用梯控二维码
        if (checkValue) {
          this.setData({
            'form.ethQrcodeTitle': '梯控二维码', // 梯控二维码标题
            'form.ethQrcodeUrl': 'https://tx.hz-hanghui.com:8088/yx-fyzd/api/v1/fkj/visitor/eth/qrCode', // 梯控二维码接口
          })
        }
        this.setData({
          [forKey]: checkValue
        })
        break;
    }
  },

  getVal(e) {
    let key = e.currentTarget.dataset.key
    let name = e.currentTarget.dataset.name
    let key1 = `form[${key}]`
    switch (key) {
      case 'transitPushUrl':
        this.setData({
          [key1]: name
        })
        break;
      case 'visiteeUrl':
        this.setData({
          [key1]: name
        })
        break;
      case 'zfbQrcodeUrl':
        this.setData({
          [key1]: name
        })
        break;
      case 'wxQrcodeUrl':
        this.setData({
          [key1]: name
        })
        break;
      default:
        this.setData({
          [key1]: e.detail.value
        })
        break;
    }
  },

  // 遮罩层
  closeMask() {
    getWaterDrop()
    this.setData({
      isShowInterviewee: false,
      isShowVisitorLog: false,
      isShowWx: false,
      isShowZfb: false,
    })
  },
  openChoose(e) {
    this.setData({
      isShowVisitorLog: false,
      isShowInterviewee: false,
      isShowWx: false,
      isShowZfb: false,
    })
    let key = e.currentTarget.dataset.key
    switch (key) {
      case 'transitPushUrl':
        this.setData({
          isShowVisitorLog: !this.data.isShowVisitorLog
        })
        break;
      case 'visiteeUrl':
        this.setData({
          isShowInterviewee: !this.data.isShowInterviewee
        })
        break;
      case 'wxQrcodeUrl':
        this.setData({
          isShowWx: !this.data.isShowWx
        })
        break;
      case 'zfbQrcodeUrl':
        this.setData({
          isShowZfb: !this.data.isShowZfb
        })
        break;
      default:
        break;
    }
  },

  async sure() {
    let {
      form
    } = this.data
    form.modifyType = app.globalData.modifyType[1],
      form.token = app.globalData.FKJConfiguration
    my.showLoading()
    try {
      // 以下字段根据切换条件重置为默认值
      const fieldArr = [
        'useIdcard', 'noIdcardRegister', 'wxScanRegister', 'zfbScanRegister'
      ]
      fieldArr.forEach(field => {
        const fieldValue = form[field]
        switch (field) {
          // 启用身份证功能
          case 'useIdcard':
            if (!fieldValue) {
              form = {
                ...form,
                usePersonCard: true // 	刷证是否人证比对 默认值：true
              }
            }
            break
          // 启用无证登记
          case 'noIdcardRegister':
            if (!fieldValue) {
              form = {
                ...form,
                noIdcardInputPhone: true // 	无证是否输入手机号 默认值：true
              }
            }
            break
          // 启用微信扫码登记
          case 'wxScanRegister':
            if (!fieldValue) {
              form = {
                ...form,
                wxQrcodeUrl: '' // 微信二维码地址
              }
            }
            break
          // 启用支付宝扫码登记
          case 'zfbScanRegister':
            if (!fieldValue) {
              form = {
                ...form,
                zfbQrcodeUrl: '' // 支付宝二维码地址
              }
            }
            break
        }
      })
      let res = await updateConfig(form)
      app.globalData.snDisposition.useAlipayFace = form.useAlipayFace;
      app.globalData.snDisposition.useIdcard = form.useIdcard;
      app.globalData.snDisposition.usePersonCard = form.usePersonCard;
      app.globalData.snDisposition.noIdcardRegister = form.noIdcardRegister;
      app.globalData.snDisposition.noIdcardInputPhone = form.noIdcardInputPhone;
      app.globalData.snDisposition.wxScanRegister = form.wxScanRegister;
      app.globalData.snDisposition.wxQrcodeUrl = form.wxQrcodeUrl;
      app.globalData.snDisposition.zfbScanRegister = form.zfbScanRegister;
      app.globalData.snDisposition.zfbQrcodeUrl = form.zfbQrcodeUrl;
      app.globalData.snDisposition.transitPushUrl = form.transitPushUrl;
      app.globalData.snDisposition.visiteeUrl = form.visiteeUrl;
      // console.log(app.globalData.snDisposition);
      my.setStorageSync({
        data: app.globalData.snDisposition,
        key: 'snDisposition'
      });
      my.reLaunch({
        url: "/pages/settings/index/index"
      })
    } catch (error) {
      console.log(error);
    } finally {
      my.hideLoading()
    }
  },
});
