const _Http = getApp().globalData.http;
import currency from "../../utils/currency";
let CNY = value => currency(value, {
  symbol: "",
  precision: 2
}).format();
Page({
  data: {
    showFiltrate: false,
    startUsing: false,
    showList: [],
    "content": {
      "pageNumber": 1,
      "pageSize": 40,
      "type": "0", //1按部门 0按人员
      "dataid": wx.getStorageSync('userMsg').userid, //部门人员id
      "querytype": "1", //0按业务员 1按客户
      "point": "全部", // 入账节点
      "where": {
        "condition": ""
      }
    },
    querytypes: [{
      name: "按业务员",
      value: 0
    }, {
      name: "按客户",
      value: 1
    }],
    points: ['全部', '订单', '出货', '开票'],
    filtratelist: [],
    table: [{
      title: '客户名称',
      width: 330,
      key: 'enterprisename',
      fun: 'toDetail'
    }, {
      title: '业务员',
      width: 200,
      key: 'name'
    }, {
      title: '总应收',
      width: 200,
      key: 'allaoverduemount'
    }, {
      title: '逾期1月',
      width: 200,
      key: 'onemonthamount'
    }, {
      title: '逾期1-3月',
      width: 200,
      key: 'threemonthamount'
    }, {
      title: '逾期3月及以上',
      width: 200,
      key: 'upthreemonthamount'
    }],
    sumWidth: 0,

  },
  onLoad(options) {
    this.setData({
      sumWidth: this.data.table.reduce((prev, cur) => {
        return prev.width ? prev.width + cur.width : prev + cur.width
      })
    })
    this.getList()
  },
  getList(init = false) {
    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": 20240320111604,
      content
    }).then(res => {
      if (res.msg != '成功') return wx.showToast({
        title: res.msg,
        icon: "none"
      })
      this.setListHeight()
      this.selectComponent('#ListBox').RefreshToComplete();
      console.log("应收账款列表", res)
      let showList = [],
        obj = {
          "sumallaoverduemount": '总应收',
          "sumonemonthamount": '逾期1月',
          "sumthreemonthamount": '逾期1-3月',
          "sumupthreemonthamount": '逾期三月以上'
        }
      if (res.data.length) {
        for (const key in obj) {
          showList.push({
            name: obj[key] + '(元)',
            value: res.data[0][key] >= 10000 ? (CNY(res.data[0][key] / 10000) + '万') : res.data[0][key]
          })
        }
      } else {
        for (const key in obj) {
          showList.push({
            name: obj[key] + '(元)',
            value: CNY(0)
          })
        }
      }
      this.setData({
        showList
      })
      let list = res.data.map(v => {
        v.allaoverduemount = CNY(v.allaoverduemount)
        v.onemonthamount = CNY(v.onemonthamount)
        v.threemonthamount = CNY(v.threemonthamount)
        v.upthreemonthamount = CNY(v.upthreemonthamount)
        return v
      })
      this.setData({
        list: res.pageNumber == 1 ? list : this.data.list.concat(list),
        "content.pageNumber": res.pageNumber + 1,
        "content.pageTotal": res.pageTotal,
        "content.total": res.total,
      })
    })
  },
  toDetail(e) {
    let {
      item
    } = e.currentTarget.dataset;
    let content = JSON.parse(JSON.stringify(this.data.content));
    content.dataid = item.userid || item.sa_customersid;
    wx.navigateTo({
      url: '/packageA/receivables/detail?content=' + JSON.stringify(content),
    })
  },
  querytypeChange(e) {
    this.setData({
      "content.querytype": e.detail.value
    });
    if (e.detail.value == 0) {
      this.data.table[0] = {
        title: '部门',
        width: 230,
        key: 'depname',
        fun: 'toDetail'
      }
    } else {
      this.data.table[0] = {
        title: '客户名称',
        width: 330,
        key: 'enterprisename',
        fun: 'toDetail'
      }
    }
    this.setData({
      table: this.data.table
    })
    this.getList(true)
  },
  pointChange(e) {
    this.data.content.point = this.data.points[e.detail.value]
    this.getList(true)
  },
  onReady() {
    this.setListHeight()
    this.selectComponent("#organization").initDepAndUser();
  },
  /* 设置页面高度 */
  setListHeight() {
    this.selectComponent("#ListBox").setHeight(".roof", this);
  },
  openFiltrate() {
    this.setData({
      showFiltrate: true
    })
  },
  handleFilter({
    detail
  }) {

    if (detail.name == 'reset') {
      this.selectComponent("#organization").initDepAndUser()
      this.setData({
        'content.dataid': wx.getStorageSync('userMsg').userid,
        'content.type': 0,
      })
    } else {
      let active = this.selectComponent("#organization").data.result;
      let type = active.userid ? 0 : 1,
        dataid = type == 0 ? active.userid : active.departmentid
      this.setData({
        'content.dataid': dataid,
        'content.type': type
      })
    }

    this.getList(true)
  },
  startSearch({
    detail
  }) {
    this.data.content.where.condition = detail;
    this.getList(true)
  },
  onClear() {
    this.data.content.where.condition = '';
    this.getList(true)
  },
  showSearch() {
    this.setData({
      startUsing: !this.data.startUsing
    })
    setTimeout(() => {
      this.setListHeight()
    }, 400)
  },
})