import request from "@/utils/request";

// 查询学习卡列表
export function listCards(query) {
  return request({
    url: "/dz/cards/list",
    method: "get",
    params: query,
  });
}

// 查询学习卡学生列表（简化版）
export function listCards2(query) {
  return request({
    url: "/dz/cards/list2",
    method: "get",
    params: query,
  });
}

// 查询学习卡详细
export function getCards(cardId) {
  return request({
    url: "/dz/cards/" + cardId,
    method: "get",
  });
}

// 新增学习卡
export function addCards(data) {
  return request({
    url: "/dz/cards",
    method: "post",
    data: data,
  });
}

// 修改学习卡
export function updateCards(data) {
  return request({
    url: "/dz/cards",
    method: "put",
    data: data,
  });
}

// 删除学习卡
export function delCards(cardIds) {
  // 如果是数组，转换为逗号分隔的字符串
  const ids = Array.isArray(cardIds) ? cardIds.join(",") : cardIds;
  return request({
    url: "/dz/cards/" + ids,
    method: "delete",
  });
}

// 制卡
export function issueCard(institutionId, type, count) {
  return request({
    url: "/dz/cards/issueCard",
    method: "post",
    params: {
      institutionId,
      type,
      count,
    },
  });
}

// 分配卡
export function assignCard(params) {
  // 支持两种方式：
  // 1. 通过cardIds（选中的卡片ID列表）
  // 2. 通过begin和end（卡号段）
  return request({
    url: "/dz/cards/assignCard",
    method: "post",
    params: params,
  });
}

// 获取考生类型列表
export function getExamTypes(location) {
  return request({
    url: "/front/user/examTypes",
    method: "get",
    params: {
      location,
    },
  });
}

// 获取代理商列表
export function getAgentList(query) {
  return request({
    url: "/dz/agent/list",
    method: "get",
    params: query,
  });
}

// 缴费
export function payCard(cardIds) {
  return request({
    url: "/dz/cards/change/pay",
    method: "post",
    params: {
      action: "Pay",
      cardIds: Array.isArray(cardIds) ? cardIds.join(",") : cardIds,
    },
  });
}

// 关卡
export function closeCard(cardIds) {
  return request({
    url: "/dz/cards/change/close",
    method: "post",
    params: {
      action: "Close",
      cardIds: Array.isArray(cardIds) ? cardIds.join(",") : cardIds,
    },
  });
}

// 重开
export function reopenCard(cardIds) {
  return request({
    url: "/dz/cards/change/reopen",
    method: "post",
    params: {
      action: "ReOpen",
      cardIds: Array.isArray(cardIds) ? cardIds.join(",") : cardIds,
    },
  });
}

// 退费
export function refundCard(cardIds) {
  return request({
    url: "/dz/cards/change/refund",
    method: "post",
    params: {
      action: "Refund",
      cardIds: Array.isArray(cardIds) ? cardIds.join(",") : cardIds,
    },
  });
}

// 结算
export function settleCard(cardIds) {
  return request({
    url: "/dz/cards/change/settlement",
    method: "post",
    params: {
      action: "Settlement",
      cardIds: Array.isArray(cardIds) ? cardIds.join(",") : cardIds,
    },
  });
}

// 续费
export function renewCard(cardIds, days) {
  const params = {
    action: "Renew",
    cardIds: Array.isArray(cardIds) ? cardIds.join(",") : cardIds,
  };
  if (days != null) {
    params.days = days;
  }
  return request({
    url: "/dz/cards/change/renew",
    method: "post",
    params: params,
  });
}

// 获取学校列表
export function getSchoolList(query) {
  return request({
    url: "/dz/school/getSchoolList",
    method: "get",
    params: query,
  });
}

// 获取校区列表
export function getCampusSchoolList(query) {
  return request({
    url: "/dz/school/getCampusSchoolList",
    method: "get",
    params: query,
  });
}

// 关联校区
export function associateCampus(params) {
  // 支持两种方式：
  // 1. 通过cardIds（选中的卡片ID列表）
  // 2. 通过begin和end（卡号段）
  return request({
    url: "/dz/cards/changeCampus",
    method: "post",
    params: params,
  });
}

// 申请开卡
export function requestOpenCard(
  agentId,
  province,
  schoolId,
  beginCardNo,
  endCardNo
) {
  return request({
    url: "/dz/cards/openCard",
    method: "post",
    params: {
      agentId: agentId,
      locationId: province,
      schoolId,
      begin: beginCardNo,
      end: endCardNo,
    },
  });
}

// 统计学习卡数据
export function getCardStatistics(query) {
  return request({
    url: "/dz/cards/statistics",
    method: "get",
    params: query,
  });
}

// 统计详情 - 查询学习卡列表
export function getStatisticsDetail(query) {
  return request({
    url: "/dz/cards/statisticsDetail",
    method: "get",
    params: query,
  });
}

// 根据卡号获取用户信息
export function getUserByCardId(cardId) {
  return request({
    url: "/dz/cards/cardUser/" + cardId,
    method: "get",
    params: {},
  });
}

// 更新卡用户信息
export function updateCardUser(data) {
  return request({
    url: "/dz/cards/updateCardUser",
    method: "post",
    data: data,
  });
}

// 获取用户模拟考试列表
export function getUserSimulateList(query) {
  return request({
    url: "/dz/cards/getUserSimulateList",
    method: "get",
    params: query,
  });
}

// 更新用户模拟考试次数
export function updateUserEvalCounts(data) {
  return request({
    url: "/dz/cards/updateUserEvalCounts",
    method: "post",
    data: data,
  });
}

// 清空用户定向信息
export function clearUserDirectedStudy(userId) {
  return request({
    url: "/system/user/clearDirectedStudy/" + userId,
    method: "put",
  });
}
