<template>
  <div class="banner-page">
    <div class="banner-container">
      <!-- 表格区域（独立卡片） -->
      <div class="table-card">
        <div class="table-header">
          <span class="table-title">广告位信息列表</span>
          <div class="table-actions">
            <el-button type="primary" icon="Plus" @click="handleAdd">添加广告</el-button>
            <el-button icon="Refresh" circle size="small" @click="getList" />
          </div>
        </div>

        <el-table v-loading="loading" :data="bannerList" border>
          <el-table-column label="公告标题" align="center" prop="title" min-width="150" />
          <el-table-column label="封面图片" align="center" width="120">
            <template #default="scope">
              <el-image
                :src="scope.row.imageUrl"
                fit="cover"
                style="width: 80px; height: 60px; border-radius: 4px"
                :preview-src-list="[scope.row.imageUrl]"
                preview-teleported
              >
                <template #error>
                  <div class="image-slot">
                    <el-icon><Picture /></el-icon>
                  </div>
                </template>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="链接地址" align="center" prop="linkUrl" min-width="250">
            <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" width="80">
            <template #default="scope">
              <el-tag :type="scope.row.status === '显示' ? 'primary' : 'info'" size="small">
                {{ scope.row.status }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="排序" align="center" prop="sort" width="80" />
          <el-table-column label="操作" align="center" width="120">
            <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="600px" 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="imageUrl">
          <div class="upload-area">
            <div class="image-preview" v-if="form.imageUrl">
              <el-image :src="form.imageUrl" fit="cover" style="width: 100px; height: 80px; border-radius: 4px" />
            </div>
            <el-button icon="Upload" @click="handleUpload">选择上传文件</el-button>
          </div>
        </el-form-item>
        <el-form-item label="链接地址" prop="linkUrl">
          <el-input v-model="form.linkUrl" 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="status">
          <el-radio-group v-model="form.status">
            <el-radio value="显示">显示</el-radio>
            <el-radio value="隐藏">隐藏</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="GiftBanner" lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Picture } from '@element-plus/icons-vue';

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

// 列表数据
const bannerList = ref([
  { id: '1', title: '轮播广告底部图片2', imageUrl: '/img/banner1.jpg', linkUrl: 'https://www.yoe365.com/goods-category?no=12', status: '显示', sort: 0 },
  { id: '2', title: '轮播广告底部图片', imageUrl: '/img/banner2.jpg', linkUrl: 'https://www.yoe365.com/solve?t=00027', status: '显示', sort: 0 }
]);
const loading = ref(false);
const total = ref(2);

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

// 表单
const formRef = ref();
const initForm = {
  id: undefined as string | undefined,
  title: '',
  imageUrl: '',
  linkUrl: '',
  sort: 0,
  status: '显示'
};
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 handleUpload = () => {
  ElMessage.info('上传功能开发中...');
};

// 添加
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(`是否确认删除该广告？`, '提示', {
    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">
.banner-page {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
}

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

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

.table-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 80px;
  height: 60px;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
  border-radius: 4px;
}

.upload-area {
  display: flex;
  align-items: center;
  gap: 15px;
}

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