<template>
  <div class="page-container">
    <el-card shadow="never">
      <template #header>
        <div class="card-header">
          <span class="title">履约者标签管理</span>
          <el-button type="primary" icon="Plus" @click="handleAdd">新增标签</el-button>
        </div>
      </template>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="name" label="标签名称" width="200">
          <template #default="scope">
            <el-tag :type="scope.row.colorType">{{ scope.row.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="说明" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-switch v-model="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button link type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button link type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </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="标签颜色">
          <el-select v-model="form.colorType" style="width: 100%">
            <el-option label="默认蓝" value="" />
            <el-option label="成功绿" value="success" />
            <el-option label="警告橙" value="warning" />
            <el-option label="危险红" value="danger" />
            <el-option label="信息灰" value="info" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述说明">
          <el-input v-model="form.desc" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveTag">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

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

const dialogVisible = ref(false);
const isEdit = ref(false);

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(100);

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

const tableData = ref([
  { id: 1, name: '接单王', colorType: 'danger', desc: '累计接单数量TOP10', status: true },
  { id: 2, name: '零差评', colorType: 'success', desc: '近半年无差评记录', status: true },
  { id: 3, name: '车技娴熟', colorType: 'primary', desc: '驾驶技术评价高', status: true },
  { id: 4, name: '五星金牌', colorType: 'warning', desc: '系统评定最高等级', status: true }
]);

const form = reactive({
  name: '',
  colorType: '',
  desc: ''
});

const handleAdd = () => {
  isEdit.value = false;
  Object.assign(form, { name: '', colorType: '', desc: '' });
  dialogVisible.value = true;
};

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

const saveTag = () => {
  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;
}
.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>
