// pages/agent/serviceBillDetail/index.js
import api from '../../api/api'
import Dialog from 'tdesign-miniprogram/dialog/index';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refresh: true,
    orderMainData: {},
    list: [],
    tabbarsList: [{
      label: "编辑",
      icon: "icon-dibu-bianji"
    }, {
      label: "提交",
      icon: "icon-tijiao",
    }, {
      label: "删除",
      icon: "icon-guanlian-shanchu"
    }]
  },
  async mainData() {
    const res = await api._post({
      "id": "20230206091603",
      "version": 1,
      "content": {
        "sa_serviceorderid": this.data.id
      }
    })
    this.setData({
      orderMainData: res.data
    })
    this.fileData()
    this.linkWorkOrder()
  },
  async fileData() {
    const res = await api._post({
      "method": "queryFileLink",
      "classname": "system.attachment.Attachment",
      "content": {
        "ownertable": "sa_serviceorder",
        "ownerid": this.data.id,
        "usetype": "附件" //传空返回有所
      }
    })
    this.setData({
      filelist: res.data,
      refresh: true
    })
  },
  addProduct() {
    wx.navigateTo({
      url: '/Eservice/saler/billCanUseProduct/index',
    })
  },
  onDelete({
    detail
  }) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: `是否确定删除“${detail.itemname}”`,
      complete: (res) => {
        if (res.confirm) api._post({
          "id": "20230206162003",
          "version": 1,
          "content": {
            sa_serviceorderitemsids: [detail.sa_serviceorderitemsid]
          }
        }).then(res => {
          console.log("删除明细", res)
          wx.showToast({
            title: res.msg == '成功' ? '删除成功' : res.msg,
            icon: "none"
          });
          that.productmx()
        })
      }
    })
  },

  async getAdd(selection) {
    const res = await api._post({
      "id": "20230206161803",
      "version": 1,
      "content": {
        "sa_serviceorderid": this.data.id,
        "iteminfos": selection.map(e => {
          return {
            "sa_serviceorderitemsid": 0,
            "itemid": e,
            "reason": "",
            "qty": 1
          }
        })
      }
    })
    this.productmx()
  },
  async changeItem({
    detail
  }) {
    const res = await api._post({
      "id": "20230206161803",
      "version": 1,
      "content": {
        "sa_serviceorderid": this.data.id,
        "iteminfos": [{
          "sa_serviceorderitemsid": detail.sa_serviceorderitemsid,
          "itemid": detail.sa_serviceorderitemsid,
          "reason": detail.reason,
          "qty": detail.qty
        }]
      }
    })
    wx.showToast({
      title: res.msg == '成功' ? '修改成功' : res.msg,
      icon: "none"
    })
    this.productmx()
  },
  async productmx() {
    const res = await api._post({
      "id": "20230206161903",
      "version": 1,
      "content": {
        nocache: true,
        "sa_serviceorderid": this.data.id
      }
    })
    this.setData({
      list: res.data.map(v => {
        v.oldQty = v.qty;
        return v
      })
    })
  },


  /* 底部按钮回调 */
  tabbarOnClick(e) {
    let that = this,
      dialogConfig = null,
      res = null;
    switch (e.detail.label) {
      case "编辑":
        getApp().globalData.handelSelect = this
        wx.navigateTo({
          url: '/Eservice/saler/editServiceBill/index',
        })
        break;
      case "提交":
        dialogConfig = {
          context: this,
          title: '提示',
          content: '确认提交当前申请单吗',
          confirmBtn: '确定',
          cancelBtn: '取消',
        };
        Dialog.confirm(dialogConfig)
          .then(async () => {
            res = await api._post({
              "id": "20230206101403",
              "version": 1,
              "content": {
                "sa_serviceorderid": that.data.id,
                "issumbit": 1,
              }
            })
            that.mainData()
          })
          .catch((err) => console.log(err))
          .finally(() => Dialog.close());
        break;
      case "删除":
        dialogConfig = {
          context: this,
          title: '提示',
          content: '确认删除当前申请单吗',
          confirmBtn: '确定',
          cancelBtn: '取消',
        };
        Dialog.confirm(dialogConfig)
          .then(async () => {
            res = await api._post({
              "id": "20230206091803",
              "version": 1,
              "content": {
                "sa_serviceorderids": [that.data.id],
              }
            })
            wx.navigateBack()

          })
          .catch((err) => console.log(err))
          .finally(() => Dialog.close());
        break;
    }
  },
  async linkWorkOrder() {
    const res = await api._post({
      "id": "20230207154203",
      "version": 1,
      "content": {
        "sa_serviceorderid": this.data.id
      }
    })
    this.setData({
      linkWorkOrders: res.data
    })
  },
  tolinkWorkOrder(e) {
    let id = e.currentTarget.dataset.item.sa_workorderid

    let app = getApp()

    app.globalData.action = ['editLeader']

    wx.navigateTo({
      url: '/Eservice/workOrderDetail/index?class=stopClick&id=' + id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })

    this.productmx()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.mainData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})