const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const app = getApp();
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    // 路由
    routeObjName: 'freeze',
    cardList: [
      { name: 'main', title: '冻结信息' },
      { name: 'items', title: mixins.$t("goodsDetail"), chooseGoodsFlag: true, itemAmt: '', displayNum: 2 },
      { name: 'other', title: mixins.$t("otherInfo") },
    ],
    contentObj: {
      main: [
        { code: 'cusId', name: 'cusName', title: '申请对象', type: 'choose', required: true, urlKey: 'selectCustomers' },
        {
          code: 'staffId', name: 'staffName',
          title: '申请员工',
          type: 'choose',
          required: true,
          urlKey: 'chooseStaff'
        },
        {
          code: 'orgId',
          name: 'orgName',
          title: '申请部门',
          type: 'choose',
          required: true,
          urlKey: 'chooseOrg'
        },
      ],
      items: [],
      other: [
        { code: 'remarks', type: 'textarea', title: mixins.$t("remarks"), readonly: false },
        { code: 'file', type: 'uploader', title: mixins.$t("uploader") }
      ]
    },

    buttonSaveList: [{ code: 'save', title: '冻结' }],
  },

  /**
     * @desc   : 设置只读
     * @author : 于继渤
     * @date   : 2024年3月12日
     */
  setColumn() {
    let formData = JSON.parse(this.data.formData)
    let cardList = this.data.cardList
    let contentObj = this.data.contentObj
    let buttonSaveList = this.data.buttonSaveList
    cardList[1].info = '(' + formData.freezeStatusName + ')'
    cardList[1].color = '#E4002B'
    cardList[1].chooseGoodsFlag = false
    contentObj.main = [
      { code: 'cusName', title: '申请对象', type: 'str', readonly: true },
      { code: 'staffName', title: '申请员工', type: 'str', readonly: true },
      { code: 'orgName', title: '申请部门', type: 'str', readonly: true },
    ]
    if (formData.freezeStatus == '冻结状态-部分解冻' || formData.freezeStatus == '冻结状态-全部解冻' ){
      buttonSaveList =  [{ code: 'close', title: '关闭' }]
    }else{
      buttonSaveList =  [{ code: 'save', title: '解冻' }]
    }
    
    this.setData({
      cardList:cardList,
      contentObj:contentObj,
      buttonSaveList:buttonSaveList,
    })
  },

  /**
     * @desc   : 绑定数据-选择页面返回的逻辑处理
     * @author : 姜永辉
     * @date   : 2024年3月12日
     */
  chooseData(e) {
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = e.detail.data.data
    let item = e.detail.data
    console.log(e)
    if (code == "staffId") { //员工
      formData.staffId = data.id
      formData.staffName = data.name
      formData.orgId = data.orgId
      formData.orgName = data.orgName
    }
    if (code == "orgId") { //部门
      formData.orgId = data.id
      formData.orgName = data.name
    }

    //设置客户信息
    if (code == 'cusId') {
      formData.cusCode = item.cusCode
      formData.cusId = item.cusId
      formData.cusName = item.cusName
      formData.cusPhone = item.cusPhone

    }
    this.setData({
      formData: JSON.stringify(formData)
    })
  },
  /**
  * @desc : 设置保存参数
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  setParams(params) {
    let itemIndex = 0
    let sumQuantity = Number(0)
    console.log('app.globalData.user', app.globalData.user)
    params.goodsList.forEach(res => {
      res.itemIndex = itemIndex++

      res.surplusQty = res.itemQty
      res.freezeQty = res.itemQty
      res.unfreezeQty = 0
      sumQuantity +=  res.itemQty
      delete res['priceLimited']
      delete res['pricePurchase']
      delete res['priceStandard']
      delete res['priceWholesale']
      // delete res['freezeQty']
      delete res['costPrice']
      delete res['costAmt']
      delete res['outingQty']
    })
    params.itemList = params.goodsList
    params.sumQuantity = sumQuantity
    params.makeStaff = app.globalData.user.userId
    delete params['goodsList']
    return params
  },
  /**
    * @desc : 保存数据服务
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  saveData() {
    return this.excute(this.data.service, this.data.service.insert, this.data.params);
  },

  /**
    * @desc : 查询商品明细
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  getItemDetal(id) {
    return this.excute(this.data.service, this.data.service.getFreezeForUpdate + '/' + id, {}).then(res => {
      if (res.data.code == 200) {
        let formData = JSON.parse(this.data.formData)
       
        formData = res.data.data.data
        res.data.data.dataItem.forEach(it=>{
          it.itemQty = it.surplusQty
        })
        formData.goodsList = res.data.data.dataItem
     
        this.setData({
          formData: JSON.stringify(formData)
        })

      }
    });
  },
  /**
     * @desc :   编辑
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
    toEdit() {
      let this_ = this
      wx.navigateTo({
        url: this_.data.route.add.url,
        events: {
          // 保存成功后刷新页面
          refresh: function (data) {
          }
        },
        success: function (res) {
          res.eventChannel.emit('params', {
            formMode: Constants.formMode.detailEdit, id: this_.data.id
          });
        }
      })
    },
  /**
 * 生命周期函数--监听页面加载
 */
  loadInit() {
    let formData = JSON.parse(this.data.formData)
    if (this.data.formMode == Constants.formMode.detail) { //详情
      //详情办理
      wx.setNavigationBarTitle({
        title: '库存冻结详情'
      })
     
      this.getItemDetal(this.data.id)
      this.setColumn()
    }else if (this.data.formMode == Constants.formMode.detailEdit) {
      //编辑
      wx.setNavigationBarTitle({
        title: '编辑库存冻结'
      })
      this.getItemDetal(this.data.id)

    }
  }
})