<template>
  <div>
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <div class="nav">
        <div
          v-for="(item, index) in navtopData"
          :key="index"
          :class="{ 'nav-item-active': activeNav == index }"
          @click="clickNav(item, index)"
          class="nav-item"
          style="font-size: 12px"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="mostHead mostHead-cid" v-if="activeNav == 0">
        <div>
          <span class="scd-title"> CID文件一次性检测 </span>
          <span class="export-bottom" @click="portExcel">结果导出Excel</span>
        </div>
        <span class="closeX" @click="closeX">×</span>
      </div>
      <el-tab-pane label="网络结构图" name="first">
        <net-work v-if="activeName == 'first' && activeNav == null"></net-work>
      </el-tab-pane>
      <el-tab-pane label="SCD可视化" name="second">
        <scd-visual v-if="!clickNavCode"></scd-visual>
      </el-tab-pane>
    </el-tabs>
    <div>
      <!-- CID一致性校核========= -->
      <div
        class="disappear"
        v-if="activeNav == 0"
        v-loading="isCidLoading"
        element-loading-text="数据加载中，请耐心等待..."
      >
        <!-- 左侧树形数据 -->
        <div style="width: 15%">
          <CidTree
            @treeBack="treeBack"
            @clickCode="clickCode"
            :allCidStart="allCidStart"
            :upName="upName"
          ></CidTree>
        </div>
        <!-- 右侧展示图 -->
        <div
          style="
            width: 85%;
            height: calc(100vh - 280px);
            margin: 20px 0 0 16px;
            background: #f7f8fb;
          "
        >
          <div class="abBox">
            <div>
              <span style="border-right: 1px solid #516380; padding-right: 30px"
                >基准文件:
                <span style="color: #09162c">{{ newChicken }}</span></span
              >
            </div>
            <div style="padding-left: 30px">
              <span
                >对比文件:<span style="color: #09162c">{{ upName }}</span></span
              >
            </div>
            <div class="uploadBox">
              <el-upload
                ref="upload"
                class="upload-demo"
                :http-request="nowUpload"
                :limit="2"
                element-loading-text="上传中..."
                element-loading-background="rgba(255, 255, 255, 0.8)"
                :on-remove="handleRemove"
                :on-change="fileSuccess"
                :show-file-list="false"
              >
                <template #trigger>
                  <el-button
                    style="border-bottom: 1px solid #255ce7; color: #255ce7"
                    type="text"
                    plain
                    >上传CID</el-button
                  >
                </template>
              </el-upload>
            </div>
          </div>
          <div v-if="!upName" class="tips-cid">请上传CID文件进行对比</div>
          <Gsix
            v-if="upName && clickCodeValue != 'scd.ied.Relation' && !isContrast"
            @clickCode="clickCode"
            :clickList="backName"
            :clickCodeInfoCid="clickCodeInfoCid"
            :isCid="isCid"
            :clickCodeValue="clickCodeValue"
            :uploadAnniu="uploadAnniu"
          ></Gsix>
          <!-- 对比失败 -->
          <div v-else-if="isContrast" class="fail-contrast">
            {{ isContrastDes }}
          </div>
        </div>
      </div>
      <!-- SCD一致性校核========= -->
      <div class="scdCheck" v-if="activeNav == 1">
        <div class="mostHead mostHead-scd" v-if="activeNav == 1">
          <div class="scd-title">SCD文件一次性检测</div>
          <span class="closeX" @click="closeX">×</span>
        </div>
        <!-- 右侧内容 -->
        <div
          style="
            width: 97%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 5px;
          "
        >
          <div class="treeBtn">
            <scdTree
              :nowStation="nowStation"
              @scdTreeBack="scdTreeBack"
              @scdView="scdView"
              @scdJpadList="scdJpadList"
            ></scdTree>
          </div>
          <div class="scdMap">
            <scdMap
              :antherBack="antherBack"
              :scdipadMini="scdipadMini"
              :activeNav="activeNav"
              :clickView="clickView"
            ></scdMap>
          </div>
        </div>
      </div>
      <CrcCheck
        v-if="activeNav == 3"
        :nowScdId="nowScdId"
        @sclBack="sclBack"
      ></CrcCheck>
      <SlcCheck
        v-if="activeNav == 2"
        :nowScdId="nowScdId"
        @sclBack="sclBack"
      ></SlcCheck>
      <!-- 虚端子关系图 -->
      <div v-if="clickNavCode == 'virtual'">
        <virtual-table  @sclBack="sclBack"></virtual-table>
      </div>
    </div>
  </div>
</template>
  

<script setup>
import { ref, watch, onMounted } from "vue";
import netWork from "./components/netWork";
import scdVisual from "./components/scdVisual";
import CidTree from "./components/CidTree.vue";
import Gsix from "./components/Gsix.vue";
import scdTree from "./components/scdTree.vue";
import scdMap from "./components/scdMap.vue";
import VirtualTable from "./components/VirtualTable.vue";
import CrcCheck from "./newTitle/CrcCheck";
import SlcCheck from "./newTitle/SlcCheck.vue";
import { useRoute } from "vue-router";
import cid from "@/api/cid/cid";
import { ElMessage, ElMessageBox, ElNotification } from "element-plus";
import systemRow from "@/api/systemRow";
import scdCheck from "@/api/scdCheck/scdCheck";
let route = useRoute();
const isCid = ref(true); //判断是否是CID界面
const isCidLoading = ref(false); //判断是否正在解析CID文件
const isContrast = ref(false); //判断是否对比成功
const isContrastDes = ref(false); //对比错误的描述
const activeName = ref("first"); //默认展示网络结构图
const navtopData = ref([
  { name: "CID一致性校核", code: "CID" },
  { name: "SCD文件一致性校核", code: "SCD" },
  { name: "SCL文件校核", code: "SCL" },
  { name: "CRC校核", code: "CRC" },
  { name: "虚端子关系表", code: "virtual" },
]);
const activeNav = ref(null);
const upload = ref(null); //上传按钮的ref
const backName = ref({}); //树组件返回名称
let mustVal = ref(false);
let upName = ref(""); //上传的文件名称
let arrName = ref(""); //基准的文件名称
let upFile = ref({}); //上传文件流
let backId = ref(""); //返回id
let nowScdId = ref(null); //路由传参的scdid
let antherBack = ref([]);
let nowStation = ref(""); //变电站id
watch(
  () => antherBack.value,
  (newVal) => {
    antherBack.value = newVal;
  },
  {
    deep: true,
  }
);
const reload = () => {
  nowScdId.value = route.query.id;
  nowStation.value = route.query.stationId;
};
//虚端子关系====
const isOpen = ref(false);
//虚端子关系====
const clickNavCode = ref(""); //点击导航栏的头部
const clickNav = (item, navIndex) => {
  //点击导航栏事件
  clickNavCode.value = item.code;
  activeNav.value = navIndex;
  switch (item.code) {
    case "virtual":
      isOpen.value = true;
      break;
  }
};
const handleClick = (val) => {
  activeNav.value = null;
  activeName.value = val.props.name;
  clickNavCode.value = "";
};
//点击左侧装置列表树返回的信息，
const newChicken = ref(""); //基准文件
const treeBack = (data, val) => {
  console.log("data", data);
  newChicken.value = `(${data.ied_name})${data.desc}`;
  isContrast.value = "";
  upName.value = "";
  backName.value = data; //返回的点击行的信息
  arrName.value = data;
  uploadAnniu.value = false;
  // backId.value = val.id;
};
const closeX = () => {
  activeName.value = "first";
  activeNav.value = null;
};
const allCidStart = ref([]);
const uploadAnniu = ref(false); //代表点击了上传按钮，用于清空上次的连线
const nowUpload = (file, e) => {
  ElMessageBox.confirm(
    "该操作耗时较长，校验开始后将清除原有校验结果且无法中止,确定开始吗?",
    "Warning",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(async () => {
      upload.value.clearFiles(); //清空上次上传的文件
      clickCodeInfoCid.value = [];
      uploadAnniu.value = true;
      upFile.value = file.file;
      upName.value = file.file.name;
      ElNotification({
        title: "信息",
        message: "正在进行CID的一致性比对...请稍候",
        duration: 0,
      });
      isCidLoading.value = true;
      cid
        .fileUpload({
          station_id: nowStation.value - 0,
          file: upFile.value,
          attachment_type: "cid",
          ied_name: backName.value.ied_name,
        })
        .then((res) => {
          if (res.code == 0) {
            allCidStart.value = [];
            let idp = `${backName.value.ied_name},${res.data.fileid}`;
            scdCheck
              .scdStart({
                type: "CID",
                station_id: route.query.stationId,
                source_scd_id: route.query.id,
                station_id: nowStation.value - 0,
                ids: idp,
              })
              .then((res) => {
                ElNotification.closeAll();
                if (res && res.code == 0) {
                  ElMessage({
                    type: "success",
                    message: "对比成功!",
                  });
                  allCidStart.value = res.data.list;
                  isCidLoading.value = false;
                  isContrast.value = res.data && res.data.list ? false : true; //对比成功并且有数据
                  isContrastDes.value =
                    res.data && res.data.list ? "" : "两个对比的CID没有差异";
                } else {
                  isCidLoading.value = false;
                  isContrast.value = true; //对比失败
                  isContrastDes.value = res.msg;
                }
              });
          } else {
            ElMessage({
              type: "error",
              message: res.msg,
            });
            isCidLoading.value = false;
          }
        });
    })
    .catch((err) => {});
};

const handleRemove = (file, e) => {};
const fileSuccess = (files, e) => {
  mustVal.value = true;
};
function scdTreeBack(data) {
  antherBack.value = data;
}
const clickView = ref(null);
//点击左侧树查看的当前数据行
const scdView = (row) => {
  clickView.value = row;
};
//点击差异项的code
const clickCodeValue = ref(null);
const clickCodeInfoCid = ref([]);
const clickCode = (code, e) => {
  clickCodeValue.value = code;
  if (e) {
    clickCodeInfoCid.value = e;
  }
  allCidStart.value = [];
};
const sclBack = (num) => {
  activeName.value = "first";
  activeNav.value = null;
};
// 点击左侧树查看返回图的数据
const scdipadMini = ref([]);
const scdJpadList = (data) => {
  scdipadMini.value = data;
};
const portExcel = () => {
  systemRow
    .portExcel({
      code: "scd-comp-result",
    })
    .then((res) => {
      if (res.data) {
        return;
      } else {
        ElMessage({
          message: res.msg,
          type: "info",
        });
      }
    });
};

onMounted(() => {
  reload();
  // Object.values(props.iedRelation)[0]
});
</script>

<style scoped lang="scss">
$height: 40px;

@mixin mid-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.el-tabs__item) {
  margin: 16px 0 0 32px;
  font-size: 18px;
}

:deep(.el-tabs__item:last-child) {
  margin-left: 0;
}

:deep(.el-tabs__nav-wrap::after) {
  --el-border-color-light: none;
}

:deep(.el-tabs__item.is-active),
:deep(.el-tabs__active-bar) {
  color: #255ce7;
}

:deep(.el-tabs__active-bar) {
  background-color: #255ce7;
}

//设置导航栏样式
.nav {
  @include mid-center;

  .nav-item {
    width: 144px;
    height: $height;
    @include mid-center;
    margin-right: 8px;
    cursor: pointer;
    background: #fff url("~@/assets/image/instruct/navtop.png") no-repeat center;
    background-size: 144px $height;
  }

  .nav-item-active {
    background: #fff url("~@/assets/image/instruct/navtop_active.png") no-repeat
      center;
    background-size: 144px $height;
    color: #fff;
  }
}

//最外层的滚动条不要
:deep(.el-main) {
  // overflow: hidden;
}

//ied的高度
:deep(.network-wrap) {
  height: 70vh;
  overflow: auto;
}

// 头部导出excel
.mostHead {
  align-items: center;
  margin-left: 32px;
  width: 97%;
  height: auto;
  border-bottom: 1px solid #a3ade0;
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.mostHead-scd {
  margin-left: 0;
}
.closeX {
  font-size: 24px;
  color: #7484ab;
  cursor: pointer;
}

// CID显示
.disappear {
  width: 97%;
  height: calc(100vh - 260px);
  margin: 0 auto;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.abBox {
  width: 95%;
  height: 40px;
  position: relative;
  margin: 10px auto;
  display: flex;
  font-size: 14px;
  background: #fff;
  justify-content: center;
  align-items: center;
  color: #516380;
}

.uploadBox {
  position: absolute;
  top: 0px;
  right: 32px;
}

.scdCheck {
  width: 97.5%;
  height: calc(100vh - 205px);
  margin: 0 33px;
  // border: 1px solid red;
}

.treeBtn {
  width: 32%;
  // margin-right: 16px;
  height: calc(100vh - 205px);
  // border: 1px solid rebeccapurple;
}

.scdMap {
  width: 67%;
  height: calc(100vh - 205px);
  // border: 1px solid teal;
  background-color: #f6f8fa;
}
//scd一致性校验
.scd-title {
  display: inline-block;
  margin-right: 16px;
  font-size: 16px;
  color: #1a2447;
  padding-bottom: 12px;
}
.mostHead-cid {
  margin-top: 20px;
  padding-bottom: 8px;
  margin-bottom: 0;
  .export-bottom {
    cursor: pointer;
    display: inline-block;
    border: 1px solid #255ce7;
    background: #f6f9ff;
    color: #255ce7;
    border-radius: 0;
    padding: 5px 8px;
    font-size: 14px;
  }
}
.tips-cid {
  text-align: center;
  font-size: 14px;
  margin: 20px 0;
}
.fail-contrast {
  text-align: center;
  margin-top: 30px;
  color: red;
  font-size: 16px;
}
</style>