<template>
  <div class="solution-container">
    <el-card shadow="hover">
      <!-- 标题区域 -->
      <div class="solution-header">
        <div class="header-left">
          <h2 class="solution-title">热门方案</h2>
          <span class="solution-subtitle">为采购人提供"业务+服务"的项目解决方案</span>
        </div>
        <div class="header-right">
          <el-link type="primary" :underline="false" @click="handleMore">更多方案></el-link>
        </div>
      </div>

      <!-- 方案卡片网格 -->
      <div class="solution-grid">
        <div
          v-for="item in solutionList"
          :key="item.id"
          class="solution-card"
          @click="handleCardClick(item)"
        >
          <!-- 卡片上半部分：标题和描述 -->
          <div class="card-header">
            <h3 class="card-title">{{ item.title }}</h3>
            <p class="card-description">{{ item.description }}</p>
          </div>
          <!-- 卡片下半部分：图片 -->
          <div class="card-image">
            <el-image :src="item.imageUrl" fit="contain" lazy class="solution-image">
              <template #error>
                <div class="image-slot">
                  <el-icon><Picture /></el-icon>
                </div>
              </template>
            </el-image>
          </div>
          <!-- 操作按钮 -->
          <div class="card-actions">
            <el-button link type="primary" icon="Edit" @click.stop="handleUpdate(item)">编辑</el-button>
            <el-button link type="danger" icon="Delete" @click.stop="handleDelete(item)">删除</el-button>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <el-empty v-if="!loading && solutionList.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="600px" append-to-body @close="cancel">
      <el-form ref="solutionFormRef" :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="description">
          <el-input v-model="form.description" type="textarea" :rows="3" placeholder="请输入方案描述" />
        </el-form-item>
        <el-form-item label="方案图片" prop="imageUrl">
          <image-upload v-model="form.imageUrl" :limit="1" />
        </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>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Solution" 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: '',
  description: '',
  imageUrl: '',
  sort: 0
});

const rules = {
  title: [{ required: true, message: '方案标题不能为空', trigger: 'blur' }],
  description: [{ required: true, message: '方案描述不能为空', trigger: 'blur' }]
};

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

// 模拟数据
const mockData = [
  { id: 1, title: '50档/50-100档', description: '对公业务伴手礼', imageUrl: 'https://via.placeholder.com/200x150/F5F5F5/333333?text=耳机', sort: 1 },
  { id: 2, title: '100-200元档', description: '对公业务伴手礼', imageUrl: 'https://via.placeholder.com/200x150/F5F5F5/333333?text=鼠标', sort: 2 },
  { id: 3, title: '200-500元档', description: '对公业务伴手礼', imageUrl: 'https://via.placeholder.com/200x150/F5F5F5/333333?text=手表', sort: 3 },
  { id: 4, title: '500-1000元档', description: '对公业务伴手礼', imageUrl: 'https://via.placeholder.com/200x150/F5F5F5/333333?text=耳机2', sort: 4 },
  { id: 5, title: '开门红电器', description: '对公业务伴手礼', imageUrl: 'https://via.placeholder.com/200x150/F5F5F5/333333?text=电饭煲', sort: 5 }
];

const getList = () => {
  loading.value = true;
  setTimeout(() => {
    const start = (queryParams.pageNum - 1) * queryParams.pageSize;
    const end = start + queryParams.pageSize;
    solutionList.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 = () => {
  solutionFormRef.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 handleCardClick = (item: any) => {
  console.log('点击方案:', item);
};

const handleMore = () => {
  ElMessage.info('更多方案功能开发中...');
};

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

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

getList();
</script>

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

.solution-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid #ebeef5;

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

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

    .solution-subtitle {
      font-size: 14px;
      color: #909399;
    }
  }
}

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

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

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

.solution-card {
  background: #f8f9fa;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s;
  display: flex;
  flex-direction: column;

  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
  }

  .card-header {
    margin-bottom: 16px;

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

    .card-description {
      margin: 0;
      font-size: 13px;
      color: #909399;
    }
  }

  .card-image {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 120px;

    .solution-image {
      max-width: 100%;
      max-height: 120px;
    }

    .image-slot {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100px;
      height: 100px;
      background: #ebeef5;
      border-radius: 8px;
      color: #c0c4cc;
      font-size: 30px;
    }
  }

  .card-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #e4e7ed;
  }
}
</style>
