const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
const Constant = require('../../../utils/Constants.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    pageFlag: false,
    firstFlag: true,
  },
  /**
   * @desc : 设置查询参数
   * @date : 2024年3月8日
   * @author : 王英杰
   */
  setSearchParams(params) {
    if (this.data.firstFlag) { //第一次进入
      let route = this.data.chooseRoute;
      if (!route || !route.type) {
        return;
      }
      let title;
      // 读取传入的单据类型
      let chooseType = route.type
      let nameKey = route.nameKey
      let idKey = route.idKey
      let typeKey = route.typeKey
      this.setData({
        nameKey:nameKey,
        idKey:idKey,
        typeKey:typeKey, 
      })
      switch (chooseType) {
        // 仓库
       case Constant.chooseType.warehouse:
          title = "仓库"
          this.setData({ 
            routeObjName: 'common',
            method: 'getWarehouse',
            title: 'whName',
            contentList: [{
                code: 'whCode',
                title: '仓库编码'
              },
              {
                code: 'whName',
                title: '仓库名称'
              },
              {
                code: 'isMain',
                title: '主仓库',
                type: 'switch'
              },
            ],
            placeholder: ['whName']
          })
          break;
       // 资金账户
       case Constant.chooseType.moneyAccount:
        title = "资金账户"
        this.setData({ 
          routeObjName: 'common',
          method: 'getMac',
          title: 'whName',
          contentList: [{
              code: 'macCode',
              title: '账户编码'
            },
            {
              code: 'macName',
              title: '账户名称'
            },
            {
              code: 'isMain',
              title: '主账户',
              type: 'switch'
            },
          ],
          placeholder: ['whName']
        })
        break; 
        } 
      wx.setNavigationBarTitle({
        title: '选择' + title,
      })
    }
    this.setData({
      firstFlag: false
    })

    //收付款类型-收款的查询参数
    //  params.rpType = Constants.rpType.receipt
    return params;
  },
  /**
   * @desc : 加载数据
   * @author : 王英杰
   * @date : 2024/1/23 9:16
   */
  getData(params) {
    let service = app.globalData[this.data.routeObjName + 'Service'];
    return this.excute(service, service[this.data.method], params);
  },
  /**
   * @desc : 查询 后续处理
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  
  /**
   * @desc : 选择 确定事件
   * @author : 王英杰
   * @date : 2024/1/23 9:16
   */
  toMulti(e) {
    let item = e.detail
    let idKey = this.data.idKey
    let nameKey = this.data.nameKey
     console.log("toMulti",e.detail);
     let ids = []
     let names = []
     e.detail.list.forEach(item => { 
        ids.push(item[idKey])
        names.push(item[nameKey]) 
    })
    // 给父页面传递数据
    const eventChannel = this.getOpenerEventChannel(); 
    eventChannel.emit('bindData',{ data: { id: ids.join(','), name: names.join(',') ,list :e.detail.list} } )
    wx.navigateBack({
      delta: 1
    })
  },

})