/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		 2024-1-23			1.00		    排序
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({

  mixins: [mixins],
  data: {
    pageFlag: false,
    //接口
    warehouseService: app.globalData['warehouseService'],
    saleChannelService: app.globalData['saleChannelService'],
    basicDataService: app.globalData['basicDataService'],
    formType: '',
    titleKey: 'whName',
    routeObjName: 'basicDataSort',
    tableData: [],
    flgDefaultItem:[],
    tableDataTemp: [],
    buttonSaveList: [{ code: 'add', title:  mixins.$t('save'), width: '120rpx',}],
  },
  /**
     * @desc : 排序事件
     * @author : 于继渤
     * @date : 2024/1/23 9:16
     */
  sortChange(e) {
    //处理排序数据
   
    let tableData = this.data.tableData
    let sortList = e.detail.sort
    let tableDataTemp = []

    sortList.forEach(item => {
      tableDataTemp.push(tableData[item])
    })
    this.setData({
      tableDataTemp: tableDataTemp
    })

  },
  posChange(e){},
  /**
      * @desc : 设置查询参数
      * @author : 于继渤
      * @date : 2024/1/23 9:16
      */
  setSearchParams(params) {
    params.dictCode = this.data.formType
    return params
  },

    /**
    * @desc :   处理接口返回数据
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
   handleSearchData(tableData) {
     let flgDefaultItem = []
     for (let i = 0; i < tableData.length; i++) {
      if(tableData[i].flgDefault){
        flgDefaultItem.push(tableData[i])
        tableData.splice(i, 1);
      }
     }
    this.setData({
      tableData: tableData,
      flgDefaultItem:flgDefaultItem
    })
  },
  /**
     * @desc : 加载数据
     * @author : 于继渤
     * @date : 2024/1/23 9:16
     */
  getData(params) {
    if (this.data.formType == mixins.$t('warehouse')) { //仓库档案
      return this.excute(this.data.warehouseService, this.data.warehouseService.selectByCond, params);
    } else if (this.data.formType == mixins.$t('saleChannel')) { //销售渠道
      return this.excute(this.data.saleChannelService, this.data.saleChannelService.selectByCond, params);
    } else {
      //基础数据接口
      return this.excute(this.data.basicDataService, this.data.basicDataService.selectByCond, params);
    }
  },

  /**
   * @desc : 设置保存参数
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  setParams(params) {
    let tableData = this.data.tableDataTemp
    let flgDefaultItem = this.data.flgDefaultItem
    if(flgDefaultItem.length > 0){
      tableData.unshift(flgDefaultItem[0]);
    }
    let tableDataIds = []
    let displayNo = 0 //序号
    tableData.forEach(res => {
      if (this.data.formType == mixins.$t('warehouse')) { //仓库档案
        tableDataIds.push({ id: res.whId, displayNo: displayNo += 1 })
      } else if (this.data.formType == mixins.$t('saleChannel')) { //销售渠道
        tableDataIds.push({ id: res.channelId, displayNo: displayNo += 1 })
      } else { //基础资料
        tableDataIds.push({ id: res.dataId, displayNo: displayNo += 1 })
      }
    })
    params.dataIds = tableDataIds
    return params
  },

  /**
     * @desc : 保存
     * @author : 于继渤
     * @date : 2024/1/23 9:16
     */
  saveData() {
    if (this.data.formType == mixins.$t('warehouse')) { //仓库档案
      return this.excute(this.data.warehouseService, this.data.warehouseService.updateBatchWarehouseDisplayNo, this.data.params);
    } else if (this.data.formType == mixins.$t('saleChannel')) { //销售渠道
      return this.excute(this.data.saleChannelService, this.data.saleChannelService.updateBatchSaleChannelDisplayNo, this.data.params);
    } else {
      //基础数据接口
      return this.excute(this.data.basicDataService, this.data.basicDataService.updateBatchDictionaryDataDisplayNo, this.data.params);
    }
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad(options) {
    //接收父页面传递的参数
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('params', function (data) {
      let titleKey = that.data.titleKey
      if (data.formType == mixins.$t('warehouse')) {
        titleKey = 'whName'
      } else if (data.formType == mixins.$t('saleChannel')) {
        titleKey = 'channelName'
      } else {
        titleKey = 'dataValue'
      }
      that.setData({
        formType: data.formType,
        titleKey: titleKey
      })
    })
  },
})