import customerSetService from '@/api/customerSet'
import { ref, reactive } from 'vue'
import clueService, { ClueParams } from '@/api/clue'
import { ColumnDefaultState } from '@/@types/enumHelper'

const ClueModule = () => {
  // 定义tab
  const useTab = () => {
    const tab = ref(1)
    const setTab = (name: number) => {
      tab.value = name
    }
    return [tab, setTab]
  }
  const TypeList = reactive([
    {
      Name: '企业',
      Id: 0
    },
    {
      Name: '昵称',
      Id: 1
    },
    {
      Name: '电话',
      Id: 2
    }
  ])
  const SeletObj = reactive({
    selectVal: '',
    selectWay: 0
  })

  const msg = reactive<ClueParams>({
    ClueState: 1,
    PageIndex: 1,
    PageSize: 10,
    pageCount: 0,
    CorpName: '', //企业名称
    CustomerName: '', //备注名
    WeChatName: '', //昵称
    CustomerMobile: '', //手机号码
    CreateSTime: '', //创建开始时间
    CreateETime: '', //创建结束时间
    FriendSTime: '', //好友开始时间
    FriendETime: '', //好友结束时间
    Q_NotFollowUpDay: 0, //几天未跟进
    Q_Friends: '', //好友关系 多选逗号分隔、
    Q_AddWay: '', //获取来源 多选英文逗号分隔
    EmpId: 0, //员工id
    CustomerType: 0, //类型1微信用户 2企业微信用户
    SelectList: [], //自定义查询
    OrderBy: 1, //排序
    AddCondition: 0 //查询条件
  })

  const data = reactive({
    columns: [
      {
        name: 'Id',
        label: '编号',
        field: 'Id',
        align: 'left'
      },
      {
        name: 'Sys_CustomerName',
        label: '客户',
        field: 'Sys_CustomerName',
        align: 'left'
      },
      { name: 'CreateTime', label: '创建时间', field: 'CreateTime', align: 'left' }
    ],
    showColumns: ['Sys_CustomerName', 'CreateTime'],
    allColumns: [{ name: '创建时间', value: 'CreateTime', visible: true, data: {}, IsCustom: 0 }],
    selected: [],
    dataList: [],
    WayList: [],
    EmployeeList: [],
    rowsPerPage: 0,
    colLoadingFinish: false,
    dataLoding: false
  })

  customerSetService
    .getCustomerFiledList({
      Enable: 1
    })
    .then(r => {
      const tempCol = r.data.Data
      if (tempCol && tempCol.length > 0) {
        console.log(tempCol)
        tempCol.sort((x, y) => {
          return x.Sort - y.Sort
        })
        tempCol.forEach(x => {
          const tempRow = { name: `diy_${x.Name}`, label: x.Name, field: `diy_${x.Name}`, align: 'left' }
          data.columns.push(tempRow)
          if (x.IsDefault == ColumnDefaultState.Yes) {
            data.showColumns.push(`diy_${x.Name}`)
          }
          data.allColumns.push({ name: x.Name, value: `diy_${x.Name}`, visible: x.IsDefault == ColumnDefaultState.Yes, data: x, IsCustom: 1 })
        })
      }
      //#region 添加菜单权限
      data.columns.push({ name: 'sys_setting', label: '自定义列', field: 'sys_setting', align: 'left' })
      data.showColumns.push('sys_setting')
      //#endregion

      data.colLoadingFinish = true
    })

  //获取线索列表
  const getCluerList = () => {
    data.dataLoding = true
    clueService.getCustomerClueList(msg).then(res => {
      data.dataList = res.data.Data.PageData
      msg.pageCount = res.data.Data.PageCount
      data.dataLoding = false
    })
  }

  const reloadListHandler = (obj: any) => {
    console.log(obj)
    msg.CustomerName = obj.CustomerName
    msg.CreateETime = obj.CreateETime
    msg.CreateSTime = obj.CreateSTime
    msg.AddCondition = obj.AddCondition
    msg.SelectList = obj.SelectList
    getCluerList()
  }

  return { useTab, msg, data, getCluerList, TypeList, SeletObj, reloadListHandler }
}
export default ClueModule
