<template>
  <div class="global-variable">
    <div class="condition">
      <!-- <el-alert
        title="分析前请核对数据是否导入，点击查看是否导入即可查看"
        type="warning"
        show-icon
      >
      </el-alert> -->
      <el-form
        :inline="true"
        ref="ruleForm"
        :model="formInline"
        class="demo-form-inline"
        :rules="rules"
      >
        <el-form-item label="分析主题:" prop="analysisName">
          <el-input
            size="small"
            v-model="formInline.analysisName"
            placeholder="请输入分析主题"
          ></el-input>
        </el-form-item>
        <el-form-item label="风场名称:" prop="fieldName">
          <el-input
            size="small"
            v-model="formInline.fieldName"
            placeholder="请输入风场名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="分析状态:" prop="analysisState">
          <el-select
            size="small"
            v-model="formInline.analysisState"
            placeholder="请选择分析状态"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="() => fetchData()" size="small"
            >查询</el-button
          >
          <el-button @click="reset('ruleForm')" size="small">重置</el-button>
        </el-form-item>
        <el-form-item class="right-align">
          <el-button @click="Newanalyse" size="small">创建分析</el-button>
          <el-button
            @click="examine"
            size="small"
            v-hasPermi="['home:offlinedata']"
            >查看导入数据</el-button
          >
          <el-button
            @click="handleAutoAsstessList"
            size="small"
            v-hasPermi="['home:performance:autoAssetss']"
            >查看自动分析列表</el-button
          >

          <!-- <el-button @click="handleDownLoadChart" size="small">下载</el-button> -->
        </el-form-item>
      </el-form>
      <div class="progress" v-if="downloadDisabled">
        <div class="progressText">文档生成进度：</div>
        <el-progress
          :percentage="progressPercent"
          :color="colors"
        ></el-progress>
      </div>
    </div>
    <div class="list-page">
      <el-table
        class="center-align-table"
        :data="tableData"
        border
        :cell-style="rowStyle"
      >
        <el-table-column
          align="center"
          label="分析主题"
          prop="analysisName"
          min-width="200"
          fixed
        >
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="scope.row.sketch || '暂无分析简述'"
              placement="top"
            >
              <span>{{ scope.row.analysisName }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="fieldName"
          label="风场名称"
          min-width="200"
        >
        </el-table-column>

        <el-table-column
          prop="loginName"
          align="center"
          label="分析状态"
          min-width="150"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.analysisState == -1">未关联</span>
            <span
              v-else-if="
                scope.row.analysisState == 20 && scope.row.errState == 0
              "
              style="color: #f90"
              >分析中</span
            >
            <span
              v-else-if="
                scope.row.errState == 1 && scope.row.analysisState == 30
              "
              style="color: #f00"
              >分析异常</span
            >

            <span
              v-else-if="scope.row.analysisState == 30"
              style="color: #4caf50"
              >分析完成</span
            >
            <span
              v-else-if="scope.row.analysisState == 10"
              style="color: #4caf50"
              >排队中</span
            >
            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="loginName"
          align="center"
          label="进度"
          min-width="150"
        >
          <template slot-scope="scope">
            <el-progress
              v-if="scope.row.analysisState == 20"
              :text-inside="true"
              :stroke-width="20"
              :percentage="scope.row.analysisProgress"
              :class="{
                'indeterminate-progress': scope.row.analysisProgress < 100,
                'animated-progress': true,
              }"
            ></el-progress>
            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="排队序号"
          prop="orderNum"
          min-width="200"
        >
          <template slot-scope="scope">
            <span> {{ scope.row.orderNum }}</span>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="数据类型名称"
          prop="dataTypeName"
          min-width="200"
        >
        </el-table-column>
        <el-table-column
          v-hasPermi="['home:performance:autoAssetss']"
          prop="onOffCall"
          align="center"
          label="自动分析状态"
          min-width="140"
        >
          <template slot-scope="scope">
            <span>
              {{
                scope.row.onOffCall == 0
                  ? "暂停"
                  : scope.row.onOffCall === 1
                  ? "开启"
                  : "/"
              }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="roleName"
          align="center"
          label="异常信息"
          min-width="120"
        >
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.errState == 1"
              @click="abnormalDialog(scope.row, '异常详情')"
              type="text"
              size="small"
              >异常详情</el-button
            >
            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="roleName"
          align="center"
          label="分析记录"
          min-width="120"
        >
          <template slot-scope="scope">
            <!-- 分析完成 -->
            <el-button
              v-if="scope.row.analysisState == 30 && scope.row.errState !== 1"
              @click="handleAssetssDetail(scope.row, '1')"
              type="text"
              size="small"
              >分析详情</el-button
            >
            <!-- 分析中 -->
            <el-button
              v-else-if="
                scope.row.analysisState == 20 && scope.row.errState == 0
              "
              @click="handleAssetssDetail(scope.row, '0')"
              type="text"
              size="small"
              >分析详情</el-button
            >

            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="reportVos"
          align="center"
          label="报告"
          min-width="120"
        >
          <template slot-scope="scope">
            <el-dropdown v-if="scope.row.reportVos.length > 0" trigger="click">
              <el-button type="text" size="small" class="el-dropdown-link">
                查看报告
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  v-for="val in scope.row.reportVos"
                  class="reportItemVal"
                >
                  <div class="reportLeft">
                    <span>{{ val.reportName }}</span>
                  </div>
                  <div class="reportRight">
                    <el-button
                      type="text"
                      size="small"
                      @click="detailReportAssetss(val)"
                    >
                      查看
                    </el-button>
                    <el-button
                      type="text"
                      size="small"
                      @click="downLoadeAssetss(val)"
                    >
                      下载
                    </el-button>
                  </div>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <span v-else>/</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="dataStartTime"
          align="center"
          label="开始时间"
          min-width="230"
        >
        </el-table-column>
        <el-table-column
          prop="dataEndTime"
          align="center"
          label="结束时间"
          min-width="230"
        >
        </el-table-column>
        <el-table-column
          prop="transition"
          align="center"
          fixed="right"
          label="操作"
          min-width="300"
        >
          <template slot-scope="scope">
            <el-button
              @click="handleDownLoadChart(scope.row)"
              type="text"
              size="small"
              :disabled="downloadDisabled"
              v-if="
                (scope.row.errState == 1 && scope.row.analysisState == 30) ||
                scope.row.analysisState == 30
              "
            >
              下载报告
            </el-button>
            <!-- <el-button
              @click="abnormalDialog(scope.row, '上传报告')"
              type="text"
              size="small"
              >上传报告</el-button
            > -->
            <el-button
              @click="handleAssetss(scope.row)"
              type="text"
              :disabled="
                (scope.row.analysisState == 20 && scope.row.errState == 0) ||
                scope.row.analysisState == 10
              "
              size="small"
              >分析</el-button
            >
            <el-button
              @click="abnormalDialog(scope.row, '机组异常记录')"
              type="text"
              size="small"
              >机组异常记录</el-button
            >
            <el-button
              v-if="scope.row.analysisState == 10"
              @click="insertQueue(scope.row, 1)"
              type="text"
              size="small"
              >插队</el-button
            >
            <el-button
              v-if="scope.row.analysisState == 10"
              @click="insertQueue(scope.row, 0)"
              type="text"
              size="small"
              >取消插队</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="formInline.pageNum"
          layout="total, prev, pager, next"
          :page-size="formInline.pageSize"
          :total="formInline.totalSize"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 弹出层 -->
    <!-- 异常信息 /机组异常记录 /上传报告-->
    <my-dialog
      :visible="dialogVisible"
      :title="title"
      :rowInfo="rowInfo"
      @confirm="handleConfirm"
      :errorInfo="errorInfo"
      @getTableList="fetchData"
    >
      <div slot="tableEl">
        <el-empty description="暂无数据"></el-empty>
      </div>
    </my-dialog>
    <!-- 查看报告详情 -->
    <el-dialog
      title="PDF 预览"
      :visible.sync="dialogReportVisible"
      :width="dialogWidth"
      class="pdfDialog"
      :before-close="handleCloses"
    >
      <span>
        <div
          style="
            float: right;
            color: #fff;
            cursor: pointer;
            position: absolute;
            left: 90%;
            top: 21px;
          "
        >
          <span
            v-if="dialogWidth === '80%'"
            @click="() => (dialogWidth = '100%')"
            >全屏查看</span
          >
          <span
            v-if="dialogWidth === '100%'"
            @click="() => (dialogWidth = '80%')"
            >还原</span
          >
          <!-- {{ isFullscreen ? "还原" : "全屏" }} -->
        </div>
      </span>
      <div class="pdf-container" ref="viewer">
        <iframe :src="pdfUrl" width="100%" height="100%"></iframe>
      </div>
      <div></div>
    </el-dialog>
    <!-- 创建分析弹出框 -->
    <el-dialog
      title="创建分析"
      :visible.sync="addDialogVisible"
      width="30%"
      :before-close="AddHandleCloses"
    >
      <el-form
        :model="addRuleForm"
        :rules="addRules"
        ref="addRuleForm"
        label-width="100px"
        class="add-ruleForm"
      >
        <el-form-item label="关联风场" prop="fieldCode">
          <el-select
            v-model="addRuleForm.fieldCode"
            placeholder="请选择关联风场"
          >
            <el-option
              :label="item.fieldName"
              v-for="item in fieldCodeList"
              :value="item.codeNumber"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="analysisName">
          <b style="color: #f00; font-size: 12px"
            >不填写分析主题,自动采用系统生成的主题！</b
          >
        </el-form-item>
        <el-form-item label="分析主题" prop="analysisName">
          <el-input v-model="addRuleForm.analysisName"></el-input>
        </el-form-item>
        <el-form-item label="分析简述" prop="sketch">
          <el-input type="textarea" v-model="addRuleForm.sketch"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addRuleFormSubmit">提交</el-button>
          <el-button @click="AddHandleCloses">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import MyDialog from "./components/dialogCom.vue";
import { downloadPDF } from "@/utils/common";
import {
  analysisResultList,
  addAnalysisResult,
  queryCodeNum,
  editPriority,
  analysisDetail,
} from "@/api/performance";
import { downloadDocx } from "@/utils/common";
import { getFieldInfo } from "@/api/overview";
import axios from "axios";
import pLimit from "p-limit";
import { allAnalysisType } from "./js/allAnalysisType";
import { mapMutations, mapState } from "vuex";
import {
  windEngineMillPage,
  getWindEngineGroupByFieldCode,
} from "@/api/ledger.js";

export default {
  components: {
    MyDialog,
  },
  data() {
    return {
      progress: {
        current: 0,
        total: 0,
      },
      colors: [
        { color: "#f56c6c", percentage: 20 },
        { color: "#e6a23c", percentage: 40 },
        { color: "#5cb87a", percentage: 60 },
        { color: "#1989fa", percentage: 80 },
        { color: "#6f7ad3", percentage: 100 },
      ],
      fileDataList: {},
      fieldInfo: {}, //风场信息
      firstLoad: false, // 是否是第一次加载
      fieldCodeList: [],
      addDialogVisible: false,
      dialogWidth: "80%",
      intervalId: null,
      startTime: null,
      maxPollingTime: 3 * 60 * 1000, //轮询最大时间
      dialogVisible: false,
      loadingView: false,
      loading: false, //数据加载中
      errorInfo: "",
      options: [
        {
          value: "-1",
          label: "未关联",
        },
        {
          value: "10",
          label: "排队中",
        },
        {
          value: "20",
          label: "分析中",
        },
        {
          value: "30",
          label: "已分析",
        },
      ],
      addRules: {
        fieldCode: [
          { required: true, message: "请选择关联风场", trigger: "change" },
        ],
      },
      addRuleForm: {
        fieldCode: "",
        analysisName: "",
        sketch: "",
      },
      rules: {
        fieldName: { trigger: "blur" },
      },
      roleList: [],
      formInline: {
        fieldName: undefined,
        pageNum: 1,
        pageSize: 10,
        totalSize: 0,
        analysisName: undefined,
        analysisState: undefined,
      },
      allAnalysis: [],
      tableData: [],
      rowInfo: {},
      title: "",
      viewerInstance: null, // PDF 文件 URL
      dialogReportVisible: false,
      pdfUrl: "",
      isPolling: false, // 轮询状态标识
      firstLoad: true, // 是否是第一次加载
    };
  },
  created() {
    this.firstLoad = true;
    window.addEventListener("message", this.handleMessage); //江
    this.fetchData();
  },
  computed: {
    ...mapState("settings", {
      downloadDisabled: "downloadDisabled",
    }),
    progressPercent() {
      if (this.progress.total === 0) return 0;
      return Math.round((this.progress.current / this.progress.total) * 100);
    },
  },
  methods: {
    ...mapMutations("settings", ["setDownloadDisabled"]),

    async postChartData(
      urlType,
      row,
      itemAnalysis,
      itemField,
      filterAnalysis,
      typeChart,
    ) {
      try {
        if (itemField.analysisTypeName === "环境温度传感器") {
          console.log(itemField, "itemField");
        }

        const objectname =
          itemAnalysis.analysisTypeCode === "temperature_large_components"
            ? filterAnalysis.filterFileAddr + "/" + itemField.fieldEngineName ||
              itemField.engineTypeCode + ".jpg"
            : itemField.engineTypeCode
            ? urlType + itemField.engineTypeCode + ".jpg"
            : urlType + itemField.fieldEngineName + ".jpg";
        console.log(
          objectname,
          "objectname",
          itemAnalysis.analysisTypeName,
          itemField.engineTypeCode,
          itemAnalysis,
        );
        // const res = await axios.post(
        //   `/downLoadChart/chartServer/charts/${urlType}`,
        //   {
        //     fieldEngineCode: itemField.fieldEngineCode,
        //     bucketName: "bucket-zhzn",
        //     engineTypeCode: itemField.machineTypeCode,
        //     objectName:
        //       `charts/${row.fieldCode}/${row.batchCode}/${itemAnalysis.analysisTypeCode}/` +
        //       objectname,
        //     fieldInfo: this.fieldInfo,
        //     fileAddr: itemField.fileAddr,
        //     chartType:
        //       filterAnalysis.typeDocxName === "production_indicator_unit"
        //         ? "radar"
        //         : urlType,
        //   },
        // );
        // // 每完成 10% 提示一次（可调）
        // const percent = Math.floor(
        //   (this.progress.current / this.progress.total) * 100,
        // );
        // if (percent % 5 === 0 && !this._shownPercents?.includes(percent)) {
        //   this._shownPercents = this._shownPercents || [];
        //   this._shownPercents.push(percent);

        //   this.$notify.info(
        //     `📈 已完成 ${percent}%/${this.progress.current}张，共 ${this.progress.total} 张图表`,
        //   );
        // }
        // let key = `zn-techcn-replace-tags-${filterAnalysis.typeDocxName}-${typeChart}`;
        // if (urlType === "yawErrorBarSumChart") {
        //   key = `zn-techcn-replace-tags-${filterAnalysis.typeDocxName}-generalFiles2`;

        //   if (!this.fileDataList["yawErrorRows"]) {
        //     this.$set(this.fileDataList, "yawErrorRows", []); // Vue 2 中响应式设置对象属性
        //   }
        //   this.fileDataList["yawErrorRows"] = res?.data?.data?.data;
        // } else if (urlType === "yawErrorChart") {
        //   key = `zn-techcn-replace-tags-${filterAnalysis.typeDocxName}-generalFiles1`;
        // }
        // if (filterAnalysis.typeDocxName === "production_indicator_all") {
        //   if (!this.fileDataList["rows"]) {
        //     this.$set(this.fileDataList, "rows", []); // Vue 2 中响应式设置对象属性
        //   }
        //   this.fileDataList["rows"] = res?.data?.data?.data;
        // }
        // if (itemField.engineTypeCode === "total_fault_result") {
        //   if (!this.fileDataList["faultRows"]) {
        //     this.$set(this.fileDataList, "faultRows", []); // Vue 2 中响应式设置对象属性
        //   }
        //   this.fileDataList["faultRows"] = res?.data?.data?.data;
        // }
        // if (itemField.engineTypeCode === "turbine_fault_result") {
        //   if (!this.fileDataList["windTurbineRows"]) {
        //     this.$set(this.fileDataList, "windTurbineRows", []); // Vue 2 中响应式设置对象属性
        //   }
        //   this.fileDataList["windTurbineRows"] = res?.data?.data?.data;
        // }
        // if (filterAnalysis.typeDocxName === "production_indicator_unit") {
        //   if (!this.fileDataList[key]) {
        //     this.$set(this.fileDataList, key, []); // Vue 2 中响应式设置对象属性
        //   }
        //   res?.data?.data?.imageUrls.map((imgval) => {
        //     this.fileDataList[key].push(imgval);
        //   });
        // } else {
        //   if (!this.fileDataList[key]) {
        //     this.$set(this.fileDataList, key, []); // Vue 2 中响应式设置对象属性
        //   }
        //   this.fileDataList[key].push(res?.data?.data?.imageUrl);
        // }
      } catch (err) {
        console.error("生成失败：", err);
      }
    },
    async handleDownLoadChart(row) {
      this.progress.current = 0;
      this.progress.total = 0;
      this.fileDataList = {};
      this.setDownloadDisabled(true);
      this.$notify.warning("开始生成 Word 文档...");
      try {
        await this.getAllAnalysis(row.batchCode);
        await this.getFieldDetail(row.batchCode);
        this.$message.info("开始生成word文档");
        const limit = pLimit(5); // 限制同时并发的请求数量为 5
        const tasks = [];
        for (const itemAnalysis of this.allAnalysis) {
          const filterAnalysis = allAnalysisType.filter(
            (itemType) => itemType.typeName === itemAnalysis.analysisTypeName,
          )[0];

          if (itemAnalysis.generalFiles) {
            //这里过滤的是发电机温度的类型 在url 中是否能找到filterFileAddr
            for (const itemField of filterAnalysis.filterFileAddr
              ? itemAnalysis.generalFiles
                  .filter((item) => item.fileAddr.endsWith(".json"))
                  .filter((item) =>
                    item.fileAddr.includes(filterAnalysis.filterFileAddr),
                  ) || []
              : itemAnalysis.generalFiles) {
              //静态偏航误差 --通过allAnalysisType.js文件配置实现需求
              if (Array.isArray(filterAnalysis.generalFiles.urlType)) {
                filterAnalysis.generalFiles.urlType.map(
                  (itemUrlType, indUrlType) => {
                    tasks.push(
                      limit(async () => {
                        await this.postChartData(
                          itemUrlType,
                          row,
                          itemAnalysis,
                          itemField,
                          filterAnalysis,
                          "generalFiles",
                        );
                        this.progress.current++;
                      }),
                    );
                  },
                );
              } else if (filterAnalysis.typeCode === "fault") {
                if (itemField.fileAddr.includes("turbine_fault_result")) {
                  if (itemField.engineTypeCode === "turbine_fault_result") {
                    tasks.push(
                      limit(async () => {
                        await this.postChartData(
                          "faultUnitChart",
                          row,
                          itemAnalysis,
                          itemField,
                          filterAnalysis,
                          "generalFiles",
                        );
                        this.progress.current++;
                      }),
                    );
                  }
                } else {
                  if (itemField.engineTypeCode === "total_fault_result") {
                    tasks.push(
                      limit(async () => {
                        await this.postChartData(
                          "faultAllChart",
                          row,
                          itemAnalysis,
                          itemField,
                          filterAnalysis,
                          "generalFiles",
                        );
                        this.progress.current++;
                      }),
                    );
                  }
                }
              } else if (filterAnalysis.typeCode === "production_indicator") {
                //总图全场
                if (
                  itemField.fileAddr.includes(
                    filterAnalysis.generalFiles.FileTypeFromUrl,
                  )
                ) {
                  tasks.push(
                    limit(async () => {
                      await this.postChartData(
                        filterAnalysis.generalFiles.urlType,
                        row,
                        itemAnalysis,
                        itemField,
                        filterAnalysis,
                        "generalFiles",
                      );
                      this.progress.current++;
                      // 每完成 10% 提示一次（可调）
                    }),
                  );
                } else {
                  tasks.push(
                    limit(async () => {
                      await this.postChartData(
                        "radarChart",
                        row,
                        itemAnalysis,
                        itemField,
                        filterAnalysis,
                        "generalFiles",
                      );
                      this.progress.current++;
                    }),
                  );
                }
              } else {
                tasks.push(
                  limit(async () => {
                    await this.postChartData(
                      filterAnalysis.generalFiles.urlType,
                      row,
                      itemAnalysis,
                      itemField,
                      filterAnalysis,
                      "generalFiles",
                    );
                    this.progress.current++;
                  }),
                );
              }
            }
          }
          if (itemAnalysis.diagramRelations) {
            for (const itemField of filterAnalysis.filterFileAddr
              ? itemAnalysis.diagramRelations
                  .filter((item) => item.fileAddr.endsWith(".json"))
                  .filter((item) =>
                    item.fileAddr.includes(filterAnalysis.filterFileAddr),
                  ) || []
              : itemAnalysis.diagramRelations) {
              const urlType = Array.isArray(
                filterAnalysis.diagramRelations.urlType,
              )
                ? this.getFileTypeFromUrl(
                    itemField.fileAddr,
                    filterAnalysis.diagramRelations.FileTypeFromUrl,
                  ) === filterAnalysis.diagramRelations.FileTypeFromUrl
                  ? filterAnalysis.diagramRelations.urlType[0]
                  : filterAnalysis.diagramRelations.urlType[1]
                : filterAnalysis.diagramRelations.urlType;
              tasks.push(
                limit(async () => {
                  await this.postChartData(
                    urlType,
                    row,
                    itemAnalysis,
                    itemField,
                    filterAnalysis,
                    "diagramRelations",
                  );
                  this.progress.current++;
                }),
              );
            }
          }
        }
        this.progress.total = tasks.length;
        await Promise.all(tasks);

        const engineTypeList = await this.getEngineTypeList(
          this.fieldInfo.engineMillTypes,
          row.batchCode,
        );
        this.$notify.success("✅ 图表全部生成完成，开始生成 Word 文档...");
        const wordFilePath = await axios.post(
          "/downLoadChart/chartServer/charts/CopyFileCsv",
          {
            fieldInfo: this.fieldInfo,
            dataTime: `${this.analysisInfo.dataStartTime}-${this.analysisInfo.dataEndTime}`,
            bucketName: "bucket-zhzn",
            objectName: `charts/${row.fieldCode}/${row.batchCode}`,
            engineTypeList: engineTypeList,
            ...this.fileDataList,
          },
        );
        // //下载minio 文件
        downloadDocx(
          wordFilePath.data.data.url,
          wordFilePath.data.data.fileName,
        );
        this.$notify.success("🎉 Word 文档生成并已下载！");
        this.setDownloadDisabled(false);
      } catch (error) {
        console.log(error, "error");
        this.$notify.error("🎉 Word 文档生成并下载失败！");
        this.setDownloadDisabled(false);
      }
    },

    async getEngineTypeList(typeList, batchCode) {
      return await Promise.all(
        typeList.map((item) => this.getEngineMillList(item, batchCode)),
      );
    },
    // 查询
    async getEngineMillList(machineTypeCode, batchCode) {
      let dataArr = {
        fieldCode: this.fieldInfo.fieldCode,
        batchCode: batchCode,
      };
      const fieldRes = await getWindEngineGroupByFieldCode(dataArr);
      const map = new Map();
      const uniqueList = [];

      for (const item of fieldRes.data.windEngineGroupVoList) {
        if (!map.has(item.millTypeCode)) {
          map.set(item.millTypeCode, true);
          uniqueList.push(item);
        }
      }
      let paramsData = {
        machineTypeCode: machineTypeCode || undefined,
        pageNum: 1,
        pageSize: 10,
      };
      this.loading = true;
      const res = await windEngineMillPage(paramsData);
      const filterMill = uniqueList.filter(
        (item) => item.millTypeCode === res.data.list[0].millTypeCode,
      );
      return { ...res.data.list[0], ratedPower: filterMill[0].ratedCapacity };
    },

    getFileTypeFromUrl(url, keyword) {
      return url.includes(keyword) ? keyword : "Unknown";
    },
    async getFieldDetail(batchCode) {
      const res = await getFieldInfo({ batchCode });
      this.fieldInfo = res.data.fieldInfo; //风场信息
      this.analysisInfo = res.data.analysisInfo;
    },
    //获取所有分析类型，分析结果接口
    async getAllAnalysis(batchCode) {
      const res = await analysisDetail({ batchCode });
      // console.log(res.data, "分析全部结果");
      this.allAnalysis = res.data;
    },
    //自动分析列表页面跳转
    handleAutoAsstessList() {
      window.open(
        this.$router.resolve({ path: "/home/performance/autoAssetss" }).href,
        "_blank",
      );
    },
    // 创建分析时请求
    async addRuleFormSubmit() {
      this.$refs.addRuleForm.validate(async (valid) => {
        if (valid) {
          try {
            const res = await addAnalysisResult(this.addRuleForm);
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "创建成功",
              });
              this.addDialogVisible = false;
              this.loading = false;
              this.isPolling = false;
              await this.fetchData();
              let obj = this.fieldCodeList.filter((itemS) => {
                if (itemS.codeNumber === this.addRuleForm.fieldCode) {
                  return itemS;
                }
              });
              this.$router.push({
                path: "/home/performance/editAssets",
                query: {
                  batchCode: this.tableData[0].batchCode,
                  analysisTypeCode: this.tableData[0].analysisTypeCode,
                  fieldEngineCode: this.tableData[0].fieldCode,
                  fieldName: obj[0].fieldName,
                  analysisName: this.addRuleForm.analysisName,
                },
              });
            }
          } catch (err) {
            console.error(err);
          }
        }
      });
    },
    AddHandleCloses(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.addDialogVisible = false;
          done();
        })
        .catch((_) => {});
    },
    //获取风场列表
    async getQueryCodeNumList() {
      this.loading = true;
      try {
        const result = await queryCodeNum();
        this.fieldCodeList = result.data.fieldCodeList;
        this.loading = false;
      } catch (error) {
        console.error(error);
        this.loading = false;
      }
    },
    handleCloses(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //查看pdf 文件
    detailReportAssetss(row) {
      this.dialogReportVisible = true;
      this.pdfUrl = row.reportAddr;
    },
    //下载pdf 文件
    downLoadeAssetss(row) {
      downloadPDF(row.reportAddr, row.reportName);
    },
    //插队接口
    async insertQueue(row, index) {
      this.$confirm(`确认插队？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const formData = new FormData();
          formData.append("batchCode", row.batchCode);
          formData.append("priority", index);
          const result = await editPriority(formData);
          if (result.code === 200) {
            this.$message({
              type: "success",
              message: "插队成功",
            });
            await this.fetchData();
          }
        })
        .catch(() => {});
    },
    //分析
    handleAssetss(row) {
      this.$router.push({
        path: "/home/performance/editAssets",
        query: {
          batchCode: row.batchCode,
          analysisTypeCode: row.analysisTypeCode,
          fieldEngineCode: row.fieldCode,
          fieldName: row.fieldName,
          analysisName: row.analysisName,
        },
      });
    },
    //分析详情
    handleAssetssDetail(row, state) {
      const navigateToDetails = () => {
        this.$router.push({
          path: "/home/performance/overview",
          query: {
            batchCode: row.batchCode,
            // analysisTypeCode: row.analysisTypeCode,
            fieldCode: row.fieldCode,
          },
        });
      };
      if (state === "0") {
        // 分析状态为分析中
        this.$confirm(
          "当前查看的分析记录为历史分析结果，最新分析记录还未分析完成不展示！",
        )
          .then(() => {
            navigateToDetails();
          })
          .catch(() => {});
      } else {
        navigateToDetails();
      }
    },
    abnormalDialog(row, title) {
      this.dialogVisible = true;
      if (title === "异常详情") {
        this.errorInfo = row.errInfo;
        this.rowInfo = {};
      } else if (title === "机组异常记录") {
        this.errorInfo = "";
        this.rowInfo = { ...row };
      } else if (title === "上传报告") {
        this.errorInfo = "";
        this.rowInfo = {};
        this.rowInfo.batchCode = row.batchCode;
      }
      this.title = title;
    },
    handleConfirm() {
      this.dialogVisible = false;
    },
    // 页码变化时才触发查询
    handleCurrentChange(val) {
      this.formInline.pageNum = val;
      this.fetchData();
    },
    // 修改 getTableList 方法，避免重复请求
    async getTableList() {
      // 如果正在请求中，跳过此次调用
      if (this.loading || this.isPolling) return;

      this.loading = true;
      try {
        const params = { ...this.formInline, totalSize: undefined };
        // 传递条件参数
        if (this.formInline.analysisState !== undefined) {
          params.analysisState = this.formInline.analysisState;
        }
        if (this.formInline.errState !== undefined) {
          params.errState = this.formInline.errState;
        }
        const result = await analysisResultList(params);
        this.tableData = result.data.list;
        this.formInline.totalSize = result.data.totalSize;
      } catch (error) {
        this.$message({
          type: "error",
          message: "请检查是否连接网络",
        });
      } finally {
        this.loading = false;
      }
    },
    // 改进 handleMessage，避免频繁请求
    handleMessage(event) {
      // 确保消息来自当前域
      if (event.origin !== window.location.origin) {
        return;
      }
      const { fieldName, analysisState, errState } = event.data;
      // 更新表单字段
      if (fieldName !== undefined) {
        this.formInline.fieldName = fieldName;
      }

      if (analysisState !== undefined) {
        this.formInline.analysisState = analysisState;
      }
      if (errState !== undefined) {
        this.formInline.errState = errState;
      }
      if (!this.loading) {
        if (this.firstLoad && (analysisState || errState || fieldName)) {
          // this.getTableList();
          this.startPolling();
          this.fetchData();
        }

        //
      }
    },
    rowStyle() {
      return "text-align:center";
    },
    // 重置
    reset(formName) {
      this.$refs[formName].resetFields();
      this.formInline.fieldName = "";
      this.formInline.analysisName = "";
      this.formInline.analysisState = "";
      this.formInline.errState = "";
      this.fetchData();
    },
    // fetchData 方法在轮询中调用
    async fetchData() {
      if (
        this.formInline.fieldName ||
        this.formInline.analysisState ||
        this.formInline.errState
      ) {
        this.firstLoad = false;
      }
      try {
        const result = await analysisResultList({
          ...this.formInline,
          totalSize: undefined,
        });
        this.tableData = result.data.list;
        this.formInline.totalSize = result.data.totalSize;
      } catch (error) {
        this.$message({
          type: "error",
          message: "请检查是否连接网络",
        });
      }
    },
    stopPolling() {
      // 停止轮询
      if (this.intervalId) {
        clearInterval(this.intervalId);
        this.intervalId = null;
      }
      this.isPolling = false;
    },
    // 启动轮询时，避免重复请求
    startPolling() {
      this.startTime = new Date().getTime();
      this.isPolling = true;

      this.intervalId = setInterval(() => {
        const currentTime = new Date().getTime();
        if (currentTime - this.startTime >= this.maxPollingTime) {
          this.stopPolling();
        } else {
          // 轮询期间不重复请求，检查是否可以调用 fetchData
          if (!this.loading) {
            this.fetchData();
          }
        }
      }, 10000); // 每10秒检查一次
    },

    //创建分析
    Newanalyse() {
      this.addDialogVisible = true;
      this.getQueryCodeNumList();
    },
    examine() {
      const targetUrl = this.$router.resolve({ name: "transition" }).href;
      window.open(targetUrl, "_blank");
    },
  },
  mounted() {
    this.startPolling();
  },
  beforeDestroy() {
    this.stopPolling();
    // 销毁消息监听器
    window.removeEventListener("message", this.handleMessage); //江
  },
};
</script>

<style lang="scss" scoped>
@keyframes striped-flow {
  from {
    background-position: 0 100px;
  }
  to {
    background-position: 1.25em 1.25em;
  }
}
.general {
  display: flex;
  flex-wrap: wrap;

  .condition {
    width: 50%;
    display: flex;

    p {
      width: 100px;
      text-align: right;
      line-height: 40px;
    }

    span {
      line-height: 40px;

      padding-left: 20px;
    }

    .el-select {
      width: 100%;
      margin-bottom: 20px;
    }

    .el-input {
      margin-bottom: 20px;
    }
  }
}
.progress {
  display: flex;
  align-items: center;
  position: fixed;
  top: 25px;
  right: 300px;
  .progressText {
    font-size: 14px;
    color: #606266;
  }
  .el-progress {
    width: 300px;
  }
}
.attachment {
  display: flex;
  padding-top: 10px;

  p {
    margin-right: 20px;
    color: #409eff;
  }
}
.add-ruleForm {
  .el-select {
    width: 100%;
  }
}
.addition {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 10px;
}
.demo-ruleForm {
  .el-form-item {
    margin-bottom: 25px;
  }
}
::v-deep .animated-progress .el-progress-bar__outer {
  height: 15px; /* Adjust height as needed */
  background-color: rgb(235, 238, 245);
  background-image: linear-gradient(
    45deg,
    rgba(0, 0, 0, 0.1) 25%,
    transparent 25%,
    transparent 50%,
    rgba(0, 0, 0, 0.1) 50%,
    rgba(0, 0, 0, 0.1) 75%,
    transparent 75%,
    transparent
  );
  background-size: 1.25em 1.25em;
  animation: striped-flow 3s linear infinite;
}
.reportItemVal {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .reportLeft {
    margin-right: 50px;
  }
}
::v-deep .pdfDialog .el-dialog {
  height: 100vh;
  display: flex;
  flex-direction: column;
  margin: 0 auto !important;
  .el-dialog__body {
    // height: 100% !important;
    flex: 1;
    background: #fff;
    .pdf-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100% !important;
    }
  }
}

canvas {
  border: 1px solid #dcdfe6;
}

.right-align {
  white-space: nowrap;
}
</style>
