// import { ResultType } from '@/@types/enumHelper'
// import router from '@/router'
// import UserActions from '@/store/modules/user/actions'//{ UserActionsType }

// import { UserGetter } from '@/store/modules/user/getters'
// import { dispatchAction, getStoreGetter, setStoreState } from '@/store/utils'
// import message from '@/utils/message'
import { StaticConfig } from '@/config/app'
import { ref, reactive } from 'vue'
import customer2, { CutomerParams } from '@/api/customer2'
interface Params {
  selectVal: string
  selectWay: number
}
interface timeParams {
  timeWay: number
}
interface customParams {
  customWay: number
}
interface friendParams {
  frendArr: Array<number>
  wayArr: Array<number>
}
interface memberParams {
  newId: number | string
  DataType: number | string
  DeptId: number
  ChildList: Array<any>
}
interface dataParams {
  columns: Array<any>
  dataList: Array<any>
  WayList: Array<any>
  selected: Array<any>
  EmployeeList: Array<memberParams>
  defaultArray: Array<number|string>
}
const CustomerModule = () => {
  //调用方法
  const getCustomerList = (param: CutomerParams) => {
    customer2.getCustomerList(param).then(res => {
      console.log(res, '数据')
      if (res.data.Code == 1) {
        data.dataList = res.data.Data.PageData
        msg.pageCount = res.data.Data.PageCount
      }
    })
  }
  //调用获取下拉
  const getWayList = (param: any) => {
    customer2.getWayList(param).then(res => {
      if (res.data.Code == 1) {
        data.WayList = res.data.Data
      }
    })
  }
  //获取员工数据
  const getEmployeeData = (param: any) => {
    customer2.getEmployeeData(param).then(res => {
      data.EmployeeList = res.data.Data
      console.log(data, '员工数据')
      // 重组唯一id
      if (data.EmployeeList.length == 0) return
      data.EmployeeList.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 title = ref(StaticConfig.appsuffix)
  //控制页显示条数
  const pagination = reactive({
    rowsPerPage: 0
  })
  //日期对象
  // const daySelect =
  const data = reactive<dataParams>({
    columns: [
      {
        name: 'CustomerName',
        label: '客户',
        field: 'CustomerName',
        align: 'left'
      },
      { name: 'StageName', align: 'center', field: 'StageName', label: '客户阶段' },
      { name: 'LableList', label: '客户标签', field: 'LableList', align: 'left' },
      { name: 'carbs', label: '内容标签', field: 'carbs', align: 'left' },
      { name: 'CreateTime', label: '创建时间', field: 'CreateTime', align: 'left' },
      { name: 'sodium', label: '负责人', field: 'sodium', align: 'left' },
      { name: 'LastFollowUpTime', label: '上次跟进', field: 'LastFollowUpTime', align: 'left' },
      { name: 'Source', label: '渠道活码来源', field: 'Source', align: 'left' },
      // { name: 'num', label: '7日内客户回复消息数', align: 'left'},
      // { name: 'num', label: '7日内员工发送消息数', align: 'left'},
      { name: 'FriendTime', label: '添加好友时间', field: 'FriendTime', align: 'left' },
      { name: 'CheckInNum', label: '签到拜访', field: 'CheckInNum', align: 'left' }
    ],
    selected: [],
    dataList: [],
    WayList: [],
    EmployeeList: [],
    defaultArray: [] //
  })
  const msg = reactive({
    PageIndex: 1,
    PageSize: 10,
    pageCount: 0,
    CorpName: '', //企业名称
    CustomerName: '', //备注名
    WeChatName: '', //昵称
    CustomerMobile: '', //手机号码
    CreateSTime: '', //创建开始时间
    CreateETime: '', //创建结束时间
    FriendSTime: '', //好友开始时间
    FriendETime: '', //好友结束时间
    Q_NotFollowUpDay: 0, //几天未跟进
    Q_Friends: '', //好友关系 多选逗号分隔、
    Q_AddWay: '', //获取来源 多选英文逗号分隔
    EmpId: 0, //员工id
    DeptId: 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 CustomList = reactive([
    {
      Name: '好友关系',
      Id: 1
    },
    {
      Name: '获取途径',
      Id: 2
    },
    {
      Name: '未跟进客户',
      Id: 3
    }
  ])
  const friendOptions = reactive([
    {
      Name: '好友客户',
      Id: 1
    },
    {
      Name: '待添加',
      Id: 2
    },
    {
      Name: '已申请',
      Id: 3
    }
  ])
  const SeletObj = reactive<Params>({
    selectVal: '',
    selectWay: 0
  })
  const TimeObj = reactive<timeParams>({
    timeWay: 1
  })
  const customSetObj = reactive<customParams>({
    customWay: 1
  })
  const friendObj = reactive<friendParams>({
    frendArr: [],
    wayArr: []
  })
  //选择方式
  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(msg)
  }
  const getChild = (deptArray: any) => {
    //数据处理 获取夏利是 1-2 前面代表部门还是人员  后面是原生ID
    if(deptArray._value){
      const Type = deptArray._value.split('-')[0];
      const MyId = deptArray._value.split('-')[1]  
      //1部门
      if(Type==1){
        msg.DeptId = MyId
      }
      //2人员
      if(Type==2){
        msg.EmpId = MyId
      }
    }else{
      msg.DeptId=0;
      msg.EmpId=0;
    }
    getCustomerList(msg)
  }
  const getCkedFriend = (e:any)=>{
    const Ids = e.map((x:any)=>{
      return x.Id
    }).toString();
    msg.Q_Friends = Ids;
    getCustomerList(msg)
  }

  return { getCustomerList, getWayList, getEmployeeData, data, msg, title, TypeList, SeletObj, getSelectWay, TimeObj, TimeList, changePage, pagination, 
    CustomList, customSetObj, friendObj, friendOptions, getChild, getCkedFriend}
}

export default CustomerModule
