<template>
  <div class="page-container">
    <div class="page-header">
      <h2 class="page-title">数据源管理</h2>
      <el-button v-if="userStore.isAdmin" type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>新增数据源
      </el-button>
    </div>

    <div class="search-bar card-shadow">
      <el-input
        v-model="keyword"
        placeholder="搜索数据源名称/主机"
        clearable
        style="width: 260px"
        @keyup.enter="handleSearch"
      >
        <template #append>
          <el-button @click="handleSearch">
            <el-icon><Search /></el-icon>
          </el-button>
        </template>
      </el-input>
    </div>

    <div class="table-card card-shadow">
      <el-table :data="tableData" v-loading="loading" stripe size="default">
        <el-table-column prop="name" label="名称" min-width="140" />
        <el-table-column prop="source_type" label="类型" width="100">
          <template #default="{ row }">
            <el-tag size="small">{{ typeText(row.source_type) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="host" label="主机" min-width="140" />
        <el-table-column prop="port" label="端口" width="80" />
        <el-table-column prop="database_name" label="数据库" min-width="120" />
        <el-table-column prop="username" label="用户名" min-width="100" />
        <el-table-column prop="status" label="状态" width="90">
          <template #default="{ row }">
            <el-tag :type="row.status === 'active' ? 'success' : 'danger'" size="small">
              {{ row.status === 'active' ? '正常' : '异常' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="260" fixed="right">
          <template #default="{ row }">
            <el-button v-if="userStore.isAdmin" type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button v-if="userStore.isAdmin" type="primary" link size="small" @click="handleSync(row)">同步元数据</el-button>
            <el-button v-if="userStore.isAdmin" type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-bar">
        <el-pagination
          v-model:current-page="page"
          v-model:page-size="pageSize"
          :total="total"
          layout="total, prev, pager, next"
          @change="fetchData"
        />
      </div>
    </div>

    <!-- Add/Edit Dialog -->
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit ? '编辑数据源' : '新增数据源'"
      width="560px"
      destroy-on-close
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入数据源名称" />
        </el-form-item>
        <el-form-item label="类型" prop="source_type">
          <el-select v-model="form.source_type" placeholder="选择数据库类型" style="width: 100%">
            <el-option label="PostgreSQL" value="postgresql" />
            <el-option label="MySQL" value="mysql" />
            <el-option label="Oracle" value="oracle" />
            <el-option label="SQL Server" value="sqlserver" />
            <el-option label="达梦(DM)" value="dm" />
          </el-select>
        </el-form-item>
        <el-form-item label="主机" prop="host">
          <el-input v-model="form.host" placeholder="IP或域名" />
        </el-form-item>
        <el-form-item label="端口" prop="port">
          <el-input-number v-model="form.port" :min="1" :max="65535" style="width: 100%" />
        </el-form-item>
        <el-form-item label="数据库" prop="database_name">
          <el-input v-model="form.database_name" placeholder="数据库名称" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="连接用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="连接密码" show-password />
        </el-form-item>
        <el-form-item label="扩展参数">
          <el-input v-model="form.extra_params" type="textarea" :rows="2" placeholder="JSON格式额外连接参数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button @click="handleTest">连接测试</el-button>
        <el-button type="primary" :loading="submitLoading" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Search } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { getDataSources, createDataSource, updateDataSource, deleteDataSource, testConnection, syncMetadata } from '@/api/datasource'
import type { DataSourceItem, DataSourceForm } from '@/api/datasource'

const userStore = useUserStore()
const loading = ref(false)
const tableData = ref<DataSourceItem[]>([])
const page = ref(1)
const pageSize = ref(20)
const total = ref(0)
const keyword = ref('')

const dialogVisible = ref(false)
const isEdit = ref(false)
const editId = ref<number | null>(null)
const submitLoading = ref(false)
const formRef = ref()

const form = reactive<DataSourceForm>({
  name: '',
  source_type: 'postgresql',
  host: '',
  port: 5432,
  database_name: '',
  username: '',
  password: '',
  extra_params: '',
})

const rules = {
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  source_type: [{ required: true, message: '请选择类型', trigger: 'change' }],
}

function typeText(type: string) {
  const map: Record<string, string> = {
    postgresql: 'PostgreSQL',
    mysql: 'MySQL',
    oracle: 'Oracle',
    sqlserver: 'SQL Server',
    dm: '达梦',
  }
  return map[type] || type
}

async function fetchData() {
  loading.value = true
  try {
    const res: any = await getDataSources({
      page: page.value,
      page_size: pageSize.value,
      keyword: keyword.value || undefined,
    })
    tableData.value = res.data || []
    total.value = res.total || 0
  } finally {
    loading.value = false
  }
}

function handleSearch() {
  page.value = 1
  fetchData()
}

function handleAdd() {
  isEdit.value = false
  editId.value = null
  form.name = ''
  form.source_type = 'postgresql'
  form.host = ''
  form.port = 5432
  form.database_name = ''
  form.username = ''
  form.password = ''
  form.extra_params = ''
  dialogVisible.value = true
}

function handleEdit(row: DataSourceItem) {
  isEdit.value = true
  editId.value = row.id
  form.name = row.name
  form.source_type = row.source_type
  form.host = row.host || ''
  form.port = row.port || 5432
  form.database_name = row.database_name || ''
  form.username = row.username || ''
  form.password = ''
  dialogVisible.value = true
}

async function handleTest() {
  try {
    const res: any = await testConnection({ ...form })
    if (res.data?.success) {
      ElMessage.success(res.data?.message)
    } else {
      ElMessage.error(res.data?.message || '测试失败')
    }
  } catch (e: any) {
    ElMessage.error(e?.message || '测试失败')
  }
}

async function handleSubmit() {
  const valid = await formRef.value?.validate().catch(() => false)
  if (!valid) return

  submitLoading.value = true
  try {
    if (isEdit.value && editId.value) {
      await updateDataSource(editId.value, { ...form })
      ElMessage.success('更新成功')
    } else {
      await createDataSource({ ...form })
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    fetchData()
  } catch (e: any) {
    ElMessage.error(e?.message || '操作失败')
  } finally {
    submitLoading.value = false
  }
}

async function handleDelete(row: DataSourceItem) {
  try {
    await ElMessageBox.confirm('确认删除该数据源？相关元数据也将被删除', '提示', { type: 'warning' })
    await deleteDataSource(row.id)
    ElMessage.success('删除成功')
    fetchData()
  } catch (e: any) {
    if (e !== 'cancel') ElMessage.error(e?.message || '删除失败')
  }
}

async function handleSync(row: DataSourceItem) {
  try {
    await ElMessageBox.confirm('确认同步该数据源的元数据？', '提示', { type: 'info' })
    const res: any = await syncMetadata(row.id)
    if (res.data?.success) {
      ElMessage.success(res.data?.message)
    } else {
      ElMessage.error(res.data?.message || '同步失败')
    }
  } catch (e: any) {
    if (e !== 'cancel') ElMessage.error(e?.message || '同步失败')
  }
}

onMounted(fetchData)
</script>

<style scoped lang="scss">
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;

  .page-title {
    font-size: 20px;
    font-weight: 600;
    color: #303133;
  }
}

.search-bar {
  padding: 16px;
  margin-bottom: 16px;
}

.table-card {
  padding: 16px;
}

.pagination-bar {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}
</style>
