const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    show:true,
    // 路由
    routeObjName: 'select-product-types',
    // 原始数据
    tableDataSocial:null,
    tableData: [],
    goodsCategoryService: app.globalData['goodsCategoryService'],
  },
  /**
   * @desc : 过滤数据
   * @date : 2024/4/29 15:49
   * @author : 周兴
   */
  filterData(e) {
    let searchForm = this.data.searchForm;
    let searchText = null;
    let tableDataSocial = this.data.tableDataSocial;
    if(!tableDataSocial)return;
    tableDataSocial = [...tableDataSocial]
    if (searchForm) {
      this.setData({
        show:false
      })
      searchForm = JSON.parse(searchForm)
      searchText = searchForm.searchText;
      if (searchText) {
        // 1 先过滤出数据
        let childs = tableDataSocial.filter(it => it.catName.indexOf(searchText) >= 0);
        let returnTable = []
        console.log('childs',childs);
        // 如果查到，需要查出其父级
        if (childs && childs.length > 0) {
          let filters = null;
          let fils = null;
          childs.forEach(it => {
            filters = tableDataSocial.filter(t=>it.levelCode.startsWith(t.levelCode));
            if(filters && filters.length > 0){
              filters.forEach(f=>{
                delete f.children 
                // 判断在返回结果中是否已经包括这条数据
                fils = returnTable.filter(c=>c.catId == f.catId)
                if(!fils || fils.length == 0){
                  returnTable.push(f);
                }
              })
            }
          })
          let data = util.convertToChildren(returnTable, 'parentId', 'catId');
          this.setData({
            tableData:data,
            show:true
          })
        }
      }else{
        let data = util.convertToChildren(tableDataSocial, 'parentId', 'catId');
        this.setData({
          tableData:data,
          show:true
        })
      }
    }

  },
   /**
  * @desc :   列表点击事件
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */ 
  clickName(e) {
    console.log("clickName回调", e)
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.emit('bindData', { data: { id: e.detail.id, name: e.detail.name } })
    wx.navigateBack({
      delta: 1
    })
  },
  /**
  * @desc :   处理接口返回数据
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  handleSearchData(tableData) {
    this.setData({
      tableDataSocial:[...tableData],
      tableData: util.convertToChildren(tableData, 'parentId', 'catId', null)
    })
    const myComponent = this.selectComponent('#treeSelect');
    myComponent.setDataFromPageTrue([]);
    myComponent.setDataFromPageTrue(this.data.tableData);
  },
    /**
   * @desc : 设置查询参数
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  setSearchParams(params){
    params.pageSize = 100000
    params.currentPage = 1 
    params.flgValid = true
    return params
  },

  /**
   * @desc : 加载数据
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  getData(params) {
    return this.excute(this.data.goodsCategoryService, this.data.goodsCategoryService.selectByCond, params);
  },
})