const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
const api = require('@/utils/api.js');

const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'staffPurview',
    commonService: app.globalData['commonService'],
    tableData: [],
    allchecked: false,
    flgNolimit: false,
    sideKey: 0,
    purviewType: Constants.purviewType.org
  },
  /**
  * @desc : 无限制
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  onChange(e) {
    this.setData({
      flgNolimit: !this.data.flgNolimit
    })
  },

  /**
  * @desc : 保存的參數設置
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  setParams(params) {
    // 修改之后的 值
    params = Object.assign({}, params, this.data.item);
    let tableData = this.data.tableData
    let dataIds = []
    //格式化数据
    let tableDataTemp = []
    tableData.forEach(res => {
      if (res.checked) {
        dataIds.push(res.id)
      }
      if (res.subMenus && res.subMenus.length > 0) {
        res.subMenus.forEach(it => {
          if (it.checked) {
            dataIds.push(it.id)
          }
        })
      }
    })
    let staffPurviewList = []
    let staffPurviewO = {
      purviewCode: this.data.purviewType,
      staffId: this.data.item.staffId,
      flgNolimit: this.data.flgNolimit,
      dataIds: dataIds
    }
    staffPurviewList.push(staffPurviewO)
    params.staffPurviewList = staffPurviewList
    return params
  },
  /**
  * @desc : 保存数据服务
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  saveData(params) {
    return this.excute(this.data.service, this.data.service.saveStaffPurview, params)
  },

  /**
   * @desc : 处理查询接口返回数据
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  handleSearchData(tableData) {
    console.log(tableData,"tableData");
    if (this.data.typeList == null) {
      tableData.forEach(it => {
        it.menuName = it.orgName
      })
      this.setData({
        tableData: util.convertToChildren(tableData),
        flgNolimit: (tableData != null && tableData.length > 0) ? tableData[0].flgNolimit : false
      })
      return
    }
    let item = this.data.typeList[this.data.sideKey];
    // 渠道
    if (item.code == Constants.purviewType.channel) {
      tableData.forEach(it => {
        it.menuName = it.channelName
      })
      this.setData({
        tableData: tableData,
        flgNolimit: (tableData != null && tableData.length > 0) ? tableData[0].flgNolimit : false
      })
    }
    // 制单员
    else if (item.code == Constants.purviewType.make) {
      tableData.forEach(it => {
        it.menuName = it.staffName
      })
      console.log(tableData[0],tableData != null && tableData.length > 0,"制单员");
      this.setData({
        tableData: tableData,
        flgNolimit: (tableData != null && tableData.length > 0) ? tableData[0].flgNolimit : false
      })

    } else {
      tableData.forEach(it => {
        it.menuName = it.orgName
      })
      this.setData({
        tableData: util.convertToChildren(tableData),
        flgNolimit: (tableData != null && tableData.length > 0) ? tableData[0].flgNolimit : false
      })
    }


  },

  /**
  * @desc   : 处理数据-保存的逻辑
  * @author : 姜永辉
  * @date   : 2024/1/26 11:46
  */
  handleData() {
    wx.navigateBack({
      data: 1
    })
  },

  /**
   * @desc : 切换左侧bar
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  changeSidebar(e) {
    this.setData({
      sideKey: e.detail
    })
    let item = this.data.typeList[this.data.sideKey];
    this.setData({
      purviewType: item.code
    })
    // 默认查询
    this.searchData();
  },

  /**
   * @desc : 设置额外参数
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  setSearchParams(params) {
    let p = params || {}
    p.staffId = this.data.item.staffId
    return p
  },
  /**
   * @desc : 获取数据
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  getData(params) {
    if (this.data.typeList == null) {
      params.purviewCode = this.data.purviewType
      params.orgName = params.searchText
      // 业务部门
      return this.excute(this.data.service, this.data.service.getStaffPurview, params)
    }

    let item = this.data.typeList[this.data.sideKey]; 
    
    // 渠道
    if (item.code == Constants.purviewType.channel) {
      params.dataSourceCode = 'saleChannelPurviewType'
      params.purviewCode = item.code
      params.channelName = params.searchText
      return this.excute(this.data.commonService, this.data.commonService.getInitData,params)
    }
    // 制单员
    else if (item.code == Constants.purviewType.make) {
      params.dataSourceCode = 'staffPurviewType'
      params.purviewCode = item.code
      params.staffName = params.searchText
      return this.excute(this.data.commonService, this.data.commonService.getInitData,params)
    } else {
      params.purviewCode = this.data.purviewType
      params.orgName = params.searchText
      // 业务部门
      return this.excute(this.data.service, this.data.service.getStaffPurview, params)
    }


  },
  /**
    * @desc : 点击功能权限的返回值
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  obtainList(e) {
    this.setData({
      tableData: e.detail.list
    })
  },

  /**
   * @desc : 加载数据
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  loadInit() {
    this.getInitData('purviewType')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },

  /**
    * @desc : 加载范围权限类型 渠道
    * @date : 2022/5/15 16:49
    * @author : 姜永辉
    */
  getInitData(dataSourceCode) {
    let param = {
      dataSourceCode: dataSourceCode,
    }
    api.request(Constants.BASIC_WX_COMMON_API + 'get_wx_init_data', 'POST', param)
      .then(res => {
        if (res.data.code === Constants.SUCESS_CODE) {
          let list = res.data.data
          let typeList = this.data.typeList || []
          list.forEach(item2 => {
            let item = {
              id: item2.id,
              code: item2.code,
              name: item2.name,
            }
            typeList.push(item)
          })
          this.setData({
            typeList: typeList
          })
        }
      })
  },
})