<template>
  <div class="flash-sale-container">
    <el-card shadow="hover">
      <!-- 标题区域（可点击编辑） -->
      <div class="section-header" @click="handleEditHeader">
        <div class="header-left">
          <h2 class="section-title">{{ headerConfig.title }}</h2>
          <span class="section-subtitle">{{ headerConfig.subtitle }}</span>
        </div>
        <div class="header-right">
          <el-link type="primary" :underline="false" @click.stop="handleLinkClick">{{ headerConfig.linkText }}></el-link>
        </div>
      </div>

      <!-- 品牌展示区域 -->
      <div class="brand-showcase">
        <!-- 左侧大图 -->
        <div class="showcase-left">
          <el-image :src="featuredImage" fit="cover" class="featured-image">
            <template #error>
              <div class="image-slot"><el-icon><Picture /></el-icon></div>
            </template>
          </el-image>
          <el-button type="warning" class="view-all-btn" @click="handleViewAll">查看全部></el-button>
        </div>

        <!-- 右侧品牌网格 -->
        <div class="showcase-right">
          <div v-for="item in brandList" :key="item.id" class="brand-item">
            <el-icon class="close-icon" @click="handleRemove(item)"><Close /></el-icon>
            <div class="brand-logo">
              <el-image :src="item.logoUrl" fit="contain" class="logo-image">
                <template #error>
                  <span class="logo-text">{{ item.title }}</span>
                </template>
              </el-image>
            </div>
            <div class="brand-info">
              <p class="brand-name">{{ item.title }}</p>
              <p class="brand-desc">{{ item.description }}</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 搜索区域 -->
      <div class="search-section">
        <h3 class="search-title">品牌闪购搜索列表</h3>
        <div class="search-bar">
          <el-input v-model="searchKeyword" placeholder="请输入品牌编号或名称" clearable style="width: 300px" @keyup.enter="handleSearch" />
          <el-button type="primary" @click="handleSearch">搜索</el-button>
        </div>
      </div>
    </el-card>

    <!-- 编辑标题对话框 -->
    <el-dialog v-model="headerDialog.visible" title="编辑标题" width="600px" append-to-body>
      <el-form :model="headerForm" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="标题名称" required>
              <el-input v-model="headerForm.title" placeholder="请输入标题名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="副标题" required>
              <el-input v-model="headerForm.subtitle" placeholder="请输入副标题" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="链接文字">
              <el-input v-model="headerForm.linkText" placeholder="请输入链接文字" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="链接">
              <el-input v-model="headerForm.link" placeholder="请输入链接地址" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="背景色">
              <el-color-picker v-model="headerForm.bgColor" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字体颜色">
              <el-color-picker v-model="headerForm.textColor" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="saveHeaderConfig">确 认</el-button>
        <el-button @click="headerDialog.visible = false">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

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

const searchKeyword = ref('');
const featuredImage = ref('https://via.placeholder.com/300x400/1a1a2e/FFFFFF?text=Featured');

// 标题配置
const headerConfig = ref({
  title: '大牌推荐',
  subtitle: '甄选大牌,优质好品',
  linkText: '查看更多品牌信息',
  link: 'https://www.yoe365.com/brand',
  bgColor: '#ffffff',
  textColor: '#303133'
});

const headerDialog = reactive({ visible: false });
const headerForm = reactive({
  title: '',
  subtitle: '',
  linkText: '',
  link: '',
  bgColor: '#ffffff',
  textColor: '#303133'
});

const handleEditHeader = () => {
  Object.assign(headerForm, headerConfig.value);
  headerDialog.visible = true;
};

const saveHeaderConfig = () => {
  Object.assign(headerConfig.value, headerForm);
  headerDialog.visible = false;
  ElMessage.success('标题配置已保存');
};

const brandList = ref([
  { id: 1, title: '联想', description: '联想', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=Lenovo' },
  { id: 2, title: '惠普', description: '惠普', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=HP' },
  { id: 3, title: '奔图', description: '奔图', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=PANTUM' },
  { id: 4, title: 'MAXHUB', description: 'MAXHUB', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=MAXHUB' },
  { id: 5, title: '苹果', description: '苹果', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=Apple' },
  { id: 6, title: '飞利浦', description: '飞利浦', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=PHILIPS' },
  { id: 7, title: '美的', description: '美的', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=Midea' },
  { id: 8, title: '格力', description: '<p><br></p>', logoUrl: 'https://via.placeholder.com/120x60/FFFFFF/333333?text=GREE' }
]);

const handleViewAll = () => {
  ElMessage.info('查看全部功能开发中...');
};

const handleRemove = (item: any) => {
  ElMessage.success(`已移除品牌: ${item.title}`);
};

const handleSearch = () => {
  ElMessage.info(`搜索: ${searchKeyword.value || '全部'}`);
};

const handleLinkClick = () => {
  if (headerConfig.value.link) {
    window.open(headerConfig.value.link, '_blank');
  }
};
</script>

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

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 12px 16px;
  background: #f8f9fa;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.3s;

  &:hover {
    background: #ebeef5;
  }

  .header-left {
    display: flex;
    align-items: baseline;
    gap: 12px;

    .section-title {
      margin: 0;
      font-size: 20px;
      font-weight: 600;
      color: #303133;
    }
    .section-subtitle {
      font-size: 14px;
      color: #909399;
    }
  }
}

.brand-showcase {
  display: flex;
  gap: 16px;
  margin-bottom: 30px;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
}

.showcase-left {
  position: relative;
  width: 200px;
  flex-shrink: 0;

  .featured-image {
    width: 100%;
    height: 280px;
    border-radius: 8px;
  }

  .view-all-btn {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
  }

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

.showcase-right {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;

  @media (max-width: 1200px) {
    grid-template-columns: repeat(3, 1fr);
  }
}

.brand-item {
  position: relative;
  background: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 16px;
  text-align: center;
  transition: all 0.3s;

  &:hover {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    .close-icon { opacity: 1; }
  }

  .close-icon {
    position: absolute;
    top: 8px;
    right: 8px;
    font-size: 16px;
    color: #909399;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.3s;

    &:hover { color: #f56c6c; }
  }

  .brand-logo {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 12px;

    .logo-image { max-height: 100%; max-width: 100%; }
    .logo-text { font-size: 18px; font-weight: 600; color: #303133; }
  }

  .brand-info {
    .brand-name {
      margin: 0 0 4px 0;
      font-size: 14px;
      font-weight: 500;
      color: #303133;
    }
    .brand-desc {
      margin: 0;
      font-size: 12px;
      color: #909399;
    }
  }
}

.search-section {
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;

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

  .search-bar {
    display: flex;
    gap: 12px;
  }
}
</style>
