import {
  getDate,
  getWaterDrop,
} from '../../../utils/index/index'
import {
  visitorReportPageList,
  visitorSignOut
} from '../../../utils/api/api'
const app = getApp()
Page({
  data: {
    formPath: null, //从哪个页面来
    tableList: [],
    searchForm: {
      data: {
        name: '',
        phone: '',
        idNumber: '',
        startTime: '',
        endTime: '',
        sn: '',
      },
      pageNum: 1,
      pageSize: 9,
    },
    total: null,
    allPageNum: 0,
    notDesensitizedColumn: [],
    signOutList: [{
        id: true,
        name: '已签离'
      },
      {
        id: false,
        name: '未签离'
      },
    ]
  },
  onLoad(e) {
    my.hideBackHome();
    this.setData({
      formPath: e.formPath || null,
      notDesensitizedColumn: app.globalData.snDisposition.notDesensitizedColumn
    })
    this.data.searchFormCopy = JSON.parse(JSON.stringify(this.data.searchForm));
    this.getTable()
  },
  async getTable() {
    let {
      searchForm,
    } = this.data;
    searchForm.data.sn = app.globalData.sn
    my.showLoading()
    try {
      let res = await visitorReportPageList(searchForm)
      this.setData({
        tableList: res.data.list,
        total: res.data.total,
        allPageNum: Math.ceil(res.data.total / searchForm.pageSize)
      })
      my.hideLoading();
    } catch (error) {
      my.hideLoading();
    }

  },
  clickAudio() {
    getWaterDrop()
  },
  //签离 
  async signOff(e) {
    let item = e.currentTarget.dataset.item
    let data = {
      sn: app.globalData.sn,
      userVisitorListDetailId: item.userVisitorListDetailId,
    }
    let res = await visitorSignOut(data)
    my.showToast({
      content: '签离成功',
      duration: 2000
    });
    this.getTable()
  },
  getVal(e) {
    let key = e.currentTarget.dataset.key
    let key1 = `searchForm.data[${key}]`
    let signOutList = this.data.signOutList
    switch (key) {
      case 'isSignOut':
        this.setData({
          'searchForm.data.isSignOut': signOutList[e.detail.value].id,
          'searchForm.data.isSignOutName': signOutList[e.detail.value].name,
        })
        break;
      default:
        this.setData({
          [key1]: e.detail.value
        })
        break;
    }

  },
  // 获取日期
  datePicker(e) {
    my.datePicker({
      format: 'yyyy-MM-dd HH:mm:ss', //返回的日期格式
      currentDate: getDate(), //初始选择的日期时间，默认当前时间
      startDate: '2012-01-01 11:11:11', //最小日期时间
      endDate: getDate(), //最大日期时间
      success: res => {
        let key = e.currentTarget.dataset.key
        switch (key) {
          case 'startTime':
            this.setData({
              'searchForm.data.startTime': res.date
            })
            break;
          case 'endTime':
            this.setData({
              'searchForm.data.endTime': res.date
            })
            break;
          default:
            break;
        }
      },
    });
  },
  //清空
  clearFun() {
    let searchForm = JSON.parse(JSON.stringify(this.data.searchFormCopy));
    this.setData({
      searchForm
    })
    this.getTable()
  },
  // 上一页
  previousFun() {
    let {
      searchForm,
    } = this.data
    if (searchForm.pageNum <= 1) {
      return
    }
    this.setData({
      'searchForm.pageNum': searchForm.pageNum - 1,
    })
    this.getTable()
  },
  // 下一页
  nextFun() {
    let {
      searchForm,
      allPageNum,
    } = this.data
    if (searchForm.pageNum >= allPageNum) {
      return
    }
    this.setData({
      'searchForm.pageNum': searchForm.pageNum + 1,
    })
    this.getTable()
  },
  // 关闭
  closeFun() {
    let url = this.data.formPath ? '/pages/home/index' : '/pages/settings/index/index'
    my.reLaunch({
      url: url
    });
  },
});