import { reactive } from 'vue'
import customerService from '@/api/customer'
import customerService2 from '@/api/customer2'
import router from '@/router/index'

interface dataParams {
  showDialog: boolean
  shape: number
  dataTree: Array<any>
  defaultArray: Array<number | string>
  editMsg: any
}

const msg = () => {
  const curQueryId = Number(router.currentRoute.value.query.Id)
  return {
    RuleId: curQueryId
  }
}

const data = reactive<dataParams>({
  showDialog: false,
  shape: 0,
  dataTree: [],
  defaultArray: [],
  editMsg: {
    Id: 0,
    RuleSelectType: 1, //客户的查询类型 1并且 2或者
    DeptList: [], //部门列表
    EmpList: [], //员工列表
    RuleAllotWay: 1, //分配方式 1依次轮流 2随机分配
    ConditionList: [] //条件列表
  }
})
// watch(
//   () =>data.defaultArray,
//   (val) => {
//     val.map(e => {
//     console.log('valDefault', e)
      
//     })
//   }
  
// )
const jumpBeforePage = () => {
  router.go(-1)
}

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

const editRuleModule = () => {
  //获取线索详情
  const getDetail = () => {
    customerService.getCustomerClueRuleDetail(msg()).then(res => {
      console.log(res.data.Data, '详情数据')
      data.defaultArray = []
      // res.data.Data.
    })
  }
  //获取部门数据
  const getEmployeeData = () => {
    customerService2.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. DeptName + '-' + x.DeptId
        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.DeptName + '-' + x.DeptId
      x.newId = x.DataType + '-' + x.DeptId
      if (x.ChildList && x.ChildList.length > 0) {
        getChildList(x.ChildList)
      }
    })
  }
  return {
    msg,
    data,
    getDetail,
    getEmployeeData,
    jumpBeforePage,
    changeDialog
  }
}

export default editRuleModule
