const _Http = getApp().globalData.http,
  currency = require("../../../utils/currency"),
  CNY = (value, symbol = "￥", precision = 2) => currency(value, {
    symbol,
    precision
  }).format();

Page({
  data: {
    condition: ""
  },
  onLoad(options) {
    getApp().globalData.Language.getLanguagePackage(this, '选择项目')
    this.setData({
      content: _Http.data.content,
    })
    const filtratelist = [{
      label: "项目类型",
      index: _Http.data.projecttypes.findIndex(v => v.value == _Http.data.content.where.projecttype) || 0,
      showName: "remarks", //显示字段
      valueKey: "projecttype", //返回Key
      selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
      value: _Http.data.content.where.projecttype, //选中值
      list: _Http.data.projecttypes
    }, {
      label: "项目等级",
      index: _Http.data.grades.findIndex(v => v.value == _Http.data.content.where.grade) || 0,
      showName: "remarks", //显示字段
      valueKey: "grade", //返回Key
      selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
      value: _Http.data.content.where.grade, //选中值
      list: _Http.data.grades.map(v => {
        v.remarks = v.remarks.split("-")[0]
        return v
      })
    }, {
      label: "领域",
      index: _Http.data.tradefields.findIndex(v => v.value == _Http.data.content.where.tradefield) || 0,
      showName: "remarks", //显示字段
      valueKey: "tradefield", //返回Key
      selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
      value: _Http.data.content.where.tradefield, //选中值
      list: _Http.data.tradefields
    }, ];
    this.setData({
      filtratelist
    })
    this.getList()
  },
  getList(init = false) {
    _Http.init(this.data.content, init).then(content => {
      _Http.basic({
        "id": 20230715111604,
        content
      }).then(res => {
        console.log("res", res)
        this.selectComponent('#ListBox').automaticSetHei();
                this.selectComponent('#ListBox').RefreshToComplete();
        if (res.code != '1') return wx.showToast({
          title: res.data,
          icon: "none"
        })
        res.data = res.data.map(v => {
          v.signamount_due = CNY(v.signamount_due)
          v.dealamount = CNY(v.dealamount)
          return v
        })
        this.setData({
          list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
          "content.pageNumber": res.pageNumber + 1,
          "content.pageSize": res.pageSize,
          "content.pageTotal": res.pageTotal,
          "total": res.total,
        })
      })
    })
  },
  handleFilter({
    detail
  }) {
    if (detail.name == "close") return;
    this.setData({
      "content.where.grade": detail.grade || "",
      "content.where.projecttype": detail.projecttype || "",
      "content.where.tradefield": detail.tradefield || "",
    })
    this.getList(true);
  },
  openFiltrate() {
    this.setData({
      showFiltrate: true
    })
  },
  selectItem(e) {
    const {
      item
    } = e.currentTarget.dataset;
    _Http.selectItem(item)
  },
  onUnload() {
    delete _Http.data
  },
  onChange(e) {
    this.setData({
      condition: e.detail,
    });
  },
  onSearch() {
    this.setData({
      "content.where.condition": this.data.condition
    })
    this.getList(true);
  },
  onClear() {
    this.setData({
      "content.where.condition": "",
      condition: "",
    })
    this.getList(true);
  }
})