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

import UserInfoAPI, { UserInfoForm, UserInfoPageQuery } from "@/api/userManage/user-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: "business:user-info",
  formItems: [
    {
      type: "input",
      label: "所属集团编号",
      prop: "ecId",
      attrs: {
        placeholder: "所属集团编号",
        clearable: true,
        style: { width: "200px" },
      },
    },
    {
      type: "input",
      label: "手机号",
      prop: "phone",
      attrs: {
        placeholder: "手机号",
        clearable: true,
        style: { width: "200px" },
      },
    },
    {
      type: "input",
      label: "所属第三方", //（无则为自营）
      prop: "groupId",
      attrs: {
        placeholder: "所属第三方",
        clearable: true,
        style: { width: "200px" },
      },
    },
  ],
});

// 列表配置
const contentConfig: IContentConfig<UserInfoPageQuery> = reactive({
  // 权限前缀
  permPrefix: "business:user-info",
  table: {
    border: true,
    highlightCurrentRow: true,
  },
  // 主键
  pk: "id",
  // 列表查询接口
  indexAction: UserInfoAPI.getPage,
  // 删除接口
  deleteAction: UserInfoAPI.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: "ecId" },
    { label: "手机号", prop: "phone" },
    { label: "积分", prop: "integralNum" },
    { label: "所属第三方", prop: "groupId" }, //（无则为自营）
    { label: "创建时间", prop: "createTime" },
    {
      label: "操作",
      prop: "operation",
      width: 220,
      templet: "tool",
      operat: [
        {
          name: "detail",
          text: "详情",
          attrs: {
            type: "primary",
            icon: "el-icon-view",
          },
        },
      ],
    },
  ],
});

// 新增配置
const addModalConfig: IModalConfig<UserInfoForm> = reactive({
  // 权限前缀
  permPrefix: "business:user-info",
  // 主键
  pk: "id",
  // 弹窗配置
  dialog: {
    title: "新增",
    width: 800,
    draggable: true,
  },
  form: {
    labelWidth: 100,
  },
  // 表单项配置
  formItems: [
    {
      type: "input",
      attrs: {
        placeholder: "所属集团编号",
      },
      label: "所属集团编号",
      prop: "ecId",
    },
    {
      type: "input",
      attrs: {
        placeholder: "手机号",
      },
      label: "手机号",
      prop: "phone",
    },
    {
      type: "input",
      attrs: {
        placeholder: "积分",
      },
      label: "积分",
      prop: "integralNum",
    },
    {
      type: "input",
      attrs: {
        placeholder: "所属第三方（无则为自营）",
      },
      label: "所属第三方（无则为自营）",
      prop: "groupId",
    },
    {
      type: "input",
      attrs: {
        placeholder: "创建时间",
      },
      label: "创建时间",
      prop: "createTime",
    },
  ],
  // 提交函数
  formAction: (data: UserInfoForm) => {
    if (data.id) {
      // 编辑
      return UserInfoAPI.update(data.id as string, data);
    } else {
      // 新增
      return UserInfoAPI.create(data);
    }
  },
});

// 编辑配置
const editModalConfig: IModalConfig<UserInfoForm> = reactive({
  permPrefix: "business:user-info",
  component: "drawer",
  drawer: {
    title: "编辑",
    size: 500,
  },
  pk: "id",
  formAction(data: any) {
    return UserInfoAPI.update(data.id as string, data);
  },
  formItems: [
    {
      type: "input",
      attrs: {
        placeholder: "所属集团编号",
        disabled: true,
      },
      label: "所属集团编号",
      prop: "ecId",
    },
    {
      type: "input",
      attrs: {
        placeholder: "手机号",
        disabled: true,
      },
      label: "手机号",
      prop: "phone",
    },
    {
      type: "input",
      attrs: {
        placeholder: "积分",
        disabled: true,
      },
      label: "积分",
      prop: "integralNum",
    },
    {
      type: "input",
      attrs: {
        placeholder: "所属第三方", //（无则为自营）
        disabled: true,
      },
      label: "所属第三方", //（无则为自营）
      prop: "groupId",
    },
    {
      type: "input",
      attrs: {
        placeholder: "创建时间",
        disabled: true,
      },
      label: "创建时间",
      prop: "createTime",
    },
  ], // 复用新增的表单项
});

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

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