<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"
    >
      <template #type="scope">
        <Dict v-model="scope.formData[scope.prop]" code="discount_type" v-bind="scope.attrs" />
      </template>
    </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 #type="scope">
        <DictLabel v-model="scope.row[scope.prop]" code="discount_type" />
      </template>
      <template #status="scope">
        <el-tag :type="scope.row.status === 0 ? 'danger' : 'success'">
          {{ scope.row.status === 0 ? "未启用" : "启用" }}
        </el-tag>
      </template>
    </page-content>

    <!-- 新增 -->
    <page-modal
      ref="addModalRef"
      :modal-config="addModalConfig"
      @submit-click="handleSubmitClick"
    >
      <template #type="scope">
        <Dict v-model="scope.formData[scope.prop]" code="discount_type" v-bind="scope.attrs" />
      </template>
    </page-modal>

    <!-- 编辑 -->
    <page-modal
      ref="editModalRef"
      :modal-config="editModalConfig"
      @submit-click="handleSubmitClick"
    >
      <template #type="scope">
        <Dict v-model="scope.formData[scope.prop]" code="discount_type" v-bind="scope.attrs" />
      </template>
    </page-modal>
  </div>
</template>

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

import DiscountsActivityAPI, {
  DiscountsActivityForm,
  DiscountsActivityPageQuery,
} from "@/api/operationsManage/discounts-activity-api";
import type { IObject, IModalConfig, IContentConfig, ISearchConfig } from "@/components/CURD/types";
import usePage from "@/components/CURD/usePage";
import { useDictStoreHook } from "@/store/modules/dict-store";
import Dict from "@/components/Dict/index.vue";
import DictLabel from "@/components/Dict/DictLabel.vue";

// 组合式 CRUD
const {
  searchRef,
  contentRef,
  addModalRef,
  editModalRef,
  handleQueryClick,
  handleResetClick,
  handleAddClick,
  handleEditClick,
  handleSubmitClick,
  handleExportClick,
  handleSearchClick,
  handleFilterChange,
} = usePage();
const dictStore = useDictStoreHook();
// 搜索配置
const searchConfig: ISearchConfig = reactive({
  permPrefix: "operationsManage:discounts-activity",
  formItems: [
    {
      type: "input",
      label: "活动名称",
      prop: "name",
      attrs: {
        placeholder: "活动名称",
        clearable: true,
        style: { width: "200px" },
      },
    },
    {
      type: "custom",
      slotName: "type",
      label: "优惠类型", // 1-新用户首单立减 2-活动折扣
      prop: "type",
      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<DiscountsActivityPageQuery> = reactive({
  // 权限前缀
  permPrefix: "operationsManage:discounts-activity",
  table: {
    border: true,
    highlightCurrentRow: true,
  },
  // 主键
  pk: "id",
  // 列表查询接口
  indexAction: DiscountsActivityAPI.getPage,
  // 删除接口
  deleteAction: DiscountsActivityAPI.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: "优惠类型", // 1-新用户首单立减 2-活动折扣
      prop: "type",
      templet: "custom",
      slotName: "type"
    },
    { label: "优惠金额/折扣", prop: "discount" },
    { label: "活动描述", prop: "activityDesc" },
    {
      label: "状态", // 0-未启用 1-启用
      prop: "status",
      templet: "custom",
    },
    { label: "创建时间", prop: "createTime" },
    {
      label: "操作",
      prop: "operation",
      width: 220,
      templet: "tool",
      operat: ["edit", "delete"],
    },
  ],
});

// 新增配置
const addModalConfig: IModalConfig<DiscountsActivityForm> = reactive({
  // 权限前缀
  permPrefix: "business:discounts-activity",
  // 主键
  pk: "id",
  // 弹窗配置
  dialog: {
    title: "新增",
    width: 800,
    draggable: true,
  },
  form: {
    labelWidth: 140,
  },
  // 表单项配置
  formItems: [
    {
      type: "input",
      attrs: {
        placeholder: "活动名称",
        style: { width: "300px" },
      },
      label: "活动名称",
      prop: "name",
      rules: [{ required: true, message: "活动名称不能为空", trigger: "blur" }],
    },
    {
      type: "custom",
      slotName: "type",
      label: "优惠类型",
      prop: "type",
      rules: [{ required: true, message: "请选择优惠类型", trigger: "blur" }],
    },
    {
      type: "input",
      attrs: {
        placeholder: "优惠金额/折扣",
        style: { width: "300px" },
      },
      label: "优惠金额/折扣",
      prop: "discount",
      rules: [{ required: true, message: "优惠金额不能为空", trigger: "blur" }],
    },
    {
      type: "input",
      attrs: {
        placeholder: "活动描述",
        style: { width: "300px" },
      },
      label: "活动描述",
      prop: "activityDesc",
    },
    {
      type: "switch",
      attrs: {
        activeText: "启用",
        inactiveText: "禁用",
        activeValue: 1,
        inactiveValue: 0,
      },
      initialValue: 1,
      label: "状态", // 0-未启用 1-启用
      prop: "status",
    },
  ],
  // 提交函数
  formAction: (data: DiscountsActivityForm) => {
    if (data.id) {
      // 编辑
      return DiscountsActivityAPI.update(data.id as string, data);
    } else {
      // 新增
      return DiscountsActivityAPI.create(data);
    }
  },
});

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

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

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