import { httpDelete, httpGet, httpPost, httpPut } from '../../utils/http'
import {
  ResPageData,
  Designer,
  AgentTask,
  ReportInfo,
  FollowUp,
  DesignerBasicInfo,
  DesignerEvent,
  DesignerFamilyInfo,
  AgentPoint,
  DesignerAward,
  DesignerPointsStatistics,
  PointsOrder,
  DesignerOrderSaleOther,
  BrowseRecordCountRes,
  BrowseRecord,
} from '@designer-hub/app/src/core/libs/models'
import dayjs from 'dayjs'
/**
 * 通过ID获取用户信息
 */
export const getUserInfoById = (id: string | number) =>
  httpGet<
    Partial<{
      mobile: string
      status: string
      nickname: string
      avatar: string
      name: string
      sex: string
      areaId: number
      areaName: string
      birthday: string
      mark: string
      tagIds: string
      levelId: number
      groupId: number
      id: number
      registerIp: string
      loginIp: string
      loginDate: string
      createTime: string
      point: number
      totalPoint: number
      tagNames: string
      levelName: string
      groupName: string
      experience: number
    }>
  >('/app-api/member/user/getByUserId', { id })
/**
 * 获取设计师信息
 */
// export const getDesignerInfo = (userId: number) =>
//   httpGet<Designer>('/app-api/member/designer/getDesignerInfo', { userId })
/**
 * 获取设计师基本信息
 */
export const getDesignerBasicInfo = (userId: number) =>
  httpGet<DesignerBasicInfo>('/app-api/member/stylist-basic-info/get', { userId })
/**
 * 更新设计师基本信息
 */
export const updateDesignerBasicInfo = (data: Partial<DesignerBasicInfo>) =>
  httpPut('/app-api/member/stylist-basic-info/update', data)
/**
 * 获取设计师家庭信息
 */
export const getDesignerFamilyInfo = (query = {}) =>
  httpGet<ResPageData<DesignerFamilyInfo>>('/app-api/member/stylist-family/list', query)
/**
 * 保存设计师家庭信息
 */
export const saveDesignerFamilyInfo = (data: Partial<DesignerFamilyInfo>) =>
  httpPost('/app-api/member/stylist-family/save', data)
/**
 * 获取设计师额外事件
 */
export const getDesignerExtraEvents = (query = {}) =>
  httpGet<ResPageData<DesignerEvent>>('/app-api/member/stylist-extra-events/pageInfo', query)
/**
 * /app-api/member/stylist-extra-events/getStudyAndActivity 获取设计师学习和活动
 */
export const getDesignerActivity = (query = {}) =>
  httpGet<{
    [key in 'study' | 'activity' | 'extraEventsQuantity']:
      | { type: number; quantity: number }[]
      | number
  }>('/app-api/member/stylist-extra-events/getStudyAndActivity', query)
/**
 * 设计师列表
 */
export const getDesigners = (query = {}) =>
  httpGet<ResPageData<Designer>>('/app-api/member/app-broker/pageStylist', query)
/**
 * 任务列表
 */
export const getTasks = (query = {}) =>
  httpGet<ResPageData<AgentTask>>('/app-api/member/task/page', query)
/**
 * 关系报备
 */
export const getReportInfoPage = (query = {}) =>
  httpGet<ResPageData<ReportInfo>>('/app-api/member/report-info/page', query)
/**
 * 删除关系报备
 */
export const deleteReportInfo = (id: number) =>
  httpDelete(`/app-api/member/report-info/delete`, { id })
/**
 * 创建设计师跟进
 */
export const createFollowUp = (data: Partial<FollowUp>) =>
  httpPost('/app-api/member/stylist-follow-up/create', data)
/**
 * 获取设计师跟进列表
 */
export const getFollowUpPage = (query = {}) =>
  httpGet<ResPageData<FollowUp & { followTypeName: string; brokerName: string }>>(
    '/app-api/member/app-broker/pageFollowUp',
    query,
  )
/**
 * 更新设计师跟进
 */
export const updateFollowUp = (data: Partial<FollowUp>) =>
  httpPut('/app-api/member/stylist-follow-up/update', data)
/**
 * /app-api/member/user-auth-info/focus 重点跟进或取消
 */
export const focusOrCancel = (data: { brokerId: number; userId: number }) =>
  httpPost('/app-api/member/user-auth-info/focus', data)
/**
 * /app-api/member/task/points_flow 积分流水
 */
export const getPointsFlow = (query = {}) =>
  httpGet<ResPageData<AgentPoint>>('/app-api/member/task/points_flow', query)
/**
 * /app-api/member/points-details/pageAllByYear/2024?pageNo=1&pageSize=10&stylistId=287&year=2024 设计师积分详情
 */
export const getPointsDetails = (query: { year?: string } = {}) =>
  httpGet<
    ResPageData<{
      id: number
      pointsType: string
      typeName: string
      pointsCategory: string
      categoryName: string
      sourceId: number
      name: string
      pointsStauts: string
      points: number
      orderMoney: string
      materialsId: number
      materialsBrand: string
      stylistId: number
      brokerId: number
      stylistName: string
      brokerName: string
      generateTime: string
      verifyTime: string
      completeTime: string
      turnDownTime: string
      cancelTime: string
      cancelReason: string
      turnDownReason: string
      remark: string
      couponId: number
      orderNo: string
      createTime: string
    }>
  >(`/app-api/member/points-details/pageAllByYear/${query.year}`, query)
/**
 * 设计师奖品列表
 */
export const getAwards = (query = {}) =>
  httpGet<DesignerAward[]>('/app-api/member/stylist-awards/list', query)
/**
 * 创建设计师奖品
 */
export const createAward = (data: Partial<DesignerAward>) =>
  httpPost('/app-api/member/stylist-awards/create', data)
/**
 * 删除设计师奖品
 */
export const deleteAward = (id: number) =>
  httpDelete('/app-api/member/stylist-awards/delete', { id })
/**
 * 获取设计师销售订单
 */
export const getSalesOrders = (query = {}) =>
  httpGet<
    ResPageData<{
      id: number
      materials: string
      materialsBrand: string
      projectName: string
      customerName: string
      customerPhone: string
      orderMoney: number
      createTime: number
    }>
  >('/app-api/member/stylist-other-sales/pageByDate', query)
/**
 * 获取设计师积分统计
 */
export const getPointsCount = (query = {}) =>
  httpGet<DesignerPointsStatistics>('/app-api/member/points-details/getPointsCount', query)
/**
 * 设计师积分订单
 */
export const getPointsOrders = (query = {}) =>
  httpGet<ResPageData<PointsOrder>>('/app-api/member/points-order/pageDitch', query)
/**
 * 获取设计师销售订单统计
 */
export const getSalesOrdersCount = (query = {}) =>
  httpGet<{
    /**
     * 今年订单成交数量
     */
    orderCountYear: number
    /**
     * 今年成交金额
     */
    salesAmountYtd: number
    /**
     * 类型订单成交数量
     */
    orderCount: number
    /**
     * 累计成交金额
     */
    salesAmount: number
    /**
     * 其他订单数量
     */
    otherSale: number
  }>('/app-api/member/stylist-other-sales/get', query)
/**
 * 其他销售列表
 */
export const getOtherSalesPage = (query = {}) =>
  httpGet<ResPageData<DesignerOrderSaleOther>>(
    '/app-api/member/stylist-other-sales/OtherSalesPage',
    query,
  )
/**
 * /app-api/member/stylist-other-sales/save 创建其他销售
 */
export const saveOtherSales = (data: Partial<DesignerOrderSaleOther>) =>
  httpPost('/app-api/member/stylist-other-sales/save', data)
/**
 * 删除其他销售
 */
export const deleteOtherSales = (id: number) =>
  httpDelete('/app-api/member/stylist-other-sales/delete', { id })
/**
 * 更新其他销售
 */
export const updateOtherSales = (data: Partial<DesignerOrderSaleOther>) =>
  httpPost('/app-api/member/stylist-other-sales/update', data)
/**
 * 创建设计师活动
 */
export const createDesignerEvent = (query: Partial<DesignerEvent>) =>
  httpPost('/app-api/member/stylist-extra-events/create', query)
/**
 * 删除设计师活动
 */
export const deleteDesignerEvent = (id: number) =>
  httpDelete('/app-api/member/stylist-extra-events/delete', { id })
/**
 * 删除家庭信息
 */
export const deleteStylistFamily = (id: number) =>
  httpDelete('/app-api/member/stylist-family/delete', { id })
/**
 * 创建设计师关注
 */
export const createFocus = (data: { userId: number; focus: string }) =>
  httpPost('/app-api/member/stylist-extra-events/createFocus', data)
/**
 * 获取设计师活动
 */
export const getDesignerEvents = (query = {}) =>
  httpGet<ResPageData<DesignerEvent>>('/app-api/member/stylist-extra-events/pageInfo', query)
/**
 * 更新设计师活动
 */
export const updateDesignerEvent = (query: Partial<DesignerEvent>) =>
  httpPost('/app-api/member/stylist-extra-events/update', query)
/**
 * 获取设计师浏览记录统计
 */
export const getBrowseRecordCount = (stylistId: number) =>
  httpGet<BrowseRecordCountRes>(
    `/app-api/member/browse-record/browseRecordCount?stylistId=${stylistId}`,
  )
/**
 * /app-api/member/browse-record/page 浏览记录
 * @param query
 */
export const getBrowseRecords = (query = {}) =>
  httpGet<ResPageData<BrowseRecord>>('/app-api/member/browse-record/page', query)
/**
 * 浏览记录备注
 * @param query
 */
export const getBrowseRecordRemarks = (query = {}) =>
  httpGet<{ id: number; remark: string }[]>('/app-api/member/browse-record/listRemark', query)
export const getSalesOrdersCounts = (query = {}) =>
  getSalesOrdersCount(query).then((res) => ({
    ...res,
    data: [
      { label: '今年成交订单数', value: res.data.orderCountYear ?? 0, userId: '' },
      { label: '今年成交金额', value: res.data.salesAmountYtd ?? 0 },
      { label: '累计成交订单数', value: res.data.orderCount ?? 0 },
      { label: '累计成交金额', value: res.data.salesAmount ?? 0 },
      { label: '其他销售信息', value: `${res.data.otherSale ?? 0}条` },
    ],
  }))
/**
 * 获取设计师积分统计数组
 */
export const getPointsCounts = (query = {}) =>
  getPointsCount(query).then((res) => ({
    ...res,
    data: [
      {
        userId: '',
        title: '当前剩余积分',
        value: res.data.points ?? 0,
        subTitle: '最近',
        subValue: dayjs(res.data.pointsTime).format('YY/MM/DD'),
      },
      {
        userId: '',
        title: '累计获得积分',
        value: res.data.gainPoints ?? 0,
        subTitle: '本年',
        subValue: res.data.gainPointsYear ?? 0,
      },
      {
        userId: '',
        title: '累计消耗积分',
        value: res.data.usePoints ?? 0,
        subTitle: '本年',
        subValue: res.data.usePointsYear ?? 0,
      },
      {
        userId: '',
        title: '累计跟进次数',
        value: res.data.followUpCount ?? 0,
        subTitle: '本年',
        subValue: res.data.followUpYearCount ?? 0,
      },
      {
        userId: '',
        title: '累计消耗次数',
        value: res.data.usageCount ?? 0,
        subTitle: '本年',
        subValue: res.data.usageYearCount ?? 0,
      },
      {
        userId: '',
        title: '累计获得次数',
        value: res.data.obtainedCount ?? 0,
        subTitle: '本年',
        subValue: res.data.obtainedYearCount ?? 0,
      },
    ],
  }))
export const getDesignerActivities = (query = {}) =>
  getDesignerActivity(query).then((res) => ({
    ...res,
    data: [
      ...(res.data.study as any[]).map((it) => ({
        type: '',
        label: it.name,
        value: it.quantity + '次',
        path: '',
      })),
      { type: 'line', label: '', value: '' },
      ...(res.data.activity as any[]).map((it) => ({
        type: '',
        label: it.name,
        value: it.quantity + '次',
        path: '',
      })),
      {
        type: '',
        label: '其他活动',
        value: res.data.extraEventsQuantity + '次',
        path: '/pages/agent/designer/archives/index',
      },
    ],
  }))
