import type { Coupon2Entity, PageConfig } from '@vben/types';

import dayjs from 'dayjs';

import { requestClient } from '#/api/request';

interface Coupon2PartialEntity
  extends Partial<Omit<Coupon2Entity, 'coupon2code'>> {
  coupon2code?: string;
}

interface Coupon2QueryParams
  extends Omit<Coupon2PartialEntity, 'coupon2adddatetime'>,
    PageConfig {
  couponmc?: string;
  productsname?: string;
  productsmodel?: string;
  productsjjlx?: string;
  coupon2adddatetime?: string[];
}

/**
 * 我的优惠券信息_列表
 */
export async function getCoupon2ListApi(data: Coupon2QueryParams) {
  return requestClient.post<any>('/api/query/list?pagevalue=101', {
    pageindex: data.pageindex,
    rows: data.rows,
  });
}

/**
 * 我的优惠券信息_详情
 */
export async function getCoupon2DetailApi(data: { coupon2sid: string }) {
  return requestClient.post<any>(
    '/api/query/view?pagevalue=102',
    {
      'coupon2sid.value': data.coupon2sid,
    },
    { formatData: true }, // 格式化返回数据
  );
}

/**
 * 我的优惠券信息_新增
 */
export async function addCoupon2Api(data: Coupon2Entity) {
  return requestClient.post<any>('/api/add?pagevalue=40', { ...data });
}

/**
 * 我的优惠券信息_编辑
 */
export async function editCoupon2Api(data: Coupon2Entity) {
  return requestClient.post<any>('/api/up?pagevalue=41', { ...data });
}

/**
 * 我的优惠券_审核_列表（用于admin）
 */
export async function getCoupon2AuditListApi(data: Coupon2QueryParams) {
  const {
    pageindex,
    rows,
    couponmc,
    coupon2sype,
    productsname,
    productsmodel,
    productsjjlx,
    coupon2adddatetime,
  } = data;
  const queryParams: Record<string, any> = {
    pageindex,
    rows,
  };

  if (couponmc) {
    queryParams['coupon2mc.like'] = couponmc;
  }
  if (coupon2sype !== undefined && coupon2sype !== null) {
    queryParams['coupon2sype.value'] = coupon2sype;
  }
  if (productsname) {
    queryParams['productsname.like'] = productsname;
  }
  if (productsmodel) {
    queryParams['productsmodel.like'] = productsmodel;
  }
  if (productsjjlx) {
    queryParams['productsjjlx.like'] = productsjjlx;
  }

  if (coupon2adddatetime && coupon2adddatetime.length > 0) {
    queryParams['coupon2adddatetime.start'] = dayjs(coupon2adddatetime[0])
      .startOf('day')
      .format('YYYY-MM-DD HH:mm:ss');
    queryParams['coupon2adddatetime.end'] = dayjs(coupon2adddatetime[1])
      .endOf('day')
      .format('YYYY-MM-DD HH:mm:ss');
  }
  queryParams['coupon2adddatetime.sort'] = 1;
  return requestClient.post<any>('/api/query/list?pagevalue=117', queryParams);
}

/**
 * 我的优惠券_编辑_列表（用于经销商查询优惠劵列表）
 */
export async function getCoupon2EditListApi(data: Coupon2QueryParams) {
  const {
    pageindex,
    rows,
    couponmc,
    coupon2sype,
    productsname,
    productsmodel,
    productsjjlx,
    coupon2adddatetime,
  } = data;
  const queryParams: Record<string, any> = {
    pageindex,
    rows,
  };

  if (couponmc) {
    queryParams['coupon2mc.like'] = couponmc;
  }
  if (coupon2sype !== undefined && coupon2sype !== null) {
    queryParams['coupon2sype.value'] = coupon2sype;
  }
  if (productsname) {
    queryParams['productsname.like'] = productsname;
  }
  if (productsmodel) {
    queryParams['productsmodel.like'] = productsmodel;
  }
  if (productsjjlx) {
    queryParams['productsjjlx.like'] = productsjjlx;
  }

  if (coupon2adddatetime && coupon2adddatetime.length > 0) {
    queryParams['coupon2adddatetime.start'] = dayjs(coupon2adddatetime[0])
      .startOf('day')
      .format('YYYY-MM-DD HH:mm:ss');
    queryParams['coupon2adddatetime.end'] = dayjs(coupon2adddatetime[1])
      .endOf('day')
      .format('YYYY-MM-DD HH:mm:ss');
  }
  queryParams['coupon2adddatetime.sort'] = 1;
  return requestClient.post<any>('/api/query/list?pagevalue=120', queryParams);
}

/**
 * 我的优惠券_审核_详情
 */
export async function getCoupon2AuditDetailApi(data: { coupon2sid: string }) {
  return requestClient.post<any>(
    '/api/query/view?pagevalue=118',
    {
      'coupon2sid.value': data.coupon2sid,
    },
    { formatData: true },
  );
}

/**
 * 更新购机者信息
 */
export async function updateCoupon2UserInfoApi(data: any) {
  return requestClient.post<any>('/api/nlua/call?pagevalue=119', {
    ...data,
  });
}

/**
 * 我的优惠券信息_审核
 */
export async function auditCoupon2Api(data: Coupon2Entity) {
  return requestClient.post<any>('/api/up?pagevalue=104', { ...data });
}
