import { wmFetch, ResponseDataEntity } from '.';

export enum FetchWasherStatus {
  unusing = 0,
  using = 1,
}
type FetchDeviceListByStatusParams = {
  campusId: number;
  customerId: number;
  status: FetchWasherStatus;
};
export enum AppointOrderStatus { // 订单状态 0：预约中；1：已使用；2：预约超期
  appointing = 0,
  using = 1,
  outtime = 2,
}
type AppointOrderService = {
  duration: number; // 功能程序洗涤时长，单位：分钟
  id: number; // 功能服务id
  modelServiceConfigId: number;
  name: string; // 功能服务名称
  price: number; // 功能服务价格
};
export type AppointOrder = {
  bluetoothMode: number; // 蓝牙模式 0：无蓝牙；1：纯蓝牙；2：蓝牙+其他
  deviceCode: string; // 设备号
  deviceName: string; // 设备名称
  endDate: string; // 预约失效时间
  money: number; // 订单金额
  orderCode: string; // 订单编号
  position: string; // 设备位置
  services: AppointOrderService[];
  startDate: string; // 预约开始时间
  status: AppointOrderStatus;
};
export type WashingMachineDevice = {
  bluetoothMode: number; // 蓝牙模式 0：无蓝牙；1：纯蓝牙；2：蓝牙+其他
  deviceCode: string; // 设备号
  deviceName: string; // 设备名称
  isRunning: boolean; // 是否运行中
  position: string; // 位置
};
type FetchDeviceListByStatusRes = {
  appointOrder: AppointOrder;
  deviceList: WashingMachineDevice[];
};
export const fetchDeviceListByStatus = (
  entity: FetchDeviceListByStatusParams,
): Promise<ResponseDataEntity<FetchDeviceListByStatusRes>> =>
  wmFetch({
    url: '/dcxy/api/washer/devices',
    data: entity,
  });
export type Position = {
  id: string;
  name: string;
};
type FetchPositionListParams = {
  campusId: number;
  customerId: number;
  name?: string;
};
type FetchPositionListRes = {
  positions: Position[];
  usedPositions: Position[];
};
export const fetchPositionList = (
  entity: FetchPositionListParams,
): Promise<ResponseDataEntity<FetchPositionListRes>> =>
  wmFetch({
    url: `/dcxy/api/washer/${entity.campusId}/${entity.customerId}/positions`,
    data: entity,
  });

type PositionHandleParams = {
  customerId: number;
  positionId: string;
};
export const addUsedPosition = (
  entity: PositionHandleParams,
): Promise<ResponseDataEntity<null>> =>
  wmFetch({
    url: `/dcxy/api/washer/${entity.customerId}/positions/${entity.positionId}?positionId=${entity.positionId}`,
    method: 'POST',
    data: entity,
  });
export const delUsedPosition = (
  entity: PositionHandleParams,
): Promise<ResponseDataEntity<null>> =>
  wmFetch({
    url: `/dcxy/api/washer/${entity.customerId}/positions/${entity.positionId}?positionId=${entity.positionId}`,
    method: 'DELETE',
    data: entity,
  });
type FetchAppointmentListParams = {
  customerId: number;
  pageNum?: number;
  pageSize?: number;
};
export const fetchAppointmentList = (
  entity: FetchAppointmentListParams,
): Promise<ResponseDataEntity<AppointOrder[]>> =>
  wmFetch({
    url: '/dcxy/api/washer/appointOrders',
    data: entity,
  });
type WasherPayData = {
  partyPayment: number; // 是否为第三方支付类型，默认：1-是
  paymentWayId: number; // 支付方式ID
  paymentWayName: string; // 支付方式名称
};
type FetchWasherDeviceInfoParams = {
  deviceCode: string;
  customerId: number;
};
export type FetchWasherDeviceInfoRes = {
  bluetoothMode: number; // 蓝牙模式 0：无蓝牙；1：纯蓝牙；2：蓝牙+其他
  deviceCode: string; // 设备号
  deviceName: string; // 设备名称
  enabledAppoint: boolean;
  optionalPrograms: AppointOrderService[];
  paymentWaysInner: WasherPayData[];
  paymentWaysOuter: WasherPayData[];
  position: string;
  requiredPrograms: AppointOrderService[];
};
export const fetchWasherDeviceInfo = (
  entity: FetchWasherDeviceInfoParams,
): Promise<ResponseDataEntity<FetchWasherDeviceInfoRes>> =>
  wmFetch({
    url: `/dcxy/api/washer/functionPage/${entity.deviceCode}`,
    data: entity,
  });

type CreateWasherOrderParams = {
  customerId: number; // 用户ID
  customerName: string; // 用户名称
  customerPhone: string; // 用户手机号
  deviceCode: string; // 设备编号
  modelServiceIds: number[];
  orderType: number; // 订单确认类型
  payType: number; // 订单支付方式
};
export type CreateWasherOrderRes = {
  appointmentTimeout: number; // 超时时间，单位分钟
  deviceCode: string; // 设备号
  deviceName: string; // 设备名称
  money: number; // 总金额，最多2位小数
  orderCode: string; // 订单编号
  orderType: number; // 订单类型，1即时订单，2预约订单
  payTimeout: number; // 支付超时时间，单位秒
  paymentWayId: number; // 支付方式ID
  position: string; // 设备位置
  serviceId: number; // 消费的服务类型ID
  services: AppointOrderService[];
};
export const createWasherOrder = (
  entity: CreateWasherOrderParams,
): Promise<ResponseDataEntity<CreateWasherOrderRes>> =>
  wmFetch({
    url: '/dcxy/api/washer/order',
    method: 'POST',
    data: entity,
  });
type PayWasherOrderParams = {
  orderCode: string;
};
type PayWasherOrderRes = {
  money: number; // 消费金额
  orderCode: string; // 订单号
  payStr: string; // 支付要素
};
export const payWasherOrder = (
  entity: PayWasherOrderParams,
): Promise<ResponseDataEntity<PayWasherOrderRes>> =>
  wmFetch({
    url: `/dcxy/api/washer/order/${entity.orderCode}/pay`,
    method: 'POST',
    data: entity,
  });
export const payWasherOrderCallback = (entity: PayWasherOrderParams) =>
  wmFetch({
    url: '/dcxy/api/washer/pay/callback?orderNum=' + entity.orderCode,
    method: 'POST',
    data: entity,
  });

type BeginWasherParams = {
  orderCode: string;
  deviceCode: string;
};
export const beginWasher = (entity: BeginWasherParams) =>
  wmFetch({
    url: `/dcxy/api/washer/begin/${entity.deviceCode}?orderCode=${entity.orderCode}`,
    method: 'POST',
    data: entity,
  });
