import {
  updateConfig,
  snConfig
} from '../../../utils/api/api'
import {
  getWaterDrop,
} from '../../../utils/index/index'

const app = getApp()
Page({
  data: {
    list: [{
      name: '选择受访人超时时长（s）',
      value: 'chooseVisiteeTimeout'
    },
    {
      name: '刷身份证超时时长（s）',
      value: 'readIdcardTimeout'
    },
    {
      name: '输身份证超时时长（s）',
      value: 'inputIdcardTimeout'
    },
    {
      name: '输手机号超时时长（s）',
      value: 'inputPhoneTimeout'
    },
    {
      name: '签离扫码超时时长（s）',
      value: 'singOutTimeout'
    },
    {
      name: '结果页超时时长（s）',
      value: 'resultPageTimeout'
    },
    {
      name: '刷脸超时时间（s）',
      value: 'faceTimeout'
    },
    {
      name: '1v1比对超时时间（s）',
      value: 'oneToOneTimeout'
    },
    {
      name: '联网查询超时时间（s）',
      value: 'connectSearchTimeout'
    },
    ],
    form: {
      chooseVisiteeTimeout: 90,
      readIdcardTimeout: 15,
      inputIdcardTimeout: 30,
      inputPhoneTimeout: 30,
      singOutTimeout: 15,
      resultPageTimeout: 5,
      faceTimeout: 40,
      oneToOneTimeout: 40,
      connectSearchTimeout: 40,
    },
    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);
      }
    })
  },
  clickAudio() {
    getWaterDrop()
  },
  async getForm() {
    my.showLoading()
    try {
      let dto = {
        sn: app.globalData.sn,
        token: app.globalData.FKJConfiguration,
      }
      let res = await snConfig(dto)
      this.setData({
        form: res.data
      })
    } catch (error) { } finally {
      my.hideLoading()
    }
  },
  // 减
  minus(e) {
    let key = e.currentTarget.dataset.item.value
    let value = this.data.form[key]
    let NameVal = `form[${key}]`
    if (value <= 0 || !value) {
      return
    }
    this.setData({
      [NameVal]: value - 1
    })
  },
  // 加
  add(e) {
    let key = e.currentTarget.dataset.item.value
    let value = this.data.form[key]
    if (!value) {
      value = 0
    }
    let NameVal = `form[${key}]`
    this.setData({
      [NameVal]: value + 1
    })
  },
  getVal(e) {
    let key = e.currentTarget.dataset.item.value
    let value = e.detail.value
    let NameVal = `form[${key}]`
    this.setData({
      [NameVal]: value
    })
  },
  async sure() {
    let {
      form
    } = this.data
    form.sn = app.globalData.sn;
    form.modifyType = app.globalData.modifyType[2];
    form.token = app.globalData.FKJConfiguration;
    my.showLoading()
    try {
      let res = await updateConfig(form)
      app.globalData.snDisposition.chooseVisiteeTimeout = form.chooseVisiteeTimeout;
      app.globalData.snDisposition.readIdcardTimeout = form.readIdcardTimeout;
      app.globalData.snDisposition.inputIdcardTimeout = form.inputIdcardTimeout;
      app.globalData.snDisposition.inputPhoneTimeout = form.inputPhoneTimeout;
      app.globalData.snDisposition.singOutTimeout = form.singOutTimeout;
      app.globalData.snDisposition.resultPageTimeout = form.resultPageTimeout;
      app.globalData.snDisposition.faceTimeout = form.faceTimeout;
      app.globalData.snDisposition.oneToOneTimeout = form.oneToOneTimeout;
      app.globalData.snDisposition.connectSearchTimeout = form.connectSearchTimeout;
      // 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()
    }
  },
});
