<!--
 * @Author: your name
 * @Date: 2025-01-10 09:26:12
 * @LastEditTime: 2025-01-16 09:06:36
 * @LastEditors: bogon
 * @Description: In User Settings Edit
 * @FilePath: /performance-test/src/views/overview/components/yaw_error_density/index.vue
-->
<template>
  <div class="type-variable">
    <!-- 动态偏航误差分析--只有分图不存在总图 -->
    <div class="left">
      <FilterChart
        :windList="windEngineGroupList"
        @getEnfineList="getEnfineList"
        @handlePrevious="handlePrevious"
        @handleNext="handleNext"
      ></FilterChart>
      <el-alert type="info" :closable="false">
        <template v-slot:title>
          <div style="display: flex; align-items: center">
            <i
              class="el-icon-info"
              style="font-size: 20px; margin-right: 5px"
            ></i>
            <h3>分析说明:</h3>
          </div>
        </template>
        <div style="font-size: 12px; margin-top: 10px">
          风能利用系数可以表示为变桨角度和叶尖速比的函数。每一个确定的桨距角，都存在一个最大风力利用系数，随着桨距角的变大，最大风力利用系数降低,其对应的叶尖速比也降低。此分析可以看出机组的发电情况表现。
        </div>
      </el-alert>
      <div class="charts" v-if="diagramRelationsDatas">
        <template v-for="(itemChart, indChart) in diagramRelationsDatas">
          <TwoDMarkersChart
            :key="itemChart.fieldEngineCode"
            @getResult="getResult"
            @changeRequestNum="changeRequestNum"
            :result="requestResult"
            :index="indChart"
            :ref="itemChart.fieldEngineCode"
            :fileAddr="itemChart.fileAddr"
          >
          </TwoDMarkersChart>
        </template>
      </div>
      <el-empty description="暂无分析记录" v-else></el-empty>
      <el-tabs value="first">
        <el-tab-pane label="意见描述" name="first">
          <TinymceEditor
            ref="editor"
            v-model="comment"
            @input="handleEditorInput($event)"
            @onClick="onClick"
          >
          </TinymceEditor>
        </el-tab-pane>
      </el-tabs>
      <el-row type="flex" class="row-bg" justify="end">
        <el-col :span="2" style="margin: 20px">
          <el-button type="primary" size="small">提交评论</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="right">
      <DicCard
        :batchCode="initBatchCode"
        :analysisTypeCode="analysisTypeCode"
        :commentDescriptionVos="commentDescriptionVos"
      >
      </DicCard>
    </div>
  </div>
</template>
<script>
import DicCard from "@/views/overview/components/dicCard/index.vue";
import FilterChart from "@/views/overview/components/filterChart/index.vue";
import TwoDMarkersChart from "@/views/performance/components/chartsCom/TwoDMarkersChart.vue";
import TinymceEditor from "@/components/Tinymce.vue";
import { analysisDetail, queryAnalysisedEngine } from "@/api/performance";

export default {
  name: "yaw_error_density",
  components: {
    DicCard,
    FilterChart,
    TwoDMarkersChart,
    TinymceEditor,
  },
  props: {
    initBatchCode: {
      default: "",
      type: String,
    },
    analysisTypeCode: {
      default: "",
      type: String,
    },
    batchCodeList: {
      default: "",
      type: Array,
    },
  },
  data() {
    return {
      form: {
        value2: "",
      },
      windEngineGroupList: [], //批次风机列表
      fieldEngineCodes: [], //选中风机
      comment: "",
      options: [],
      generalFilesDatas: [], //总图
      diagramRelationsDatas: [], //分图,
      requestResult: [], // 请求结果
      requestRecord: [],
      commentDescriptionVos: [], //评论列表
    };
  },
  watch: {
    initBatchCode(newVal) {
      if (newVal) {
        this.fetchData(); // 调用合并后的函数
      }
    },
    analysisTypeCode(newVal) {
      if (newVal) {
        this.fetchData(); // 调用合并后的函数
      }
    },
  },
  mounted() {
    if (this.initBatchCode && this.analysisTypeCode) {
      this.fetchData(); // 调用合并后的函数
    }
  },
  methods: {
    getResult({ index, result }) {
      console.log(index, result);
      this.$set(this.requestResult, index, result);
      // this.requestResult[index] = result
      this.requestRecord[index] = result;
    },
    changeRequestNum(index) {
      if (index <= 1) {
        this.$set(this.requestRecord, index, "start");
        return;
      }
      if (index > 1) {
        if (
          this.requestRecord.every((item) =>
            ["success", "error"].includes(item)
          )
        ) {
          this.$set(this.requestRecord, index, "start");
          this.$set(this.requestResult, index, "start");
        } else {
          this.$set(this.requestRecord, index, "start");
        }
      }
    },
    onSubmit() {
      console.log("submit!");
    },
    // 合并后的函数，处理数据请求
    async fetchData() {
      try {
        console.log(
          this.initBatchCode,
          this.analysisTypeCode,
          "请求详情 分钟级"
        );
        // 获取分析详情
        await this.getAnalysisDetail();

        // 获取风机列表
        await this.getWindEnfineList(this.initBatchCode, this.analysisTypeCode);
      } catch (err) {
        console.error("Failed to fetch data:", err);
      }
    },
    // 获取分析详情接口
    async getAnalysisDetail() {
      try {
        const result = await analysisDetail({
          batchCode: this.initBatchCode,
          analysisTypeCode: this.analysisTypeCode,
          fieldEngineCodes:
            this.fieldEngineCodes.length === 0
              ? undefined
              : this.fieldEngineCodes,
        });
        //当前评论展示获取
        if (
          result.data &&
          result.data[0] &&
          result.data[0].commentDescriptionVos
        ) {
          this.commentDescriptionVos = result.data[0].commentDescriptionVos;
        }
        this.generalFilesDatas =
          result.data && result.data[0] && result.data[0].generalFiles; //总图数据
        this.diagramRelationsDatas =
          result.data && result.data[0] && result.data[0].diagramRelations;
      } catch (err) {
        console.error("Failed to fetch analysis details:", err);
      }
    },

    // 请求风机列表
    async getWindEnfineList(batchCode, analysisTypeCode) {
      // console.log("请求风机列表 分钟级");
      const resEngineList = await queryAnalysisedEngine({
        batchCode: batchCode,
        analysisTypeCode,
      });
      this.windEngineGroupList = resEngineList.data;
    },
    handleEditorInput(index, newVal) {
      // 更新对应的 comment 值
      // 如果该功能没有实现，可以删除这个方法
    },
    //获取选中风机list
    getEnfineList(data) {
      this.fieldEngineCodes = data;
      console.log(this.fieldEngineCodes, "this.fieldEngineCodes");
    },
    //下一条
    handleNext() {
      const index = this.batchCodeList.findIndex(
        (item) => item === this.initBatchCode
      );
      if (index === this.batchCodeList.length - 1) {
        this.$message.warning("已经是最后一个分析结果了");
        return;
      }
      this.$emit("setInitBathCode", this.batchCodeList[index + 1]);
    },
    //上一条
    handlePrevious() {
      const index = this.batchCodeList.findIndex(
        (item) => item === this.initBatchCode
      );
      if (index === 0) {
        this.$message.warning("没有上一条了");
        return;
      }
      this.$emit("setInitBathCode", this.batchCodeList[index - 1]);
    },
    onClick() {},
  },
};
</script>
<style scoped lang="scss">
.type-variable {
  display: flex;
  height: 90%;
  overflow: hidden;

  .left {
    width: 30%;
    height: 100%;
    overflow: auto;
    padding: 20px;
    flex: 1;
  }

  .right {
    width: 250px;
    height: 100%;
    overflow: hidden;
  }
}
</style>
