const _Http = getApp().globalData.http;
let sa_aftersalesmagid = null;
Page({
  data: {
    hidePrice: wx.getStorageSync('hidePrice'),
    tabsList: [{
      label: "详细信息",
      icon: "icon-tabchanpin"
    }, {
      label: "商品明细",
      icon: "icon-tabcaozuojilu1",
      model: "#Product"
    }],
    tabsActive: 1
  },
  onLoad(options) {
    sa_aftersalesmagid = options.id;
    this.setData({
      userrole: wx.getStorageSync('userrole')
    })
    this.getDetail();
  },
  //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_aftersalesmagid;
      if (total == null || init) {
        Component.getList(id, init);
      } else if (pageNumber <= pageTotal) {
        Component.getList(id, false);
      }
    }
  },
  getDetail() {
    _Http.basic({
      "id": 20230104160603,
      "version": 1,
      "content": {
        sa_aftersalesmagid
      }
    }).then(res => {
      console.log("退返单", res)
      if (res.msg != '成功') {
        wx.showToast({
          title: res.msg,
          icon: "none",
          mask: true
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 500)
        return;
      };
      this.setPreview(res.data);
      this.partialRenewal(true)
    })
  },
  /* 设置详情信息 */
  setPreview(data) {
    let list1 = [{
        label: "单号",
        value: data.billno
      }, {
        label: "单据日期",
        value: data.billdate
      }, {
        label: "状态",
        value: data.status
      }, {
        label: '来源单号',
        value: data.sonum
      }, {
        label: '是否实物返回',
        value: data.isitemreturn ? '是' : '否'
      }, {
        label: '定金账户',
        value: data.accountname
      }, {
        label: "提交日期",
        value: data.submitdate
      }, {
        label: "原因",
        value: data.reason
      }],
      list2 = [{
        label: "创建人",
        value: data.createby
      }, {
        label: "创建时间",
        value: data.createdate
      }, {
        label: "审核人",
        value: data.checkby
      }, {
        label: "审核时间",
        value: data.checkdate
      }];
    this.setData({
      list1,
      list2,
      detail: data
    })
  },
  submit() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否确定提交申请单?',
      complete: ({
        confirm
      }) => {
        if (confirm) _Http.basic({
          "id": 20230105161603,
          "version": 1,
          "content": {
            sa_aftersalesmagid
          }
        }).then(res => {
          console.log("提交订单", res)
          wx.showToast({
            title: res.msg == '成功' ? '提交成功!' : res.msg,
            icon: "none",
            mask: true
          });
          if (res.msg == '成功') that.setData({
            "detail.status": "提交"
          })
        })
      }
    })
  },
  notarize() {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '是否确定取消提交申请单?',
      complete: ({
        confirm
      }) => {
        if (confirm) _Http.basic({
          "id": 20230105161703,
          "version": 1,
          "content": {
            sa_aftersalesmagid
          }
        }).then(res => {
          console.log("取消提交订单", res)
          wx.showToast({
            title: res.msg == '成功' ? '取消成功!' : res.msg,
            icon: "none",
            mask: true
          });
          if (res.msg == '成功') that.setData({
            "detail.status": "新建"
          })
        })
      }
    })
  },
  deleteItem() {
    wx.showModal({
      title: '提示',
      content: '是否确定删除申请单?',
      complete: ({
        confirm
      }) => {
        if (confirm) _Http.basic({
          "id": 20230104160803,
          "version": 1,
          "content": {
            "sa_aftersalesmagids": [sa_aftersalesmagid]
          }
        }).then(res => {
          console.log("删除", res)
          wx.showToast({
            title: res.msg == '成功' ? '删除成功!' : res.msg,
            icon: "none",
            mask: true
          });
          if (res.msg == '成功') setTimeout(() => {
            wx.navigateBack()
          }, 500)
        })
      }
    })
  },
  onUnload() {
    let page = getCurrentPages().find(v => v.__route__ == 'packageA/returnOne/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": 20230105161503,
      content
    }).then(res => {
      console.log("更新申请单", res)
      page.setData({
        list: res.data,
      })
    })
  },
})