<template>
  <div class="search-nav-page">
    <div class="search-nav-container">
      <!-- 搜索区域（独立卡片） -->
      <div class="search-card">
        <el-form :model="queryParams" :inline="true">
          <el-form-item label="导航标题">
            <el-input v-model="queryParams.title" placeholder="请输入导航标题" clearable style="width: 240px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            <el-button type="primary" icon="Plus" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格区域（独立卡片） -->
      <div class="table-card">
        <div class="table-header">
          <span class="table-title">搜索导航信息列表</span>
          <el-button icon="Refresh" circle size="small" @click="getList" />
        </div>

        <el-table v-loading="loading" :data="navList" border>
          <el-table-column label="导航标题" align="center" prop="title" min-width="120" />
          <el-table-column label="链接地址" align="center" prop="linkUrl" min-width="280">
            <template #default="scope">
              <el-link type="primary" :href="scope.row.linkUrl" target="_blank">{{ scope.row.linkUrl }}</el-link>
            </template>
          </el-table-column>
          <el-table-column label="排序" align="center" prop="sort" width="100" />
          <el-table-column label="导航类型" align="center" prop="navType" width="120" />
          <el-table-column label="操作" align="center" width="150">
            <template #default="scope">
              <span class="action-link primary" @click="handleEdit(scope.row)">编辑</span>
              <span class="action-link danger" @click="handleDelete(scope.row)">删除</span>
            </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"
        />
      </div>
    </div>

    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="导航标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入导航标题" />
        </el-form-item>
        <el-form-item label="链接地址" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="导航类型" prop="navType">
          <el-select v-model="form.navType" placeholder="请选择导航类型" style="width: 100%">
            <el-option label="搜索导航" value="搜索导航" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" :max="999" controls-position="right" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="IndustrialSearchNav" lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

// 查询参数
const queryParams = reactive({
  title: '',
  pageNum: 1,
  pageSize: 10
});

// 列表数据
const navList = ref([
  { id: '1', title: '世达工具', linkUrl: 'https://mro.yoe365.com/', sort: 10, navType: '搜索导航' },
  { id: '2', title: '劳保', linkUrl: 'https://mro.yoe365.com/', sort: 8, navType: '搜索导航' },
  { id: '3', title: '电器辅材', linkUrl: 'https://mro.yoe365.com/theme?t=000000006', sort: 0, navType: '搜索导航' },
  { id: '4', title: '施耐德电气超品周', linkUrl: '#', sort: 0, navType: '搜索导航' }
]);
const loading = ref(false);
const total = ref(4);

// 对话框
const dialog = reactive({
  visible: false,
  title: ''
});

// 表单
const formRef = ref();
const initForm = {
  id: undefined as string | undefined,
  title: '',
  linkUrl: '',
  navType: '搜索导航',
  sort: 0
};
const form = ref({ ...initForm });

const rules = {
  title: [{ required: true, message: '请输入导航标题', trigger: 'blur' }],
  linkUrl: [{ required: true, message: '请输入链接地址', trigger: 'blur' }]
};

// 查询列表
const getList = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
  }, 300);
};

// 搜索
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

// 重置
const resetQuery = () => {
  queryParams.title = '';
  handleQuery();
};

// 添加
const handleAdd = () => {
  form.value = { ...initForm };
  dialog.title = '新增导航';
  dialog.visible = true;
};

// 编辑
const handleEdit = (row: any) => {
  form.value = { ...row };
  dialog.title = '编辑导航';
  dialog.visible = true;
};

// 删除
const handleDelete = (row: any) => {
  ElMessageBox.confirm(`是否确认删除导航"${row.title}"？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功');
    getList();
  }).catch(() => {});
};

// 提交表单
const submitForm = () => {
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      ElMessage.success(form.value.id ? '修改成功' : '添加成功');
      dialog.visible = false;
      getList();
    }
  });
};

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

<style scoped lang="scss">
.search-nav-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
}

.search-nav-container {
  max-width: 1200px;
  margin: 0 auto;
}

.search-card {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 12px;
}

.table-card {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.table-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.action-link {
  cursor: pointer;
  margin: 0 6px;
  
  &.primary {
    color: #409eff;
    &:hover { color: #66b1ff; }
  }
  
  &.danger {
    color: #f56c6c;
    &:hover { color: #f78989; }
  }
}
</style>
