<template>
  <div class="app-container dpc-detail-container">
    <!-- 原页面头部 (已隐藏，使用UploadData组件的头部) -->
    <div class="detail-header">
      <div class="header-left">
        <el-button size="small" icon="el-icon-back" @click="handleBack"
          >返回</el-button
        >
        <div class="title-section">
          <div class="page-title">
            <h2>
              {{ projectInfo.projectName }}
            </h2>
            <el-tag
              :type="getStatusType(projectInfo.projectStatus)"
              size="small"
            >
              {{ getStatusLabel(projectInfo.projectStatus) }}
            </el-tag>
            <!-- 配置类型标签 -->
            <el-tag
              :type="getConfigTypeStyle(projectInfo.configType)"
              size="small"
              style="margin-left: 8px"
            >
              {{ getConfigTypeLabel(projectInfo.configType) }}
            </el-tag>
          </div>

          <p class="update-time">
            最后更新时间：{{ formatUpdateTime(projectInfo.updateTime) }}
          </p>
        </div>
      </div>
      <div class="header-right">
        <el-button
          class="evidence-entry-btn"
          size="mini"
          plain
          icon="el-icon-collection-tag"
          @click="evidenceDrawerVisible = true"
        >
          证据线索
        </el-button>
        <el-menu
          :default-active="activeTab"
          mode="horizontal"
          @select="handleMenuSelect"
          class="nav-menu"
        >
          <el-menu-item index="upload" :disabled="isArchiveLockedTab('upload')">上传数据</el-menu-item>
          <el-menu-item index="config" :disabled="isArchiveLockedTab('config')">参数配置</el-menu-item>
          <el-menu-item index="overview">结果总览</el-menu-item>
          <el-menu-item index="special">专项排查</el-menu-item>
          <el-menu-item index="detail">流水明细查询</el-menu-item>
        </el-menu>
      </div>
    </div>

    <div
      v-if="isProjectTagFailed && projectInfo.latestTagTaskErrorMessage"
      class="tag-failure-alert"
    >
      <div class="tag-failure-alert__content">
        <i class="el-icon-warning-outline"></i>
        <div>
          <div class="tag-failure-alert__title">项目打标失败</div>
          <div class="tag-failure-alert__message">
            {{ tagFailureSummary }}
          </div>
        </div>
      </div>
      <el-button type="text" size="mini" @click="openTagFailureDialog">
        查看完整错误
      </el-button>
    </div>

    <!-- 动态组件渲染区域 -->
    <component
      :is="currentComponent"
      :project-id="projectId"
      :project-info="projectInfo"
      :can-operate="canOperate"
      @menu-change="handleMenuChange"
      @refresh-project="handleRefreshProject"
      @data-uploaded="handleDataUploaded"
      @name-selected="handleNameSelected"
      @generate-report="handleGenerateReport"
      @fetch-bank-info="handleFetchBankInfo"
      @evidence-confirm="handleEvidenceConfirm"
      @open-detail-query="handleOpenDetailQuery"
      :detail-query-prefill="detailQueryPrefill"
    />

    <evidence-confirm-dialog
      :visible.sync="evidenceConfirmVisible"
      :payload="evidencePayload"
      :can-operate="canOperate"
      @saved="handleEvidenceSaved"
    />
    <evidence-drawer
      ref="evidenceDrawer"
      :visible.sync="evidenceDrawerVisible"
      :project-id="projectId"
    />
    <el-dialog
      title="打标失败详情"
      :visible.sync="tagFailureDialogVisible"
      width="720px"
      append-to-body
    >
      <div
        v-if="projectInfo.latestTagTaskEndTime"
        class="tag-failure-dialog__time"
      >
        失败时间：{{ formatUpdateTime(projectInfo.latestTagTaskEndTime) }}
      </div>
      <pre class="tag-failure-dialog__message">{{ projectInfo.latestTagTaskErrorMessage }}</pre>
    </el-dialog>
  </div>
</template>

<script>
import UploadData from "./components/detail/UploadData";
import ParamConfig from "./components/detail/ParamConfig";
import PreliminaryCheck from "./components/detail/PreliminaryCheck";
import SpecialCheck from "./components/detail/SpecialCheck";
import DetailQuery from "./components/detail/DetailQuery";
import EvidenceConfirmDialog from "./components/detail/EvidenceConfirmDialog";
import EvidenceDrawer from "./components/detail/EvidenceDrawer";
import { getProject } from "@/api/ccdiProject";

export default {
  name: "ProjectDetail",
  components: {
    UploadData,
    ParamConfig,
    PreliminaryCheck,
    SpecialCheck,
    DetailQuery,
    EvidenceConfirmDialog,
    EvidenceDrawer,
  },
  data() {
    return {
      // 当前激活的菜单项索引
      activeTab: "upload",
      // 当前显示的组件名称
      currentComponent: "UploadData",
      // 项目ID
      projectId: this.$route.params.projectId,
      // 项目信息
      projectInfo: {
        projectId: this.$route.params.projectId,
        projectName: "",
        projectDesc: "",
        createTime: "",
        updateTime: "",
        startDate: "",
        endDate: "",
        targetCount: 0,
        warningCount: 0,
        warningThreshold: 60,
        projectStatus: "0",
        latestTagTaskErrorMessage: "",
        latestTagTaskEndTime: "",
        canOperate: true,
      },
      evidenceConfirmVisible: false,
      evidenceDrawerVisible: false,
      evidencePayload: {},
      detailQueryPrefill: null,
      tagFailureDialogVisible: false,
      projectStatusPollingTimer: null,
      projectStatusPollingInterval: 1000,
      projectStatusPollingLoading: false,
    };
  },
  computed: {
    isProjectArchived() {
      return String(this.projectInfo.projectStatus) === "2";
    },
    canOperate() {
      return this.projectInfo.canOperate !== false;
    },
    isProjectTagFailed() {
      return String(this.projectInfo.projectStatus) === "4";
    },
    tagFailureSummary() {
      const message = this.projectInfo.latestTagTaskErrorMessage || "";
      return message.length > 120 ? `${message.slice(0, 120)}...` : message;
    },
  },
  watch: {
    "$route.params.projectId"(newId) {
      this.stopProjectStatusPolling();
      this.projectStatusPollingLoading = false;
      if (newId) {
        this.projectId = newId;
        this.projectInfo.projectId = newId;
        this.initActiveTabFromRoute();
        this.initPageData();
      }
    },
    "$route.query.tab"() {
      this.initActiveTabFromRoute();
    },
    "projectInfo.projectStatus"() {
      this.syncAccessibleTab();
    },
    canOperate() {
      this.syncAccessibleTab();
    },
  },
  created() {
    // 初始化页面数据
    this.initActiveTabFromRoute();
    this.initPageData();
    this.$root.$on("ccdi-evidence-confirm", this.handleEvidenceConfirm);
  },
  beforeDestroy() {
    this.$root.$off("ccdi-evidence-confirm", this.handleEvidenceConfirm);
    this.stopProjectStatusPolling();
  },
  methods: {
    syncAccessibleTab() {
      this.syncProjectStatusPolling();
      const accessibleTab = this.resolveAccessibleTab(this.activeTab);
      if (accessibleTab !== this.activeTab) {
        this.setActiveTab(accessibleTab);
        this.syncRouteTab(accessibleTab);
      }
    },
    initActiveTabFromRoute() {
      const tab = (this.$route.query && this.$route.query.tab) || "";
      const validTabs = ["upload", "config", "overview", "special", "detail"];
      const targetTab = validTabs.includes(tab) ? tab : "upload";
      const accessibleTab = this.resolveAccessibleTab(targetTab);
      this.setActiveTab(accessibleTab);
      if (accessibleTab !== targetTab) {
        this.syncRouteTab(accessibleTab);
      }
    },
    isArchiveLockedTab(tab) {
      return (this.isProjectArchived || !this.canOperate) && ["upload", "config"].includes(tab);
    },
    resolveAccessibleTab(tab) {
      if (this.isArchiveLockedTab(tab)) {
        return "overview";
      }
      return tab;
    },
    setActiveTab(index) {
      this.activeTab = index;
      const componentMap = {
        upload: "UploadData",
        config: "ParamConfig",
        overview: "PreliminaryCheck",
        special: "SpecialCheck",
        detail: "DetailQuery",
      };
      this.currentComponent = componentMap[index] || "UploadData";
    },
    syncRouteTab(tab) {
      const currentTab = (this.$route.query && this.$route.query.tab) || "";
      if (currentTab === tab) {
        return;
      }
      this.$router.replace({
        path: this.$route.path,
        query: {
          ...this.$route.query,
          tab,
        },
      });
    },
    /** 初始化页面数据 */
    initPageData() {
      return this.fetchProjectDetail();
    },
    async fetchProjectDetail(options = {}) {
      const { silent = false } = options;
      if (!this.projectId) {
        return null;
      }
      if (!silent) {
        this.projectInfo.projectName = "";
        this.updatePageTitle();
      }

      try {
        const res = await getProject(this.projectId);
        const data = res.data || {};
        this.projectInfo = {
          ...this.projectInfo,
          ...data,
          projectId: data.projectId || this.projectId,
          projectName: data.projectName || "",
          projectDesc: data.projectDesc || data.description || "",
          projectStatus: String(
            data.projectStatus !== undefined && data.projectStatus !== null
              ? data.projectStatus
              : data.status !== undefined && data.status !== null
                ? data.status
                : this.projectInfo.projectStatus
          ),
        };
        this.updatePageTitle();
        this.syncProjectStatusPolling();
        this.syncAccessibleTab();
        return this.projectInfo;
      } catch (error) {
        if (!silent) {
          this.$message.error("加载项目详情失败");
        } else {
          console.error("轮询项目状态失败:", error);
          throw error;
        }
        this.updatePageTitle();
        return null;
      }
    },
    syncProjectStatusPolling() {
      if (String(this.projectInfo.projectStatus) === "3") {
        this.startProjectStatusPolling();
        return;
      }
      this.stopProjectStatusPolling();
    },
    startProjectStatusPolling() {
      if (this.projectStatusPollingTimer) {
        return;
      }
      this.projectStatusPollingTimer = setInterval(() => {
        this.pollProjectStatus();
      }, this.projectStatusPollingInterval);
    },
    stopProjectStatusPolling() {
      if (!this.projectStatusPollingTimer) {
        return;
      }
      clearInterval(this.projectStatusPollingTimer);
      this.projectStatusPollingTimer = null;
    },
    async pollProjectStatus() {
      if (this.projectStatusPollingLoading || !this.projectId) {
        return;
      }

      this.projectStatusPollingLoading = true;
      try {
        await this.fetchProjectDetail({ silent: true });
        if (String(this.projectInfo.projectStatus) !== "3") {
          this.stopProjectStatusPolling();
        }
      } catch (error) {
        console.error("项目状态轮询请求失败:", error);
      } finally {
        this.projectStatusPollingLoading = false;
      }
    },
    updatePageTitle() {
      const title = this.projectInfo.projectName || `ProjectDetail-${this.projectId}`;
      this.$route.meta.title = title;
      this.$store.dispatch("settings/setTitle", title);
      this.$store.dispatch("tagsView/updateVisitedView", {
        path: this.$route.path,
        title,
      });
    },
    /** 格式化更新时间 */
    formatUpdateTime(time) {
      if (!time) return "-";
      const date = new Date(time);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      const seconds = String(date.getSeconds()).padStart(2, "0");
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    /** 模拟项目信息 */
    mockProjectInfo() {
      // 模拟数据，实际应该调用API
      this.projectInfo = {
        projectId: this.projectId,
        projectName: "2024年Q1初核项目",
        projectDesc: "第一季度员工异常行为排查",
        createTime: "2024-01-01 10:00:00",
        updateTime: new Date().toISOString(),
        startDate: "2024-01-01",
        endDate: "2024-03-31",
        targetCount: 500,
        warningCount: 15,
        warningThreshold: 60,
        projectStatus: "0",
      };
    },
    /** 获取状态类型 */
    getStatusType(status) {
      const statusMap = {
        0: "primary", // 进行中
        1: "success", // 已完成
        2: "info", // 已归档
        3: "warning", // 打标中
        4: "danger", // 打标失败
      };
      return statusMap[status] || "info";
    },
    /** 获取状态标签 */
    getStatusLabel(status) {
      const statusMap = {
        0: "进行中",
        1: "已完成",
        2: "已归档",
        3: "打标中",
        4: "打标失败",
      };
      return statusMap[status] || "未知";
    },
    openTagFailureDialog() {
      this.tagFailureDialogVisible = true;
    },
    /** 获取配置类型标签文字 */
    getConfigTypeLabel(configType) {
      const configTypeMap = {
        "default": "默认配置",
        "custom": "自定义配置"
      }
      return configTypeMap[configType] || "默认配置"
    },
    /** 获取配置类型标签样式 */
    getConfigTypeStyle(configType) {
      const styleMap = {
        "default": "info",      // 蓝色
        "custom": "warning"     // 橙色
      }
      return styleMap[configType] || "info"
    },
    /** 标签页切换 */
    handleTabChange(tab) {
      console.log("切换到标签页:", tab.name);
    },
    /** 返回列表页 */
    handleBack() {
      this.$router.push("/ccdiProject");
    },
    /** 菜单选择事件 */
    handleMenuSelect(index) {
      if (this.isArchiveLockedTab(index)) {
        this.$message.warning(this.canOperate ? "已归档项目暂不可进入该页签" : "当前项目仅可查看，不能进入写入页签");
        return;
      }
      console.log("菜单选择:", index);
      this.setActiveTab(index);
      this.syncRouteTab(index);
    },
    /** UploadData 组件：菜单切换 */
    handleMenuChange({ key, route }) {
      console.log("切换到菜单:", key, route);
      // 直接触发菜单选择
      this.handleMenuSelect(route);
    },
    handleOpenDetailQuery(payload = {}) {
      this.detailQueryPrefill = {
        ...payload,
        nonce: Date.now(),
      };
      this.setActiveTab("detail");
      this.syncRouteTab("detail");
    },
    /** UploadData 组件：数据上传完成 */
    handleDataUploaded({ type }) {
      console.log("数据上传完成:", type);
      this.$message.success(`${type} 数据上传成功`);
    },
    /** UploadData 组件：名单选择完成 */
    handleNameSelected(nameList) {
      console.log("名单选择完成:", nameList);
      this.$message.success("名单选择成功");
    },
    /** UploadData 组件：生成报告 */
    handleGenerateReport() {
      console.log("生成报告");
      // this.$message.info("生成报告功能开发中");
    },
    /** UploadData 组件：拉取本行信息 */
    handleFetchBankInfo() {
      console.log("拉取本行信息");
      this.$message.info("拉取本行信息功能开发中");
    },
    /** 数据上传完成 */
    handleDataUploaded() {
      console.log("数据上传完成");
      this.$message.success("数据上传成功");
    },
    /** 刷新页面 */
    handleRefresh() {
      this.initPageData();
      this.$message.success("刷新成功");
    },
    handleRefreshProject() {
      this.initPageData();
    },
    handleEvidenceConfirm(payload) {
      if (!this.canOperate) {
        this.$message.warning("当前项目仅可查看，不能确认入库证据");
        return;
      }
      this.evidencePayload = {
        projectId: this.projectId,
        ...(payload || {}),
      };
      this.evidenceConfirmVisible = true;
    },
    handleEvidenceSaved() {
      this.evidenceDrawerVisible = true;
      this.$nextTick(() => {
        if (this.$refs.evidenceDrawer) {
          this.$refs.evidenceDrawer.loadEvidence();
        }
      });
    },
    /** 导出报告 */
    handleExport() {
      console.log("导出报告");
      this.$message.info("报告导出功能开发中");
    },
    /** 完成项目 */
    handleComplete() {
      this.$confirm("确定要完成当前项目吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message.success("项目已完成");
          this.projectInfo.projectStatus = "1";
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
    /** 归档项目 */
    handleArchive() {
      this.$confirm(
        "确定要归档当前项目吗？归档后将不能进行修改操作。",
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$message.success("项目已归档");
          this.projectInfo.projectStatus = "2";
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.dpc-detail-container {
  padding: 16px;
  background: var(--ccdi-page-bg);
  min-height: calc(100vh - 84px);
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  padding: 16px 20px;
  background: #ffffff;
  border: 1px solid var(--ccdi-border);
  border-radius: 14px;
  box-shadow: var(--ccdi-shadow);

  .header-left {
    display: flex;
    align-items: flex-start;
    gap: 12px;

    .el-button {
      padding: 8px 12px;
      font-size: 13px;
      margin-top: 4px;
    }

    .title-section {
      .page-title {
        display: flex;
        align-items: center;
        h2 {
          margin: 0 0 4px 0;
          font-size: 20px;
          font-weight: 700;
          color: var(--ccdi-text-primary);
          margin-right: 5px;
        }
      }

      .update-time {
        margin: 0;
        font-size: 13px;
        color: var(--ccdi-text-muted);
      }
    }
  }

  .header-right {
    display: flex;
    align-items: center;
    gap: 10px;

    .evidence-entry-btn {
      padding: 6px 10px;
      font-size: 12px;
      color: #5b7fb8;
      border-color: #d6e4f7;
      background: #f8fbff;

      &:hover {
        color: var(--ccdi-primary);
        border-color: #9fc3ff;
        background: #edf5ff;
      }
    }

    .nav-menu {
      // 移除默认背景色和边框
      background-color: transparent;
      border-bottom: none;

      // 菜单项基础样式
      .el-menu-item,
      .el-submenu__title {
        font-size: 14px;
        color: var(--ccdi-text-secondary);
        padding: 0 16px;
        height: 40px;
        line-height: 40px;
        border-bottom: 2px solid transparent;
        border-radius: 10px 10px 0 0;

        &:hover {
          background-color: #f7fafd;
          color: var(--ccdi-text-primary);
        }
      }

      // 子菜单容器高度统一
      .el-submenu {
        height: 40px;
        line-height: 40px;
      }

      // 激活状态：底部下划线 + 蓝色文字
      .el-menu-item.is-active {
        color: var(--ccdi-primary);
        border-bottom: 2px solid var(--ccdi-primary);
        background-color: transparent;
      }

      // 下拉菜单激活状态
      .el-submenu.is-active > .el-submenu__title {
        color: var(--ccdi-primary);
        border-bottom: 2px solid var(--ccdi-primary);
      }

      // 下拉菜单图标
      .el-submenu__icon-arrow {
        margin-left: 4px;
      }
    }
  }
}

.tag-failure-alert {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  padding: 12px 16px;
  color: #9f3a38;
  background: #fff2f0;
  border: 1px solid #ffd6d1;
  border-radius: 8px;
}

.tag-failure-alert__content {
  display: flex;
  align-items: flex-start;
  min-width: 0;
  gap: 10px;

  i {
    margin-top: 2px;
    font-size: 18px;
    color: #f56c6c;
  }
}

.tag-failure-alert__title {
  font-size: 14px;
  font-weight: 600;
  line-height: 20px;
}

.tag-failure-alert__message {
  margin-top: 2px;
  font-size: 13px;
  line-height: 20px;
  word-break: break-word;
}

.tag-failure-dialog__time {
  margin-bottom: 12px;
  color: #606266;
  font-size: 13px;
}

.tag-failure-dialog__message {
  max-height: 420px;
  margin: 0;
  padding: 12px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  color: #303133;
  background: #f8f9fb;
  border: 1px solid #ebeef5;
  border-radius: 6px;
  font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.6;
}

.info-card {
  margin-bottom: 16px;

  :deep(.el-card__body) {
    padding: 20px;
  }
}

.info-header {
  margin-bottom: 16px;

  h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
    color: #303133;
  }
}

.info-content {
  .info-row {
    display: flex;
    gap: 32px;
    margin-bottom: 12px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .info-item {
    display: flex;
    align-items: center;
    font-size: 14px;

    .label {
      color: #606266;
      min-width: 100px;
      font-weight: 500;
    }

    .value {
      color: #303133;
      font-weight: 400;

      &.warning-count {
        color: #e6a23c;
        font-weight: 600;
      }
    }
  }
}

.content-card {
  margin-bottom: 16px;

  :deep(.el-card__body) {
    padding: 0;

    .el-tabs {
      height: 100%;

      .el-tabs__content {
        padding: 20px;
      }
    }
  }
}

.action-bar {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  background: #ffffff;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);

  .el-button {
    padding: 10px 20px;

    i {
      margin-right: 4px;
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .dpc-detail-container {
    padding: 8px;
  }

  .detail-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;

    .header-right {
      width: 100%;
      margin-top: 12px;

      .nav-menu {
        width: 100%;
        display: flex;
        justify-content: flex-start;

        .el-menu-item,
        .el-submenu {
          flex: 1;
          text-align: center;
          padding: 0 8px;
          font-size: 13px;
        }
      }
    }
  }

  .info-content {
    .info-row {
      flex-direction: column;
      gap: 8px;

      .info-item {
        .label {
          min-width: auto;
        }
      }
    }
  }

  .action-bar {
    flex-wrap: wrap;

    .el-button {
      width: 100%;
    }
  }
}

// 下拉菜单弹窗样式
::v-deep .el-menu--popup {
  min-width: 140px;

  .el-menu-item {
    font-size: 14px;

    &:hover {
      background-color: #f5f7fa;
    }

    &.is-active {
      color: #1890ff;
      background-color: #e6f7ff;
    }
  }
}
</style>
