<template>
  <div class="p-2">
    <el-card shadow="hover">
      <template #header>
        <el-tabs v-model="activeTab" @tab-change="handleTabChange">
          <el-tab-pane label="首页导航" name="home" />
          <el-tab-pane label="内页导航" name="inner" />
          <el-tab-pane label="搜索导航" name="search" />
          <el-tab-pane label="底部导航" name="footer" />
          <el-tab-pane label="浮动导航" name="float" />
        </el-tabs>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
          <right-toolbar @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="navList" border>
        <el-table-column label="导航名称" align="center" prop="navigationName" :show-overflow-tooltip="true" />
        <el-table-column label="链接地址" align="center" prop="url" :show-overflow-tooltip="true" />
        <el-table-column label="排序" align="center" prop="sort" width="80" />
        <el-table-column label="是否启用" align="center" prop="isEnable" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.isEnable === 1 ? 'success' : 'info'">
              {{ scope.row.isEnable === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        :total="total"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="navFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="导航名称" prop="navigationName">
          <el-input v-model="form.navigationName" placeholder="请输入导航名称" />
        </el-form-item>
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnable">
          <el-radio-group v-model="form.isEnable">
            <el-radio :value="1">启用</el-radio>
            <el-radio :value="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Navigation" lang="ts">
import { listNavigation, getNavigation, addNavigation, updateNavigation, delNavigation } from '@/api/system/navigation';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const navList = ref<any[]>([]);
const loading = ref(false);
const total = ref(0);
const activeTab = ref('home');

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  navType: 'home'
});

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const navFormRef = ref<ElFormInstance>();

const initFormData = {
  id: undefined as number | undefined,
  navType: 'home',
  navigationName: '',
  url: '',
  sort: 0,
  isEnable: 1
};

const form = ref({ ...initFormData });

const rules = reactive({
  navigationName: [{ required: true, message: '导航名称不能为空', trigger: 'blur' }],
  url: [{ required: true, message: '链接地址不能为空', trigger: 'blur' }]
});

/** Tab 切换 */
const handleTabChange = (tabName: string) => {
  queryParams.value.navType = tabName;
  queryParams.value.pageNum = 1;
  getList();
};

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  try {
    const res = await listNavigation(queryParams.value);
    navList.value = res.rows;
    total.value = res.total;
  } finally {
    loading.value = false;
  }
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData, navType: activeTab.value };
  navFormRef.value?.resetFields();
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加导航';
};

/** 修改按钮操作 */
const handleUpdate = async (row: any) => {
  reset();
  const res = await getNavigation(row.id);
  form.value = res.data;
  dialog.visible = true;
  dialog.title = '修改导航';
};

/** 提交按钮 */
const submitForm = () => {
  navFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      if (form.value.id) {
        await updateNavigation(form.value);
        proxy?.$modal.msgSuccess('修改成功');
      } else {
        await addNavigation(form.value);
        proxy?.$modal.msgSuccess('新增成功');
      }
      dialog.visible = false;
      getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row: any) => {
  await proxy?.$modal.confirm('是否确认删除导航名称为"' + row.navigationName + '"的数据项?');
  await delNavigation(row.id);
  proxy?.$modal.msgSuccess('删除成功');
  getList();
};

onMounted(() => {
  getList();
});
</script>
