<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: "RechargeLevel" });

import RechargeLevelAPI, {
  RechargeLevelForm,
  RechargeLevelPageQuery,
} from "@/api/operationsManage/recharge-level-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: "business:recharge-level",
  formItems: [
    {
      type: "input",
      label: "充电券名称",
      prop: "name",
      attrs: {
        placeholder: "充电券名称",
        clearable: true,
        style: { width: "200px" },
      },
    },
    {
      type: "input",
      label: "充值金额",
      prop: "money",
      attrs: {
        placeholder: "充值金额",
        clearable: true,
        style: { width: "200px" },
      },
    },
    {
      type: "select",
      label: "状态 ", //(0-不可用 1-可用)
      prop: "status",
      attrs: {
        placeholder: "状态", // (0-不可用 1-可用)
        clearable: true,
        style: { width: "200px" },
      },
      options: [
        {
          label: "不可用",
          value: 0,
        },
        {
          label: "可用",
          value: 1,
        },
      ],
    },
  ],
});

// 列表配置
const contentConfig: IContentConfig<RechargeLevelPageQuery> = reactive({
  // 权限前缀
  permPrefix: "business:recharge-level",
  table: {
    border: true,
    highlightCurrentRow: true,
  },
  // 主键
  pk: "id",
  // 列表查询接口
  indexAction: RechargeLevelAPI.getPage,
  // 删除接口
  deleteAction: RechargeLevelAPI.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: ["add", "delete"],
  defaultToolbar: ["refresh", "filter"],
  // 表格列配置
  cols: [
    { type: "selection", width: 55, align: "center" },
    { label: "充电券名称", prop: "name" },
    { label: "充值金额", prop: "money" },
    { label: "创建时间", prop: "createTime" },
    { label: "状态 (0-不可用 1-可用)", prop: "status" },
    { label: "充值提示", prop: "tips" },
    {
      label: "操作",
      prop: "operation",
      width: 220,
      templet: "tool",
      operat: ["edit", "delete"],
    },
  ],
});

// 新增配置
const addModalConfig: IModalConfig<RechargeLevelForm> = reactive({
  // 权限前缀
  permPrefix: "business:recharge-level",
  // 主键
  pk: "id",
  // 弹窗配置
  dialog: {
    title: "新增",
    width: 800,
    draggable: true,
  },
  form: {
    labelWidth: 100,
  },
  // 表单项配置
  formItems: [
    {
      type: "input",
      attrs: {
        placeholder: "充电券名称",
      },
      label: "充电券名称",
      prop: "name",
      rules: [
        {
          required: true,
          message: "请输入充电券名称",
          trigger: "blur",
        },
      ],
    },
    {
      type: "input",
      attrs: {
        placeholder: "充值金额",
      },
      label: "充值金额",
      prop: "money",
      rules: [
        {
          required: true,
          message: "请输入充值金额",
          trigger: "blur",
        },
      ],
    },
    {
      type: "input",
      attrs: {
        placeholder: "充值提示",
      },
      label: "充值提示",
      prop: "tips",
      rules: [
        {
          required: true,
          message: "请输入充值提示",
          trigger: "blur",
        },
      ],
    },
    {
      type: "switch",
      attrs: {
        activeText: "启用",
        inactiveText: "禁用",
        activeValue: 1,
        inactiveValue: 0,
      },
      initialValue: 1,
      label: "状态", // (0-不可用 1-可用)
      prop: "status",
    },
  ],
  // 提交函数
  formAction: (data: RechargeLevelForm) => {
    if (data.id) {
      // 编辑
      return RechargeLevelAPI.update(data.id as string, data);
    } else {
      // 新增
      return RechargeLevelAPI.create(data);
    }
  },
});

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

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

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