import request from "@/utils/request";

const API_PATH = "/business/vardict";

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

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

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

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

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

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

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

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

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

export default BizVarDictAPI;

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

// 列表查询参数
export interface BizVarDictPageQuery extends PageQuery {
  var_code?: string;
  var_name?: string;
  mec_type?: string;
  data_type?: string;
  switch_type?: string;
  is_alert?: string;
  modbus_address?: string;
  modbus_data_type?: string;
  crane_id?: string;
  status?: string;
  created_id?: number;
  updated_id?: number;
  created_time?: string[];
  updated_time?: string[];
}

// 列表展示项
export interface BizVarDictTable extends BaseType{
  var_code?: string;
  var_name?: string;
  mec_type?: string;
  data_type?: string;
  switch_type?: string;
  is_alert?: string;
  modbus_address?: string;
  modbus_data_type?: string;
  is_reverse?: string;
  unit?: string;
  crane_id?: string;
  order?: string;
  created_id?: string;
  updated_id?: string;
  created_by?: creatorType;
  updated_by?: updatorType;
}

// 新增/修改/详情表单参数
export interface BizVarDictForm extends BaseFormType{
  var_code?: string;
  var_name?: string;
  mec_type?: string;
  data_type?: string;
  switch_type?: string;
  is_alert?: string;
  modbus_address?: string;
  modbus_data_type?: string;
  is_reverse?: string;
  unit?: string;
  crane_id?: string;
  order?: string;
}
