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

import { parseQueryValues } from '@vben/utils';

import dayjs from 'dayjs';

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

interface OrdersPartialEntity
  extends Partial<Omit<OrdersEntity, 'ordersnumber'>> {
  ordersnumber?: string;
}

export interface OrdersQueryParams extends OrdersPartialEntity, PageConfig {}

/**
 * 订单信息_列表
 */
export async function getOrdersListApi(params: any) {
  const {
    ordersnumber,
    ordersproductname,
    ordersuserid1,
    ordersorderdate,
    pageindex,
    rows,
  } = params;

  const queryParams = {
    pageindex,
    rows,
  } as any;

  if (ordersnumber) {
    queryParams['ordersnumber.value'] = ordersnumber;
  }

  if (ordersproductname) {
    queryParams['ordersproductname.like'] = ordersproductname;
  }

  if (ordersuserid1) {
    queryParams['ordersuserid1.value'] = ordersuserid1;
  }

  if (ordersorderdate && ordersorderdate.length > 0) {
    queryParams['ordersorderdate.start'] = dayjs(ordersorderdate[0])
      .startOf('day')
      .format('YYYY-MM-DD HH:mm:ss');
    queryParams['ordersorderdate.end'] = dayjs(ordersorderdate[1])
      .endOf('day')
      .format('YYYY-MM-DD HH:mm:ss');
  }

  queryParams['ordersorderdate.sort'] = 1;

  return requestClient.post<any>('/api/query/list?pagevalue=101', queryParams);
}

/**
 * 订单信息_详情
 */
export async function getOrdersDetailApi(data: { ordersid: string }) {
  return requestClient.post<any>(
    '/api/query/view?pagevalue=102',
    {
      ...parseQueryValues(data),
    },
    { formatData: true }, // 格式化返回数据
  );
}

/**
 * 订单信息_新增
 */
export async function addOrdersApi(data: OrdersEntity) {
  return requestClient.post<any>('/api/add?pagevalue=53', { ...data });
}

/**
 * 客户信息_编辑
 */
export async function editOrdersApi(data: OrdersEntity) {
  return requestClient.post<any>('/api/up?pagevalue=54', { ...data });
}

/**
 * 订单信息_删除
 */
export async function deleteOrdersApi(data: { 'ordersid.value': string }) {
  return requestClient.post<any>('/api/del?pagevalue=55', { ...data });
}

/**
 * 订单信息_审核
 */
export async function auditOrdersApi(data: {
  'ordersid.value': string;
  ordersshbz?: string;
  ordsesshzt: number;
}) {
  return requestClient.post<any>('/api/up?pagevalue=114', { ...data });
}
