/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：库存查询
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		姜永辉		 2024-1-23			1.00		    选品
 *******************************************************************************/
const Constants = require('@/utils/Constants.js');
const mixins = require('@/mixins/index.js')
const config = require('@/config/config.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    firstFlag: true,
    orderBy: [
      {
        "text": mixins.$t('SellableQuantityAsc'),
        "value": 1
      },
      {
        "text": mixins.$t('SellableQuantityDesc'),
        "value": 2
      }
    ],
    orderByValue: null,
    routeObjName: 'report',
    // routeObjName: 'goodsSku',
    dataItem: null,
    choosedGoodsList: [],
    commonService: app.globalData['commonService'],
    inventoryService: app.globalData['inventoryService'],
    typeList: [
      {
        brandName: mixins.$t('brandAllName')
      }
    ],
    brandId: null,
    active: 0,

    popupTopObj: [{
      leftTitle: mixins.$t('storageNum'), leftCode: 'usableQty',
      rightTitle: mixins.$t('sellableQuantity'), rightCode: 'invQty'
    }],
    searchContent: [{
      code: 'warehouse',
      title: mixins.$t('reservoirArea'),
      dropType: 'warehouse'
    },{
      code: 'SellableQuantityAsc',
      title: mixins.$t('SellableQuantityAsc'),
      searchType:Constants.searchType.radio, 
      value:1, 
      options: [
        {
          "text": "可售量升序",
          "value": 1
        },
        {
          "text": "可售量降序",
          "value": 2
        }
      ],
    }],
    contentList: [],
    item: Constants.billType.out,
    form: {
      whIds: [],
      whNames: ''
    }
  },

  /**
   * @desc : 加载数据
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  loadInit() {
    this.setData({
      billType: this.data.item,
      title: '库存明细表',
      objectCode: Constants.menuCode.ivtDetailReport,
      method: 'getIvtDetailReport',
      exportCols: {
        "skuCode": "商品编号",
        "skuName": "商品名称",
        "skuModel": "商品型号",
        "nonStdCode": "色号",
        "brandName": "商品品牌",
        "categoryName": "种类名称",
        "seriesName": "系列名称",
        "skuSpec": "商品规格",
        "whName": "仓库名称",
        "invQty": "库存数量",
        // "outingQty": "出库中数量",
        "freezeQty": "冻结数量",
        "usableQty": "可用数量",
        "remarks": "备注",
      },
    })
  },

  /**
  * @desc : 分类切换
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  changeSidebar(e) {
    let index = e.detail
    let typeList = this.data.typeList
    this.setData({
      brandId: typeList[index].brandId
    })
    this.searchData()
  },

  /**
   * @desc : 跳转明细
   * @author : 周兴
   * @date : 2024/1/22
   */
  toDetail(e) {
    console.log("toDetail", e);
    if (e) {
      let item = e.currentTarget.dataset.item;
      e.detail.item = item
      if (item) {
        let button = {
          formMode: Constants.formMode.edit
        }
        e.detail.item.button = button
      }
      // 如果name不传递，默认为edit
      e.detail.name = 'editIvt'
    }
    this.open(e);
  },


  /**
   * @desc : 设置查询参数
   * @author : 姜永辉
   * @date : 2024/1/23 9:16
   */
  setSearchParams(params) {
    params.brandId = this.data.brandId //品牌id 
    //顶部的选择库区是否选择用户默认的库区
    params.whIds = this.data.form.whIds //仓库  
    //排序
    params.desc = this.data.orderByValue
    return params
  },
  /**
  * @desc : 查询
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  doSearch() {
    this.searchData()
  },
  /**
   * @desc : 查询
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  getData(params) {
    if (this.data.item == Constants.billType.sale) {
      return this.excute(this.data.commonService, this.data.commonService.getGoodsForOrderByPage, params);
    } else if (this.data.item == Constants.billType.out || this.data.item == Constants.billType.purReturn) {
      console.log('params', params);
      return this.excute(this.data.inventoryService, this.data.inventoryService.selectByCond, params);
    } else {
      return this.excute(this.data.service, this.data.service.selectByCond, params);
    }
  },

  /**
     * @desc :   处理接口返回数据
     * @date : 2024/2/1 15:49
     * @author : 姜永辉
     */
  handleSearchData(tableData) {
    tableData.forEach(item => {
      let list = []
      // 商品名称
      list.push({
        title: mixins.$t('skuName'),
        value: item.skuName ? item.skuName : '',
        copyValue: item.skuName ? item.skuName : '',
      })
      // 库区
      list.push({
        title: mixins.$t('reservoirArea'),
        value: item.whName ? item.whName : '',
        copyValue: item.whName ? item.whName : '',
      })
      // 规格
      list.push({
        title: mixins.$t('goodSpec'),
        value: item.skuSpec ? item.skuSpec : '',
        copyValue: item.skuSpec ? item.skuSpec : '',
      })
      // 色号
      list.push({
        title: mixins.$t('colorNumber'),
        value: item.nonStdCode ? item.nonStdCode : '',
        copyValue: item.nonStdCode ? item.nonStdCode : '',
      })
      // 库存量
      list.push({
        title: mixins.$t('storageNum'),
        value: (item.invQty ? item.invQty : '0') + item.unitName,
        copyValue: item.invQty ? item.invQty : '0',
      })
      // 可售包装 
      if (item.flgSubUnit) {
        item['box'] = parseInt(item.invQty / item.packBox) 
        item['piece'] = parseFloat( Number(item.invQty % item.packBox)).toFixed(item.decimalPlaces)
        item.packageBox = item['box'] + item.subUnitName + item['piece'] + item.unitName
        list.push({
          title: mixins.$t('packageIvtBox'),
          value: item.packageBox,
          copyValue: item.packageBox,
        })
      }
      item.list = list
      if (item.skuImages && item.skuImages.length > 0) {
        item.skuImages.forEach(it => {
          it.url = config.image_url + it.path
        });
        item.imagesUrl = item.skuImages[0].url
      }
    })
    this.setData({
      tableData: tableData
    })
  },
  /**
  * @desc : 获取品牌
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  getGoodsBrandNoPage() {
    return this.excute(this.data.commonService, this.data.commonService.getGoodsBrandNoPage, {}).then(res => {
      this.setData({
        typeList: this.data.typeList.concat(res.data.data)
      })
    })
  },

  /**
    * @desc : 查询库存
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  getInventory(params) {
    return this.excute(this.data.commonService, this.data.commonService.getInventory, params);
  },

  /**
   * @desc : 下拉多选事件
   * @author : 姜永辉
   * @date : 2022/5/26 12:16
   */
  dropdowncommit(e) {
    this.setData({
      whFlag: true
    })
  },
  /**
   * @desc : 下拉多选确定
   * @author : jyh
   * @date : 2023年9月8日
   */
  mutiSearch(e) {
    console.log("mutiSearch", e);
    let id = e.target.id
    let form = this.data.form || {}
    if (id === 'warehouse') {
      form.whId = e.detail.item.id
      form.whName = e.detail.item.name
      this.setData({
        whFlag: false,
        form: form
      })
      this.selectComponent("#statusItemWareHouse").toggle(false)
      this.searchData()
    }
  },


  /** 下拉多选关闭 */
  muticloseAll(e) {
    this.setData({
      whFlag: false
    })
    this.selectComponent("#statusItemWareHouse").toggle()
  },

  /** 下拉多选确定 */
  mutiSearchAll(e) {
    let form = this.data.form || {}
    form.whIds = e.detail.idList
    form.whNames = e.detail.names
    this.setData({
      whFlag: false,
      form: form
    })
    this.selectComponent("#statusItemWareHouse").toggle(false)
    this.searchData()
  },
  /**
  * @desc : 排序
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  onSwitchChange(e) {
    console.log("onSwitchChange", e);
    this.setData({
      orderByValue: e.detail == 2 ? 'desc' : null
    })
    this.searchData()
  },

  muticlose() {
    this.selectComponent("#statusItemWareHouse").toggle(true)
  },
  /**
  * @desc : 初始化
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  onLoad() {
    this.getGoodsBrandNoPage()
    let _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
})