<template>
  <el-dialog
    title="项目分析"
    :visible.sync="visibleProxy"
    width="80%"
    top="5vh"
    append-to-body
    custom-class="project-analysis-dialog"
    @close="handleDialogClosed"
  >
    <div class="project-analysis-header">
      <div class="project-analysis-header__main">
        <div class="project-analysis-header__title-row">
          <div class="project-analysis-header__title">{{ dialogData.sidebar.basicInfo.name || "-" }}</div>
          <span class="project-analysis-header__risk">{{ dialogData.sidebar.basicInfo.riskLevel || "-" }}</span>
        </div>
        <div class="project-analysis-header__meta">
          <span>工号 {{ dialogData.sidebar.basicInfo.staffCode || "-" }}</span>
          <span>部门 {{ dialogData.sidebar.basicInfo.department || "-" }}</span>
          <span>所属项目 {{ dialogData.sidebar.basicInfo.projectName || "-" }}</span>
        </div>
      </div>
      <div
        v-if='dialogData.sourceSummary.showCurrentModel && source === "riskModelPeople"'
        class="project-analysis-header__context"
      >
        <span class="project-analysis-header__context-label">当前命中模型</span>
        <span class="project-analysis-header__context-value">{{ dialogData.sourceSummary.currentModelValue }}</span>
      </div>
    </div>
    <div class="project-analysis-overview">
      <div class="project-analysis-overview__item">
        <span class="project-analysis-overview__label">风险等级</span>
        <strong class="project-analysis-overview__value">{{ dialogData.sidebar.basicInfo.riskLevel || "-" }}</strong>
      </div>
      <div class="project-analysis-overview__item">
        <span class="project-analysis-overview__label">命中模型数</span>
        <strong class="project-analysis-overview__value">{{ dialogData.sidebar.modelSummary.modelCount || "-" }}</strong>
      </div>
      <div class="project-analysis-overview__item">
        <span class="project-analysis-overview__label">核心异常标签</span>
        <strong class="project-analysis-overview__value">{{ dialogData.sidebar.modelSummary.riskTags.length || 0 }}</strong>
      </div>
    </div>
    <div class="project-analysis-layout">
      <project-analysis-sidebar
        class="project-analysis-layout__sidebar"
        :sidebar-data="dialogData.sidebar"
      />
      <div v-loading="detailLoading" class="project-analysis-layout__main">
        <div class="project-analysis-layout__main-scroll">
          <el-alert
            v-if="detailError"
            :closable="false"
            class="project-analysis-layout__alert"
            type="error"
            show-icon
            :title="detailError"
          >
            <template slot="default">
              <el-button type="text" size="mini" @click="handleRetryDetail">重试</el-button>
            </template>
          </el-alert>
          <el-tabs v-model="activeTab" stretch>
            <el-tab-pane label="异常明细" name="abnormalDetail">
              <project-analysis-abnormal-tab :detail-data="dialogData.abnormalDetail" />
            </el-tab-pane>
            <el-tab-pane label="资产分析" name="assetAnalysis">
              <project-analysis-placeholder-tab :tab-data="getTabData('assetAnalysis')" />
            </el-tab-pane>
            <el-tab-pane label="征信摘要" name="creditSummary">
              <project-analysis-placeholder-tab :tab-data="getTabData('creditSummary')" />
            </el-tab-pane>
            <el-tab-pane label="关系图谱" name="relationshipGraph">
              <project-analysis-placeholder-tab :tab-data="getTabData('relationshipGraph')" />
            </el-tab-pane>
            <el-tab-pane label="资金流向" name="fundFlow">
              <project-analysis-placeholder-tab :tab-data="getTabData('fundFlow')" />
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getOverviewPersonAnalysisDetail } from "@/api/ccdi/projectOverview";
import ProjectAnalysisAbnormalTab from "./ProjectAnalysisAbnormalTab";
import ProjectAnalysisPlaceholderTab from "./ProjectAnalysisPlaceholderTab";
import ProjectAnalysisSidebar from "./ProjectAnalysisSidebar";
import { buildProjectAnalysisDialogData } from "./preliminaryCheck.mock";

export default {
  name: "ProjectAnalysisDialog",
  components: {
    ProjectAnalysisAbnormalTab,
    ProjectAnalysisPlaceholderTab,
    ProjectAnalysisSidebar,
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    projectId: {
      type: [String, Number],
      default: null,
    },
    person: {
      type: Object,
      default: () => null,
    },
    source: {
      type: String,
      default: "riskPeople",
    },
    modelSummary: {
      type: Object,
      default: () => ({
        modelCount: 0,
        riskTags: [],
      }),
    },
    projectName: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      activeTab: "abnormalDetail",
      detailLoading: false,
      detailError: "",
      detailData: null,
    };
  },
  computed: {
    dialogData() {
      const mockData = buildProjectAnalysisDialogData({
        person: this.person,
        source: this.source,
        projectName: this.projectName,
      });
      return {
        ...mockData,
        sidebar: {
          ...mockData.sidebar,
          basicInfo: (this.detailData && this.detailData.basicInfo) || mockData.sidebar.basicInfo,
          modelSummary: {
            ...mockData.sidebar.modelSummary,
            ...(this.modelSummary || {}),
          },
        },
        abnormalDetail: (this.detailData && this.detailData.abnormalDetail) || mockData.abnormalDetail,
      };
    },
    visibleProxy: {
      get() {
        return this.visible;
      },
      set(value) {
        this.$emit("update:visible", value);
      },
    },
  },
  watch: {
    visible(value) {
      if (value) {
        this.resetDialogState();
        this.fetchDetailData();
      }
    },
  },
  methods: {
    resetDialogState() {
      this.activeTab = "abnormalDetail";
      this.detailLoading = false;
      this.detailError = "";
      this.detailData = null;
    },
    resolveStaffIdCard() {
      return (this.modelSummary && this.modelSummary.staffIdCard)
        || (this.person && (this.person.idNo || this.person.staffIdCard))
        || "";
    },
    async fetchDetailData() {
      const staffIdCard = this.resolveStaffIdCard();
      if (!this.projectId || !staffIdCard) {
        return;
      }
      this.detailLoading = true;
      this.detailError = "";
      try {
        const response = await getOverviewPersonAnalysisDetail({
          projectId: this.projectId,
          staffIdCard,
        });
        this.detailData = (response && response.data) || null;
      } catch (error) {
        this.detailError = "项目分析详情加载失败，请稍后重试";
        this.detailData = null;
        console.error("加载项目分析详情失败", error);
      } finally {
        this.detailLoading = false;
      }
    },
    handleRetryDetail() {
      this.fetchDetailData();
    },
    getTabData(tabKey) {
      return (
        this.dialogData.tabs.find((item) => item.key === tabKey) || {
          label: "",
          description: "",
        }
      );
    },
    handleDialogClosed() {
      this.resetDialogState();
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.project-analysis-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  padding: 24px;
  border: 1px solid #e2e8f0;
  border-radius: 20px;
  background: linear-gradient(135deg, #fffdf7 0%, #ffffff 60%, #f8fafc 100%);
}

.project-analysis-header__main {
  min-width: 0;
  flex: 1;
}

.project-analysis-header__title-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.project-analysis-header__title {
  font-size: 24px;
  font-weight: 600;
  line-height: 1.2;
  color: #0f172a;
}

.project-analysis-header__risk {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 999px;
  background: #fee2e2;
  font-size: 13px;
  font-weight: 600;
  color: #b91c1c;
}

.project-analysis-header__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  margin-top: 14px;
  font-size: 13px;
  color: #475569;
}

.project-analysis-header__context {
  display: flex;
  min-width: 220px;
  flex-direction: column;
  gap: 8px;
  padding: 16px 18px;
  border-radius: 16px;
  background: #fff7ed;
}

.project-analysis-header__context-label {
  font-size: 12px;
  color: #9a3412;
}

.project-analysis-header__context-value {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.6;
  color: #7c2d12;
}

.project-analysis-overview {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.project-analysis-overview__item {
  padding: 18px 20px;
  border: 1px solid #e2e8f0;
  border-radius: 18px;
  background: #ffffff;
}

.project-analysis-overview__label {
  display: block;
  font-size: 12px;
  color: #64748b;
}

.project-analysis-overview__value {
  display: block;
  margin-top: 8px;
  font-size: 18px;
  color: #0f172a;
}

.project-analysis-layout {
  display: flex;
  gap: 20px;
  margin-top: 16px;
  min-height: 640px;
  max-height: calc(90vh - 120px);
}

.project-analysis-layout__sidebar {
  flex: 0 0 340px;
  padding: 0;
}

.project-analysis-layout__main {
  flex: 1;
  min-width: 0;
  padding: 0;
}

.project-analysis-layout__main-scroll {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}

.project-analysis-layout__alert {
  margin-bottom: 16px;
}
</style>
