<template>
  <div>
    <h2>租户管理</h2>
    <el-form :model="form" inline>
      <el-form-item label="租户编码">
        <el-input v-model="form.tenantCode" placeholder="租户编码" />
      </el-form-item>
      <el-form-item label="租户名称">
        <el-input v-model="form.tenantName" placeholder="租户名称" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleCreate">创建</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tenants" border>
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="tenantCode" label="租户编码" />
      <el-table-column prop="tenantName" label="租户名称" />
      <el-table-column prop="status" label="状态" />
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { createTenant, listTenants } from '@/api/account'

const form = reactive({
  tenantCode: '',
  tenantName: '',
})

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

async function loadTenants() {
  try {
    const res: any = await listTenants()
    tenants.value = res.data || []
  } catch (e: any) {
    ElMessage.error(e.message)
  }
}

async function handleCreate() {
  try {
    await createTenant(form)
    ElMessage.success('创建成功')
    await loadTenants()
  } catch (e: any) {
    ElMessage.error(e.message)
  }
}

onMounted(loadTenants)
</script>
