import { number } from "echarts"


//指标定义获取option    
export function salesRegulationOption(assessmentIndicators, value) {
  //assessmentIndicators 1:回款 2：合同 3：拜访客户
  let valueOption = [], statusOption = []
  if (assessmentIndicators == 1) {
    valueOption = [{
      name: '发票管理',
      value: '1'
    }, {
      name: '应收管理',
      value: '2'
    }]
    if (value == 1) {
      statusOption = [
        {
          name: '未开票',
          value: '0'
        }, {
          name: '已开票',
          value: '1'
        }
      ]
    }
    if (value == 2) {
      statusOption = [
        {
          name: '未收款',
          value: 0,
        },
        {
          name: '部分收款',
          value: 1,
        },
        {
          name: '已收全款',
          value: 2,
        },
      ]
    }
  }
  if (assessmentIndicators == 2) {
    valueOption = [{
      name: '合同台账',
      value: '1'
    }]
    if (value == 1) {

      statusOption = [
        {
          name: '未提交',
          value: 0,
        },
        {
          name: '审核中',
          value: 1,
        },
        {
          name: '已审核',
          value: 2,
        }, {
          name: '审核不通过',
          value: 3,
        },
      ]
    }
  }
  if (assessmentIndicators == 3) {
    valueOption = [{
      name: '客户管理',
      value: '1'
    }]
    if (value == 1) {

      statusOption = [
        {
          name: '禁用',
          value: 2,
        },
        {
          name: '启用',
          value: 1,
        },

      ]
    }
  }
  if (assessmentIndicators == 4) {
    valueOption = [{
      name: '我的客户',
      value: '1'
    }]
    if (value == 1) {

      statusOption = [
        {
          name: '客户拜访数',
          value: 0,
        },
      ]
    }
  }
  if (assessmentIndicators == 5) {
    valueOption = [{
      name: '商机管理',
      value: '1'
    }]
    if (value == 1) {

      statusOption = [
        {
          name: '未提交',
          value: 0,
        },
        {
          name: '审核中',
          value: 1,
        },
        {
          name: '已审核',
          value: 2,
        }, {
          name: '审核不通过',
          value: 3,
        },

      ]
    }
  }
  if (assessmentIndicators == 6) {
    valueOption = [{
      name: '市场管理',
      value: '1'
    }]
    if (value == 1) {

      statusOption = [
        {
          name: '未提交',
          value: 0,
        },
        {
          name: '审核中',
          value: 1,
        },
        {
          name: '已审核',
          value: 2,
        }, {
          name: '审核不通过',
          value: 3,
        },

      ]
    }
  }
  if (assessmentIndicators == 7) {
    valueOption = [{
      name: '发货单',
      value: '1'
    }]
    if (value == 1) {

      statusOption = [
        {
          name: '未提交',
          value: 0,
        },
        {
          name: '审核中',
          value: 1,
        },
        {
          name: '已审核',
          value: 2,
        }, {
          name: '审核不通过',
          value: 3,
        },{
          name: '已回执',
          value: 4,
        },
      ]
    }
  }
  return { valueOption, statusOption }
}
//考核指标option
export const assessmentIndicatorsOptions = [
  {
    label: '回款额',
    value: '1'
  },
  {
    label: '合同额',
    value: '2'
  },
  {
    label: '客户数',
    value: '3'
  },
  {
    label: '拜访客户数',
    value: '4'
  },
  {
    label: '商机数',
    value: '5'
  },
  {
    label: '来司接待次数',
    value: '6'
  },
  {
    label: '发货额',
    value: '7'
  }
]


//目标tree
export function getTargetManageTree(data, type) {
  let tree = {
    label: data.name,
    id: data.id,
    expand: true,
    children: [
      {
        id: data.id + '-' + '1',
        label: '年度目标',
        value: '',
        pid: data.id,
        type: '1',
        unit: '',
        children: [
          {
            id: data.id + '-' + '2',
            pid: data.id + '-' + '1',
            label: '1季度目标',
            value: '',
            unit: '',
            ratio: '',
            type: '2',
            children: [
              {
                id: data.id + '-' + '6',
                pid: data.id + '-' + '2',
                label: '1月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3',

              },
              {
                id: data.id + '-' + '7',
                pid: data.id + '-' + '2',
                label: '2月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              },
              {
                id: data.id + '-' + '8',
                pid: data.id + '-' + '2',
                label: '3月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              }
            ]
          },
          {
            id: data.id + '-' + '3',
            pid: data.id + '-' + '1',
            label: '2季度目标',
            unit: '',
            ratio: '',
            value: '',
            type: '2',
            children: [
              {
                id: data.id + '-' + '9',
                pid: data.id + '-' + '3',
                label: '4月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              },
              {
                id: data.id + '-' + '10',
                pid: data.id + '-' + '3',
                label: '5月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              },
              {
                id: data.id + '-' + '11',
                pid: data.id + '-' + '3',
                label: '6月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              }
            ]
          },
          {
            id: data.id + '-' + '4',
            pid: data.id + '-' + '1',
            label: '3季度目标',
            unit: '',
            ratio: '',
            value: '',
            type: '2',
            children: [
              {
                id: data.id + '-' + '12',
                pid: data.id + '-' + '4',
                label: '7月目标',
                ratio: '',
                unit: '',
                value: '',
                type: '3'
              },
              {
                id: data.id + '-' + '13',
                pid: data.id + '-' + '4',
                label: '8月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              },
              {
                id: data.id + '-' + '14',
                pid: data.id + '-' + '4',
                label: '9月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              }
            ]
          },
          {
            id: data.id + '-' + '5',
            pid: data.id + '-' + '1',
            label: '4季度目标',
            unit: '',
            ratio: '',
            value: '',
            type: '2',
            children: [
              {
                id: data.id + '-' + '15',
                pid: data.id + '-' + '4',
                label: '10月目标',
                value: '',
                unit: '',
                ratio: '',
                type: '3'
              },
              {
                id: data.id + '-' + '16',
                pid: data.id + '-' + '4',
                label: '11月目标',
                ratio: '',
                unit: '',
                value: '',
                type: '3'
              },
              {
                id: data.id + '-' + '17',
                pid: data.id + '-' + '4',
                label: '12月目标',
                unit: '',
                ratio: '',
                value: '',
                type: '3'
              }
            ]
          }
        ]
      }
    ]
  }
  if (type == 2) {
    tree.children = tree.children[0].children
  }
  if (type == 3) {
    let data = []
    tree.children[0].children.forEach(item => {
      item.children.forEach(val => {
        data.push(val)
      })
    })
    tree.children = data
  }
  return tree
}
//单位选择框
export function renderSelect(h, data, form, children) {
  let option = [{ name: '元' }, { name: '万元' }, { name: '个' }]
  let disabled = false;
  if (!data.type) {
    return;
  }
  if (form.type == 1 && data.type != 1) {
    disabled = true
  }
  if (form.type == 2 && data.type != 2) {
    disabled = true
  }
  return h(
    'el-select',
    {
      attrs: {
        value: data.unit, // 设置输入框的值
        disabled,
        placeholder: '单位'
      },
      on: {
        change: (value) => {
          data.unit = value
          flattenTree(data, 'select', children);
        }
      }
    },
    [...option.map((item) => {
      return h('el-option', {
        attrs: {
          key: item.name,
          label: item.name,
          value: item.name,
        }

      })
    })]
  );
}

//创建输入框
export function renderInput(h, data, className, placeholder, form, children) {
  if (!data.type) {
    return;
  }
  if (className == 'ratioInput') {
    if (form.type == 2 && data.type == 2) {
      return;
    }
    if (form.type == 3 && data.type == 3) {
      return;
    }
  }

  let disabled = false;
  if (className == 'valueInput') {
    if (form.type == 1 && data.type != 1) {
      disabled = true;
    }
    if (form.type == 2 && data.type != 2) {
      disabled = true;
    }
  }

  return h(
    'el-input',
    {
      attrs: {
        type: 'text',
        value: data[className == 'ratioInput' ? 'ratio' : 'value'], // 设置输入框的值
        class: className,
        placeholder,
        disabled,
        max: className == 'ratioInput' ? 100 : '',
        min: 0
      },
      on: {
        input: (value) => {
          data[className == 'ratioInput' ? 'ratio' : 'value'] = value; // 监听输入事件，更新数据
          flattenTree(data, className, children, 'input');
        },
        focus: () => {
          flattenTree(data, className, children, 'focus');
        }
      }
    },
    [className == 'ratioInput' ? h('slot', { slot: 'suffix' }, ['%']) : '']
  );
}
//占比改变
function setRatio(data, tree, eventFn) {
  let ratio = 0
  tree.forEach((node) => {
    if (data.pid == node.pid && data.id !== node.id) {
      ratio += (+node.ratio)
      if (eventFn == 'focus' && ratio) {
        data.ratio = 100 - ratio >= 0 ? 100 - ratio : 0
      }
      if (eventFn == 'input') {
        if (ratio + (+data.ratio) > 100) {
          data.ratio = 100 - ratio
        }
      }

    }
    if (node.children && node.children.length > 0) {
      setRatio(data, node.children, eventFn);
    }
  })

}
//占比改变值改变
function setValue(data, tree) {
  tree.forEach((node) => {

    if (node.id == data.pid) {
      data.value = +(node.value * (data.ratio / 100)).toFixed(2)
      //季度占比改变计算月度目标值
    }
    if (data.type == '2') {
      data.children.forEach((val) => {
        val.value = +(data.value * (val.ratio / 100)).toFixed(2)
      });
    }
    if (node.children && node.children.length > 0) {
      setValue(data, node.children);
    }
  })

}


//设置单位
function setSelect(data, tree) {
  tree.forEach((node) => {

    if (data.id == node.pid) {
      node.unit = data.unit
      node.children.forEach((val) => {
        val.unit = data.unit
      });
    }
    if (node.children && node.children.length > 0) {
      setSelect(data, node.children);
    }
  })

}
//改变值
function setValueInput(data, tree) {
  tree.forEach((node) => {

    if (node.pid == data.id) {
      node.value = +(data.value * (node.ratio / 100)).toFixed(2)
      //年度目标值改变月度目标
      if (data.type == '1') {
        node.children.forEach((val) => {
          val.value = +(node.value * (val.ratio / 100)).toFixed(2)
        });
      }
    }
    if (node.children && node.children.length > 0) {
      setValueInput(data, node.children);
    }
  })

}


// 目标值改变递归更新数据
function flattenTree(data, className, tree, eventFn) {

  if (className == 'ratioInput') {
    setRatio(data, tree, eventFn) //设置占比
    setValue(data, tree) //占比改变值更新

  }
  if (className == 'select') {
    setSelect(data, tree,) //设置单位

  }
  //目标值改变根据子级占比计算
  if (className == 'valueInput') {
    setValueInput(data, tree) //改变值
  }
}