<template>
  <div class="app-container h-full flex flex-1 flex-col">
    <!-- 搜索 -->
    <page-search
      ref="searchRef"
      :search-config="searchConfig"
      @query-click="handleQueryClick"
      @reset-click="handleResetClick"
    ></page-search>

    <!-- 列表 -->
    <page-content
      ref="contentRef"
      :content-config="contentConfig"
      @add-click="handleAddClick"
      @export-click="handleExportClick"
      @search-click="handleSearchClick"
      @toolbar-click="handleToolbarClick"
      @operate-click="handleOperateClick"
      @filter-change="handleFilterChange"
    ></page-content>

    <!-- 新增 -->
    <page-modal
      ref="addModalRef"
      :modal-config="addModalConfig"
      @submit-click="handleSubmitClick"
    ></page-modal>

    <!-- 编辑 -->
    <page-modal
      ref="editModalRef"
      :modal-config="editModalConfig"
      @submit-click="handleSubmitClick"
    ></page-modal>
  </div>
</template>

<script setup lang="ts">
defineOptions({ name: "ThirdPartyEquipmentInfo" });

import ThirdPartyEquipmentInfoAPI, {
  ThirdPartyEquipmentInfoForm,
  ThirdPartyEquipmentInfoPageQuery,
} from "@/api/equipmentManage/third-party-equipment-info-api";
import type { IObject, IModalConfig, IContentConfig, ISearchConfig } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage";

// 组合式 CRUD
const {
  searchRef,
  contentRef,
  addModalRef,
  editModalRef,
  handleQueryClick,
  handleResetClick,
  handleAddClick,
  handleEditClick,
  handleSubmitClick,
  handleExportClick,
  handleSearchClick,
  handleFilterChange,
} = usePage();

// 搜索配置
const searchConfig: ISearchConfig = reactive({
  permPrefix: "system:third-party-equipment-info",
  formItems: [
    {
      type: "input",
      label: "设备编码",
      prop: "equipmentId",
      attrs: {
        placeholder: "设备编码",
        clearable: true,
        style: { width: "200px" },
      },
    },
    {
      type: "input",
      label: "设备名称",
      prop: "equipmentName",
      attrs: {
        placeholder: "设备名称",
        clearable: true,
        style: { width: "200px" },
      },
    },
  ],
});

// 列表配置
const contentConfig: IContentConfig<ThirdPartyEquipmentInfoPageQuery> = reactive({
  // 权限前缀
  permPrefix: "system:third-party-equipment-info",
  table: {
    border: true,
    highlightCurrentRow: true,
  },
  // 主键
  pk: "id",
  // 列表查询接口
  indexAction: ThirdPartyEquipmentInfoAPI.getPage,
  // 删除接口
  deleteAction: ThirdPartyEquipmentInfoAPI.deleteByIds,
  // 数据解析函数
  parseData(res: any) {
    return {
      total: res.total,
      list: res.list,
    };
  },
  // 分页配置
  pagination: {
    background: true,
    layout: "total, sizes, prev, pager, next, jumper",
    pageSize: 20,
    pageSizes: [10, 20, 30, 50],
  },
  // 工具栏配置
  toolbar: ["export"],
  defaultToolbar: ["refresh", "filter"],
  // 表格列配置
  cols: [
    { type: "selection", width: 55, align: "center" },
    { label: "接口ID", prop: "connectorId" },
    { label: "接口名称", prop: "connectorName" },
    { label: "所属充电站", prop: "stationName" },
    { label: "总功率(kW)", prop: "power" },
    { label: "设备编码", prop: "equipmentId" },
    { label: "设备名称", prop: "equipmentName" },
    {
      label: "车位号",
      prop: "parkNo",
      templet: "format",
      formatter: (row: any) => {
        return row.parkNo || "--";
      },
    },
    {
      label: "国家标准",
      prop: "nationalStandard",
      templet: "format",
      formatter: (row: any) => {
        return row.nationalStandard == 1 ? "2011" : "2015";
      },
    },
    {
      label: "操作",
      prop: "operation",
      width: 220,
      templet: "tool",
      operat: ["edit"],
    },
  ],
});

// 新增配置
const addModalConfig: IModalConfig<ThirdPartyEquipmentInfoForm> = reactive({
  // 权限前缀
  permPrefix: "system:third-party-equipment-info",
  // 主键
  pk: "id",
  // 弹窗配置
  dialog: {
    title: "新增",
    width: 800,
    draggable: true,
  },
  form: {
    labelWidth: 100,
  },
  // 表单项配置
  formItems: [
    {
      type: "input",
      attrs: {
        placeholder: "设备编码",
        disabled: true,
      },
      label: "设备编码",
      prop: "equipmentId",
    },
    {
      type: "input",
      attrs: {
        placeholder: "设备名称",
        disabled: true,
      },
      label: "设备名称",
      prop: "equipmentName",
    },
  ],
  // 提交函数
  formAction: (data: ThirdPartyEquipmentInfoForm) => {
    if (data.id) {
      // 编辑
      return ThirdPartyEquipmentInfoAPI.update(data.id as string, data);
    } else {
      // 新增
      return ThirdPartyEquipmentInfoAPI.create(data);
    }
  },
});

// 编辑配置
const editModalConfig: IModalConfig<ThirdPartyEquipmentInfoForm> = reactive({
  permPrefix: "system:third-party-equipment-info",
  component: "drawer",
  drawer: {
    title: "编辑",
    size: 500,
  },
  pk: "id",
  formAction(data: any) {
    return ThirdPartyEquipmentInfoAPI.update(data.id as string, data);
  },
  formItems: addModalConfig.formItems, // 复用新增的表单项
});

// 处理操作按钮点击
const handleOperateClick = (data: IObject) => {
  if (data.name === "edit") {
    handleEditClick(data.row, async () => {
      return await ThirdPartyEquipmentInfoAPI.getFormData(data.row.id);
    });
  }
};

// 处理工具栏按钮点击（删除等）
const handleToolbarClick = (name: string) => {
  console.log(name);
};
</script>
