const mixins = require('@/mixins/index.js')
const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const app = getApp();
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    table: Constants.tables.income,
    cardList: [{
        name: 'main'
      },
      {
        name: 'items',
        title: mixins.$t('receivable'),//'应收',
        chooseFlag: true,
        addFlag: false,
        chooseTitle:  mixins.$t('selectCategory'),//'选类别',
        urlKey: 'selectIncomeCategory'
      },
      {
        name: 'file'
      }
    ],
    contentObj: {
      main: [{
          code: 'accDate',
          name: 'accDate',
          title:  mixins.$t('receiptDate'),//'收款日期',
          type: 'date',
          required: true,
        },
        {
          code: 'staffId',
          name: 'staffName',
          required: true,
          title:  mixins.$t('payee'),//''收款人',
          type: 'choose',
          urlKey: 'chooseStaff'
        },
        {
          code: 'orgId',
          name: 'orgName',
          required: true,
          type: 'choose',
          urlKey: 'chooseOrg'
        },
        {
          code: 'sumAmount',
          type: 'number',
          title: mixins.$t('thisPayment'),//'本次收款',
          required: true
        },

      ],
      items: [{
          code: 'receivableTypeName',
          name: '',
          type: 'str',
          title: mixins.$t('incomeCategory'),//'收入类别',
          readonly: true,
          required: true
        },
        {
          code: 'amtReceivable',
          type: 'number',
          title: mixins.$t('incomeAmount'),//''收入金额',
          required: true,
          control:'receivableTypeName'
        },

      ],
      file: [{
          code: 'remarks',
          type: 'textarea',
          title: mixins.$t('remarks'),//'备注'
        },
        {
          code: 'file',
          type: 'uploader',
          title: mixins.$t('uploader'),//'附件'
        }
      ]
    },
    totalObj: {
      items: [{
          code: 'receivableWaive',
          title: mixins.$t('totalAccountsReceivable'),//'合计应收',
        },
        {
          code: 'verificationAmount',
          title: mixins.$t('verificationAmount'),//'核销金额',
        },
      ],
    },
    // 弹出按钮
    buttonList: [{
      name: 'merge',
      title: mixins.$t('collection')
    }],
    contentList: [{
      code: 'need',
      title: mixins.$t('totalAmount'),
      type: 'str'
    }, {
      code: 'amount',
      content: 0,
      type: 'number'
    }],
    btnFormData: null,
    routeObjName: 'otherReceivable'
  },
  /**
  * @desc : 给表单赋值
  * @date : 2024/2/1 15:49
  * @author : 王英杰
  */
 setValuesByEdit(data) {
  console.log("setValuesByEdit", data);
  let  formData = JSON.parse(this.data.formData)
  let cardList = this.data.cardList
  formData = data.data
  data.dataItem.forEach(element => {
    element.dataId = element.receivableType
    element.receivableTypeName = element.receivableTypeName
  });
  formData.itemList = data.dataItem
  formData.cusId = data.data.cusId
 
  let objInfo = {
    cusId: data.data.objectId,
    cusCode: data.data.cusCode,
    cusName: data.data.cusName,
    cusPhone: data.data.cusPhone, 
  }
  formData.objInfo  = objInfo
  formData.sumAmount  = data.data.sumAmtReceivable
  this.setData({
    itemData: data,
    formData: JSON.stringify(formData),
    cardList:cardList,
  })
  this.changeItemNumberField()
  this.changeNumber()
},
  /**
   * @desc   : 绑定数据-选择页面返回的逻辑处理
   * @author : 王英杰
   * @date   : 2024年3月12日
   */
  chooseData(e) {
    console.log("t", e, this.data.formData);
    let formData = JSON.parse(this.data.formData)
    let cardList_ = this.data.cardList
    let code = e.detail.code
    let data = e.detail.data.data
    if (code == "supId") { //供应商
      formData.supId = data.id
      formData.supName = data.name
    }
    if (code == "staffId") { //员工
      formData.staffId = data.id
      formData.staffName = data.name
    }
    if (code == "orgId") { //部门
      formData.orgId = data.id
      formData.orgName = data.name
    }
    if (code == "items") { //选单 
      if (data && data.list) {
        data.list.forEach(element => {
          element.receivableType = element.dataCode
          element.receivableTypeName = element.dataValue
        });
        formData.itemList = data.list
      }
    }

    this.setData({
      cardList: cardList_,
      formData: JSON.stringify(formData)
    })
  },
    /**
   * @desc   : 删除明细回调
   * @author : 王英杰
   * @date   : 2024年3月12日
   */
  delItem(){
    let formData = JSON.parse(this.data.formData)
    let cardList = this.data.cardList
    let contentList = this.data.contentList
    if (formData.itemList) {
      let itemNumber = 0
      formData.itemList.forEach(element => {
        if (!isNaN(element.amtReceivable)) { //是数字才能加减
          itemNumber = Number(itemNumber)+ Number(element.amtReceivable)
        }
      });
      cardList.forEach(element => {
        if (element.name == 'items') {
          element.receivableWaive = itemNumber
        }
      }); 
      contentList.forEach(it => {
        if (it.type == 'number') {
          it.content = itemNumber
        }
      })
      this.setData({
        cardList: cardList,
        contentList:contentList
      })
    }
  },
    /**
   * @desc : form表单 上 number类型修改回调
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  changeNumber() {
    let formData = JSON.parse(this.data.formData)
    let cardList = this.data.cardList
    if (formData.itemList) { 
      cardList.forEach(element => {
        if (element.name == 'items') {
          element.verificationAmount = formData.sumAmount
        }
      }); 
  
      this.setData({
        cardList: cardList, 
      })
    }
  },
  /**
   * @desc : 明细里得 收入金额 输入完回调 计算出合计
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  changeItemNumberField() {
    let formData = JSON.parse(this.data.formData)
    let cardList = this.data.cardList
    let contentList = this.data.contentList
    if (formData.itemList) {
      let itemNumber = 0
      formData.itemList.forEach(element => {
        if (!isNaN(element.amtReceivable)) { //是数字才能加减
          itemNumber = Number(itemNumber)+ Number(element.amtReceivable)
        }
      });
      cardList.forEach(element => {
        if (element.name == 'items') {
          element.receivableWaive = itemNumber
        }
      }); 
      contentList.forEach(it => {
        if (it.type == 'number') {
          it.content = itemNumber
        }
      })
      this.setData({
        cardList: cardList,
        contentList:contentList
      })
    }
  },
 /**
     * @desc : 保存数据服务--校验
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    validData() { 
      let cardList = this.data.cardList 
      for(let i = 0 ;i<cardList.length;i++){
        if (cardList[i].name == 'items') {
          if (cardList[i].receivableWaive <cardList[i].verificationAmount ) { //核销金额不能大于应收金额
            util.showToast('核销金额不能大于应收金额');
            return false
          }
        }
      }
   
    return true
    },
  /**
   * @desc : 保存的參數設置
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setParams(params) {
    let formData = JSON.parse(this.data.formData)
    //收付款类型-收款的查询参数
    params.objectType = '对象类型-客户'
    params.objectId = formData.objInfo.cusId
    params.orgId = formData.orgId //组织部门
    params.staffId = formData.staffId // 员工
    params.accDate = formData.accDate
    params.annexPaths = formData.annexPaths || [] //附件 
    params.remarks = formData.remarks // 备注
    params.makeStaff = app.globalData.user.staffId // 制单人 

    let itemList = []
    let itemNumber = 0
    formData.itemList.forEach(element => {
      if (!isNaN(element.amtReceivable)) { //是数字才能加减
        itemNumber = Number(itemNumber)+ Number(element.amtReceivable)
      }
      let item = {}
      item.receivableType = element.dataId //收入类别
      item.amtReceivable = element.amtReceivable //收入金额  
      itemList.push(item)
    });
    params.itemList = itemList //商品明细 
    params.sumAmtReceivable = itemNumber
    console.log("setParams", params);
    return params
  },

  /**
   * @desc : 保存数据服务
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  saveData() {
    if (this.data.formMode == Constants.formMode.edit) {
       return this.excute(this.data.service, this.data.service.update, this.data.params)
    }else{
      this.open()
    }  
  },
  /**
   * @desc : 保存数据服务-跳转收款页面
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  open(e) {
    let _this = this
    wx.navigateTo({
      url: '/package-base-select/pages/select-collection_confirm/select-collection_confirm',
      events: {
        // 回调后
        bindData: function (data) {
          // 保存成功后 回调
          wx.navigateBack({
            data: 1
          })
        }
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据 
        res.eventChannel.emit('params', {
          item: _this.data.formData,
          formType: 7
        })
      }
    })
  },
  /**
     * @desc : 页面加载数据方法
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    loadInit() {
      if (this.data.formMode == Constants.formMode.edit) {
          wx.setNavigationBarTitle({
              title: mixins.$t('otherPaymentReceiptsEdit'),
          })
      } else {
          wx.setNavigationBarTitle({
              title: mixins.$t('otherPaymentReceiptsAdd'),
          })
      }
  },
})