/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：组件-面板
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		  	2024-03-20    1.00		   	  新建
 *******************************************************************************/
const util = require('@/utils/util.js')
 Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 数据源
    list: {
      type: Array,
      value: []
    },
    // 内容
    content: {
      type: Array,
      value: []
    },
    // 回显名称
    nameKey: { 
      type: String
    },
    idKey: {
      type: String
    },
    //类型key
    typeKey: {
      type: String
    },
  },

  options: {
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
      * @desc : checkBox选中/取消事件
      * @date : 2024/3/20 15:49
      * @author : 于继渤
      */
    onChangeFlag(event) { 
      let allIsMain = false
      let index = event.currentTarget.dataset.index
      var _this = this
      var list = _this.data.list
      var content = _this.data.content
      let colSwitchList = content.filter(res => {
        return res.type == 'switch'
      })
      let commissionList = content.filter(res => {
        return res.type == 'commission'
      })
      let switchCode = null
      let commissionCode = null
      if (colSwitchList.length > 0) {
        switchCode = colSwitchList[0].code
      }
      if (commissionList.length > 0) {
        commissionCode = commissionList[0].code
      }
      // 设置点击的项目checkbox的值
      for (let i = 0; i < list.length; i++) {
        if (i == index) {
          list[i].checked = !list[i].checked
          break;
        }
      }
      if (list[index].checked) {
        var isMain = false;
        for (let i = 0; i < list.length; i++) {
          if (list[i][switchCode]) {
            isMain = true;
            break;
          }
        }
        list[index][switchCode] = !isMain;
      } else {
        if (switchCode) {
          list[index][switchCode] = false;
        }

        if (commissionCode) {
          list[index][commissionCode] = parseFloat(100).toFixed(2)
        }

      }

      //实现一个关闭出现一换个主业务员
      for (let i = 0; i < list.length; i++) {
        if (list[i][switchCode]) {
          allIsMain = true
        }
      }
      if (!allIsMain) {
        for (let i = 0; i < list.length; i++) {
          if (list[i].checked) {
            list[i][switchCode] = true
            break
          }
        }
      }
      console.log('this.data.inputBlurChange', this.data.inputBlurChange)
      this.calculationScale(list, commissionCode, switchCode)
      // 选择条数
      _this.setData({
        list: list
      })
    },

    /**
       * @desc : Switch切换事件
       * @date : 2024/3/20 15:49
       * @author : 于继渤
       */
    onChangeSwitch(e) {
      var _this = this
      var list = _this.data.list
      var index = e.target.dataset.index
      console.log('onChangeSwitch', e, list)
      //如果没有选中就不能触发这个主从的勾选
      if (!list[index].checked) {
        return;
      }
      //获取需要需改的 key
      var content = _this.data.content
      let colSwitchList = content.filter(res => {
        return res.type == 'switch'
      })
      let switchCode = null
      if (colSwitchList.length > 0) {
        switchCode = colSwitchList[0].code
      }
      // 设置点击的项目checkbox的值
      for (let i = 0; i < list.length; i++) {
        if (i == index && list[i].checked) {
          list[i][switchCode] = !list[i][switchCode]
          continue;
        }
        list[i][switchCode] = false;
      }
      // 选择条数
      _this.setData({
        list: list,
      })
    },









    /**
      * @desc : 输入提成比例的
      * @date : 2024/3/20 15:49
      * @author : 于继渤
      */
    inputChange(e) {
      let list = this.data.list;
      let index = e.currentTarget.dataset.index;
      var content = this.data.content
      let commissionList = content.filter(res => {
        return res.type == 'commission'
      })
      let commissionCode = null
      if (commissionList.length > 0) {
        commissionCode = commissionList[0].code
      }
      if (commissionCode) {
        list[index][commissionCode] = e.detail.value
      }

      this.setData({
        list: list
      })
    },


    /**
      * @desc : 输入提成比例 离开焦点事件
      * @date : 2024/3/20 15:49
      * @author : 于继渤
      */
    inputBlurChange(e) {
      console.log('inputBlurChange', e)
      let value = null
      let index = e.currentTarget.dataset.index
      let id = e.currentTarget.dataset.id
      let list = this.data.list
      let valueBlur = e.detail.value

      if (Number(valueBlur) == 0 || Number(valueBlur)) {
        value = Number(valueBlur)
        if (Number(valueBlur) > 100) {
          value = Number(100)
        }
        if (Number(valueBlur) < 0) {
          value = Number(0)
        }

      }

      var content = this.data.content
      let commissionList = content.filter(res => {
        return res.type == 'commission'
      })

      let commissionCode = null
      let idKey = null
      if (commissionList.length > 0) {
        commissionCode = commissionList[0].code
        idKey = commissionList[0].idKey
      }
      if (commissionCode) {
        list[index][commissionCode] = value.toFixed(2)
      }
      this.setData({
        list,
        inputBlurChange: true
      })


      console.log('idKey', idKey)
      console.log('commissionCode', commissionCode)
      let number = Number(100 - value.toFixed(2))
      let showListTemp = []
      list.forEach(it => {
        if (id != it[idKey] && it.checked) {
          console.log('it', it)
          showListTemp.push(it)
        }
      })
      console.log('showListTemp', showListTemp)
      console.log('number', number)
      let allocationRatio = (number / showListTemp.length).toFixed(2)
      let allocationRatioTotal = 0
      console.log('allocationRatio', allocationRatio)
      //数据取余2为0
      if (number % showListTemp.length == 0) {
        //循环将计算好的百分比放入数据中
        for (let i in showListTemp) {
          showListTemp[i][commissionCode] = Number(allocationRatio).toFixed(2)
        }
        list.forEach(item1 => {
          showListTemp.forEach(item2 => {
            if (item1[idKey] == item2[idKey]) {
              console.log('item2', item2)
              item1[commissionCode] = Number(item2[commissionCode]).toFixed(2)
            }
          })
        })
        this.setData({
          list
        })
      } 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][commissionCode] = Number(allocationRatio).toFixed(2)
        }
        if (endList[0]) {
          endList[0][commissionCode] = (number - allocationRatioTotal).toFixed(2)
        }
        list.forEach(item1 => {
          showListTemp2.forEach(item2 => {
            if (item1[idKey] == item2[idKey]) {
              item1[commissionCode] = item2[commissionCode]
            }
          })
        })
        this.setData({
          list
        })
      }




    },

    /**
        * @desc : 选择计算事件
        * @date : 2024/3/20 15:49
        * @author : 于继渤
        */
    calculationScale(list, commissionCode) {
      list = list.filter((item) => {
        return item.checked == true
      })
      let allocationRatio = (100 / list.length).toFixed(2)
      let allocationRatioTotal = 0
      //数据取余2为0
      if (100 % list.length == 0) {
        //循环将计算好的百分比放入数据中
        for (let i in list) {
          list[i][commissionCode] = Number(allocationRatio).toFixed(2)

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

      }

    },
        //设置控件信息
        setDataFromPage(data) {
          this.setData({
            list:data
          })
        },
    /**
   * 确定
   * @param {*} e 
   */
    btnOK(e) {


      var content = this.data.content
      let colSwitchList = content.filter(res => {
        return res.type == 'switch'
      })
      let commissionList = content.filter(res => {
        return res.type == 'commission'
      })
      let switchCode = null
      let commissionCode = null
      if (colSwitchList.length > 0) {
        switchCode = colSwitchList[0].code
      }
      if (commissionList.length > 0) {
        commissionCode = commissionList[0].code
      }
      var indexswitch = 0;
      // 判断是否有选择的checkbox
      var allcheck = false;
      var allswitch = false;
      var list = this.data.list;
      for (let i = 0; i < list.length; i++) {
        if (list[i].checked) {
          allcheck = true;
          break;
        }
      }
      // 校验
      if (!allcheck) {
        util.showToast('请至少选择一条数据');
        return;
      }

      if (switchCode) {
        for (let i = 0; i < list.length; i++) {
          if (list[i][switchCode]) {
            allswitch = true;
            indexswitch = i;
            break;
          }
        }
        // 校验
        if (!allswitch) {
          util.showToast('请选择' + colSwitchList[0].title);
          return;
        }
      }


      if (commissionCode) {
        let commission = Number(0)
        for (let i = 0; i < list.length; i++) {
          if (list[i].checked) {
            commission += Number(list[i][commissionCode])
          }
        }
        if (commission !== 100) {
          util.showToast('提成比例不足 100');
          return;
        }
        let sum = 0
        list.forEach(res => {
          if (res.checked) {
            sum += parseFloat(res[commissionCode])
          }
        })
        if (sum > 100 || sum < 100) {
          util.showToast('提成比率不正确');
          return;
        }
      }

      let nameKey = this.data.nameKey
      let idKey = this.data.idKey
      let typeKey = this.data.typeKey
      let names = []
      let ids = []
      let tableDataList = []
      let main = {}
      list.forEach(item => {
        if (item.checked) {
          item.allocationRatio = commissionCode ? item[commissionCode] : null
          item.ownerFlag = switchCode ? item[switchCode] : null
          tableDataList.push(item)
          if (nameKey) {
            names.push(item[nameKey])
          }
          if (idKey) {
            ids.push(item[idKey])
          }
          if (item[switchCode]) {
            main = item
          }
        }
      })


      // 给父页面传递数据

      let obj = {
        main: main,
        list: tableDataList
      }
      obj['item'] = {
        text: names,
        value: ids
      }
      this.triggerEvent("toMulti", obj)

    },








  }





})
