const mixins = require('@/mixins/index.js')
const app = getApp()
const util = require('@/utils/util.js')
const Constants = require('@/utils/Constants.js');
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    cardList: [{
      name: 'items'
    }],
    buttonSaveList: [{
      code: 'add',
      title: mixins.$t('confirm'),
      width: '120rpx'
    }],
    routeObjName: 'goodsBrand',
    contentObj: {
      items: [{
          code: 'supId',
          name: 'supName',
          title: mixins.$t("supplier"), //'供应商',
          type: 'choose',
          required: true,
          urlKey: 'chooseOutSupplier'
        },
        {
          code: 'skuModel',
          type: 'str',
          title: mixins.$t("skuModel"),
          required: true
        },
        {
          code: 'skuName',
          type: 'str',
          title: mixins.$t("skuName"),
        },
        {
          code: 'itemQty',
          required: true,
          type: 'number',
          sign: "",
          digits: 0,
          title: mixins.$t('count')
        },
        {
          code: 'itemPrice',
          required: true,
          type: 'number',
          title: mixins.$t("salePrice")
        },
        {
          code: 'itemAmt',
          readonly: true,
          type: 'number',
          title: mixins.$t("amount")
        },
        {
          code: 'remarks',
          type: 'str',
          title: mixins.$t("remarks")
        }
      ],
    }

  },
  chooseData(e) {
    console.log(e)
  },
  /**
   * @desc : number输入事件
   * @date : 2024/5/11 15:49
   * @author : 于继渤
   */
  changeItemNumberField(e) {
    let formData = JSON.parse(this.data.formData)
    let key = e.detail.key
    let index = e.detail.index
    if (key == 'itemPrice' || key == 'itemQty') {
      //计算金额
      formData.itemList[index]['itemAmt'] = Number(formData.itemList[index]['itemPrice'] * formData.itemList[index]['itemQty'])
    }
    this.setData({
      formData: JSON.stringify(formData)
    })
  },

  /**
   * @desc : 确定
   * @date : 2024/5/11 15:49
   * @author : 于继渤
   */
  saveButton(e) {
    let formData = JSON.parse(this.data.formData)
    console.log(formData.itemList) 
    formData.itemList.forEach(res => {
      if (this.data.item == Constants.billType.saleOut) {
        res['priceOut'] = res.itemPrice
        res['outingQty'] = res.itemQty
        res['outingAmt'] = res.itemAmt
      }
      if (this.data.item == Constants.billType.sale) {
        res['priceSale'] = res.itemPrice 
        res['sumPrice'] = res.itemAmt
      }
      res['brandName'] =  res['supName']+"(外协)"
    })



    const eventChannel = this.getOpenerEventChannel();
    eventChannel.emit('bindData', {
      choosedGoodsList: formData.itemList
    })
    wx.navigateBack({
      delta: 1
    })
  },




  /**
   * 生命周期函数--监听页面加载
   */
  loadInit(options) {
    // let formData = JSON.parse(this.data.formData)
    // let itemList = JSON.parse(formData.itemList)
    // console.log('itemList',itemList)
    // itemList[0]['itemQty'] = 1
    // itemList[0]['itemSale'] = 1
    // itemList[0]['itemAmt'] = 0
    // formData.itemList = JSON.stringify(this.data.itemList)
    // this.setData({
    //   formData: JSON.stringify(this.data.formData)
    // })

  },


})