const _Http = getApp().globalData.http;

Page({
  data: {
    result: [],
    content: {
      "nocache": true,
      "pageNumber": 1,
      "pageSize": 20,
      "total": null,
      "where": {
        "condition": "",
        "type": 7
      }
    }
  },
  onLoad(options) {
    this.getList();
  },
  tabChange(e) {
    this.data.content.where.type = e.detail.title == '客户' ? 7 : 5;
    this.getList(true)
  },
  changeResult(e) {
    const {
      item
    } = e.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: `是否确认选择${item.enterprisename}?`,
      complete: ({
        confirm
      }) => {
        if (confirm) getApp().globalData.handleSelect(item);
      }
    })
  },
  /* 获取列表 */
  getList(init = false) {
    //init 用于初始化分页
    if (init.detail != undefined) init = init.detail;
    let content = this.data.content;
    if (init) content.pageNumber = 1;
    if (content.pageNumber > content.pageTotal) return;
    _Http.basic({
      "id": 20220920083901,
      content
    }).then(res => {
      console.log("关联客户列表", res)
      this.selectComponent('#ListBox').RefreshToComplete();
      if (res.msg != '成功') return wx.showToast({
        title: res.data,
        icon: "none"
      })
      this.setData({
        'content.pageNumber': res.pageNumber + 1,
        'content.pageTotal': res.pageTotal,
        'content.total': res.total,
        list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
      })
      this.getTags();
    })
  },
  /* 开始搜索 */
  startSearch({
    detail
  }) {
    if (detail == this.data.content.where.condition) return;
    this.setData({
      'content.where.condition': detail
    });
    this.getList(true);
  },
  /* 取消搜索 */
  onClear() {
    this.setData({
      'content.where.condition': ""
    });
    this.getList(true);
  },
  /* 获取标签 */
  getTags() {
    let list = this.data.list,
      ownerids = list.map(v => v.sa_customersid);
    _Http.basic({
      "id": 20221018102001,
      "content": {
        "ownertable": "sa_customers",
        ownerids
      }
    }).then(res => {
      console.log("标签", res)
      if (res.msg != '成功') return;
      for (let key in res.data) {
        let index = list.findIndex(v => v.sa_customersid == key);
        if (index != -1) list[index].tags = res.data[key]
      };
      this.setData({
        list
      })
    })
  },
  onReady() {
    this.selectComponent("#ListBox").setHeight(".search", this);
  },
  onUnload() {
    getApp().globalData.handleSelect = null;
  }
})