<template>
  <Dialog v-bind="$attrs" :options="{ title: '实名认证信息', size: '3xl' }" v-model="show">
    <template #body>
      <div v-if="loading" class="flex justify-center items-center py-12">
        <div class="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
        <span class="ml-3 text-gray-600">加载中...</span>
      </div>
      
      <div v-else-if="error" class="text-center py-12">
        <div class="text-red-500 mb-4">
          <AlertTriangleIcon class="h-12 w-12 mx-auto" />
        </div>
        <p class="text-gray-600">{{ error }}</p>
      </div>
      
      <div v-else-if="realNameData" class="space-y-6">


        <!-- 认证状态 -->
        <div class="bg-gradient-to-r from-green-50 to-emerald-50 rounded-xl p-6 border border-green-200">
          <div class="flex items-center">
            <div class="flex-shrink-0">
              <CheckCircleIcon class="h-8 w-8 text-green-500" />
            </div>
            <div class="ml-4">
              <h3 class="text-lg font-medium text-green-900">认证已通过</h3>
              <p class="text-sm text-green-700">该域名已完成实名认证</p>
            </div>
          </div>
        </div>

        <!-- 基本信息 -->
        <div class="bg-white rounded-xl border border-gray-200 shadow-sm">
          <div class="px-6 py-4 border-b border-gray-100">
            <h3 class="text-lg font-semibold text-gray-900 flex items-center">
              <FileTextIcon class="h-5 w-5 mr-2 text-blue-600" />
              域名基本信息
            </h3>
          </div>
          <div class="p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
              <div class="space-y-4">
                <div>
                  <dt class="text-sm font-medium text-gray-600">域名</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.domain || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">注册时间</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ formatDateTime(realNameData.regdate) || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">到期时间</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ formatDateTime(realNameData.rexpiredate) || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">域名状态</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ getDomainStatusText(realNameData.status) || '未填写' }}</dd>
                </div>
              </div>
              <div class="space-y-4">
                <div>
                  <dt class="text-sm font-medium text-gray-600">实名状态</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ getRealNameStatusText(realNameData.real_name_status?.r_status) || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">注册类型</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ getRegTypeText(realNameData.c_regtype) || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">模板ID</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.c_sysid || '未填写' }}</dd>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 所有者信息 -->
        <div class="bg-white rounded-xl border border-gray-200 shadow-sm">
          <div class="px-6 py-4 border-b border-gray-100">
            <h3 class="text-lg font-semibold text-gray-900 flex items-center">
              <UserIcon class="h-5 w-5 mr-2 text-blue-600" />
              所有者信息
            </h3>
          </div>
          <div class="p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
              <div class="space-y-4">
                <div>
                  <dt class="text-sm font-medium text-gray-600">中文姓名</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ getFullChineseName() || '未填写' }}</dd>
                </div>

                <div v-if="realNameData.owner?.dom_org_m">
                  <dt class="text-sm font-medium text-gray-600">中文单位</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner.dom_org_m || '未填写' }}</dd>
                </div>

                <div>
                  <dt class="text-sm font-medium text-gray-600">邮箱</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_em || '未填写' }}</dd>
                </div>
              </div>
              <div class="space-y-4">
                <div>
                  <dt class="text-sm font-medium text-gray-600">电话</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_ph || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">传真</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_fax || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">国家/地区</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_co || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">省份</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_st_m || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">城市</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_ct_m || '未填写' }}</dd>
                </div>
              </div>
            </div>
            <div class="mt-4">
              <div>
                <dt class="text-sm font-medium text-gray-600">地址</dt>
                <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_adr_m || '未填写' }}</dd>
              </div>
              <div>
                <dt class="text-sm font-medium text-gray-600">邮编</dt>
                <dd class="mt-1 text-sm text-gray-900">{{ realNameData.owner?.dom_pc || '未填写' }}</dd>
              </div>
            </div>
          </div>
        </div>

        <!-- 实名证件信息 -->
        <div v-if="realNameData.orgfile" class="bg-white rounded-xl border border-gray-200 shadow-sm">
          <div class="px-6 py-4 border-b border-gray-100">
            <h3 class="text-lg font-semibold text-gray-900 flex items-center">
              <FileTextIcon class="h-5 w-5 mr-2 text-blue-600" />
              实名证件信息
            </h3>
          </div>
          <div class="p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
              <div class="space-y-4">
                <div>
                  <dt class="text-sm font-medium text-gray-600">实名证件类型</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ getDocumentTypeText(realNameData.orgfile.f_type) || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">实名证件号码</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.orgfile.f_code || '未填写' }}</dd>
                </div>
              </div>
              <div class="space-y-4">
                <div>
                  <dt class="text-sm font-medium text-gray-600">证件上传时间</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ formatDateTime(realNameData.orgfile.f_date) || '未填写' }}</dd>
                </div>
                <div>
                  <dt class="text-sm font-medium text-gray-600">证件ID</dt>
                  <dd class="mt-1 text-sm text-gray-900">{{ realNameData.orgfile.f_sysid || '未填写' }}</dd>
                </div>
              </div>
            </div>
          </div>
        </div>


      </div>
    </template>
  </Dialog>
</template>

<script>
import { Dialog, createResource } from 'jingrow-ui';
import CheckCircleIcon from '~icons/lucide/check-circle';
import AlertTriangleIcon from '~icons/lucide/alert-triangle';
import FileTextIcon from '~icons/lucide/file-text';
import UserIcon from '~icons/lucide/user';





export default {
  name: 'JsiteDomainRealNameInfoDialog',
  components: {
    Dialog,
    CheckCircleIcon,
    AlertTriangleIcon,
    FileTextIcon,
    UserIcon
  },
  props: {
    domain: String,
    domainDoc: Object
  },
  data() {
    return {
      show: true,
      loading: false,
      error: null,
      realNameData: null
    };
  },
  methods: {
    async loadRealNameInfo() {
      console.log('loadRealNameInfo called, domainDoc:', this.domainDoc);
      
      if (!this.domainDoc?.domain) {
        console.error('域名信息不存在, domainDoc:', this.domainDoc);
        this.error = '域名信息不存在';
        return;
      }

      this.loading = true;
      this.error = null;

      try {
        const getRealNameRequest = createResource({
          url: 'jcloud.api.domain_west.get_west_domain_real_info',
          params: {
            domain: this.domainDoc.domain
          },
          onSuccess: (response) => {
            console.log('API响应:', response);
            
            // 修复API响应处理逻辑
            let data = null;
            if (response && response.status === "success" && response.data) {
              data = response.data;
            } else if (response && response.message && response.message.status === "success" && response.message.data) {
              data = response.message.data;
            }
            
            if (data) {
              console.log('提取的数据:', data);
              this.realNameData = data;
            } else {
              console.error('无效的响应格式:', response);
              this.error = '获取实名认证信息失败';
            }
            
            this.loading = false;
          },
          onError: (error) => {
            console.error('获取域名实名信息失败:', error);
            this.error = '获取实名认证信息失败';
            this.loading = false;
          }
        });
        getRealNameRequest.submit();
      } catch (error) {
        console.error('获取域名实名信息失败:', error);
        this.error = '获取实名认证信息失败';
        this.loading = false;
      }
    },
    formatDateTime(dateStr) {
      if (!dateStr) return '未知';
      try {
        const date = new Date(dateStr);
        return date.toLocaleString('zh-CN', {
          year: 'numeric',
          month: '2-digit',
          day: '2-digit',
          hour: '2-digit',
          minute: '2-digit',
          second: '2-digit'
        });
      } catch {
        return dateStr;
      }
    },
    getDomainStatusText(status) {
      const statusMap = {
        'ok': '正常',
        'pending': '待处理',
        'error': '错误'
      };
      return statusMap[status] || status || '未知';
    },
    getRealNameStatusText(status) {
      const statusMap = {
        1: '实名成功',
        2: '实名中',
        3: '实名失败',
        0: '未实名'
      };
      return statusMap[status] || status || '未知';
    },
    getDocumentTypeText(type) {
      const typeMap = {
        1: '身份证',
        2: '护照',
        3: '工商营业执照(填统一社会信用代码)',
        4: '组织机构代码证',
        5: '其他'
      };
      return typeMap[type] || type || '未知';
    },
    getRegTypeText(regType) {
      const typeMap = {
        'I': '个人',
        'E': '企业'
      };
      return typeMap[regType] || regType || '未知';
    },
    getFullChineseName() {
      if (!this.realNameData?.owner) return '未填写';
      const lastName = this.realNameData.owner.dom_ln_m || '';
      const firstName = this.realNameData.owner.dom_fn_m || '';
      return lastName + firstName || '未填写';
    }

  },
  mounted() {
    this.loadRealNameInfo();
  }
};
</script> 