import request from "./request";
import {
  ResultListPromise,
  CostItem,
  LoginRequestBody,
  UserLoginResult,
  ResultPromise,
  AccountMoneyInfo,
  UserInfo,
  CreateOrderRequestBody,
  CreateOrderInfo,
  SendCodeRequestBody,
  PersonalAuthRequestBody,
  CompanyAuthRequestBody,
  ApplyContractRequestBody,
  ApplyInvoiceRequestBody,
  InvoiceItem,
  ForgotPasswordRequestBody,
  GetOrderInfoRequestBody,
  GetOrderInfoResult,
} from "./interface";
import { Pagination, Sort } from "../types";
import { SORT } from "../constants";
import { options2Pagination } from "../utils";

/** 登陆 */
export function userLogin(
  data: LoginRequestBody
): ResultPromise<UserLoginResult> {
  return request.post("/api/user/login", data);
}

/** 当前登录用户信息 */
export function getUserInfo(): ResultPromise<UserInfo> {
  return request.get("/api/user/info");
}

/** 发送短信验证码 */
export function sendSmsCode(data: SendCodeRequestBody): ResultPromise<null> {
  // return new Promise((resolve) => {
  //   setTimeout(() => {
  //     resolve(null);
  //   }, 3000);
  // });
  return request.post("/api/user/send-code", data);
}

/** 文件上传 */
export function uploadFile(formData: FormData, onProgress?) {
  return request.post("/api/file/UploadFile", formData, onProgress);
}

/** 忘记密码 */
export function forgotPassword(
  data: ForgotPasswordRequestBody
): ResultPromise<null> {
  return request.post("/api/user/forgot-password", data);
}

/** 账单列表 */
export function getCostList(
  options?: Pagination & Sort
): ResultListPromise<CostItem> {
  return request.post("/api/user/bill/list", {
    ...options2Pagination(options),
    sort: options?.sort || SORT.DESC.value,
  });
}

/** 账户余额与消费合计信息 */
export function getAccountMoneyInfo(): ResultPromise<AccountMoneyInfo> {
  return request.post("/api/user/account-money");
}

/** 创建订单充值 */
export function createOrder(
  data: CreateOrderRequestBody
): ResultPromise<CreateOrderInfo> {
  return request.post("/api/user/pay-order", data);
}

/** 获取订单信息 */
export function getOrderInfo(id: string): ResultPromise<GetOrderInfoResult> {
  return request.post("/api/user/GetOrderInfo", { order_num: id });
}

/** 个人用户认证 */
export function personalAuthentication(
  data: PersonalAuthRequestBody
): ResultPromise<null> {
  return request.post("/api/user/UserAuthentication", data);
}

/** 企业用户认证 */
export function companyAuthentication(
  data: CompanyAuthRequestBody
): ResultPromise<null> {
  return request.post("/api/user/company-certification", data);
}

/** 合同申请 */
export function applyContract(
  data: ApplyContractRequestBody
): ResultPromise<null> {
  return request.post("/api/user/ApplyForContract", data);
}

/** 发票申请 */
export function applyInvoice(
  data: ApplyInvoiceRequestBody
): ResultPromise<null> {
  return request.post("/api/user/ApplyForInvoice", data);
}

/** 发票列表 */
export function getInvoiceList(
  options?: Pagination
): ResultListPromise<InvoiceItem> {
  return request.post("/api/user/InvoiceList", {
    ...options2Pagination(options),
  });
}
