import { ref, reactive } from 'vue'
import customerSetService from '@/api/customerSet'
import customerService from '@/api/customer'
import router from '@/router/index'
import message from '@/utils/message'
interface dataParams {
  showDialog: boolean
  shape: number
  dataTree: Array<any>
  defaultArray: Array<number | string>
  defaultConditionList: Array<any>
  editMsg: any
  peopleNum: number
}

const msg = () => {
  const curQueryId = Number(router.currentRoute.value.query.Id)
  return {
    RuleId: curQueryId
  }
}
const condition = ref<any>(null)
const data = reactive<dataParams>({

  showDialog: false,
  shape: 0,
  dataTree: [],
  defaultArray: [],
  defaultConditionList:[],
  editMsg: {
    Id: 0,
    RuleSelectType: 1, //客户的查询类型 1并且 2或者
    DeptList: [], //部门列表
    EmpList: [], //员工列表
    RuleAllotWay: 1, //分配方式 1依次轮流 2随机分配
    ConditionList: [] //条件列表
  },
  peopleNum: 0
})
const jumpBeforePage = () => {
  router.push({
    path: '/customerSetup',
    query: {
      type: 'clue'
    }
  })
}

const changeDialog = () => {
  data.showDialog = true
}

const editRuleModule = () => {
  //获取分配客户条件
  const getCondition = val => {
    data.editMsg.ConditionList = [...val]
  }
  //获取已选部门成员
  const getdpt = val => {
    data.showDialog = false
    data.editMsg.DeptList = []
    data.editMsg.EmpList = []
    val.map(e => {
      if (e.Id.slice(0, 1) == 1) {
        data.editMsg.DeptList.push({
          DeptId: e.Id.slice(2),
          DeptName: e.Name
        })
      } else {
        data.editMsg.EmpList.push({
          Id: e.Id.slice(2),
          EmployeeName: e.Name
        })
      }
    })
    console.log(val, 'dpt')
  }
  //获取已选人数
  const getNum = n => {
    data.peopleNum = n
  }
  //获取线索详情
  const getDetail = () => {
    customerSetService.getCustomerClueRuleDetail(msg()).then(res => {
      const d = res.data.Data
      data.editMsg.DeptList = d.DeptList
      data.editMsg.EmpList = d.EmpList
      data.editMsg.ConditionList = d.ConditionList
      data.defaultConditionList = d.ConditionList
      data.editMsg.RuleAllotWay = d.RuleAllotWay
      data.editMsg.RuleSelectType = d.RuleSelectType
      const Dep = d.DeptList.map(e => {
        return '1-' + e.DeptId
      })
      const Emp = d.EmpList.map(e => {
        return '2-' + e.Id
      })
      data.defaultArray = [...Dep, ...Emp]
      console.log(d.ConditionList, 'ConditionList')
    })
  }
  //获取部门数据
  const getEmployeeData = () => {
    customerService.getEmployeeData({}).then(res => {
      console.log(res.data.Data, '部门')
      data.dataTree = res.data.Data
      // 重组唯一id
      if (data.dataTree.length == 0) return
      data.dataTree.forEach(x => {
        x.newId = x.DataType + '-' + x.DeptId
        if (x.ChildList && x.ChildList.length > 0) {
          getChildList(x.ChildList)
        }
      })
    })
  }
  const getChildList = (ChildList: Array<any>) => {
    ChildList.forEach(x => {
      x.newId = x.DataType + '-' + x.DeptId
      if (x.ChildList && x.ChildList.length > 0) {
        getChildList(x.ChildList)
      }
    })
  }
  // 新增，修改
  const setCustomerClueRuleInfo = () => {
    // console.log(data.editMsg.ConditionList)
    // const flag = condition.value.checkRule(data.editMsg.ConditionList)
    // console.log(94, flag)
    //  if (!flag) return
    customerSetService.setCustomerClueRuleInfo(data.editMsg).then(res => {
      message.successMsg(res.data.Message)
      setTimeout(() => {
        router.push({
          path: '/customerSetup',
          query: {
            type: 'clue'
          }
        })
      }, 2000)
    })
  }
  return {
    msg,
    condition,
    data,
    getDetail,
    getEmployeeData,
    jumpBeforePage,
    changeDialog,
    setCustomerClueRuleInfo,
    getCondition,
    getdpt,
    getNum
  }
}

export default editRuleModule
