<template>
  <el-dialog v-model="visible" title="选择主控服务" width="640px" append-to-body @close="handleClose">
    <div class="selector-toolbar">
      <el-input v-model="keyword" placeholder="搜索服务名称/地址" clearable style="width:220px"
        @keyup.enter="handleSearch" @clear="handleSearch">
        <template #prefix><el-icon><Search /></el-icon></template>
      </el-input>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
      <el-button :icon="Refresh" @click="handleRefresh" :loading="loading">刷新</el-button>
    </div>
    <el-table :data="list" v-loading="loading" highlight-current-row
      @current-change="row => selected = row" :height="320" stripe size="small">
      <el-table-column prop="id" label="ID" width="70" />
      <el-table-column prop="name" label="服务名称" min-width="160" show-overflow-tooltip />
      <el-table-column label="地址" min-width="180">
        <template #default="{ row }">
          <span style="font-family:monospace;color:#409eff">{{ row.host }}:{{ row.port }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="note" label="备注" min-width="120" show-overflow-tooltip>
        <template #default="{ row }">{{ row.note || '-' }}</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 { Search, Refresh } from '@element-plus/icons-vue'
import { getKvmServiceList } from '@/api/admin/kvmService'

const props = defineProps({ modelValue: { type: Boolean, default: false } })
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 keyword = ref('')
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 (keyword.value) params.key = keyword.value
    const res = await getKvmServiceList(params)
    if (res?.data?.code === 200 && res?.data?.data) {
      const inner = res.data.data
      const raw = inner.data || inner.list || (Array.isArray(inner) ? inner : [])
      list.value = raw.map(s => ({ id: s.id ?? s.Id, name: s.name ?? s.Name, host: s.host ?? s.Host, port: s.port ?? s.Port, note: s.note ?? s.Note }))
      total.value = inner.all_count ?? inner.total ?? list.value.length
    }
  } catch { /* */ } finally { loading.value = false }
}

const handleSearch = () => { page.value = 1; loadList() }
const handleRefresh = () => { keyword.value = ''; page.value = 1; loadList() }
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>
