<template>
  <div class="p-2">
    <el-card shadow="hover">
      <template #header>
        <div class="card-header">
          <span class="title">首页导航信息列表</span>
          <div>
            <el-button type="primary" @click="handleAdd" v-hasPermi="['system:navigation:add']">+ 新增</el-button>
            <el-button @click="getList">刷新</el-button>
          </div>
        </div>
      </template>

      <el-table v-loading="loading" :data="dataList" border>
        <el-table-column label="导航标题" prop="navigationName" align="center" />
        <el-table-column label="链接地址" prop="url" align="center" show-overflow-tooltip />
        <el-table-column label="排序" prop="sort" width="100" align="center" />
        <el-table-column label="是否显示" prop="isEnable" width="100" align="center">
          <template #default="scope">
            {{ scope.row.isEnable === '0' ? '显示' : '不显示' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" align="center">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['system:navigation:edit']">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['system:navigation:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

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

    <!-- 添加或修改对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="导航标题" prop="navigationName">
              <el-input v-model="form.navigationName" placeholder="请输入导航标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="导航链接" prop="url">
              <el-input v-model="form.url" placeholder="请输入导航链接" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" :min="0" controls-position="right" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isEnable">
              <el-switch v-model="form.isEnable" active-value="0" inactive-value="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </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="HomeNav" lang="ts">
import { listNavigation, getNavigation, addNavigation, updateNavigation, delNavigation } from '@/api/system/navigation';
import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs } from 'vue';
import type { FormInstance } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const dataList = ref([]);
const loading = ref(true);
const total = ref(0);

const formRef = ref<FormInstance>();

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

const data = reactive({
  form: {} as any,
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    navType: 'setting_home'
  },
  rules: {
    navigationName: [{ required: true, message: '导航名称不能为空', trigger: 'blur' }],
    url: [{ required: true, message: '链接地址不能为空', trigger: 'blur' }],
    sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
    isEnable: [{ required: true, message: '请选择是否显示', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

const getList = async () => {
  loading.value = true;
  const res = await listNavigation(queryParams.value);
  dataList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

const reset = () => {
  form.value = {
    id: undefined,
    navType: 'setting_home',
    navigationName: undefined,
    url: undefined,
    sort: 0,
    isEnable: '0',
    remark: undefined
  };
  formRef.value?.resetFields();
};

const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加首页导航';
};

const handleUpdate = async (row: any) => {
  reset();
  const res = await getNavigation(row.id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改首页导航';
};

const submitForm = () => {
  formRef.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('是否确认删除？');
  await delNavigation(row.id);
  proxy?.$modal.msgSuccess('删除成功');
  getList();
};

const cancel = () => {
  dialog.visible = false;
  reset();
};

getList();
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-header .title {
  font-size: 16px;
  font-weight: bold;
}
</style>
