import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { CustomerVO, CustomerForm, CustomerQuery } from './types';

/**
 * 查询客户基本信息列表
 * @param query
 * @returns {*}
 */

export const listCustomer = (query?: CustomerQuery & PageQuery): AxiosPromise => {
  return request({
    url: '/customer/list',
    method: 'post',
    data: query
  });
};

/**
 * 查询客户基本信息详细
 * @param id
 */
export const getCustomer = (id: string | number, scopeMenuId?: string): AxiosPromise<CustomerVO> => {
  return request({
    url: '/customer/get-all-by-id',
    method: 'post',
    data: {
      id: id,
      scopeMenuId: scopeMenuId
    }
  });
};

/**
 * 客户详情 列表进
 * @param id
 */
export const getNexCustomer = (query): AxiosPromise<CustomerVO> => {
  return request({
    url: '/customer/get-pre-nex-by-id',
    method: 'post',
    data: query
  });
};

/**
 * 新增客户基本信息
 * @param data
 */
export const addCustomer = (data: CustomerForm) => {
  return request({
    url: '/customer/add',
    method: 'post',
    data: data
  });
};

/**
 * 修改客户基本信息
 * @param data
 */
export const updateCustomer = (data: CustomerForm) => {
  return request({
    url: '/customer/edit',
    method: 'post',
    data: data
  });
};

/**
 * 修改线索状态
 * @param data
 */
export const updateClueStatus = (data: CustomerForm) => {
  return request({
    url: '/customer/chenge-clus-status',
    method: 'post',
    data: data
  });
};

/**
 * 删除客户基本信息
 * @param id
 */
export const delCustomer = (id?: string | number | Array<string | number>) => {
  return request({
    url: '/customer/delete',
    method: 'post',
    data: { id }
  });
};

/**
 * 客户管理-新增重点关注
 * @param customerId
 */
export const followCustomer = (customerId?: string | number | Array<string | number>) => {
  return request({
    url: '/customer/add-follow',
    method: 'post',
    data: { customerId }
  });
};

/**
 * 客户管理-取消重点关注
 * @param data
 */
export const cancelCustomer = (customerId?: string | number | Array<string | number>) => {
  return request({
    url: '/customer/cancel',
    method: 'post',
    data: { customerId }
  });
};

/**
 * 合并客户
 * @param data
 */
export const mergeCustomer = (data?: CustomerForm) => {
  return request({
    url: '/customer/merge',
    method: 'post',
    data: data
  });
};

/**
 * 转为客户
 * @param data
 */
export const convertCustomer = (data?: CustomerForm) => {
  return request({
    url: '/customer/convert-to-customer',
    method: 'post',
    data: data
  });
};

/**
 * 领取线索/客户
 * @param data
 */
export const receiveClue = (data: { idList: (string | number)[] }) => {
  return request({
    url: '/customer/collect-clues',
    method: 'post',
    data: data
  });
};

/**
 * 分配线索
 * @param data
 */
export const distributionClue = (data: { idList: (string | number)[], ownerBy: number, remark: string }) => {
  return request({
    url: '/customer/assign-clues',
    method: 'post',
    data: data
  });
};

/**
 * 移入公海
 * @param data
 */
export interface movePublicSeaTypes {
  idList: (string | number)[],
  hasPublicCustomer: boolean,
  reasonPublicCustomerDictValue: string
}
export const movePublicSea = (data: movePublicSeaTypes) => {
  return request({
    url: '/customer/for-reason',
    method: 'post',
    data: data
  });
};

/**
 * 转移客户\线索
 * @param data
 */
export interface tranferTypes {
  idList: (string | number)[],
  newOwnerBy: string,
  remark: string,
  resourceType: number | string
}
export const tranferCustomer = (data: tranferTypes) => {
  return request({
    url: '/customer/transfer',
    method: 'post',
    data: data
  });
};

/**
 * 离职继承
 * @param data
 */
export interface ResignTransferTypes {
  oldOwnerByIdls: (string | number)[],
  newOwnerBy?: string | number
}
export const resignTransfer = (data: ResignTransferTypes) => {
  return request({
    url: '/customer/resignTransfer',
    method: 'post',
    data: data
  });
};

/**
 * 添加协作
 * @param data
 */
export interface collaboratorDTO {
  resourceIdList?: (string | number)[],
  resourceType?: string,
  type?: string | number,
  userIdList?: string[]
}
export const collaborator = (data: collaboratorDTO) => {
  return request({
    url: '/customer/collaborator',
    method: 'post',
    data: data
  });
};

/**
 * 关联数据统计
 * @param data
 */
export const dataCount = (id?: string | number | Array<string | number>) => {
  return request({
    url: '/customer/data-count',
    method: 'post',
    data: { id }
  });
};

/**
 * 通过id查看客户资料详情
 * @param data
 */
export const getAllById = (id: string | number | Array<string | number>) => {
  return request({
    url: '/customer/get-all-by-id',
    method: 'post',
    data: { id }
  });
};

/**
 * 获取用户code
 * @param data
 */
export const generateBizCode = (data: any) => {
  return request({
    url: '/common/generateBizCode',
    method: 'post',
    data: data
  });
};

/**
 * 批量编辑客户
 * @param data
 */
export const editCustomerList = (data: any) => {
  return request({
    url: '/customer/edit-list',
    method: 'post',
    data: data
  });
};

/**
 * 校验客户
 * @param data
 */
export const checkCustomer = (data: CustomerForm) => {
  return request({
    url: '/customer/add-check',
    method: 'post',
    data: data
  });
};

/**
 * 单个查重
 * @param data
 */
export const singleCheckCustomer = (data) => {
  return request({
    url: '/customer/add-check-by-field',
    method: 'post',
    data: data
  });
};

/**
 * 报价记录
 * @param data
 */
export const quotationList = (query) => {
  return request({
    url: '/quotation/list',
    method: 'get',
    params: query
  });
};

/**
 * 归属记录
 * @param data
 */
export const ownerRecordList = (query?: CustomerQuery & PageQuery): AxiosPromise => {
  return request({
    url: '/customer-owner-record/list',
    method: 'post',
    data: query
  });
};

/**
 * 变更首联系人
 * @param data
 */
export const changeLialison = (query) => {
  return request({
    url: '/liaison/change-primary-lialison',
    method: 'post',
    data: query
  });
};

/**
 * 数据中有字段为空的处理, 字段为空（null、字符串为空，带空格字符串）都改为'--'
 * @param {*} initialData 数据类型包含基础类型值（布尔型数据保留）、数组、对象
 */
export const formateNullFieldData = (initialData: any) => {
  const isEmpty = val => {
    console.log(typeof (val), ">>>>>>>>>>>", val)

    // null、字符串为空（注意存在数值为0时），带空格字符串，后台处理成的'-'
    if (!String(val) || String(val) === 'null' || !String(val).replace(/\s+/g, '') || val === '-') {
      return '--'
    }
    return val
  }

  if (initialData instanceof Array) {
    // 当数据为数组时
    initialData.forEach((val, key, array) => {
      array[key] = formateNullFieldData(val)
    })
  } else if (initialData instanceof Object) {
    // 当数据为对象时
    for (let pro in initialData) {
      initialData[pro] = formateNullFieldData(initialData[pro])
    }
  } else {
    // 当数据为基础类型时
    initialData = isEmpty(initialData)
  }
  return initialData
}

/**
 * 合并客户处理数据需要
 * 数据中有字段为空的处理, 字段为空（null、字符串为空，带空格字符串）都改为''
 * @param {*} initialData 数据类型包含基础类型值（布尔型数据保留）、数组、对象
 */
export const formateStringFieldData = initialData => {
  const isEmpty = val => {
    // null、字符串为空（注意存在数值为0时），带空格字符串，后台处理成的'-'
    if (!String(val) || String(val) === 'null' || !String(val).replace(/\s+/g, '') || val === '-') {
      return ''
    }
    return val
  }

  if (initialData instanceof Array) {
    // 当数据为数组时
    initialData.forEach((val, key, array) => {
      array[key] = formateStringFieldData(val)
    })
  } else if (initialData instanceof Object) {
    // 当数据为对象时
    for (let pro in initialData) {
      initialData[pro] = formateStringFieldData(initialData[pro])
    }
  } else {
    // 当数据为基础类型时
    initialData = isEmpty(initialData)
  }
  return initialData
}
