// import message from '@/utils/message'
import dayjs from 'dayjs'
import { reactive } from 'vue'
import customer, { CutomerParams } from '@/api/customer'
import customerSetService from '@/api/customerSet'
import { ColumnDefaultState } from '@/@types/enumHelper'

interface memberParams {
  newId: number | string
  DataType: number | string
  DeptId: number
  ChildList: Array < any > | null
}
interface dataParams {
  loading: boolean
  columns: Array<any>
  sysColumns:Array<any>
  showColumns: Array<any>
  allColumns: Array<any>
  dataList: Array<any>
  WayList: Array<any>
  selected: Array<any>
  EmployeeList: Array<memberParams>
  defaultArray: Array<number | string>
  customerCascaderValue: Array<any> | string
  deptCascaderValue: string | null
  colLoadingFinish:boolean
}
const CustomerModule = () => {
  //获取列表
  const getCustomerList = () => {
    data.loading=true
    customer.getCustomerList(msg).then(res => {
      if (res.data.Code == 1) {
        data.loading = false
        data.dataList = res.data.Data.PageData
        msg.pageCount = res.data.Data.PageCount
      }
    })
  }
  //调用获取下拉
  const getWayList = (param: any) => {
    customer.getWayList(param).then(res => {
      if (res.data.Code == 1) {
        data.WayList = res.data.Data
      }
    })
  }
  //获取员工数据
  const getEmployeeData = () => {
    customer.getEmployeeData({}).then(res => {
      data.EmployeeList = res.data.Data
      // 重组唯一id
      if (data.EmployeeList.length == 0) return
      data.EmployeeList.forEach(x => {
        x.newId = x.DataType + '-' + x.DeptId
        if (x.DataType === 2) {
          x.ChildList = null
        }
        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.DataType === 2) {
        x.ChildList = null
      }
      if (x.ChildList && x.ChildList.length > 0) {
        getChildList(x.ChildList)
      }
    })
  }
  //控制页显示条数
  const pagination = reactive({
    rowsPerPage: 0
  })

  const data = reactive<dataParams>({
    loading: false,
    columns: [
      {
        name: 'CustomerName',
        label: '客户',
        field: 'CustomerName',
        align: 'left'
      }
    ],
    sysColumns: [
      {
        name: 'StageName',
        label: '客户阶段',
        field: 'StageName',
        align: 'left'
      },
      {
        name: 'LableList',
        label: '客户标签',
        field: 'LableList',
        align: 'left'
      },
      {
        name: 'carbs',
        label: '内容标签',
        field: 'carbs',
        align: 'left'
      },
      {
        name: 'sodium',
        label: '负责人',
        field: 'sodium',
        align: 'left'
      },
      {
        name: 'LastFollowUpTime',
        label: '上次跟进',
        field: 'LastFollowUpTime',
        align: 'left'
      },
      {
        name: 'Source',
        label: '渠道活码来源',
        field: 'Source',
        align: 'left'
      },
      {
        name: 'FriendTime',
        label: '添加好友时间',
        field: 'FriendTime',
        align: 'left'
      },
      {
        name: 'CheckInNum',
        label: '签到拜访',
        field: 'CheckInNum',
        align: 'left'
      }
    ],
    showColumns: ['CustomerName'], //对应allColumns的value
    allColumns: [],
    selected: [],
    dataList: [],
    WayList: [],
    EmployeeList: [],
    defaultArray: [], //
    deptCascaderValue: null,
    customerCascaderValue: '',
    colLoadingFinish: false
  })
  const msg = reactive<CutomerParams>({
    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 TypeList = reactive([{
      Name: '企业',
      Id: 0
    },
    {
      Name: '昵称',
      Id: 1
    },
    {
      Name: '备注名',
      Id: 2
    },
    {
      Name: '电话',
      Id: 3
    }
  ])
  const TimeList = reactive([{
      Name: '客户创建时间',
      Id: 1
    },
    {
      Name: '好友添加时间',
      Id: 2
    }
  ])
  //切换时间范围
  const changeTime = () => {
    msg.FriendSTime = ''
    msg.FriendETime = ''
    msg.CreateSTime = ''
    msg.CreateETime = ''
    TimeObj.timeRange=null
  }
  //选择时间范围
  const changeTimeRange = val => {
    if (val) {
      const s = dayjs(val[0]).format('YYYY-MM-DD HH:mm:ss')
      const e = dayjs(val[1]).format('YYYY-MM-DD HH:mm:ss')
      if (TimeObj.timeWay == 1) {
        msg.CreateSTime = s
        msg.CreateETime = e
      } else {
        msg.FriendSTime = s
        msg.FriendETime = e
      }
    } else {
       msg.FriendSTime = ''
       msg.FriendETime = ''
       msg.CreateSTime = ''
       msg.CreateETime = ''
    }
    getCustomerList()
  }
  //客户筛选
  const CustomList = reactive([{
      label: '好友关系',
      value: '1',
      children: [{
          label: '未添加',
          value: '1-1'
        },
        {
          label: '已添加',
          value: '1-2'
        }
      ]
    },
    {
      label: '获取途径',
      value: 2
    },
    {
      label: '未跟进客户',
      value: 3
    }
  ])

  const SeletObj = reactive ({
    selectVal: '',
    selectWay: 0
  })
  const TimeObj = reactive ({
    timeWay: 1,
    timeRange: null
  })
  //选择方式
  const getSelectWay = () => {
    msg.CorpName = ''
    msg.WeChatName = ''
    msg.CustomerName = ''
    msg.CustomerMobile = ''
    //1企业名称 2姓名 3备注名 4手机号
    if (SeletObj.selectWay == 0) {
      msg.CorpName = SeletObj.selectVal
    }
    if (SeletObj.selectWay == 1) {
      msg.WeChatName = SeletObj.selectVal
    }
    if (SeletObj.selectWay == 2) {
      msg.CustomerName = SeletObj.selectVal
    }
    if (SeletObj.selectWay == 3) {
      msg.CustomerMobile = SeletObj.selectVal
    }
  }
  const changePage = (val: any) => {
    msg.PageIndex = val
    getCustomerList()
  }
  const getDept = (id: any) => {
    //数据处理 获取id是 1-id,2-id 前面1代表部门2代表人员 后面是原生ID
    if (id) {
      msg.EmpId = id.slice(2)
    } else {
      msg.EmpId = 0
    }
    getCustomerList()
  }
  const customerUpdateValue = val => {
    console.log('customerUpdateValue', val)
  }
  data.sysColumns.map(e => {
    data.columns.push(e)
    data.showColumns.push(e.field)
    data.allColumns.push({ name: e.label, value: e.field, visible: true, IsCustom: 0 })
  })
   customerSetService
     .getCustomerFiledList({
       Enable: 1
     })
     .then(r => {
       const tempCol = r.data.Data
       if (tempCol && tempCol.length > 0) {
         
         tempCol.sort((x, y) => {
           return x.Sort - y.Sort
         })
         tempCol.forEach(x => {
           const tempRow = { name: x.Name, label: x.Name, field: x.Name, align: 'left' }
           data.columns.push(tempRow)
           if (x.IsDefault == ColumnDefaultState.Yes) {
             data.showColumns.push(x.Name)
           }
           data.allColumns.push({ name: x.Name, value: x.Name, visible: x.IsDefault == ColumnDefaultState.Yes, data: x, IsCustom: 1 })
         })
       }
       //#region 添加菜单权限
       data.columns.push({ name: 'setting', label: '自定义列', field: 'setting', align: 'left' })
       data.showColumns.push('setting')
       //#endregion

       data.colLoadingFinish = true
     })

  return {
    getCustomerList,
    getWayList,
    getEmployeeData,
    data,
    msg,
    TypeList,
    SeletObj,
    getSelectWay,
    TimeObj,
    TimeList,
    changeTime,
    changeTimeRange,
    changePage,
    pagination,
    CustomList,
    getDept,
    customerUpdateValue,
  }
}

export default CustomerModule
