<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" @tab-change="handleTabChange">
        <el-tab-pane label="用户标签" name="user">
          <div class="operation-bar">
            <el-button type="primary" icon="Plus" @click="handleAdd('user')" v-hasPermi="['archieves:tag:add']">新增标签</el-button>
          </div>
          <el-table :data="tagList" v-loading="loading" 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.colorType || 'info'" 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="80" align="center">
              <template #default="scope">
                <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button link type="primary" @click="handleEdit(scope.row)" v-hasPermi="['archieves:tag:edit']">编辑</el-button>
                <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['archieves:tag:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              v-model:current-page="queryParams.pageNum"
              v-model:page-size="queryParams.pageSize"
              :total="total"
              :page-sizes="[10, 20, 50]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="getList"
              @current-change="getList"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="宠物标签" name="pet">
          <div class="operation-bar">
            <el-button type="primary" icon="Plus" @click="handleAdd('pet')" v-hasPermi="['archieves:tag:add']">新增标签</el-button>
          </div>
          <el-table :data="tagList" v-loading="loading" 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.colorType || 'info'" 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="80" align="center">
              <template #default="scope">
                <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
              </template>
            </el-table-column>
            <el-table-column label="操作" width="180">
              <template #default="scope">
                <el-button link type="primary" @click="handleEdit(scope.row)" v-hasPermi="['archieves:tag:edit']">编辑</el-button>
                <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['archieves:tag:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              v-model:current-page="queryParams.pageNum"
              v-model:page-size="queryParams.pageSize"
              :total="total"
              :page-sizes="[10, 20, 50]"
              layout="total, sizes, prev, pager, next, jumper"
              @size-change="getList"
              @current-change="getList"
            />
          </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="标签颜色">
          <div class="color-picker-row">
            <span
              v-for="item in colorOptions"
              :key="item.value"
              class="color-dot"
              :class="{ active: form.colorType === item.value }"
              :style="{ backgroundColor: item.color }"
              :title="item.label"
              @click="form.colorType = item.value"
            />
          </div>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="parseInt(dict.value)">{{ dict.label }}</el-radio>
          </el-radio-group>
        </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" :loading="submitLoading" @click="saveData">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance, toRefs } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { listTag, addTag, updateTag, delTag } from '@/api/archieves/tag';

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = toRefs(proxy?.useDict('sys_normal_disable'));

const colorOptions = [
  { label: '默认', value: 'info', color: '#909399' },
  { label: '主要', value: 'primary', color: '#409EFF' },
  { label: '成功', value: 'success', color: '#67C23A' },
  { label: '警告', value: 'warning', color: '#E6A23C' },
  { label: '危险', value: 'danger', color: '#F56C6C' }
];

const activeTab = ref('user');
const dialogVisible = ref(false);
const isEdit = ref(false);
const loading = ref(false);
const submitLoading = ref(false);
const tagList = ref([]);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  category: 'user'
});

const form = reactive({
  id: undefined,
  name: '',
  category: 'user',
  colorType: 'info',
  description: '',
  type: 2,
  status: 0
});

const getList = () => {
  loading.value = true;
  queryParams.category = activeTab.value;
  listTag(queryParams).then((res) => {
    tagList.value = res.rows;
    total.value = res.total;
  }).finally(() => {
    loading.value = false;
  });
};

const handleTabChange = () => {
  queryParams.pageNum = 1;
  getList();
};

const handleAdd = (type) => {
  isEdit.value = false;
  Object.assign(form, { id: undefined, name: '', category: type, colorType: 'info', description: '', type: 2, status: 0 });
  dialogVisible.value = true;
};

const handleEdit = (row) => {
  isEdit.value = true;
  Object.assign(form, { id: row.id, name: row.name, category: row.category, colorType: row.colorType, description: row.description, type: row.type, status: row.status });
  dialogVisible.value = true;
};

const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该标签吗?', '提示', { type: 'warning' }).then(() => {
    delTag(row.id).then(() => {
      ElMessage.success('删除成功');
      getList();
    });
  });
};

const saveData = () => {
  if (!form.name) return ElMessage.warning('请输入标签名称');
  submitLoading.value = true;
  const api = isEdit.value ? updateTag(form) : addTag(form);
  api.then(() => {
    ElMessage.success('保存成功');
    dialogVisible.value = false;
    getList();
  }).finally(() => {
    submitLoading.value = false;
  });
};

onMounted(() => {
  getList();
});
</script>

<style scoped>
.page-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.title {
  font-weight: bold;
}
.operation-bar {
  margin-bottom: 10px;
}
.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.color-picker-row {
  display: flex;
  gap: 10px;
  align-items: center;
}
.color-dot {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: all 0.2s;
}
.color-dot:hover {
  transform: scale(1.2);
}
.color-dot.active {
  border-color: #303133;
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.1);
}
</style>
