<template>
  <el-dialog v-model="visible" title="关注类型管理" width="600px">
    <div class="mb-10 text-right">
      <el-button type="primary" size="small" @click="handleAdd"> <vab-icon icon="add-line" /> 新增类型</el-button>
    </div>
    
    <el-table :data="localTypes" border stripe>
      <el-table-column prop="label" label="类型名称" width="120">
        <template #default="{ row }">
          <el-input v-if="row.editing" v-model="row.label" size="small" />
          <span v-else>{{ row.label }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="value" label="标识(Key)" width="120">
        <template #default="{ row }">
          <el-input v-if="row.editing && row.isNew" v-model="row.value" size="small" placeholder="英文标识" />
          <span v-else>{{ row.value }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="color" label="颜色" width="120">
        <template #default="{ row }">
           <el-select v-if="row.editing" v-model="row.color" size="small">
             <el-option label="Primary" value="text-primary" />
             <el-option label="Warning" value="text-warning" />
             <el-option label="Danger" value="text-danger" />
             <el-option label="Success" value="text-success" />
             <el-option label="Info" value="text-info" />
           </el-select>
           <el-tag v-else :type="getColorType(row.color)" size="small">Sample</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row, $index }">
          <template v-if="row.editing">
            <el-button type="success" text size="small" @click="handleSave(row)">保存</el-button>
            <el-button type="info" text size="small" @click="handleCancel(row, $index)">取消</el-button>
          </template>
          <template v-else>
            <el-button type="primary" text size="small" @click="handleEdit(row)">编辑</el-button>
            <el-popconfirm title="确定删除该类型吗？" @confirm="handleDelete($index)">
              <template #reference>
                <el-button type="danger" text size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <template #footer>
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" @click="confirm">  应用更改</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { cloneDeep } from 'lodash-es'

const props = defineProps({
  types: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['update:types', 'change'])

const visible = ref(false)
const localTypes = ref([])

const open = () => {
  // Filter out 'all' type if present, or handle it? Usually 'all' is a virtual type.
  // Assuming props.types includes the actual data types.
  localTypes.value = cloneDeep(props.types).map(t => ({ ...t, editing: false, isNew: false }))
  visible.value = true
}

const getColorType = (colorClass) => {
  const map = {
    'text-primary': '',
    'text-warning': 'warning',
    'text-danger': 'danger',
    'text-success': 'success',
    'text-info': 'info'
  }
  return map[colorClass] || 'info'
}

const handleAdd = () => {
  localTypes.value.push({
    label: '',
    value: '',
    color: 'text-info',
    icon: 'star-line', // Default icon
    desc: '新添加的关注类型',
    editing: true,
    isNew: true
  })
}

const handleEdit = (row) => {
  row.editing = true
}

const handleSave = (row) => {
  if (!row.label || !row.value) {
    ElMessage.warning('请填写完整信息')
    return
  }
  row.editing = false
  row.isNew = false
}

const handleCancel = (row, index) => {
  if (row.isNew) {
    localTypes.value.splice(index, 1)
  } else {
    row.editing = false
    // Restore original value if needed, but for simplicity we just exit edit mode
    // Ideally we should keep a backup of the original row data
  }
}

const handleDelete = (index) => {
  localTypes.value.splice(index, 1)
}

const confirm = () => {
  // Validate
  if (localTypes.value.some(t => t.editing)) {
    ElMessage.warning('请先保存所有编辑项')
    return
  }
  emit('update:types', localTypes.value)
  emit('change', localTypes.value)
  visible.value = false
  ElMessage.success('关注类型配置已更新')
}

defineExpose({
  open
})
</script>

<style scoped>
.mb-10 { margin-bottom: 10px; }
.text-right { text-align: right; }
</style>
