<template>
  <div class="app-container h-full flex flex-1 flex-col">
    <!-- 返回按钮 -->
    <div class="mb-4">
      <el-button icon="ArrowLeft" @click="router.back()">返回</el-button>
    </div>

    <!-- 搜索 -->
    <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"
    >
      <template #status="scope">
        <el-tag :type="scope.row.status == 0 ? 'warning' : 'success'">
          {{ scope.row.status == 0 ? "已下线" : "上线中" }}
        </el-tag>
      </template>
    </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: "FirmInfo" });

import FirmInfoAPI, { FirmInfoForm, FirmInfoPageQuery } from "@/api/toBManage/firm-info-api";
import type { IObject, IModalConfig, IContentConfig, ISearchConfig } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage";

const router = useRouter();

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

// 搜索配置
const searchConfig: ISearchConfig = reactive({
  permPrefix: "business:firm-info",
  formItems: [
    {
      type: "input",
      label: "充电站ID",
      prop: "name",
      attrs: {
        placeholder: "请输入",
        clearable: true,
        style: { width: "200px" },
      },
    },
    {
      type: "select",
      label: "充电站名称", // 0 已下线  1 上线中
      prop: "status",
      attrs: {
        placeholder: "请输入状态",
        clearable: true,
        style: { width: "200px" },
      },
      options: [
        { label: "已下线", value: 0 },
        { label: "上线中", value: 1 },
      ],
    },
  ],
});

// 列表配置
const contentConfig: IContentConfig<FirmInfoPageQuery> = reactive({
  // 权限前缀
  permPrefix: "business:firm-info",
  table: {
    border: true,
    highlightCurrentRow: true,
  },
  // 主键
  pk: "id",
  // 列表查询接口
  indexAction: FirmInfoAPI.getPage,
  // 删除接口
  deleteAction: FirmInfoAPI.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],
  },
  // 工具栏配置
  defaultToolbar: ["refresh", "filter"],
  // 表格列配置
  cols: [
    { type: "selection", width: 55, align: "center" },
    { label: "企业名称", prop: "name" },
    { label: "员工数", prop: "" },
    {
      label: "状态", // 0 已下线  1 上线中
      prop: "status",
      templet: "custom",
    },
    { label: "创建时间", prop: "createTime" },
    {
      label: "操作",
      prop: "operation",
      templet: "tool",
      operat: [
        {
          name: "setting",
          text: "设置企业专享价",
          attrs: {
            type: "success",
            icon: "Setting",
          },
        },
      ],
    },
  ],
});

// 新增配置
const addModalConfig: IModalConfig<FirmInfoForm> = reactive({
  // 权限前缀
  permPrefix: "business:firm-info",
  // 主键
  pk: "id",
  // 弹窗配置
  dialog: {
    title: "新增",
    width: 800,
    draggable: true,
  },
  form: {
    labelWidth: 100,
  },
  // 表单项配置
  formItems: [
    {
      type: "input",
      attrs: {
        placeholder: "企业名称",
      },
      label: "企业名称",
      prop: "name",
    },
    {
      type: "switch",
      attrs: {
        activeValue: 1,
        inactiveValue: 0,
      },
      initialValue: 1,
      label: "状态", // 0 已下线  1 上线中
      prop: "status",
    },
  ],
  // 提交函数
  formAction: (data: FirmInfoForm) => {
    if (data.id) {
      // 编辑
      return FirmInfoAPI.update(data.id as string, data);
    } else {
      // 新增
      return FirmInfoAPI.create(data);
    }
  },
});

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

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

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