<template>
  <div class="floor-page">
    <div class="floor-container">
      <!-- 搜索区域（独立卡片） -->
      <div class="search-card">
        <el-form :model="queryParams" :inline="true">
          <el-form-item label="广告名称">
            <el-input v-model="queryParams.name" 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="floorList" border>
          <el-table-column label="楼层名称" align="center" prop="name" min-width="200" />
          <el-table-column label="排序" align="center" prop="sort" width="100" />
          <el-table-column label="更新时间" align="center" prop="updateTime" width="200" />
          <el-table-column label="是否显示" align="center" width="100">
            <template #default="scope">
              <span :class="scope.row.visible ? 'status-show' : 'status-hide'">
                {{ scope.row.visible ? '显示' : '不显示' }}
              </span>
            </template>
          </el-table-column>
          <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 primary" @click="handleManage(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="name">
          <el-input v-model="form.name" placeholder="请输入楼层名称" />
        </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-item label="是否显示" prop="visible">
          <el-radio-group v-model="form.visible">
            <el-radio :value="true">显示</el-radio>
            <el-radio :value="false">不显示</el-radio>
          </el-radio-group>
        </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="DecorationFloor" lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';

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

// 列表数据
const floorList = ref([
  { id: '1', name: '3C家电', sort: 1, updateTime: '2023-04-23 11:49:57', visible: true },
  { id: '2', name: '营销福利', sort: 2, updateTime: '2023-05-06 10:48:19', visible: true },
  { id: '3', name: '固定资产', sort: 3, updateTime: '2023-05-11 22:49:38', visible: false },
  { id: '4', name: '行政办公', sort: 7, updateTime: '2023-12-21 17:00:23', visible: true }
]);
const loading = ref(false);
const total = ref(4);

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

// 表单
const formRef = ref();
const initForm = {
  id: undefined as string | undefined,
  name: '',
  sort: 0,
  visible: true
};
const form = ref({ ...initForm });

const rules = {
  name: [{ required: true, message: '请输入楼层名称', trigger: 'blur' }]
};

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

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

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

// 添加
const handleAdd = () => {
  form.value = { ...initForm };
  dialog.title = '添加楼层';
  dialog.visible = true;
};

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

// 管理
const handleManage = (row: any) => {
  ElMessage.info(`管理楼层：${row.name}`);
};

// 提交表单
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">
.floor-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
}

.floor-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;
}

.status-show {
  color: #409eff;
  cursor: pointer;
}

.status-hide {
  color: #f56c6c;
  cursor: pointer;
}

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