const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js');
const { onLoad } = require('@/mixins/index.js');
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */

  data: {
    // 主键Id
    primaryKey: 'orderId',
    routeObjName: 'order',
    pageFlag: false,
    other: {
      id: ''
    },
    cardList: [
      { name: 'items', title:  mixins.$t('goodsDetail'), chooseGoodsFlag: false, displayNum: 2 },
      { name: 'other', title:  mixins.$t('otherInfo') }
    ],
    contentObj: {
      items: [
        { code: 'collectType', name: 'collectName', type: 'drop', required: true, dropType: 'collect' },
        { code: 'collectAmount', type: 'number', title:  mixins.$t('amountCollected'), required: true }
      ],
      other: [
        // { code: 'contractNo', type: 'textarea', title:  mixins.$t('contractNumber'), readonly: true },
        { code: 'deliveryDate', type: 'textarea', title:  mixins.$t('deliveryDate'), readonly: true },
        { code: 'remarks', type: 'textarea', title:  mixins.$t('remarks'), readonly: false, readonly: true },
        { code: 'file', type: 'uploader', title:  mixins.$t('uploader'), readonly: true }
      ]
    },
    //按钮
    buttonSaveList: [
      { code: 'collection', title: mixins.$t('collection'), width: '120rpx', color: '#CAA977' },
      { code: 'save', title: mixins.$t('save'), width: '120rpx', color: '#1B365D' }
    ],
    contentSaveList: [
      { code: 'flag', title: mixins.$t('saleOutBoundFlag'), type: 'checkbox' },
      { code: 'receivable', content: 0, type: 'number' ,title: mixins.$t('saleCollection')},
      {
        code: 'factAmt',
        content: 0,
        type: 'number',
        title: mixins.$t("factAmt")
      },
    ],
    popContent: [
      { code: 'itemQty', type: 'step', title: mixins.$t("count"), required: true },
      { code: 'packageBox', type: 'packageBox', title: mixins.$t("packageBox"),   },
      // { code: 'box', type: 'step', title: mixins.$t("goodsBox"), required: true },
      // { code: 'piece', type: 'step', title: mixins.$t("goodsPiece"), required: true },
      { code: 'priceSale', type: 'number', title: mixins.$t("priceSale"), required: true },
      {
        code: 'whId', name: 'whName', title: mixins.$t("warehouseId"), type: 'choose', urlKey: 'openingInventory',
      },
      { code: 'nonStdCode', title: mixins.$t("goodsBatch"), type: 'str', },
      { code: 'usePlace', title: mixins.$t("usePlace"), type: 'str', },
      { code: 'flgGift', title: mixins.$t("flgGift"), type: 'checkbox', },
    ],

    //按钮
    buttonSaveList: [
      { code: 'collection', title: mixins.$t('collection'), plain: true }
    ],
    contentSaveList: [
      { code: 'receivable', content: 0, type: 'number', title:  mixins.$t('saleCollection') },
      {
        code: 'factAmt',
        content: 0,
        type: 'number',
        title: mixins.$t("factAmt")
      },
    ],
  },



  /**
    * @desc : 给表单赋值
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  setValuesByEdit(data) {
    let cardList = this.data.cardList
    let contentSaveList = this.data.contentSaveList
    let formData = JSON.parse(this.data.formData)
    formData = data.data

    data.dataItem.forEach(res => {
      //处理图片
      if (res.skuImages) {
        res['imagesUrl'] = res.skuImages.length > 0 ? res.skuImages[0].url : null
      }
    })

    formData.goodsList = data.dataItem
    formData.goodsList.forEach(element => {
      if (element.flgSubUnit) {
        element.calculationFormula = element.packBox + element.unitName + '= 1' + element.subUnitName
        element.packageBox = element['box'] + element.subUnitName + element['piece'] + element.unitName
      }
    });
    let index = cardList.findIndex(it => it.name == 'items');
    cardList[index].sumAmount = data.data.sumAmount
    cardList[index].sumStandard = data.data.sumStandard
    cardList[index].sumQuantity = data.data.sumQuantity
    cardList[index].saleDiscount = data.data.saleDiscount 
    cardList[index].discount =  data.data.discount //整单折扣
    cardList[index].discAmt =  data.data.discAmt //优惠金额
    cardList[index].factAmt =  data.data.factAmt // 实际合计金额  
    //组件显示 需收款 
    let index_factAmt = contentSaveList.findIndex(it => it.code == 'factAmt');
    if(index_factAmt!=-1){
      contentSaveList[index_factAmt]['content'] =  data.data.factAmt
    }
     let index_receivable = contentSaveList.findIndex(it => it.code == 'receivable');
    if(index_receivable!=-1){
      contentSaveList[index_receivable]['content'] =  data.data.amtResidue
    } 

    this.setData({
      item: data.data,
      contentSaveList: contentSaveList,
      formData: JSON.stringify(formData),
      cardList: cardList
    })
  },

  saveButton() {
    let formData = JSON.parse(this.data.formData)
    let item = {}
    formData.objInfo = {}
    formData.objInfo['cusPhone'] = formData.cusPhone
    formData.objInfo['cusName'] = formData.cusName
    formData.objInfo['objectId'] = formData.cusId
    formData.objInfo['addressFull'] = formData.addressFull
    formData.sumAmount = formData.amtResidue
    formData.deleteItemList = []
    formData.itemList = formData.goodsList
    formData.updateFlag = true
    let _this = this
    wx.navigateTo({
      url: this.data.route.selectCollectionConfirm.url,
      events: {
        // 回调后
        bindData: function (data) {
          // 保存成功后 回调
          wx.navigateBack({
            data: 1
          })
        }
      },
      success: function (res) {
        //设置参数
        //let item = _this.setSaveData()
        res.eventChannel.emit('params', { item: JSON.stringify(formData), formType: 8 })
      }
    })
  },


  /**
     * @desc : 加载数据
     * @author : 于继渤
     * @date : 2024/1/23 9:16
     */
  getData(params) {
    return this.excute(this.data.service, this.data.service.getOrderForUpdate + '/' + this.data.item.orderId, params);
  },

  /**
    * @desc : 详细页面 （编辑）
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  toEdit(e) {
    let formData = JSON.parse(this.data.formData)
    let orderId = formData.orderId
    let that = this
    wx.navigateTo({
      url: this.data.route.add.url,
      events: {
        // 回调后，在这里给页面赋值
        bandData: function (e) {
        }
      },
      success: function (res) {
        res.eventChannel.emit('params', {
          id: orderId,
          formMode: Constants.formMode.edit
        })
      }
    })
  },
})