<template>
  <el-dialog v-model="visible" title="选择套餐" width="700px" append-to-body @close="handleClose">
    <div class="selector-toolbar">
      <el-button :icon="Refresh" @click="loadList" :loading="loading">刷新</el-button>
      <span style="color:#909399;font-size:13px" v-if="goodId">商品 ID: {{ goodId }}</span>
    </div>
    <el-table :data="list" v-loading="loading" highlight-current-row @current-change="row => selected = row" :height="360" stripe size="small">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="套餐名称" min-width="160" show-overflow-tooltip />
      <el-table-column prop="note" label="说明" min-width="160" show-overflow-tooltip>
        <template #default="{ row }">{{ row.note || '-' }}</template>
      </el-table-column>
      <el-table-column label="状态" width="80">
        <template #default="{ row }">
          <el-tag :type="row.disable ? 'danger' : 'success'" size="small">{{ row.disable ? '禁用' : '启用' }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-empty v-if="!list.length && !loading" :image-size="60" description="暂无套餐" />
    <div class="selector-footer-bar">
      <span v-if="selected" style="color:#606266;font-size:13px">已选：{{ selected.name }} (ID: {{ selected.id }})</span>
      <el-pagination v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="[10,20]" :total="total"
        layout="total,sizes,prev,pager,next" small background
        @size-change="s => { pageSize = s; page = 1; loadList() }" @current-change="p => { page = p; loadList() }" />
    </div>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :disabled="!selected" @click="handleConfirm">确定选择</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import { getProductPlanList } from '@/api/admin/product'

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  goodId: { type: [Number, String], default: 0 }
})
const emit = defineEmits(['update:modelValue', 'confirm'])

const visible = ref(false)
const loading = ref(false)
const list = ref([])
const total = ref(0)
const page = ref(1)
const pageSize = ref(10)
const selected = ref(null)

watch(() => props.modelValue, (v) => { visible.value = v; if (v) { selected.value = null; loadList() } })
watch(visible, (v) => emit('update:modelValue', v))

const loadList = async () => {
  loading.value = true
  try {
    const params = { page: page.value, count: pageSize.value }
    if (props.goodId) params.good_id = props.goodId
    const res = await getProductPlanList(params)
    if (res?.data?.code === 200 && res?.data?.data) {
      const d = res.data.data
      list.value = d.data || (Array.isArray(d) ? d : [])
      total.value = d.all_count ?? d.total ?? list.value.length
    }
  } catch { /* */ } finally { loading.value = false }
}

const handleClose = () => { visible.value = false }
const handleConfirm = () => { if (selected.value) { emit('confirm', selected.value); handleClose() } }
</script>

<style scoped>
.selector-toolbar { display: flex; gap: 8px; margin-bottom: 12px; align-items: center; }
.selector-footer-bar { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; }
</style>
