const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    commonService: app.globalData['commonService'],
  },
  /**
    * @desc :   处理接口返回数据
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  handleSearchData(tableData) {
    tableData.forEach(res => {
      res['commission'] = Number(100)
    })
    this.setData({
      tableData: tableData
    })
  },
  /**
      * @desc : 加载数据
      * @author : 于继渤
      * @date : 2024/1/23 9:16
      */
  getData(params) {
    return this.excute(this.data.commonService, this.data.commonService.getStaff, params);
  },
  /**
 * 确定
 * @param {*} e 
 */
  btnOK(e) {
    let TempStaffTable = []
    // 判断是否有选择的checkbox
    var allcheck = false;
    var allswitch = false;
    var indexswitch = 0;
    var tableData = this.data.tableData;
    for (let i = 0; i < tableData.length; i++) {
      if (tableData[i].checked) {
        allcheck = true;
        break;
      }
    }
    for (let i = 0; i < tableData.length; i++) {
      if (tableData[i].isMain) {
        allswitch = true;
        indexswitch = i;
        break;
      }
    }
    let commission = Number(0)
    for (let i = 0; i < tableData.length; i++) {
      if (tableData[i].checked) {
        commission += Number(tableData[i].commission)
      }
    }

    // 校验
    if (!allcheck) {
      util.showToast('请至少选择一条业务员');
      return;
    }

    // 校验
    if (!allswitch) {
      util.showToast('请选择主业务员');
      return;
    }
    if (commission !== 100) {
      util.showToast('提成比例不足 100');
      return;
    }

    let sum = 0
    tableData.forEach(res => {
      if (res.checked) {
        sum += parseFloat(res.commission)
      }
    })
    if (sum > 100 || sum < 100) {
      util.showToast('提成比率不正确');
      return;
    }
    let tempNames = []
    let tempIds = []
    let satffList = []
    let staffMain = null

    tableData.forEach(item => {
      if (item.checked) {
        item.allocationRatio = item.commission
        item.ownerFlag = item.isMain
        satffList.push(item)
        tempNames.push(item.staffName)
        tempIds.push(item.staffId)
        if (item.isMain) {
          staffMain = item
        }
      }

    })



    // 给父页面传递数据
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.emit('bindData', {
      staffMain: staffMain,
      staffList: satffList,
      staffItem: {
        text: tempNames.join(','),
        value: tempIds
      },

    })

    wx.navigateBack({
      delta: 1
    })
  },

  //输入提成比例的
  inputChange(e) {
    let tableData = this.data.tableData;
    tableData[e.currentTarget.dataset.index].commission = e.detail.value;
    tableData[e.currentTarget.dataset.index].allocationRatio = e.detail.value;
    this.setData({
      tableData: tableData
    })
  },
  inputBlurChange(e) {
    let value = null
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let tableData = this.data.tableData
    if (Number(e.detail.value) == 0 || Number(e.detail.value)) {
      value = Number(e.detail.value)
      if (Number(e.detail.value) > 100) {
        value = Number(100)
      }
      if (Number(e.detail.value) < 0) {
        value = Number(0)
      }

    }
    tableData[index].commission = value.toFixed(2)
    this.setData({
      tableData,
      inputBlurChange: true
    })
    //   let number = Number(100 - value.toFixed(2))
    //   let showListTemp = []
    //   tableData.forEach(it => {
    //     if (id != it.staffId && it.checked) {
    //       console.log('it', it)
    //       showListTemp.push(it)
    //     }
    //   })
    //   let allocationRatio = (number / showListTemp.length).toFixed(2)
    //   let allocationRatioTotal = 0
    //  //数据取余2为0
    //   if (number % showListTemp.length == 0) {
    //     //循环将计算好的百分比放入数据中
    //     for (let i in showListTemp) {
    //       showListTemp[i].commission = Number(allocationRatio).toFixed(2)
    //     }
    //     tableData.forEach(item1 => {
    //       showListTemp.forEach(item2=>{
    //         if(item1.staffId == item2.staffId){
    //           item1.commission = item2.commission.toFixed(2)
    //         }
    //       })
    //     })
    //     this.setData({
    //       tableData
    //     })
    //   } else {
    //     let showListTemp2 = [...showListTemp]
    //     let endList = showListTemp2.splice(-1)
    //     for (let i = 0; i < showListTemp2.length; i++) {
    //       console.log('showListTemp[i]', showListTemp2[i])
    //       allocationRatioTotal = allocationRatioTotal + Number(allocationRatio)
    //       showListTemp2[i].commission = Number(allocationRatio).toFixed(2)
    //     }
    //     if (endList[0]) {
    //       endList[0].commission = (number - allocationRatioTotal).toFixed(2)
    //     }
    //     tableData.forEach(item1 => {
    //       showListTemp2.forEach(item2=>{
    //         if(item1.staffId == item2.staffId){
    //           item1.commission = item2.commission
    //         }
    //       })
    //     })
    //     this.setData({
    //       tableData
    //     })
    //   }
  },
  calculationScale(tableData) {


    tableData = tableData.filter((item) => {
      return item.checked == true
    })
    let allocationRatio = (100 / tableData.length).toFixed(2)
    let allocationRatioTotal = 0
    //数据取余2为0
    if (100 % tableData.length == 0) {
      //循环将计算好的百分比放入数据中
      for (let i in tableData) {
        tableData[i].commission = Number(allocationRatio).toFixed(2)

      }
    } else { //数据取余2不为0
      //获取最后选中checkbox数组中最后一个元素
      let endList = tableData.splice(-1)
      for (let i = 0; i < tableData.length; i++) {
        allocationRatioTotal = allocationRatioTotal + Number(allocationRatio)
        tableData[i].commission = Number(allocationRatio).toFixed(2)
      }
      if (endList[0]) {
        endList[0].commission = (100 - allocationRatioTotal).toFixed(2)
      }

    }

  },
  /**
  * 列表选中
  */
  onChangeFlag(event) {

    let allIsMain = false
    var _this = this
    var tableData = _this.data.tableData

    let index = event.currentTarget.dataset.index
    let checked = event.currentTarget.dataset.value
    // 设置点击的项目checkbox的值
    for (let i = 0; i < tableData.length; i++) {
      if (i == index) {
        tableData[i].checked = !tableData[i].checked
        break;
      }
    }
    if (tableData[index].checked) {
      var isMain = false;
      for (let i = 0; i < tableData.length; i++) {
        if (tableData[i].isMain) {
          isMain = true;
          break;
        }
      }
      tableData[index].isMain = !isMain;
    } else {
      tableData[index].isMain = false;
      tableData[index].commission = parseFloat(100).toFixed(2)
    }

    //实现一个关闭出现一换个主业务员
    for (let i = 0; i < tableData.length; i++) {
      if (tableData[i].isMain) {
        allIsMain = true
      }
    }
    if (!allIsMain) {
      for (let i = 0; i < tableData.length; i++) {
        if (tableData[i].checked) {
          tableData[i].isMain = true
          break
        }
      }
    }
    // tableData.forEach(it => {
    //     if (it.checked) {
    //         it.commission = parseFloat(100 / tableData.filter(item => item.checked == true).length).toFixed(2)
    //     } else {
    //         it.commission = parseFloat(100).toFixed(2)
    //     }

    // })
    if (!this.data.inputBlurChange) {
      this.calculationScale(tableData)
    }



    // 选择条数
    _this.setData({
      tableData: tableData,
    })
  },
  /**
 * 主从业务员的勾选
 * @param {*} e 
 */
  onChangeSwitch(e) {
    var _this = this
    var tableData = _this.data.tableData
    //如果没有选中就不能触发这个主从的勾选
    if (!tableData[e.target.dataset.index].checked) {
      return;
    }
    // 设置点击的项目checkbox的值
    for (let i = 0; i < tableData.length; i++) {
      if (i == e.target.dataset.index && tableData[i].checked) {
        tableData[i].isMain = !tableData[i].isMain
        continue;
      }
      tableData[i].isMain = false;
    }

    // 选择条数
    _this.setData({
      tableData: tableData,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

})