const _Http = getApp().globalData.http,
  currency = require("../../utils/currency");

let sa_dispatchid = null;
Page({
  data: {
    tabsActive: 1,
    tabsList: [{
      label: "详细信息",
      icon: "icon-tabchanpin"
    }, {
      label: "发货明细",
      icon: "icon-tabcaozuojilu1",
      model: "#Rows"
    }]
  },
  onLoad(options) {
    sa_dispatchid = options.id;
    this.getDetail();
  },
  /* 获取详情 */
  getDetail() {
    _Http.basic({
      "id": 20221114135303,
      "content": {
        nocache: true,
        sa_dispatchid
      }
    }).then(res => {
      console.log("发货详情", res)
      if (res.msg != '成功') return wx.showToast({
        title: res.msg,
        icon: "none"
      });
      this.setPreview(res.data);
      this.setData({
        detail: res.data,
        loading: false
      });
      this.partialRenewal()
    })
  },
  /* 设置详情信息 */
  setPreview(data) {
    let list1 = [{
        label: "发货单号",
        value: data.billno
      }, {
        label: "单据日期",
        value: data.billdate
      }, {
        label: "单据状态",
        value: data.STATUS
      }, {
        label: "来源单号",
        value: data.sonum
      }, {
        label: "经销商编号",
        value: data.agentnum
      }, {
        label: "经销商名称",
        value: data.enterprisename
      }, {
        label: "发货数量",
        value: data.qty
      }, {
        label: "发货金额",
        value: currency(data.sumamount, {
          symbol: "￥",
          precision: 2
        }).format()
      }, {
        label: "备注",
        value: data.remarks
      }],
      list2 = [{
        label: "创建人",
        value: data.createby
      }, {
        label: "创建时间",
        value: data.createdate
      }, {
        label: "审核人",
        value: data.checkby
      }, {
        label: "审核时间",
        value: data.checkdate
      }];
    this.setData({
      list1,
      list2
    })
  },
  //tabs 切换
  tabsChange({
    detail
  }) {
    this.setData({
      tabsActive: detail
    });
    this.partialRenewal();
  },
  //局部数据更新 tabs
  partialRenewal(init = false) {
    let model = this.data.tabsList[this.data.tabsActive].model;
    if (model) {
      let Component = this.selectComponent(model),
        {
          total,
          pageNumber,
          pageTotal
        } = Component.data.content,
        id = this.data.detail.sa_dispatchid;
      if (total == null || init) {
        Component.getList(id, init);
      } else if (pageNumber < pageTotal) {
        Component.getList(id, false);
      }
    }
  },
  onReachBottom() {
    this.partialRenewal();
  },
  onUnload() {
    console.log("更新列表数据")
    let page = getCurrentPages().find(v => v.__route__ == 'packageA/dispatchBill/index');
    let content = JSON.parse(JSON.stringify(page.data.content));
    content.pageNumber = 1;
    content.pageSize = (page.data.content.pageNumber - 1) * page.data.content.pageSize;
    _Http.basic({
      "id": 20230320160203,
      content
    }).then(res => {
      console.log("更新订单列表", res)
      if (res.msg == '成功') page.setData({
        list: res.data
      })
    })
  },
})