const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    logs: [],
    currentTabIndex: 0,
    select_all: false,
    isTrue: false,
    isAllTrue: false, // 控制全选和反选的按钮
    listData: [],
    batchIds: '', //选中的ids，
    active0: 0,
    active1: 0,
    show: false,
    infos: {},
    photos: [],
    scrollHeight: '',
    scrollHeight1: '',
    list: [], //巡访的数据哈
    number: null,
    isShowTrue: false
  },
  onTabsItemTap: function (event) {
    let index = event.currentTarget.dataset.index;
    this.setData({
      currentTabIndex: index
    })
  },
  cellClick(e) {
    if (e.currentTarget.dataset.infos.status !== "2") {
      let fid = e.currentTarget.dataset.infos.fid;
      let name = e.currentTarget.dataset.infos.name;
      const app = getApp()
      app.globalData.id = fid
      app.globalData.name = name
      wx.switchTab({
        url: `/pages/collectInfo/collectInfo`,
        success: function (e) {
          var page = getCurrentPages().pop();
          if (page == undefined || page == null) return;
          page.onLoad();
        }
      })
    } else {
      wx.showToast({
        title: "审核中禁止修改",
        icon: 'none',
        duration: 2000,
      })
    }
  },
  // 巡访的记录详情页面
  visitJilu(e) {
    wx.navigateTo({
      url: '../../pages/workDailyInfo/workDailyInfo?infos=' + JSON.stringify(e.currentTarget.dataset.infos),
    })
  },
  onClickHide() {
    this.setData({
      show: false
    });
  },
  //删除
  deletes() {
    if (this.data.batchIds.length) {
      wx.showModal({
        content: '该操作会删除所有选中数据,是否继续？',
        success: (res) => {
          if (res.confirm) {
            let num = 0;
            for (let item of this.data.batchIds) {
              wx.request({
                url: app.globalData.publicUrl + '/wxinfo/delete',
                method: "GET",
                header: {
                  appletsId: wx.getStorageSync('openId')
                },
                data: {
                  id: item
                },
                success: () => {
                  num += 1;
                  if (num == this.data.batchIds.length) {
                    wx.showToast({
                      title: '删除成功',
                      icon: 'none',
                      duration: 2000,
                      success: () => {
                        switch (this.data.number) {
                          case 0:
                            this.openIdInfo();
                            break;
                          case 1:
                            this.openIdInfo({
                              status: "1"
                            });
                            break;
                          case 2:
                            this.openIdInfo({
                              status: "0"
                            });
                            break;
                          case 3:
                            this.openIdInfo({
                              status: "2"
                            });
                            break;
                          case 4:
                            this.openIdInfo({
                              status: "3"
                            });
                            break;
                          case 5:
                            this.openIdInfo({
                              status: "4"
                            });
                            break;
                          default:
                            this.openIdInfo();
                        }
                      }
                    })
                    this.setData({
                      isAllTrue: false,
                    })
                  }
                }
              })
            }
            console.log(num)
          }
        }
      })
    } else {
      wx.showToast({
        title: '请选择要删除的记录',
        icon: 'none',
        duration: 2000,
      })
    }
  },
  //全选与反全选
  selectall: function () {
    var arr = []; //存放选中id的数组
    for (let i = 0; i < this.data.listData.length; i++) {
      if (this.data.listData[i].status != '3') {
        this.data.listData[i].checked = (!this.data.select_all);
        if (this.data.listData[i].checked == true) {
          arr = arr.concat(this.data.listData[i]._id.split(','));
        }
      }
    }
    this.setData({
      listData: this.data.listData,
      select_all: (!this.data.select_all),
      batchIds: arr
    })
  },
  //单选
  checkboxChange: function (e) {
    console.log(e.detail.value, "打印的值是啥呢")
    this.setData({
      batchIds: e.detail.value //单个选中的值
    })
  },
  tabsChange(e) {
    this.setData({
      number: e.detail.index,
      isAllTrue: false,
      select_all: false
    })
    switch (e.detail.index) {
      case 0:
        this.openIdInfo();
        break;
      case 1:
        this.openIdInfo({
          status: "1"
        });
        break;
      case 2:
        this.openIdInfo({
          status: "0"
        });
        break;
      case 3:
        this.openIdInfo({
          status: "2"
        });
        break;
      case 4:
        this.openIdInfo({
          status: "3"
        });
        break;
      case 5:
        this.openIdInfo({
          status: "4"
        });
        break;
      default:
        this.openIdInfo();
        break;
    }
  },
  openIdInfo(p) {
    this.oldInfo(p);
  },
  oldInfo(p) {
    console.log('我查数据了')
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.publicUrl + '/wxinfo/listByOpenid',
      method: "GET",
      data: p,
      header: {
        appletsId: wx.getStorageSync('openId')
      },
      success: (res) => {
        console.log(res.data.data, "00111111100");
        if (res.data.code == 0) {
          wx.hideLoading();
          this.setData({
            listData: res.data.data
          })
          if (res.data.data.length == 0) {
            this.setData({
              isShowTrue: 'none'
            })
          } else {
            this.setData({
              isShowTrue: 'block'
            })
          }
        }
      }
    })
  },
  visitInfo() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: app.globalData.publicUrl + '/visit/list',
      method: "GET",
      header: {
        appletsId: wx.getStorageSync('openId')
      },
      success: (res) => {
        console.log(res, "0000");
        if (res.data.code == 0) {
          wx.hideLoading();
          this.setData({
            list: res.data.data
          })
        }
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  },
  computeScrollViewHeight() {
    this.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight * 2 - 115 - wx.getSystemInfoSync().screenHeight + wx.getSystemInfoSync().statusBarHeight
    })
  },
  computeScrollViewHeight1() {
    this.setData({
      scrollHeight1: wx.getSystemInfoSync().windowHeight * 2 - wx.getSystemInfoSync().screenHeight + wx.getSystemInfoSync().statusBarHeight
    })
  },
  onShow() {
    wx.showLoading({
      title: '加载中',
    })
    tools.rzStatus().then((res) => {
      if (res.data.code == 0) {
        if (res.data.data) {
          tools.finishInfo().then((res) => {
            console.log(res.data.data.perfect, '我完善信息了吗 1代表完后 0代表未完成')
            console.log(res.data.data.comparison, '我人脸识别过了吗 1代表完后 0代表未完成')
            if (res.data.data.perfect == '0') {
              wx.redirectTo({
                url: '/pages/finishInfo/finishInfo',
              })
              return;
            }
            if (res.data.data.perfect == '1' && res.data.data.comparison == '0') {
              wx.redirectTo({
                url: '/pages/faceRecognition/faceRecognition',
              })
              return;
            }
            this.setData({
              isAllTrue: false,
              select_all: false,
              isTrue: true,
            })
            if (this.data.isTrue) {
              this.computeScrollViewHeight();
              this.computeScrollViewHeight1();
            }
            this.oldInfo();
            this.visitInfo();
            switch (this.data.number) {
              case 0:
                this.openIdInfo();
                break;
              case 1:
                this.openIdInfo({
                  status: "1"
                });
                break;
              case 2:
                this.openIdInfo({
                  status: "0"
                });
                break;
              case 3:
                this.openIdInfo({
                  status: "2"
                });
                break;
              case 4:
                this.openIdInfo({
                  status: "3"
                });
                break;
              case 5:
                this.openIdInfo({
                  status: "4"
                });
                break;
              default:
                this.openIdInfo();
                break;
            }
            this.visitInfo();
            wx.hideLoading()
          })
        }
      } else {
        wx.hideLoading()
        wx.clearStorageSync();
        wx.showModal({
          showCancel: false,
          content: '当前您未登录,请登录',
          success(res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            }
          }
        })
      }
    })
  },
  onHide() {
    this.setData({
      show: false,
      listData: []
    })
  }
})