<script lang="ts" setup>
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 } from "@/common/utils/format-utils";
import TreeSidePageableTable from "@/components/base/treeside-pageable-table/TreeSidePageableTable.vue";
import DefaultStatusSwitchColumn from "@/components/base/default-column/DefaultStatusSwitchColumn.vue";
import StatusSwitch from "@/components/base/base-switch/StatusSwitch.vue";

/**
 * 必须要的变量
 */

const getPageUrl = "/sys/syschannel/getSysChannelPage";
const treeSideUrl = "/sys/syschannel/getMenuTree";
const addUrl = "/sys/syschannel/addSysChannel";
const editUrl = "/sys/syschannel/updateSysChannel";
const removeUrl = "/sys/syschannel/deleteSysChannel";
const statusUrl = "/sys/syschannel/setStatus";
const searchers = [
  { name: "channelName", type: "text" as const, placeholder: $t("_prop.systemset.syschannel.channelName") },
];
const rules = reactive<FormRules>({
  channelName: [{ required: true, message: $t("_message.systemset.syschannel.input_channelName"), trigger: "blur" }],
  channelLink: [{ required: true, message: $t("_message.systemset.syschannel.input_channelLink"), trigger: "blur" }],
  eventName: [{ required: true, message: $t("_message.systemset.syschannel.input_eventName"), trigger: "blur" }],
  className: [{ required: true, message: $t("_message.systemset.syschannel.input_className"), trigger: "blur" }],
  iconName: [{ required: true, message: $t("_message.systemset.syschannel.input_iconName"), trigger: "blur" }],
  channelType: [{ required: true, message: $t("_message.systemset.syschannel.input_channelType"), trigger: "blur" }],
  sort: [{ required: true, message: $t("_message.systemset.syschannel.input_sort"), trigger: "blur" }],
  show: [{ required: true, message: $t("_message.systemset.syschannel.input_show"), trigger: "blur" }],
  status: [{ required: true, message: $t("_message.systemset.syschannel.input_status"), trigger: "blur" }],
  createDate: [{ required: true, message: $t("_message.systemset.syschannel.input_createDate"), trigger: "blur" }],
});
/**
 * 基本不变通用变量
 */
const tableRef = ref<InstanceType<typeof TreeSidePageableTable> | null>(null);
const { useAdd, useEdit, useRemove, useGeneralPageRef } = usePage(tableRef);
const { title, visible, formType, form } = useGeneralPageRef();
/**
 * 可以自定义的变量
 */

const add = () => {
  form.value = {};
  title.value = "_title.systemset.syschannel.add";
  visible.value = true;
  formType.value = false;
};
const edit = (row: any) => {
  title.value = "_title.systemset.syschannel.edit";
  form.value = { ...row };
  visible.value = true;
  formType.value = true;
};
const remove = (row: any) => {
  useRemove(removeUrl, row.id, "_message.systemset.syschannel.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;
  }
};

// 通用状态标签类型映射（启用/禁用）
const getCommonStatusTagType = (code: number | null): string => {
  const tagTypeMap: Record<number, string> = {
    0: "success", // 启用
    1: "info", // 禁用
  };
  return tagTypeMap[code ?? -1] || "info";
};
</script>
<template>
  <TreeSidePageableTable
    :url="getPageUrl"
    :searchers="searchers"
    ref="tableRef"
    :tree-side-url="treeSideUrl"
    tree-side-param-name="parentId"
    tree-side-node-name="id"
  >
    <template #tool-button>
      <DefaultToolButton @top-button-click="topButtonClick" />
    </template>
    <template #columns>
      <el-table-column :label="$t('_prop.systemset.syschannel.channelName')" prop="channelName" />
      <el-table-column :label="$t('_prop.systemset.syschannel.channelLink')" prop="channelLink" />
      <el-table-column :label="$t('_prop.systemset.syschannel.eventName')" prop="eventName" />
      <el-table-column :label="$t('_prop.systemset.syschannel.className')" prop="className" />
      <el-table-column :label="$t('_prop.systemset.syschannel.iconName')" prop="iconName" />
      <el-table-column :label="$t('_prop.systemset.syschannel.channelType')" prop="channelType" />
      <el-table-column :label="$t('_prop.systemset.syschannel.sort')" prop="sort" />
      <el-table-column :label="$t('_prop.systemset.syschannel.show')" prop="show" />
      <DefaultStatusSwitchColumn :url="statusUrl" :tag-type-mapping="getCommonStatusTagType" />
      <el-table-column :label="$t('_prop.common.createDate')" prop="createDate" :formatter="formatDate" />
      <DefaultOperateButtonColumn @operate-button-click="operateButtonClick" />
    </template>
  </TreeSidePageableTable>
  <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>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.channelName')" prop="channelName">
            <el-input v-model="form.channelName" :placeholder="$t('_message.systemset.syschannel.input_channelName')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.eventName')" prop="eventName">
            <el-input v-model="form.eventName" :placeholder="$t('_message.systemset.syschannel.input_eventName')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.channelType')" prop="channelType">
            <el-input v-model="form.channelType" :placeholder="$t('_message.systemset.syschannel.input_channelType')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.channelType')" prop="channelType">
            <el-input v-model="form.channelType" :placeholder="$t('_message.systemset.syschannel.input_channelType')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.channelLink')" prop="channelLink">
            <el-input v-model="form.channelLink" :placeholder="$t('_message.systemset.syschannel.input_channelLink')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.viewLink')" prop="viewLink">
            <el-input v-model="form.viewLink" :placeholder="$t('_message.systemset.syschannel.input_viewLink')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.sort')" prop="sort">
            <el-input v-model="form.sort" :placeholder="$t('_message.systemset.syschannel.input_sort')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.iconName')" prop="iconName">
            <el-input v-model="form.iconName" :placeholder="$t('_message.systemset.syschannel.input_iconName')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.className')" prop="className">
            <el-input v-model="form.className" :placeholder="$t('_message.systemset.syschannel.input_className')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('_prop.common.status')" prop="status">
            <StatusSwitch v-model="form.status" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.show')" prop="show">
            <el-input v-model="form.show" :placeholder="$t('_message.systemset.syschannel.input_show')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('_prop.systemset.syschannel.show')" prop="show">
            <el-input v-model="form.show" :placeholder="$t('_message.systemset.syschannel.input_show')" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item :label="$t('_prop.systemset.syschannel.channelType')" prop="channelType">
        <el-input v-model="form.channelType" :placeholder="$t('_message.systemset.syschannel.input_channelType')" />
      </el-form-item>
    </template>
  </BaseForm>
</template>
