/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		王英杰		 2024-1-24			1.00		    采购订单
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
const Constants = require('@/utils/Constants');
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    // 弹出按钮
    buttonList: [{
      name: 'merge',
      title: mixins.$t('close')//'关闭'
    }],
    contentList: [{
      code: 'name',
      title: mixins.$t('totalAmount'),
      type: 'str'
    }, {
      code: 'amount',
      content: 0,
      type: 'number'
    }],
    cardList: [{
      name: 'items',
      title:  mixins.$t('goodsDetail'),//'商品明细',
      chooseGoodsFlag: '',
      itemAmt: '',
      displayNum: 2
    }, {
      name: 'other',
      title: mixins.$t('otherInfo'),//'其他信息'
    }],
    contentObj: {
      items: [{
        code: 'sumQuantity',
        name: 'collectName',
        type: 'drop',
        required: true,
        dropType: 'collect',

      }, {
        code: 'sumAmount',
        type: 'number',
        title: mixins.$t('amountCollected'),//'收款金额',
        required: true
      }],

      other: [{
        code: 'remarks',
        type: 'textarea',
        title:  mixins.$t('remarks'),//'备注'
      }, {
        code: 'file',
        type: 'uploader',
        title:  mixins.$t('uploader'),//'附件'
      }]
    },
    popContent: [{
        code: 'itemQty',
        type: 'step',
        title: mixins.$t('count'),//'数量',
        required: true
      }, 
      {
        code: 'skuSpec',
        title: mixins.$t('goodSpec'),//'规格',
        type: 'str',
        readonly: true,
      },
      {
        code: 'nonStdCode',
        title:  mixins.$t('goodsBatch'),//'商品批次',
        type: 'str',
      },
    ],
    // 路由
    routeObjName: 'purchase',
  },


  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setValuesByEdit(data) {
    data.goodsList = data.purchaseItemResponseList
    let cardList_ = this.data.cardList
    let contentList_ = this.data.contentList

    cardList_.forEach(card => {
      if (card.name === 'items') {
        card.itemAmt = data.goodsList.sum("itemAmt")
      }
    })
    contentList_.forEach(card => {
      if (card.code === 'amount') {
        card.content = data.goodsList.sum("itemAmt")
      }
    })
        
   
    this.setData({
      item: data,
      contentList: contentList_,
      cardList: cardList_,
      formData: JSON.stringify(data)
    })
     // 刷新商品明细 显示 查看更多
     let card = this.data.cardList.filter(it => it.name == 'items');
     this.selectComponent('#'+this.data.formName).handleContentObj(card[0])
  },
  /**
   * @desc : 关闭
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  open() {
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * @desc : （编辑）
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  toEdit(e) {
    let purId = e.currentTarget.dataset.item.purId
    wx.navigateTo({
      url: this.data.route.add.url,
      events: {
        // 回调后，在这里给页面赋值
        bandData: function (e) {}
      },
      success: function (res) {
        res.eventChannel.emit('params', {
          id: purId,
          formMode: Constants.formMode.edit
        })
      }
    })
  },
})