import {
  visiteeList,
} from '../../../utils/api/api'
import {
  getWaterDrop
} from '../../../utils/index/index'
const app = getApp()
Page({
  data: {
    formPath: null, //从哪个页面来
    searchList: [{
        name: '姓名',
        val: 'name'
      },
      {
        name: '手机号',
        val: 'phone'
      },
      {
        name: '身份证',
        val: 'idNumber'
      },
      {
        name: '单位',
        val: 'company'
      },
      {
        name: '部门',
        val: 'unitName'
      },
      {
        name: '楼层',
        val: 'floor'
      },
      {
        name: '房间号',
        val: 'room'
      },
    ],
    searchForm: {
      name: '',
      phone: '',
      company: '',
      unitName: '',
      floor: '',
      room: '',
    },
    allList: [],
    allListCopy: [],
    tableList: [],
    total: 0,
    pageNum: 0, //总页数
    pageSize: 0, //当前页数
    sliceStart: 0, //截取数组开始
    sliceEnd: 8, //截取数组结束
  },
  onLoad(e) {
    my.hideBackHome();
    this.setData({
      formPath: e.formPath || null
    })
    this.data.searchFormCopy = JSON.parse(JSON.stringify(this.data.searchForm));
    this.getTable()
  },
  async getTable(e) {
    my.showLoading()
    try {
      let dto = {
        sn: app.globalData.sn,
      }
      let res = await visiteeList(app.globalData.snDisposition.visiteeUrl, dto)
      if (res.data) {
        var pageNum = Math.ceil(res.data.length / 8);
        let {
          sliceEnd,
          sliceStart
        } = this.data
        this.setData({
          allList: res.data,
          allListCopy: res.data,
          tableList: res.data.slice(sliceStart, sliceEnd),
          total: res.data.length,
          pageNum,
          pageSize: res.data.length > 0 ? 1 : 0,
        })
      }
    } catch (error) {
      console.log(error);
    } finally {
      my.hideLoading()
    }
  },
  clickAudio() {
    getWaterDrop()
  },
  getVal(e) {
    let key = e.currentTarget.dataset.key
    let key1 = `searchForm[${key}]`
    this.setData({
      [key1]: e.detail.value
    })
  },
  // 查询
  inquireFun() {
    let {
      searchForm,
      allList,
      allListCopy
    } = this.data
    allList = JSON.parse(JSON.stringify(allListCopy))
    // 根据条件过滤数据
    let result = this.filterData(searchForm, allList)
    var pageNum = Math.ceil(result.length / 8);
    let sliceStart = 0
    let sliceEnd = 8
    this.setData({
      allList: result,
      tableList: result.slice(sliceStart, sliceEnd),
      sliceEnd,
      sliceStart,
      total: result.length,
      pageNum,
      pageSize: result.length > 0 ? 1 : 0,
    })
  },
  // 多条件过滤，参数queryObj(过滤条件对象)，list(需要过滤的数组)
  filterData(queryObj, list) {
    let arr = list
    Object.keys(queryObj).forEach(key => {
      if (queryObj[key] == undefined || queryObj[key] == '') return arr
      arr = arr.filter(p => p[key] !== null && (p[key].indexOf(queryObj[key]) > -1))
    })
    return arr
  },
  //清空
  clearFun() {
    let searchForm = JSON.parse(JSON.stringify(this.data.searchFormCopy));
    this.setData({
      searchForm
    })
    this.getTable()
  },
  // 上一页
  previousFun() {
    let {
      allList,
      pageSize,
      sliceEnd,
      sliceStart
    } = this.data
    if (pageSize <= 1) {
      return
    }
    this.setData({
      tableList: allList.slice(sliceStart - 8, sliceEnd - 8),
      sliceStart: sliceStart - 8,
      sliceEnd: sliceEnd - 8,
      pageSize: pageSize - 1
    })
  },
  // 下一页
  nextFun() {
    let {
      allList,
      pageSize,
      pageNum,
      sliceEnd,
      sliceStart
    } = this.data
    if (pageSize >= pageNum) {
      return
    }
    this.setData({
      tableList: allList.slice(sliceStart + 8, sliceEnd + 8),
      sliceStart: sliceStart + 8,
      sliceEnd: sliceEnd + 8,
      pageSize: pageSize + 1
    })
  },
});