<template>
  <div class="container-box">
    <div class="header-bar">
      <h2>租户域名审核</h2>
      <el-button @click="fetchData" :loading="loading">
        <el-icon><Refresh /></el-icon>
        刷新
      </el-button>
    </div>

    <el-divider></el-divider>

    <!-- 搜索区域 -->
    <div class="search-box">
      <el-input
        v-model="searchForm.sub_domain"
        placeholder="搜索二级域名"
        clearable
        style="width: 200px; margin-right: 10px;"
        @keyup.enter="handleSearch"
      />
      <el-select v-model="searchForm.status" placeholder="状态" clearable style="width: 120px; margin-right: 10px;">
        <el-option label="审核中" :value="0" />
        <el-option label="已生效" :value="1" />
        <el-option label="已禁用" :value="2" />
      </el-select>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      v-loading="loading"
      element-loading-text="正在加载..."
    >
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column prop="tenant_name" label="租户名称" min-width="150" />
      <el-table-column prop="sub_domain" label="二级域名前缀" width="150" />
      <el-table-column prop="main_domain" label="主域名" min-width="150" />
      <el-table-column prop="full_domain" label="完整域名" min-width="200" />
      <el-table-column prop="status" label="状态" width="100" align="center">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0" type="warning">审核中</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">已生效</el-tag>
          <el-tag v-else type="danger">已禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="申请时间" width="180" />
      <el-table-column label="操作" width="200" fixed="right" align="center">
        <template #default="scope">
          <template v-if="scope.row.status === 0">
            <el-button size="small" type="success" @click="handleAudit(scope.row, 'approve')">
              通过
            </el-button>
            <el-button size="small" type="danger" @click="handleAudit(scope.row, 'reject')">
              拒绝
            </el-button>
          </template>
          <template v-else-if="scope.row.status === 1">
            <el-button size="small" text type="danger" @click="handleDisable(scope.row)">
              禁用
            </el-button>
          </template>
          <span v-else style="color: #999;">-</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-wrap">
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.pageSize"
        :total="pagination.total"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import { getTenantDomainList, auditTenantDomain, toggleTenantDomainStatus } from '@/api/domain'

const loading = ref(false)

const searchForm = reactive({
  sub_domain: '',
  status: ''
})

const pagination = reactive({
  page: 1,
  pageSize: 10,
  total: 0
})

const tableData = ref<any[]>([])

const fetchData = async () => {
  loading.value = true
  try {
    const res = await getTenantDomainList({
      page: pagination.page,
      pageSize: pagination.pageSize,
      ...searchForm
    })
    if (res.code === 200) {
      tableData.value = res.data.list || []
      pagination.total = res.data.total || 0
    }
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  pagination.page = 1
  fetchData()
}

const handleAudit = async (row: any, action: string) => {
  const msg = action === 'approve' ? '确定要通过该域名申请吗？' : '确定要拒绝该域名申请吗？'
  await ElMessageBox.confirm(msg, '提示', { type: 'warning' })
  
  const res = await auditTenantDomain({ id: row.id, action })
  if (res.code === 200) {
    ElMessage.success(res.msg)
    fetchData()
  } else {
    ElMessage.error(res.msg || '操作失败')
  }
}

const handleDisable = async (row: any) => {
  await ElMessageBox.confirm('确定要禁用该域名吗？', '提示', { type: 'warning' })
  
  const res = await toggleTenantDomainStatus(row.id)
  if (res.code === 200) {
    ElMessage.success(res.msg)
    fetchData()
  } else {
    ElMessage.error(res.msg || '操作失败')
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style lang="less" scoped>
.container-box {
  padding: 20px;
}

.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;

  h2 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
  }
}

.search-box {
  margin-bottom: 20px;
}

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