<script lang="ts" setup>
import BasePageableTable from "@/components/base/base-pageable-table/BasePageableTable.vue";
import DefaultToolButton from "@/components/base/default-tool-button/DefaultToolButton.vue";
import DefaultOperateButtonColumn from "@/components/base/default-column/DefaultOperateButtonColumn.vue";
import { usePage } from "@/composables/use-page";
import BaseForm from "@/components/base/base-form/BaseForm.vue";
import { $t } from "@/common/languages";
import type { FormInstance, FormRules } from "element-plus";
import { formatDate, formatIsSuccess, formatRecordType } from "@/common/utils/format-utils";

/**
 * 必须要的变量
 */
const isSuccessSelectData = {
  true: "成功",
  false: "失败",
};
const recordTypeSelectData = {
  "1": "添加",
  "2": "修改",
  "3": "删除",
  "4": "启用",
  "5": "禁用",
  "6": "回收",
  "7": "还原",
  "8": "登录",
  "9": "退出",
  "10": "其他",
};

const getPageUrl = "/sys/sysrecord/getSysRecordPage";
const addUrl = "/sys/sysrecord/addSysRecord";
const editUrl = "/sys/sysrecord/updateSysRecord";
const removeUrl = "/sys/sysrecord/deleteSysRecord";
const searchers = [
  { name: "createUserName", type: "text" as const, placeholder: $t("_prop.systemset.sysrecord.createUserName") },
  {
    name: "isSuccess",
    type: "select" as const,
    placeholder: $t("_prop.systemset.sysrecord.isSuccess"),
    selectData: isSuccessSelectData,
  },
  {
    name: "recordType",
    type: "select" as const,
    placeholder: $t("_prop.systemset.sysrecord.recordType"),
    selectData: recordTypeSelectData,
  },
  { name: "createDate", type: "date-range-pick" as const, placeholder: $t("_prop.systemset.sysrecord.createDate") },
];
const rules = reactive<FormRules>({});
/**
 * 基本不变通用变量
 */
const tableRef = ref<InstanceType<typeof BasePageableTable> | null>(null);
const { useAdd, useEdit, useRemove, useGeneralPageRef } = usePage(tableRef);
const { title, visible, formType, form } = useGeneralPageRef();
/**
 * 可以自定义的变量
 */

const add = () => {
  form.value = {};
  title.value = "_title.systemset.sysrecord.add";
  visible.value = true;
  formType.value = false;
};
const edit = (row: any) => {
  title.value = "_title.systemset.sysrecord.edit";
  form.value = { ...row };
  visible.value = true;
  formType.value = true;
};
const remove = (row: any) => {
  useRemove(removeUrl, row.id, "_message.systemset.sysrecord.delete_message");
};
const submit = (form: any, formRef: FormInstance | undefined) => {
  if (formRef !== undefined) {
    formRef.validate(valid => {
      if (valid) {
        if (formType.value) useEdit(editUrl, form, visible);
        else useAdd(addUrl, form, visible);
      }
    });
  }
};
const topButtonClick = (eventName: string) => {
  switch (eventName) {
    case "add":
      add();
      break;
  }
};
const operateButtonClick = (eventName: string, row: any) => {
  switch (eventName) {
    case "edit":
      edit(row);
      break;
    case "remove":
      remove(row);
      break;
  }
};
</script>
<template>
  <BasePageableTable :url="getPageUrl" :searchers="searchers" ref="tableRef">
    <template #tool-button>
      <DefaultToolButton @top-button-click="topButtonClick" />
    </template>
    <template #columns>
      <el-table-column prop="id" type="hidden" width="40" />
      <el-table-column :label="$t('_prop.systemset.sysrecord.channelName')" prop="channelName" />
      <el-table-column
        :label="$t('_prop.systemset.sysrecord.recordType')"
        prop="recordType"
        :formatter="formatRecordType"
      />
      <el-table-column
        :label="$t('_prop.systemset.sysrecord.isSuccess')"
        prop="isSuccess"
        :formatter="formatIsSuccess"
      />
      <el-table-column :label="$t('_prop.common.remark')" prop="remark" />
      <el-table-column :label="$t('_prop.systemset.sysrecord.linkUrl')" prop="linkUrl" />
      <el-table-column :label="$t('_prop.systemset.sysrecord.ip')" prop="ip" />
      <el-table-column :label="$t('_prop.systemset.sysrecord.createUserName')" prop="createUserName" />
      <el-table-column :label="$t('_prop.common.createDate')" prop="createDate" :formatter="formatDate" />
      <DefaultOperateButtonColumn @operate-button-click="operateButtonClick" />
    </template>
  </BasePageableTable>
  <BaseForm v-model:visible="visible" @submit="submit" v-model:form="form" :title="$t(title)" :rules="rules">
    <template #form-items>
      <el-form-item prop="id" v-if="false"><el-input v-model="form.id" /></el-form-item>
    </template>
  </BaseForm>
</template>
