<template>
  <div class="tenant-detail" v-loading="loading">
    <el-card shadow="hover">
      <template #header>
        <div class="card-header">
          <span>租户详情</span>
          <div>
            <el-button @click="$router.back()">返回</el-button>
            <el-button type="primary" @click="isEditing = !isEditing">
              {{ isEditing ? '取消编辑' : '编辑' }}
            </el-button>
          </div>
        </div>
      </template>
      
      <el-form 
        ref="formRef" 
        :model="form" 
        :rules="rules" 
        label-width="120px"
        :disabled="!isEditing"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="租户编码">
              <el-input v-model="form.code" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-tag :type="form.status === 'active' ? 'success' : 'danger'">
                {{ form.status === 'active' ? '启用' : '禁用' }}
              </el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="租户名称" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示名称" prop="display_name">
              <el-input v-model="form.display_name" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="域名" prop="domain">
              <el-input v-model="form.domain" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业" prop="industry">
              <el-select v-model="form.industry" style="width: 100%;">
                <el-option label="轻医美" value="medical_beauty" />
                <el-option label="宠物" value="pet" />
                <el-option label="教育" value="education" />
                <el-option label="其他" value="other" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact_name">
              <el-input v-model="form.contact_name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contact_phone">
              <el-input v-model="form.contact_phone" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系邮箱" prop="contact_email">
              <el-input v-model="form.contact_email" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务到期">
              <el-date-picker v-model="form.expire_at" type="date" style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" :rows="3" />
        </el-form-item>
        
        <el-form-item v-if="isEditing">
          <el-button type="primary" :loading="submitting" @click="handleSave">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <!-- 快捷操作 -->
    <el-card shadow="hover" style="margin-top: 20px;">
      <template #header>
        <span>快捷操作</span>
      </template>
      
      <div class="quick-actions">
        <el-button @click="$router.push(`/tenants/${tenantId}/configs`)">
          <el-icon><Setting /></el-icon>
          配置管理
        </el-button>
        <el-button @click="$router.push(`/tenants/${tenantId}/features`)">
          <el-icon><Switch /></el-icon>
          功能开关
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Setting, Switch } from '@element-plus/icons-vue'
import api from '@/api'

const route = useRoute()
const tenantId = route.params.id

const loading = ref(false)
const isEditing = ref(false)
const submitting = ref(false)
const formRef = ref()

const form = reactive({
  code: '',
  name: '',
  display_name: '',
  domain: '',
  industry: '',
  contact_name: '',
  contact_phone: '',
  contact_email: '',
  expire_at: null,
  remarks: '',
  status: ''
})

const rules = {
  name: [{ required: true, message: '请输入租户名称', trigger: 'blur' }],
  domain: [{ required: true, message: '请输入域名', trigger: 'blur' }]
}

async function fetchTenant() {
  loading.value = true
  try {
    const res = await api.tenants.get(tenantId)
    Object.assign(form, res)
  } finally {
    loading.value = false
  }
}

async function handleSave() {
  await formRef.value.validate()
  
  submitting.value = true
  try {
    await api.tenants.update(tenantId, {
      name: form.name,
      display_name: form.display_name,
      domain: form.domain,
      industry: form.industry,
      contact_name: form.contact_name,
      contact_phone: form.contact_phone,
      contact_email: form.contact_email,
      expire_at: form.expire_at,
      remarks: form.remarks
    })
    ElMessage.success('保存成功')
    isEditing.value = false
  } finally {
    submitting.value = false
  }
}

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

<style lang="scss" scoped>
.tenant-detail {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .quick-actions {
    display: flex;
    gap: 12px;
    
    .el-button {
      display: flex;
      align-items: center;
      gap: 8px;
    }
  }
}
</style>

