<script setup lang="ts">
import { DIALOG_WIDTH } from "../../../constants/ui";

const visible = defineModel<boolean>({ required: true });
const laborAgreementExtractConfirmData = defineModel<any>("laborAgreementExtractConfirmData", { required: true });

const props = withDefaults(defineProps<{
  toYuan: (cent?: number) => string;
  applying?: boolean;
}>(), {
  applying: false,
});

defineEmits<{
  cancel: [];
  confirm: [];
}>();

const resolveLaborFeeText = () => {
  const cent = Number(laborAgreementExtractConfirmData.value?.parsedExpert?.laborFeeCent || 0);
  const raw = String(laborAgreementExtractConfirmData.value?.parsedExpert?.laborFeeText || "").trim();
  if (cent > 0) {
    return `${props.toYuan(cent)}元`;
  }
  return raw || "-";
};

const resolveExistingExpertSummary = () => {
  const existing = laborAgreementExtractConfirmData.value?.existingExpert || {};
  const parts = [
    String(existing.expertName || "").trim(),
    String(existing.hospitalName || "").trim(),
    String(existing.phoneMasked || "").trim(),
    String(existing.idNoMasked || "").trim(),
  ].filter(Boolean);
  return parts.join(" / ") || "-";
};

const hasExistingNameMismatch = () => {
  const parsedName = String(laborAgreementExtractConfirmData.value?.parsedExpert?.expertName || "").trim();
  const existingName = String(laborAgreementExtractConfirmData.value?.existingExpert?.expertName || "").trim();
  return !!parsedName && !!existingName && parsedName !== existingName;
};
</script>

<template>
  <el-dialog
    v-model="visible"
    title="劳务协议抽取结果确认"
    :width="DIALOG_WIDTH.md"
    :close-on-click-modal="false"
    :close-on-press-escape="!applying"
    :show-close="!applying"
  >
    <div class="lh-loose">
      <el-alert
        v-if="laborAgreementExtractConfirmData.needsConfirm"
        type="warning"
        :closable="false"
        show-icon
        title="系统已按身份证号匹配到已有专家，确认后将复用该专家并更新资料。"
      />
      <el-alert
        v-if="laborAgreementExtractConfirmData.nameMismatchFlag"
        type="warning"
        :closable="false"
        show-icon
        title="OCR识别到乙方与账户名不一致，系统允许保存并会自动打标，请人工复核。"
      />
      <div><strong>协议文件</strong>{{ laborAgreementExtractConfirmData.fileName || "-" }}</div>
      <div>
        <strong>导入方式</strong>
        <el-tag size="small" :type="laborAgreementExtractConfirmData.needsConfirm ? 'warning' : 'success'">
          {{ laborAgreementExtractConfirmData.needsConfirm ? "复用已有专家并更新资料" : "新建专家并绑定会议" }}
        </el-tag>
      </div>
      <div v-if="laborAgreementExtractConfirmData.needsConfirm">
        <strong>系统匹配专家</strong>{{ resolveExistingExpertSummary() }}
      </div>
      <div v-if="laborAgreementExtractConfirmData.needsConfirm">
        <strong>姓名比对</strong>
        <el-tag size="small" :type="hasExistingNameMismatch() ? 'danger' : 'success'">
          {{ hasExistingNameMismatch() ? "OCR姓名与系统专家不一致" : "OCR姓名与系统专家一致" }}
        </el-tag>
      </div>
      <div><strong>乙方</strong>{{ laborAgreementExtractConfirmData.parsedExpert?.expertName || "-" }}</div>
      <div><strong>账户名</strong>{{ laborAgreementExtractConfirmData.parsedExpert?.accountName || "-" }}</div>
      <div>
        <strong>乙方/账户名</strong>
        <el-tag size="small" :type="laborAgreementExtractConfirmData.nameMismatchFlag ? 'danger' : 'success'">
          {{ laborAgreementExtractConfirmData.nameMismatchFlag ? "不一致" : "一致" }}
        </el-tag>
      </div>
      <div><strong>工作单位</strong>{{ laborAgreementExtractConfirmData.parsedExpert?.hospitalName || "-" }}</div>
      <div><strong>联系电话</strong>{{ laborAgreementExtractConfirmData.parsedExpert?.phone || "-" }}</div>
      <div><strong>劳务费</strong>{{ resolveLaborFeeText() }}</div>
      <div><strong>开户银行</strong>{{ laborAgreementExtractConfirmData.parsedExpert?.bankName || "-" }}</div>
      <div><strong>开户帐号</strong>{{ laborAgreementExtractConfirmData.parsedExpert?.bankCardNo || "-" }}</div>
      <div><strong>身份证号码</strong>{{ laborAgreementExtractConfirmData.parsedExpert?.idNo || "-" }}</div>
    </div>
    <template #footer>
      <el-button :disabled="applying" @click="$emit('cancel')">取消</el-button>
      <el-button type="primary" :loading="applying" @click="$emit('confirm')">
        {{ laborAgreementExtractConfirmData.needsConfirm ? "确认复用并导入" : "确认导入" }}
      </el-button>
    </template>
  </el-dialog>
</template>
