import request from "@/utils/request";

const API_PATH = "/business/gateway";

const GatewayAPI = {
  // 列表查询
  listGateway(query: GatewayPageQuery) {
    return request<ApiResponse<PageResult<GatewayTable[]>>>({
      url: `${API_PATH}/list`,
      method: "get",
      params: query,
    });
  },

  // 详情查询
  detailGateway(id: number) {
    return request<ApiResponse<GatewayTable>>({
      url: `${API_PATH}/detail/${id}`,
      method: "get",
    });
  },

  // 新增
  createGateway(body: GatewayForm) {
    return request<ApiResponse>({
      url: `${API_PATH}/create`,
      method: "post",
      data: body,
    });
  },

  // 修改（带主键）
  updateGateway(id: number, body: GatewayForm) {
    return request<ApiResponse>({
      url: `${API_PATH}/update/${id}`,
      method: "put",
      data: body,
    });
  },

  // 删除（支持批量）
  deleteGateway(ids: number[]) {
    return request<ApiResponse>({
      url: `${API_PATH}/delete`,
      method: "delete",
      data: ids,
    });
  },

  // 批量启用/停用
  batchGateway(body: BatchType) {
    return request<ApiResponse>({
      url: `${API_PATH}/available/setting`,
      method: "patch",
      data: body,
    });
  },

  // 导出
  exportGateway(query: GatewayPageQuery) {
    return request<Blob>({
      url: `${API_PATH}/export`,
      method: "post",
      data: query,
      responseType: "blob",
    });
  },

  // 下载导入模板
  downloadTemplateGateway() {
    return request<Blob>({
      url: `${API_PATH}/download/template`,
      method: "post",
      responseType: "blob",
    });
  },

  // 导入
  importGateway(body: FormData) {
    return request<ApiResponse>({
      url: `${API_PATH}/import`,
      method: "post",
      data: body,
      headers: { "Content-Type": "multipart/form-data" },
    });
  },
};

export default GatewayAPI;

// ------------------------------
// TS 类型声明
// ------------------------------

// 列表查询参数
export interface GatewayPageQuery extends PageQuery {
  crane_no?: string;
  gateway_type?: string;
  status?: string;
  created_time?: string[];
  updated_time?: string[];
}

// 列表展示项
export interface GatewayTable extends BaseType{
  crane_no?: string;
  crane_name?: string;
  gateway_name?: string;
  gateway_type?: string;
  gateway_ipaddress?: string;
  gateway_port?: string;
  plc_brand?: string;
  plc_model?: string;
  serial_port_name?: string;
  serial_baud_rate?: string;
  serial_data_bits?: string;
  serial_stop_bits?: string;
  serial_parity?: string;
  created_by?: creatorType;
  updated_by?: updatorType;
}

// 新增/修改/详情表单参数
export interface GatewayForm extends BaseFormType{
  crane_no?: string;
  gateway_name?: string;
  gateway_type?: string;
  gateway_ipaddress?: string;
  gateway_port?: string;
  plc_brand?: string;
  plc_model?: string;
  serial_port_name?: string;
  serial_baud_rate?: string;
  serial_data_bits?: string;
  serial_stop_bits?: string;
  serial_parity?: string;
}
