<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="treeData" border row-key="id" :tree-props="{ children: 'children' }" default-expand-all>
        <el-table-column label="导航标题" prop="navigationName" />
        <el-table-column label="链接地址" prop="url" 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="200" align="center">
          <template #default="scope">
            <el-button link type="primary" @click="handleAdd(scope.row)" v-if="!scope.row.parentId" v-hasPermi="['system:navigation:add']">新增子级</el-button>
            <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>
    </el-card>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="上级导航" prop="parentId">
          <el-select v-model="form.parentId" placeholder="无（顶级导航）" clearable style="width: 100%">
            <el-option label="无（顶级导航）" :value="0" />
            <el-option v-for="item in parentOptions" :key="item.id" :label="item.navigationName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="导航名称" prop="navigationName">
          <el-input v-model="form.navigationName" placeholder="请输入导航名称" />
        </el-form-item>
        <el-form-item label="链接地址" prop="url" required>
          <el-input v-model="form.url" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isEnable">
          <el-radio-group v-model="form.isEnable">
            <el-radio value="0">是</el-radio>
            <el-radio value="1">否</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="BottomNav" lang="ts">
import { listNavigation, getNavigation, addNavigation, updateNavigation, delNavigation } from '@/api/system/navigation';
import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs, computed } from 'vue';
import type { FormInstance } from 'element-plus';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const dataList = ref<any[]>([]);
const loading = ref(true);

const formRef = ref<FormInstance>();

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

const data = reactive({
  form: {} as any,
  queryParams: {
    pageNum: 1,
    pageSize: 1000,
    navType: 'setting_footer'
  },
  rules: {
    navigationName: [{ required: true, message: '导航名称不能为空', trigger: 'blur' }],
    sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
    isEnable: [{ required: true, message: '请选择是否启用', trigger: 'change' }]
  }
});

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

/** 构建树形结构 */
const treeData = computed(() => {
  const list = dataList.value;
  const map = new Map<number, any>();
  const roots: any[] = [];

  // 先将所有节点放入map
  list.forEach((item: any) => {
    map.set(item.id, { ...item, children: [] });
  });

  // 构建树
  list.forEach((item: any) => {
    const node = map.get(item.id);
    if (item.parentId && item.parentId !== 0) {
      const parent = map.get(item.parentId);
      if (parent) {
        parent.children.push(node);
      } else {
        roots.push(node);
      }
    } else {
      roots.push(node);
    }
  });

  // 移除空的children数组
  const cleanChildren = (nodes: any[]) => {
    nodes.forEach((node) => {
      if (node.children && node.children.length === 0) {
        delete node.children;
      } else if (node.children) {
        cleanChildren(node.children);
      }
    });
  };
  cleanChildren(roots);

  return roots;
});

/** 上级导航选项（只显示顶级，最多二级） */
const parentOptions = computed(() => {
  return dataList.value.filter((item: any) => !item.parentId || item.parentId === 0);
});

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

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

const handleAdd = (row?: any) => {
  reset();
  if (row) {
    form.value.parentId = row.id;
  }
  dialog.visible = true;
  dialog.title = row ? '添加子级导航' : '添加底部导航';
};

const handleUpdate = async (row: any) => {
  reset();
  const res = await getNavigation(row.id);
  Object.assign(form.value, res.data);
  // 确保parentId有值
  if (!form.value.parentId) {
    form.value.parentId = 0;
  }
  dialog.visible = true;
  dialog.title = '修改底部导航';
};

const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      // 如果parentId为0，传null给后端
      const submitData = { ...form.value };
      if (submitData.parentId === 0) {
        submitData.parentId = null;
      }
      if (form.value.id) {
        await updateNavigation(submitData);
        proxy?.$modal.msgSuccess('修改成功');
      } else {
        await addNavigation(submitData);
        proxy?.$modal.msgSuccess('新增成功');
      }
      dialog.visible = false;
      getList();
    }
  });
};

const handleDelete = async (row: any) => {
  // 如果是顶级且有子级，提示不能删除
  const hasChildren = dataList.value.some((item: any) => item.parentId === row.id);
  if (hasChildren) {
    proxy?.$modal.msgWarning('该导航下存在子级，请先删除子级');
    return;
  }
  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>
