const app = require('../../utils/util.js');
Page({
  data: {
    logs: [],
    currentTabIndex: 0,
    select_all: false,
    isAllTrue: false, // 控制全选和反选的按钮
    listData: [
      // {
      //   code: "1",
      //   text: "测试1"
      // },
      // {
      //   code: "2",
      //   text: "测试2"
      // },
      // {
      //   code: "3",
      //   text: "测试3"
      // }
    ],
    batchIds: '', //选中的ids，
    active: 0,
    show: false,
    infos: {},
    photos: [],
    scrollHeight: '',
    scrollHeight1: '',
    list: [
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      //   {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
      // {visitTime: "2020-02-16T11:22:07.697Z",oldInfo:'战三的'},
    ], //探访的数据哈
    number: null,
  },
  onTabsItemTap: function (event) {
    let index = event.currentTarget.dataset.index;
    this.setData({
      currentTabIndex: index,
      // isAllTrue: false,
      // select_all: false
    })
  },
  cellClick(e) {
    console.log(e.currentTarget.dataset.infos);
    if (e.currentTarget.dataset.infos.status !== "2") {
      let that = this
      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.reLaunch({
      //   url: '/pages/collectInfo/collectInfo?idss=' + fid +'&namess=' +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) {
    console.log(e.currentTarget.dataset.infos, "000");
    let photos = e.currentTarget.dataset.infos.visitPhoto.split(",");
    console.log(photos, "打印下锕");
    this.setData({
      show: true,
      infos: e.currentTarget.dataset.infos,
      photos: photos
    })
  },
  onClickHide() {
    this.setData({
      show: false
    });
  },
  //删除
  deletes() {
    console.log("ssss");
    let that = this;
    wx.showModal({
      content: '该操作会将所有选中数据删除", "是否删除？',
      success(res) {
        if (res.confirm) {
          //确定绑定的操作
          if (that.data.batchIds.length) {
            for (let item of that.data.batchIds) {
              that.deleteById(item);
            }
            wx.showToast({
              title: '删除成功',
              icon: 'none',
              duration: 2000,
              success: function () {
                switch (that.data.number) {
                  case 0:
                    that.openIdInfo();
                    break;
                  case 1:
                    that.openIdInfo({
                      status: "1"
                    });
                    break;
                  case 2:
                    that.openIdInfo({
                      status: "0"
                    });
                    break;
                  case 3:
                    this.openIdInfo({
                      status: "2"
                    });
                    break;
                  case 4:
                    this.openIdInfo({
                      status: "3"
                    });
                    break;
                  case 5:
                    that.openIdInfo({
                      status: "4"
                    });
                    break;
                  default:
                    that.openIdInfo();
                }
              }
            })

          } else {
            wx.showToast({
              title: '请选择要删除的记录',
              icon: 'none',
              duration: 2000,
            })
          }


        } else if (res.cancel) {
          //取消绑定的操作
        }
      }
    })
  },
  deleteById(id) {
    wx.request({
      url: app.globalData.publicUrl + '/wxinfo/delete',
      method: "GET",
      header: {
        appletsId: wx.getStorageSync('openId')
      },
      data: {
        id: id
      },
      success: (res) => {
        console.log(res, "000");
      }
    })
  },
  //全选与反全选
  selectall: function (e) {
    console.log(e)
    var that = this;
    var arr = []; //存放选中id的数组
    console.log(that.data.listData);

    for (let i = 0; i < that.data.listData.length; i++) {
      if (that.data.listData[i].status != '3') {
        that.data.listData[i].checked = (!that.data.select_all);
        console.log(that.data.select_all);

        if (that.data.listData[i].checked == true) {
          // 全选获取选中的值
          arr = arr.concat(that.data.listData[i]._id.split(','));
        }
      }
    }
    console.log(arr)
    that.setData({
      listData: that.data.listData,
      select_all: (!that.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, "0000");
        if (res.data.code == 0) {
          wx.hideLoading();
          this.setData({
            listData: res.data.data
          })
        }
      }
    })
  },
  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() {
    let that = this
    let query = wx.createSelectorQuery().in(this)
    query.select('.zizi').boundingClientRect()
    query.select('.delete-box').boundingClientRect()
    query.exec(res => {
      let searchHeight = res[0].height
      let titleHeight = res[1].height
      let windowHeight = wx.getSystemInfoSync().windowHeight
      let scrollHeight = windowHeight - searchHeight - titleHeight - 30 - 5 - 50 - 44
      this.setData({
        scrollHeight: scrollHeight
      })
    })
  },
  computeScrollViewHeight1() {
    let that = this
    let query = wx.createSelectorQuery().in(this)
    query.select('.zizi').boundingClientRect()
    query.exec(res => {
      let searchHeight = res[0].height
      let windowHeight = wx.getSystemInfoSync().windowHeight
      let scrollHeight = windowHeight - searchHeight - 30 - 5 - 50
      this.setData({
        scrollHeight1: scrollHeight
      })
    })
  },

  onLoad() {
    this.oldInfo();
    this.visitInfo();
    this.computeScrollViewHeight();
    this.computeScrollViewHeight1();
  },
  onShow() {
    console.log(wx.getStorageSync('user'), "打印");
    if (wx.getStorageSync('user') == '') {
      wx.redirectTo({
        url: '/pages/login/login',
      })
    };
    this.setData({
      isAllTrue: false,
      select_all: false
    })
    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();
  },
  onHide(){
    this.setData({
      show :false
    })
  }
})