<template>
  <div class="page-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span class="title">标签管理</span>
        </div>
      </template>

      <el-tabs v-model="activeTab" class="demo-tabs">
        <el-tab-pane label="用户标签" name="user">
          <div class="operation-bar">
            <el-button type="primary" icon="Plus" @click="handleAdd('user')">新增标签</el-button>
          </div>
          <el-table :data="userTags" style="width: 100%; margin-top: 20px" :header-cell-style="{ background: '#f5f7fa' }">
            <el-table-column label="标签名称" width="200">
              <template #default="scope">
                <el-tag :type="scope.row.type" effect="light">{{ scope.row.name }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="description" label="说明" show-overflow-tooltip />
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button link type="primary" @click="handleEdit(scope.row, 'user')">编辑</el-button>
                <el-button link type="danger" @click="handleDelete(scope.row, 'user')">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              v-model:current-page="userPage.current"
              v-model:page-size="userPage.size"
              :total="userPage.total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="宠物标签" name="pet">
          <div class="operation-bar">
            <el-button type="primary" icon="Plus" @click="handleAdd('pet')">新增标签</el-button>
          </div>
          <el-table :data="petTags" style="width: 100%; margin-top: 20px" :header-cell-style="{ background: '#f5f7fa' }">
            <el-table-column label="标签名称" width="200">
              <template #default="scope">
                <el-tag :type="scope.row.type" effect="light">{{ scope.row.name }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="description" label="说明" show-overflow-tooltip />
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button link type="primary" @click="handleEdit(scope.row, 'pet')">编辑</el-button>
                <el-button link type="danger" @click="handleDelete(scope.row, 'pet')">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              v-model:current-page="petPage.current"
              v-model:page-size="petPage.size"
              :total="petPage.total"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="isEdit ? '编辑标签' : '新增标签'" width="400px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="标签名称" required>
          <el-input v-model="form.name" placeholder="如：非常有耐心" />
        </el-form-item>
        <el-form-item label="标签颜色" required>
          <el-select v-model="form.type" placeholder="Select">
            <el-option v-for="color in tagColors" :key="color.value" :label="color.label" :value="color.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述说明">
          <el-input v-model="form.description" type="textarea" placeholder="请输入描述说明" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveData">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const activeTab = ref('user');
const dialogVisible = ref(false);
const isEdit = ref(false);
const currentType = ref('user'); // 'user' or 'pet'

const userPage = reactive({ current: 1, size: 10, total: 20 });
const petPage = reactive({ current: 1, size: 10, total: 15 });

const handleSizeChange = (val) => {
  console.log(`每页 ${val} 条`);
};
const handleCurrentChange = (val) => {
  console.log(`当前页: ${val}`);
};

const userTags = ref([
  { id: 1, name: '优质客户', type: 'success', description: '累计消费金额TOP10' },
  { id: 2, name: '潜在流失', type: 'warning', description: '近半年无消费记录' },
  { id: 3, name: '黑名单', type: 'danger', description: '有过多次投诉行为' },
  { id: 4, name: '新手', type: 'primary', description: '注册未满1个月' }
]);

const petTags = ref([
  { id: 1, name: '易过敏', type: 'danger', description: '对海鲜、谷物过敏' },
  { id: 2, name: '胆小', type: 'warning', description: '害怕陌生环境和声音' },
  { id: 3, name: '攻击性', type: 'info', description: '洗澡时需要戴嘴套' },
  { id: 4, name: '粘人', type: 'success', description: '非常喜欢和人互动' }
]);

const tagColors = [
  { label: '默认蓝', value: 'primary' },
  { label: '成功绿', value: 'success' },
  { label: '警告橙', value: 'warning' },
  { label: '危险红', value: 'danger' },
  { label: '信息灰', value: 'info' }
];

const form = reactive({
  id: null,
  name: '',
  type: 'primary',
  description: ''
});

const handleAdd = (type) => {
  isEdit.value = false;
  currentType.value = type;
  Object.assign(form, { id: null, name: '', type: 'primary', description: '' });
  dialogVisible.value = true;
};

const handleEdit = (row, type) => {
  isEdit.value = true;
  currentType.value = type;
  Object.assign(form, row);
  dialogVisible.value = true;
};

const handleDelete = (row, type) => {
  ElMessageBox.confirm('确认删除该标签吗?', '提示', { type: 'warning' }).then(() => {
    const targetList = type === 'user' ? userTags : petTags;
    targetList.value = targetList.value.filter((item) => item.id !== row.id);
    ElMessage.success('删除成功');
  });
};

const saveData = () => {
  if (!form.name) return ElMessage.warning('请输入标签名称');

  const targetList = currentType.value === 'user' ? userTags : petTags;

  if (isEdit.value) {
    const idx = targetList.value.findIndex((item) => item.id === form.id);
    if (idx !== -1) Object.assign(targetList.value[idx], form);
  } else {
    targetList.value.push({
      id: Date.now(),
      ...form
    });
  }
  ElMessage.success('保存成功');
  dialogVisible.value = false;
};
</script>

<style scoped>
.page-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.title {
  font-weight: bold;
}
.title {
  font-weight: bold;
}
.operation-bar {
  margin-bottom: 10px;
}
.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
