/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：  入库办理列表-采购入库 其它入库 销售退货出库的数据240408
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		姜永辉      2024-03-12   1.00		   	    
 *******************************************************************************/
const Constants = require('@/utils/Constants.js');
const mixins = require('@/mixins/index.js')

const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据 
   */
  data: {
    // 待入库 已入库的taglist
    tagList: [
      { title: mixins.$t('warehousingProcessing') },
      { title: mixins.$t('checkined') }],
    // 查询条件
    searchContent: [{
      code: 'makeTime',
      title: mixins.$t('pastThirthtyDays'),
      defaultValue: 5,
      searchType: Constants.searchType.date
    }, {
      code: 'choose',
      title: mixins.$t('choose'),
      searchType: Constants.searchType.pick
    }],

    // 查询条件-筛选
    pullMenuList: [
      {
        code: 'intoType',
        pullType: 'mSelect',
        typeName: 'intoType'
      },
      // {
      //   code: 'inReason',
      //   pullType: 'mSelect',
      //   typeName: 'intoReason'
      // },
      {
        code: 'cusName',
      },
      {
        code: 'cusPhone',
      },
      {
        code: 'addressFull',
      },
      {
        code: 'supName',
      },
      {
        code: 'staffName',
      },
      {
        code: 'orgName',
      },
      { 
        code: 'flgValid',
        title: mixins.$t('flgValid'),
        dataType: 'checkbox'
      },
    ],

    // 列表区（脚部金额）
    footerAmount: {
      name: 'intoingAmt',
      title: mixins.$t('totalAmount')
    },
    // 列表区（脚部信息）
    footerInfo: [{ prefix: mixins.$t('together'), name: 'intoQty', title: mixins.$t('piece') }],
    // 列表区（内容）
    contentList: [],
    // 列表的显示字段
    contentObj: {
      '采购入库': [{
        name: 'intoNo',
        title: mixins.$t('orderNo')
      }, {
        name: 'whName',
        title: mixins.$t('eiWhName')
      }, {
        name: [{
          name: 'orgName',
          title: ''
        }, {
          name: 'staffName',
          title: mixins.$t('staffId')
        }],
        title: mixins.$t('purInfo')
      }, {
        name: 'intoDate',
        title: mixins.$t('intoDate')
      }],
      '销退出库': [{
        name: 'intoNo',
        title: mixins.$t('orderNo')
      }, {
        name: 'cusPhone',
        title: mixins.$t('cusPhone')
      }, {
        name: 'addressFull',
        title: mixins.$t('addressFull')
      }, {
        name: [{
          name: 'orgName',
          title: ''
        }, {
          name: 'staffName',
          title: mixins.$t('staffId')
        }],
        title: mixins.$t('storeInformation')
      }, {
        name: 'intoDate',
        title: mixins.$t('intoDate')
      }],
      '其他入库': [{
        name: 'intoNo',
        title: mixins.$t('inOrderNo')
      }, {
        name: 'intoReasonName',
        title: mixins.$t('intoReason')
      }, {
        name: [{
          name: 'orgName',
          title: ''
        }, {
          name: 'staffName',
          title: mixins.$t('staffId')
        }],
        title: mixins.$t('cpManager')
      }, {
        name: 'intoObject',
        title: mixins.$t('inObject')
      }, {
        name: 'intoDate',
        title: mixins.$t('intoDate')
      }],

    },
    // 弹出按钮
    buttonList: [
      {
        name: 'invaidOrder',
        title: mixins.$t('invaidOrder'),

      }, {
        name: 'ticketPirnt',
        title: mixins.$t('ticketPirnt'),
      }
      , {
        name: 'editOrder',
        title: mixins.$t('edit'),
      }
    ],
    // 底部保存按钮
    buttonFootList: [{
      name: 'addOther',
      title: mixins.$t('otherInWh'),
    }],
    // 底部左侧合计金额及数量
    contentnFootList: [
      {
        title: mixins.$t('totalAmount'),
        code: 'intoingAmt',
        content: 10,
        type: 'number'
      }, {
        type: 'count',
        code: 'count',
        bill: 1,
        quantity: 1
      }
    ],
    // 底部的按钮数据
    btnFormData: null,
    // 路由
    routeObjName: 'inboundProcessing',
    inboundOtherService: app.globalData['inboundOtherService'],
    outboundSaleReturnService: app.globalData['outboundSaleReturnService'],
    // active
    active: 0,
    // 主键Id
    primaryKey: 'intoId',
  },

  /**
   * @desc : 切换 待入库 已出库
   * @date : 2024年3月8日
   * @author : 姜永辉
   */
  onChangeTabs(e) {
    let index = e.detail.detail.index
    this.setData({
      active: index,
    })
    let buttonList = this.data.buttonList
    buttonList = buttonList.filter(item => item.name !== 'returnCancel')
    console.log();
    if (index == 1 && buttonList.length == 3) {
      buttonList.push(
        {
          name: 'returnCancel',
          title: mixins.$t('returnCancel'),
        }
      )
    }
    this.setData({
      buttonList,
    })
    this.searchData()
  },

  /**
   * @desc : addOther
   * @date : 2024年3月8日
   * @author : 姜永辉
   */
  addOther() {
    wx.navigateTo({
      url: this.data.route.addOther.url,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
        res.eventChannel.emit('params', {})
      }
    })
  },

  /**
   * @desc : 跳转明细
   * @author : 周兴
   * @date : 2024/1/22
   */
  toDetail(e) { 
    if (e) {
      let item = e.detail.item;      
      if (e.detail.item) {
        let button = {
          formMode: Constants.formMode.edit
        }
        e.detail.item.button = button
      }
      // 如果name不传递，默认为edit
      if (item.intoType == Constants.intoType.pur || item.intoType == Constants.intoType.other) {
        e.detail.name = 'edit'
      } else {
        e.detail.name = 'editReturn'
      }
    }
    this.open(e);
  },

  /**
   * @desc : 设置查询参数
   * @date : 2024年3月8日
   * @author : 姜永辉
   */
  setSearchParams(params) {
    if (this.data.active == 0) {
      //待入库的查询参数
      params.intoStatus = Constants.intoStatus.inBounding
      params.outStatus = Constants.outStatus.outBounding
    }
    if (this.data.active == 1) {
      //已入库的查询参数 
      params.intoStatus = Constants.intoStatus.inBounded
      params.outStatus = Constants.outStatus.outBounded
    }
    params.intoTypes = params.intoTypeList

    return params;
  },
  /** 
   * @desc : 查询 入库单
   * @date : 2024年3月8日
   * @author : 姜永辉
   */
  getData(params) {
    return this.excute(this.data.inboundOtherService, this.data.inboundOtherService.selectInbound, params);
  },
  /**
   * @desc : 处理接口返回数据
   * @date : 2024年3月8日
   * @author : 姜永辉
   */
  handleSearchData(tableData) {
    let temp = tableData
    let contentnFootList = this.data.contentnFootList
    let footerAmount = this.data.footerAmount
    let footerInfo = this.data.footerInfo
    let intoingQty = Number(0)
    let intoingAmt = Number(0)
    if (this.data.active == 0) {
      // 列表区（脚部金额）
      footerAmount.name = 'intoingAmt'
      // 列表区（脚部信息）
      footerInfo[0].name = 'intoingQty'
      temp.forEach(it => {
        it.makeTime = it.makeTime.substring(0, 10)
        intoingQty += Math.abs(Number(it.intoingQty))
        intoingAmt += Math.abs(Number(it.intoingAmt))
        // 入库对象
        if (it.intoType == Constants.intoType.other) {
          it.intoObject = it.cusName != null ? it.cusName : it.supplierName
        }
      })
    } else {
      // 列表区（脚部金额）
      footerAmount.name = 'intoAmt'
      // 列表区（脚部信息）
      footerInfo[0].name = 'intoQty'
      temp.forEach(it => {
        it.makeTime = it.makeTime.substring(0, 10)
        intoingQty += Math.abs(Number(it.intoQty))
        intoingAmt += Math.abs(Number(it.intoAmt))
        // 入库对象
        if (it.intoType == Constants.intoType.other) {
          it.intoObject = it.cusName != null ? it.cusName : it.supplierName
        }
      })
    }

    contentnFootList.forEach(it => {
      if (it.type == 'count') {
        it.bill = tableData.length
        it.quantity = intoingQty
      }
      if (it.type == 'number') {
        it.content = intoingAmt
      }
    })
    this.setData({
      tableData: temp,
      footerInfo,
      footerAmount,
      contentnFootList,
    })
  },
  /**
    * @desc : （撤销）
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  returnCancel(e) {
    let item = e
    let id = item.intoId
    // 采购入库
    if (item.intoType == Constants.intoType.pur) {
      this.excute(this.data.service, this.data.service.inBoundCancel, item).then(res => {
        if (res.data.code == Constants.SUCESS_CODE) {
          // 重新查询
          this.searchData()
        }
      })
    }
    // 其他入库
    else if (item.intoType == Constants.intoType.other) {
      this.excute(this.data.inboundOtherService, this.data.inboundOtherService.inBoundCancel, item).then(res => {
        if (res.data.code == Constants.SUCESS_CODE) {
          // 重新查询
          this.searchData()
        }
      })
      // 销售退货
    } else {
      this.excute(this.data.outboundSaleReturnService, this.data.outboundSaleReturnService.cancel, { outId: id }).then(res => {
        if (res.data.code == Constants.SUCESS_CODE) {
          // 重新查询
          this.searchData()
        }
      })
    }
  },

  /**
  * @desc : （编辑）
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  editOrder(e) {
    let item = e
    let id = item.intoId
    // 采购入库
    if (item.intoType == Constants.intoType.pur) {
      wx.navigateTo({
        url: this.data.route.add.url,
        events: {
          // 回调后，在这里给页面赋值
          bandData: function (e) { }
        },
        success: function (res) {
          res.eventChannel.emit('params', {
            id: id,
            formMode: Constants.formMode.edit
          })
        }
      })
    }
    // 其他入库
    else if (item.intoType == Constants.intoType.other) {
      wx.navigateTo({
        url: this.data.route.addOther.url,
        events: {
          // 回调后，在这里给页面赋值
          bandData: function (e) { }
        },
        success: function (res) {
          res.eventChannel.emit('params', {
            id: id,
            formMode: Constants.formMode.edit
          })
        }
      })
      // 销售退货
    } else {
      wx.navigateTo({
        url: this.data.route.addReturn.url,
        events: {
          // 回调后，在这里给页面赋值
          bandData: function (e) { }
        },
        success: function (res) {
          res.eventChannel.emit('params', {
            id: id,
            formMode: Constants.formMode.other,
            formType: Constants.formType.edit,
          })
        }
      })
    }

  },

  /**
    * @desc : 作废
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  invaidOrder(e) {
    let item = e
    let id = item.intoId
    // 采购入库
    if (item.intoType == Constants.intoType.pur) {
      this.excute(this.data.service, this.data.service.inboundRepeal + id, null).then(res => {
        if (res.data.code == Constants.SUCESS_CODE) {
          // 重新查询
          this.searchData()
        }
      })
    }
    // 其他入库
    else if (item.intoType == Constants.intoType.other) {
      this.excute(this.data.inboundOtherService, this.data.inboundOtherService.inboundRepeal + id, null).then(res => {
        if (res.data.code == Constants.SUCESS_CODE) {
          // 重新查询
          this.searchData()
        }
      })
      // 销售退货
    } else {
      this.excute(this.data.outboundSaleReturnService, this.data.outboundSaleReturnService.outboundRepeal + id, null).then(res => {
        if (res.data.code == Constants.SUCESS_CODE) {
          // 重新查询
          this.searchData()
        }
      })
    }

  },


})