<template>
  <div class="brand-container">
    <el-card shadow="hover">
      <!-- 操作栏 -->
      <div class="action-bar mb-[16px]">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">添加品牌</el-button>
        <el-button icon="Refresh" @click="getList">刷新</el-button>
      </div>

      <!-- 品牌卡片网格 -->
      <div class="brand-grid">
        <div v-for="item in brandList" :key="item.id" class="brand-card">
          <el-image :src="item.imageUrl" fit="cover" lazy class="brand-image">
            <template #error>
              <div class="image-slot">
                <el-icon><Picture /></el-icon>
              </div>
            </template>
          </el-image>
          <!-- 悬浮操作 -->
          <div class="card-overlay">
            <el-button type="primary" size="small" icon="Edit" @click="handleUpdate(item)">编辑</el-button>
            <el-button type="danger" size="small" icon="Delete" @click="handleDelete(item)">删除</el-button>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <el-empty v-if="!loading && brandList.length === 0" description="暂无品牌数据" />

      <!-- 分页 -->
      <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 @close="cancel">
      <el-form ref="brandFormRef" :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="imageUrl">
          <image-upload v-model="form.imageUrl" :limit="1" />
          <div class="upload-tip">建议尺寸：400x300，支持 jpg/png 格式</div>
        </el-form-item>
        <el-form-item label="跳转链接" prop="link">
          <el-input v-model="form.link" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" controls-position="right" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Brand" lang="ts">
import { reactive, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Picture } from '@element-plus/icons-vue';

const queryParams = reactive({ pageNum: 1, pageSize: 10 });
const form = reactive({
  id: undefined as number | undefined,
  title: '',
  imageUrl: '',
  link: '',
  sort: 0
});
const rules = {
  title: [{ required: true, message: '品牌名称不能为空', trigger: 'blur' }],
  imageUrl: [{ required: true, message: '品牌图片不能为空', trigger: 'change' }]
};

const dialog = reactive({ visible: false, title: '' });
const loading = ref(false);
const total = ref(0);
const brandList = ref<any[]>([]);
const brandFormRef = ref();

// 模拟数据
const mockData = [
  { id: 1, title: '罗技', imageUrl: 'https://via.placeholder.com/400x300/1a1a2e/FFFFFF?text=Logitech', link: '/brand/logitech', sort: 1 },
  { id: 2, title: '摩飞', imageUrl: 'https://via.placeholder.com/400x300/16213e/FFFFFF?text=Morphy+Richards', link: '/brand/morphy', sort: 2 },
  { id: 3, title: '小熊', imageUrl: 'https://via.placeholder.com/400x300/f5e6ca/333333?text=Bear', link: '/brand/bear', sort: 3 },
  { id: 4, title: '希诺', imageUrl: 'https://via.placeholder.com/400x300/e8e8e8/333333?text=HEENOOR', link: '/brand/heenoor', sort: 4 },
  { id: 5, title: '什湖', imageUrl: 'https://via.placeholder.com/400x300/87ceeb/333333?text=什湖', link: '/brand/shihu', sort: 5 }
];

const getList = () => {
  loading.value = true;
  setTimeout(() => {
    const start = (queryParams.pageNum - 1) * queryParams.pageSize;
    const end = start + queryParams.pageSize;
    brandList.value = mockData.slice(start, end);
    total.value = mockData.length;
    loading.value = false;
  }, 300);
};

const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加品牌';
};

const handleUpdate = (row: any) => {
  reset();
  Object.assign(form, row);
  dialog.visible = true;
  dialog.title = '修改品牌';
};

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

const handleDelete = (row: any) => {
  ElMessageBox.confirm('是否确认删除品牌"' + row.title + '"？').then(() => {
    ElMessage.success('删除成功');
    getList();
  }).catch(() => {});
};

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

const reset = () => {
  Object.assign(form, { id: undefined, title: '', imageUrl: '', link: '', sort: 0 });
  brandFormRef.value?.resetFields();
};

getList();
</script>

<style scoped lang="scss">
.brand-container {
  height: 100%;
}

.action-bar {
  display: flex;
  gap: 10px;
}

.brand-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 16px;
  margin-bottom: 20px;

  @media (max-width: 1400px) {
    grid-template-columns: repeat(4, 1fr);
  }
  @media (max-width: 1100px) {
    grid-template-columns: repeat(3, 1fr);
  }
  @media (max-width: 800px) {
    grid-template-columns: repeat(2, 1fr);
  }
}

.brand-card {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  aspect-ratio: 4 / 3;

  &:hover .card-overlay {
    opacity: 1;
  }

  .brand-image {
    width: 100%;
    height: 100%;
  }

  .image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
    color: #c0c4cc;
    font-size: 40px;
  }

  .card-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    opacity: 0;
    transition: opacity 0.3s;
  }
}

.upload-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 8px;
}
</style>
