// package-basic-data/pages/test/add/add.js
const mixins = require('@/mixins/index.js')
const Constants = require('@/utils/Constants');
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    // 弹出按钮
    buttonList: [{
      name: 'merge',
      title: ''
    }],
    contentList: [{
      code: 'need',
      title:  mixins.$t("totalAmount"), //合计金额
      type: 'str'
    }, {
      code: 'amount',
      content: 2000,
      type: 'number'
    }],
    cardList: [{
      name: 'warehouse'
    }, {
      name: 'relation'
    }, {
      name: 'items',
      title:  mixins.$t("goodsDetail"), //'商品明细'
      chooseGoodsFlag: '',
      itemAmt: '',
      displayNum: 2
    }, {
      name: 'other',
      title:  mixins.$t("otherInfo") //'其他信息'
    }],
    contentObj: {
      warehouse: [{ //入库仓库
        code: 'whId',
        name: 'whName',
        title: mixins.$t("whName"),
        type: 'choose',
        urlKey: 'chooseWh',
      }],
      relation: [{
        code: 'sourceNo',
        name: 'fromNo',
        type: 'choose',
        controlChooseFlag: true,
        title:  mixins.$t("sourceOrder"),//'关联源单'
        urlKey: 'choosePurchaseOrder'
      }],
      items: [  ],

      other: [{
        code: 'remarks',
        type: 'textarea',
        title:  mixins.$t("remarks"),//'备注'
      }, {
        code: 'file',
        type: 'uploader',
        title: mixins.$t("uploader"),//'附件'
      }]
    },
    popContent: [{
        code: 'intoingQty',
        type: 'step',
        title: mixins.$t("count"),//'数量',
        required: true
      },
      {
        code: 'whId',
        name: 'whName',
        title: mixins.$t("warehouseId"),//'仓库',
        type: 'drop',
        dropType: 'warehouse'
      },
      {
        code: 'skuSpec',
        title:  mixins.$t("goodSpec"),//'规格',
        type: 'str',
        readonly: true
      },
      {
        code: 'nonStdCode',
        title: mixins.$t("goodsBatch"),//'商品批次',
        type: 'str',
      },
    ],


    inboundService: app.globalData['inboundService'],
    inboundProcessingService: app.globalData['inboundProcessingService'],
    // 路由
    routeObjName: 'inbound',
  },
    /**
   * @desc : （编辑）
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  toEdit(e) {
    let intoId = e.currentTarget.dataset.item.intoId
    wx.navigateTo({
      url: this.data.route.add.url,
      events: {
        // 回调后，在这里给页面赋值
        bandData: function (e) {}
      },
      success: function (res) {
        res.eventChannel.emit('params', {
          id: intoId,
          formMode: Constants.formMode.edit
        })
      }
    })
  },
  /**
   * @desc : dk-from choose 回调事件
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  chooseData(e) {
    console.log("t", e, this.data.formData);
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = e.detail.data.data
    if (code == "whId") { //商品品牌
      formData.whId = data.id
      formData.whName = data.name
    }

    this.setData({
      formData: JSON.stringify(formData)
    })
  },
  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setValuesByEdit(data) {
    let cardList_ = this.data.cardList
    let contentList_ = this.data.contentList
    let sumitemAmt = 0
    let formType = this.data.formType 
    if (this.data.formType == 2) { //查询采购订单表 未入库的 详细 
      this.setData({
        buttonList: [{
          name: 'merge',
          title:  mixins.$t("close"),//'关闭'
        }],
      })
    }else{
      this.setData({
        buttonList: [{
          name: 'merge',
          title:  mixins.$t("inProcessing"),//'入库办理'
        }],
      })
    }
    data.goodsList.forEach(element => { //删掉查出的无用数据
      delete element.opAppCode
      delete element.opCreateTime
      delete element.opCreateUserId
      delete element.opDbUser
      delete element.opTimestamp
      delete element.opUpdateTime
      delete element.opUpdateUserId
    });
    sumitemAmt = Number(data.intoingQty) * Number(data.intoingAmt) 
    cardList_.forEach(card => {
      if (card.name === 'items') {
        if(formType ==1 ){
          card.info = "（"+ mixins.$t("warehousingProcessing"),+"）",
          card.color = "#E4002B"
        }else {
          card.info = "（"+ mixins.$t("checkined"),+"）",
          card.color = "#1B365D"
        } 
        card.itemAmt = sumitemAmt
      }
    })
    contentList_[1].content = sumitemAmt

    // 刷新商品明细 显示 查看更多
    let card = this.data.cardList.filter(it => it.name == 'items');
    this.selectComponent('#'+this.data.formName).handleContentObj(card[0])

    this.setData({
      itemData: data,
      cardList: cardList_,
      contentList: contentList_,
      formData: JSON.stringify(data),
      sumitemAmt
    })

  },


  /**
   * @desc : 保存的參數設置
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  setParams(params) {

    let formData = JSON.parse(this.data.formData)
    formData.goodsList.forEach(element => {
      element.sPurId = element.fromId //来源id
      element.sPurItemId = element.fromItemId //来源单明细id
    });
    params.allEdit = true //大编辑
    params.itemList = formData.goodsList //商品明细
    params.deleteItemList = []
    params.fromId = formData.fromId //来源id
    params.fromNo = formData.fromNo //来源单号 
    params.orgId = formData.orgId //组织部门
    params.staffId = formData.staffId // 员工
    params.supId = formData.supId // 供应商
    params.whId = formData.whId // 仓库
    params.remarks = formData.remarks // 备注
    params.makeStaff = formData.staffId // 制单人
    params.intoDate = this.formatDate()
    console.log("save", this.data.params);
    return params
  },
  /**
   * @desc : 保存数据服务
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  saveData() {
    if(this.data.formType==1){ 
      return this.excute(this.data.inboundProcessingService, this.data.inboundProcessingService.inBoundHandle, this.data.params)
    }else{
       wx.navigateBack({
        delta: 1
      }) 
    } 
  },
  formatDate() {
    var date = new Date();
    var year = date.getFullYear();
    var month = ("0" + (date.getMonth() + 1)).slice(-2);
    var day = ("0" + date.getDate()).slice(-2);

    return year + "-" + month + "-" + day;
  },
})