<template>
  <div>
    <el-card shadow="never">
      <div style="display:flex;gap:12px;margin-bottom:16px">
        <el-input v-model="searchQuery" placeholder="搜索产品名称..." style="width:260px" clearable @clear="load" @keyup.enter="load" />
        <el-button type="primary" @click="showForm = true">新增产品</el-button>
      </div>
      <el-table :data="list" v-loading="loading" stripe style="width:100%">
        <el-table-column prop="name" label="名称" min-width="140" />
        <el-table-column prop="name_en" label="英文名" min-width="160" />
        <el-table-column prop="category" label="类别" width="120" />
        <el-table-column prop="price" label="价格" width="100">
          <template #default="{ row }">{{ row.price }} {{ row.currency || 'USD' }}</template>
        </el-table-column>
        <el-table-column prop="moq" label="MOQ" width="80" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link size="small" @click="editRow(row)">编辑</el-button>
            <el-popconfirm title="确定删除？" @confirm="deleteRow(row.id)">
              <template #reference>
                <el-button type="danger" link size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-empty v-if="!loading && !list.length" description="暂无产品" />
    </el-card>

    <el-dialog v-model="showForm" :title="editing ? '编辑产品' : '新增产品'" width="520">
      <el-form :model="form" label-width="80">
        <el-form-item label="名称"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="英文名"><el-input v-model="form.name_en" /></el-form-item>
        <el-form-item label="类别"><el-input v-model="form.category" /></el-form-item>
        <el-form-item label="描述"><el-input v-model="form.description" type="textarea" :rows="2" /></el-form-item>
        <el-form-item label="价格">
          <el-input-number v-model="form.price" :min="0" style="width:200px" />
          <el-select v-model="form.currency" style="width:100px;margin-left:8px">
            <el-option label="USD" value="USD" />
            <el-option label="CNY" value="CNY" />
            <el-option label="EUR" value="EUR" />
          </el-select>
        </el-form-item>
        <el-form-item label="MOQ"><el-input-number v-model="form.moq" :min="1" /></el-form-item>
        <el-form-item label="关键词"><el-input v-model="form.keywords" placeholder="逗号分隔" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showForm = false">取消</el-button>
        <el-button type="primary" :loading="saving" @click="save">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { listProducts, createProduct, updateProduct, deleteProduct } from '@/api'
import { ElMessage } from 'element-plus'

const loading = ref(false)
const list = ref([])
const searchQuery = ref('')
const showForm = ref(false)
const editing = ref(false)
const saving = ref(false)
const form = reactive({ name: '', name_en: '', category: '', description: '', price: 0, currency: 'USD', moq: 1, keywords: '' })

onMounted(load)

async function load() {
  loading.value = true
  try {
    const params = {}
    if (searchQuery.value) params.query = searchQuery.value
    const res = await listProducts(params)
    const d = res.data || res
    list.value = d.items || d.rows || d.data || d || []
  } catch { /* ignore */ }
  finally { loading.value = false }
}

async function save() {
  saving.value = true
  try {
    const payload = { ...form }
    if (editing.value) {
      await updateProduct(editing.value, payload)
      ElMessage.success('已更新')
    } else {
      await createProduct(payload)
      ElMessage.success('已创建')
    }
    showForm.value = false
    load()
  } catch (e) { ElMessage.error(e?.detail || '操作失败') }
  finally { saving.value = false }
}

function editRow(row) {
  editing.value = row.id
  Object.assign(form, { name: row.name, name_en: row.name_en || '', category: row.category || '', description: row.description || '', price: row.price || 0, currency: row.currency || 'USD', moq: row.moq || 1, keywords: row.keywords || '' })
  showForm.value = true
}

async function deleteRow(id) {
  try {
    await deleteProduct(id)
    ElMessage.success('已删除')
    load()
  } catch { ElMessage.error('删除失败') }
}
</script>
