// packageA/remitVoucher/modules/remitDetail.js
const _Http = getApp().globalData.http
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    detail: {
      type: Object
    },
    disabled:{
      type:Boolean
    }
  },
  options: {
    addGlobalClass:true,
  },
  /**
   * 组件的初始数据
   */
  data: {
    content: {
      nocache: true,
      pageNumber: 1,
      pageTotal: 1,
      total: null
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    isEdit () {
      if (this.data.disabled) wx.showToast({
				title: '当前状态不可编辑！',
				icon: "none"
			})
    },
    getList(id, init) {
      let content = this.data.content;
      content.sa_paybillid = id;
      if (init) content.pageNumber = 1;
      _Http.basic({
        "id": "20221227092804",
        content
      }).then(res => {
        console.log("打款明细", res)
        if (res.msg != '成功') return wx.showToast({
          title: res.msg,
          icon: "none"
        })
        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,
          "content.total": res.total,
          sa_paybillid: id
        })
      })
    },
    /* 添加明细 */
    addProduct () {
      wx.navigateTo({
        url: `/packageA/remitVoucher/modules/selectAccount/index?params=${
          JSON.stringify({
            "id": 20221228085004,
            "content": {
              "pageNumber": 1,
              "pageSize": 20,
              "sa_paybillid":getCurrentPages()[getCurrentPages().length - 1].data.sa_paybillid,
              "where": {
                  "condition": ""
              }
            }
          })
        }`,
      })
      getApp().globalData.handleSelect = this.handleSelect.bind(this)
    },
    /* 操作选中的账户信息 */
    async handleSelect (data) {
      wx.navigateBack()
      let paybilldetails = data.list.map(item => {
        return {
          "sa_paybilldetailid": 0,
          "sa_accountclassid": item.sa_accountclassid,
          "amount": item.amount
        }
      }),page = getCurrentPages()[getCurrentPages().length - 2]
      let res = await _Http.basic({
        "id":20221227092904,
        "content": {
          sa_paybillid:page.data.sa_paybillid,
          paybilldetails
        }
      })
      if( res.msg == '成功' ) {
        this.getList(page.data.sa_paybillid,true)
      }
    },
    /* 金额改变 */
    async priceChange (e) {
      console.log(e);
      let data = e.currentTarget.dataset.data
      if (e.detail.value == data.amount) return
      let res = await _Http.basic({
        "id":20221227092904,
        "content": {
          sa_paybillid:data.sa_paybillid,
          paybilldetails: [
            {
              "sa_paybilldetailid": data.sa_paybilldetailid,
                "sa_accountclassid":data.sa_accountclassid,
                "amount":e.detail.value,
            }
          ]
        }
      })
      if (res.msg == '成功') {
        this.triggerEvent('onSuccess')
      } else {
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    },
    /* 删除明细 */
    deleteProduct (e) {
      if (this.properties.detail.status != '新建') return wx.showToast({
        title: '非新建状态无法删除',
        icon:'none'
      })
      wx.showModal({
        title:'提示',
        content:'是否删除当前打款明细?',
        complete: async (res) => {
          console.log(res);
          if (res.confirm) {
            let res = await _Http.basic({
              "id": 20221227093004,
              "content": {
                  "sa_paybillid":e.currentTarget.dataset.data.sa_paybillid,
                  "sa_paybilldetailids":[e.currentTarget.dataset.data.sa_paybilldetailid]
              }
            })
            if ( res.msg == '成功' ) {
              this.getList(getCurrentPages()[getCurrentPages().length - 1].data.sa_paybillid,true)
            } else {
              console.log('出发');
              wx.showToast({
                title: res.msg,
                icon:'none'
              })
            }
          }
        }
      })
    }
  }
})
