<template>
  <div class="history-warning">
    <div class="form-wrapper">
      <div class="search-wrapper">
        <div class="search-item">
          <span class="label">类型：</span>
          <div class="search-content">
            <el-select
              v-model="state.typeVal"
              style="width: 100px"
              clearable
              size="mini"
              placeholder="全部"
              popper-class="select"
              @change="
                () => {
                  getStationList();
                  typechange();
                }
              "
            >
              <el-option
                v-for="item in state.typeList"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="search-item">
          <span class="label">{{
            state.isshowwindturbineName ? "场站：" : "升压站："
          }}</span>
          <div class="search-content">
            <el-select
              v-model="state.stationId"
              style="width: 120px"
              clearable
              size="mini"
              placeholder="全部"
              popper-class="select"
              @change="getWindturbineList"
            >
              <el-option
                v-for="item in stationList"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="search-item" v-if="state.isshowwindturbineName">
          <span class="label">机组：</span>
          <div class="search-content">
            <el-select
              v-model="state.deviceId"
              style="width: 120px"
              clearable
              size="mini"
              placeholder="全部"
              popper-class="select"
            >
              <el-option
                v-for="item in state.windturbineList"
                :key="item.id"
                :value="item.id"
                :label="item.aname"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="search-item" v-if="state.isshowwindturbineName">
          <span class="label">型号：</span>
          <div class="search-content">
            <el-select
              v-model="state.modelId"
              style="width: 120px"
              clearable
              size="mini"
              placeholder="全部"
              popper-class="select"
            >
              <el-option
                v-for="item in modelList"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="search-item" v-if="state.isshowwindturbineName">
          <span class="label">部件：</span>
          <div class="search-content">
            <el-select
              v-model="state.components"
              style="width: 120px"
              clearable
              size="mini"
              placeholder="全部"
              popper-class="select"
              multiple
              collapse-tags
            >
              <el-option
                v-for="item in componentList"
                :key="item.id"
                :value="item.nemCode"
                :label="item.name"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="search-item">
          <span class="label">描述：</span>
          <div class="search-content"></div>
          <el-input
            v-model="state.description"
            placeholder="请输入..."
            size="mini"
          ></el-input>
        </div>
        <div class="search-item">
          <span class="label">日期：</span>
          <div class="search-content">
            <el-date-picker
              v-model="state.dateTime"
              size="mini"
              type="datetimerange"
              range-separator="-"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              start-placeholder="开始"
              end-placeholder="结束"
              popper-class="date-select"
            >
            </el-date-picker>
          </div>
        </div>
      </div>
      <div class="btns">
        <el-button class="buttons" round size="mini" @click="getAlarmHistoryt"
          >查询</el-button
        >
        <el-button
          size="mini"
          class="buttons"
          @click="export2Excel"
          round
          :disabled="state.tableData?.length == 0 ? true : false"
        >
          导出</el-button
        >
        <!-- <el-button
          class="buttons"
          round
          size="mini"
          :disabled="!state.tableData?.length"
          @click="confirmItem(state.tableData)"
          >确认本页</el-button
        > -->
      </div>
    </div>

    <div class="table-wrapper">
      <div class="leftContent">
        <span>{{ pageTitle }}</span>
      </div>
      <el-table
        size="mini"
        :data="state.tableData"
        class="warpperTable"
        style="width: 100%; height: calc(100% - 35px - 55px)"
        stripe
      >
        <template v-if="state.isshowwindturbineName">
          <el-table-column
            v-for="item in state.tableHeader"
            :label="item.title"
            :prop="item.code"
            :key="item.code"
            :width="item.width || ''"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              <p :style="item.style && item.style(scope.row)">
                <span v-if="item.code == 'rank'">
                  {{ tableFilter(scope.row.rank) }}
                </span>
                <span v-else-if="item.code == 'alarmtype'">
                  {{ tableFilter(scope.row.alarmtype) }}
                </span>
                <span v-else-if="item.code == 'ts'">
                  {{ formatTime(scope.row.ts) }}
                </span>
                <span
                  :style="`color:${
                    scope.row.confirmed ? '#05bb4c' : 'var(--el-color-danger)'
                  }`"
                  v-else-if="item.code == 'confirmed'"
                >
                  {{ scope.row.confirmed ? "是" : "否" }}
                </span>
                <span v-else-if="item.code == 'components'">
                  <!-- {{ getComponentsName(scope.row.components) }} -->
                  {{ scope.row.components }}
                </span>
                <span v-else>
                  {{
                    scope.row[item.code] != "NULL" ? scope.row[item.code] : "--"
                  }}
                </span>
              </p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="100"
            header-align="center"
            align="center"
          >
            <template #default="scope">
              <el-button
                type="text"
                :style="`color: ${
                  $store.state.theme ? '#1890ff' : '#47aee7'
                } !important`"
                @click="confirmItem([scope.row])"
                >确认本条</el-button
              >
            </template>
          </el-table-column>
        </template>
        <template v-else>
          <el-table-column
            v-for="item in state.tableHeader1"
            :label="item.title"
            :prop="item.code"
            :key="item.code"
            :width="item.width || ''"
            show-overflow-tooltip
            header-align="center"
          >
            <template #default="scope">
              <p :style="item.style && item.style(scope.row)">
                <span v-if="item.code == 'rank'">
                  {{ tableFilter(scope.row.rank) }}
                </span>
                <span v-else-if="item.code == 'alarmtype'">
                  {{ tableFilter(scope.row.alarmtype) }}
                </span>
                <span v-else-if="item.code == 'ts'">
                  {{ formatTime(scope.row.ts) }}
                </span>
                <span
                  :style="`color:${
                    scope.row.confirmed ? '#05bb4c' : 'var(--el-color-danger)'
                  }`"
                  v-else-if="item.code == 'confirmed'"
                >
                  {{ scope.row.confirmed ? "是" : "否" }}
                </span>
                <span v-else>
                  {{
                    scope.row[item.code] != "NULL" ? scope.row[item.code] : "--"
                  }}
                </span>
              </p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="100"
            header-align="center"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="text"
                style="color: #05bb4c"
                @click="confirmItem([scope.row])"
                >确认本条</el-button
              >
            </template>
          </el-table-column>
        </template>
      </el-table>
      <div class="pagination-wrapper">
        <el-pagination
          layout="total, sizes, prev, pager, next"
          :current-page="query.page"
          :page-size="query.limit"
          :page-sizes="[21, 100, 500, 1000]"
          :total="query.pageTotal"
          @size-change="
            (value) => {
              query.limit = value;
              query.page = 1;
              getAlarmHistoryt();
            }
          "
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script setup name="historyWarning">
import { watch, reactive, nextTick, computed, onMounted, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import BASE from "@/tools/basicTool.js";
import dayjs from "dayjs";
import {
  alarm_history,
  new_alarm_history,
  fetchWindturbineList,
  fetchModel,
  fetchRelatePartAndAlarmType,
  getWpList,
  confirmAlart,
} from "@/api/zhbj/index.js";
import { ElMessageBox, ElMessage } from "element-plus";
import { outExportExcel } from "@/tools/excel/exportExcel.js"; //引入文件
import { useStore } from "vuex";
const pageTitle = "报警查询";
const store = useStore();

const route = useRoute();

const getComponentsName = (components) => {
  const listItem =
    componentList?.value?.find((ele) => {
      return ele.nemCode === components;
    }) || {};
  return listItem?.name || "";
};

onMounted(() => {
  state.dateTime = [
    dayjs().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
    dayjs().format("YYYY-MM-DD HH:mm:ss"),
  ];
  if (route.query.deviceType != "booststation") {
    state.deviceId = route.query.deviceId || "";
    state.alarmId = route.query.alarmId || "";
    state.typeVal = route.query.deviceType || "windturbine";
    state.modelId = route.query.modelId || "";
  } else {
    state.stationName = route.query.deviceId;
    state.deviceId = "";
    state.alarmId = route.query.alarmId || "";
    state.typeVal = route.query.deviceType || "booststation";
  }
  state.isshowwindturbineName = state.typeVal == "booststation" ? false : true;
  //   if (route.query.ts) {
  //     state.dateTime = [
  //       `${dayjs(Number(route.query.ts)).format("YYYY-MM-DD")} 00:00:00`,
  //       dayjs(Number(route.query.ts)).format("YYYY-MM-DD HH:mm:ss"),
  //     ];
  //   }

  

  getStationList(); //场站
  // getAlarmHistoryt();

  getequipmentmodel_list(); //机型
  getfetchRelatePart(); //部件
});
// 机型
const getequipmentmodel_list = async () => {
  const { data } = await fetchModel();
  state.modelListAll = data;
};
//所属部件
const getfetchRelatePart = async () => {
  const { data } = await fetchRelatePartAndAlarmType();
  state.fetchListAll = data;
};

const confirmItem = (alarmItem) => {
  ElMessageBox.confirm("您确定要执行此操作吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      confirmAlart(alarmItem)
        .then((res) => {
          if (res.code === 200) {
            ElMessage.success("确认成功");
            store.commit("removeWarning", alarmItem);
            getAlarmHistoryt();
          }
        })
        .catch(() => {
          ElMessage.error("确认失败，请重试");
        });
    })
    .catch(() => {});
};

const getColumnStyle = (columnItem) => {
  let style = "color:";
  if (columnItem.endts) {
    style += " #05bb4c";
  } else {
    style += "var(--el-color-danger)";
  }
  return style;
};

const state = reactive({
  typeList: [
    {
      label: "升压站",
      value: "booststation",
    },
    // {
    //   label: "自定义",
    //   value: "custom",
    // },
    {
      label: "风机",
      value: "windturbine",
    },
    // {
    //   label: "光伏",
    //   value: "inverter",
    // },
  ],
  typeVal: "windturbine",
  stationId: "NX_FGS_HA_FDC_STA",
  stationName: "",
  alarmId: "",
  windturbineList: [],
  deviceId: "",
  modelListAll: {},
  fetchListAll: {},
  modelId: "", //型号
  components: [], //部件
  description: "", //描述
  dateTime: [],
  startDate: null,
  endDate: null,
  tableData: [],
  isshowwindturbineName: true,
  ts: "",
  tableHeader: [
    { title: "时间", code: "ts", width: "150" },
    { title: "场站", code: "wpName", width: "120" },
    { title: "机组", code: "code", width: "100" },
    { title: "报警信息", code: "description" },
    // { title: "故障原因", code: "faultCause" },
    // { title: "级别", code: "rank", width: "80" },
    { title: "部件类型", code: "components", width: "120" },
    // { title: "故障解决方法", code: "resolvent" },
    { title: "报警解除时间", code: "endtsName", width: "150" },
    {
      title: "状态",
      code: "isCloseName",
      //   width: "80",
      style: getColumnStyle,
      width: 100,
    },
    { title: "是否确认", code: "confirmed", width: "100" },
    { title: "类型", code: "alarmTypeName", width: "80" },
  ],
  tableHeader1: [
    { title: "时间", code: "ts", width: "150" },
    { title: "升压站", code: "stationname", width: "150" },
    { title: "报警信息", code: "description" },
    // { title: "级别", code: "rank", width: "80" },
    { title: "报警解除时间", code: "endtsName", width: "150" },
    {
      title: "状态",
      code: "isCloseName",
      style: getColumnStyle,
      width: 100,
      //   width: "80",
    },
    { title: "是否确认", code: "confirmed", width: "100" },
    { title: "类型", code: "alarmTypeName", width: "80" },
  ],
});
// 场站列表/升压站列表
const stationList = ref([]);
//获取场站列表
const getStationList = async () => {
  const { data } = await getWpList(state.typeVal);
  stationList.value = data;
  if (state.deviceId && state.typeVal != "booststation") {
    let station = data.find((i) => {
      let st = i.id.split("_")[2];
      let dt = state.deviceId.split("_")[2];
      if (st == dt) {
        return i;
      }
    });
    // state.stationId = station?.id;
  } else if (state.typeVal == "booststation") {
    let station = data.find((i) => i.name == state.stationName);
    // state.stationId = station ? station?.id : data[0]?.id;
  } else {
    // state.stationId = data[0]?.id;
  }

  state.stationId = route.query.stationId || state.stationId;
  route.query.stationId ? (state.modelId = modelList.value?.[0]?.id || "") : "";
  if (stationList.value.length) {
    getWindturbineList();
  }
};

// watch(
//   () => stationList,
//   (val, old) => {
//     val?.value?.length &&
//       nextTick(async () => {
//         await getWindturbineList();
//       });
//   },
//   {
//     deep: true,
//     immediate: true,
//   }
// );
watch(
  () => route,
  (val, old) => {
    if (route.query.deviceType != "booststation") {
      state.deviceId = route.query.deviceId || "";
      state.alarmId = route.query.alarmId || "";
      state.typeVal = route.query.deviceType || "windturbine";
      state.modelId = route.query.modelId || "";
    } else {
      state.stationName = route.query.deviceId;
      state.deviceId = "";
      state.alarmId = route.query.alarmId || "";
      state.typeVal = route.query.deviceType || "booststation";
    }
    state.isshowwindturbineName =
      state.typeVal == "booststation" ? false : true;
    getStationList();
  },
  {
    deep: true,
    immediate: true,
  }
);
//型号列表
const modelList = computed(() => {
  if (state.typeVal != "booststation") {
    if (state.stationId == "") {
      return [];
    } else {
      state.modelId = route.query.deviceId ? route.query.modelId : "";
      return state.modelListAll[state.stationId];
    }
  } else {
    return [];
  }
});
//部件列表
const componentList = computed(() => {
  if (state.typeVal != "booststation") {
    if (state.stationId == "") {
      return [];
    } else {
      if (state.stationId.includes("FDC")) {
        return state.fetchListAll?.fjbj;
      } else {
        return state.fetchListAll?.gfbj;
      }
    }
  } else {
    return [];
  }
});
//get 风机
const getWindturbineList = async () => {
  state.deviceId = "";
  const { data } = await fetchWindturbineList(state.stationId || "");
  state.windturbineList = data;
  //   state.modelId = modelList.value?.[0]?.id || "";
  await getAlarmHistoryt();
};
const query = reactive({
  page: 1,
  limit: 1000,
  pageTotal: null,
});

// 获取历史记录表
const getAlarmHistoryt = async () => {
  BASE.showLoading();
  let params = {
    pageNum: query.page,
    pageSize: query.limit,
    alarmId: state.alarmId,
    alarmType: state.typeVal,
    stationid: state.stationId,
    deviceid: state.typeVal == "booststation" ? "" : state.deviceId,
    modelId: state.typeVal == "booststation" ? "" : state.modelId,
    components: state.components.toString(),
    description: state.description,
    begin: state.dateTime[0],
    end: state.dateTime[1],
  };
  const { data } = await alarm_history(params);
  BASE.closeLoading();
  if (data) {
    query.pageTotal = data?.total;
    data?.ls?.forEach((ele) => {
        ele.isCloseName = ele.endts ? "已解除" : "未解除";
        ele.alarmTypeName =
        ele.alarmType === "booststation"
            ? "升压站"
            : ele.alarmType === "windturbine"
            ? "风机"
            : ele.alarmType === "inverter"
            ? "光伏"
            : "";
        ele.endtsName = ele.endts > 0 ? formatTime(ele.endts) : "--";
    });
    state.tableData = data?.ls;
  } else {
    state.tableData = new Array(5).fill({
        tbName: null,
        alarmId: "SQ_0125",
        alarmType: "windturbine",
        characteristic: "报警",
        components: "BPQ",
        confirmed: false,
        description: "变频器故障27",
        deviceId: "SXJ_KGDL_XWT_F_WT_0014_EQ",
        devicename: "14号风机",
        deviceType: "windturbine",
        enabled: true,
        lineid: "SXJ_KGDL_XWTF01_LN",
        linename: "一号风机线",
        modelId: "SEC-W02B-1250kW",
        projectid: "SXJ_KGDL_XWTF01_EG",
        projectname: "一期项目",
        rank: 3,
        resettable: false,
        stationid: "",
        stationname: "13风电场",
        subcomponents: "NULL",
        suffix: null,
        tagid: "FD-YYXWT-ShangQi.140623F1412MDL01BF399DB0122SP01RAW00",
        triggertype: 1463,
        uniformcode: "BJ0001",
        superTableName: null,
        ts: 1718160795000,
        val: 1,
        oval: null,
        endts: 0,
        timelong: 0,
        name: null,
        nemCode: "状态码1463",
        faultCause: "13.7s>信号<=14.2s",
        resolvent: "输入端142功率上升延迟300s",
        wpName: "13风电场",
        code: "#14",
        wpId: null,
        faultType: null,
        id: "sxj_kgdl_xwt_f_wt_0014_eq_sq_0125",
    });
  }
  
};
//报警类型变化
const typechange = () => {
  state.alarmId = "";
  state.deviceId = "";
  state.isshowwindturbineName = state.typeVal == "booststation" ? false : true;
};

// 批量导出
const export2Excel = async () => {
  let params = {
    pageNum: query.page,
    pageSize: query.limit,
    alarmType: state.typeVal,
    stationId: state.stationId,
    deviceId: state.typeVal == "booststation" ? "" : state.deviceId,
    modelId: state.modelId,
    components: state.components,
    description: state.description,
    begin: state.dateTime[0],
    end: state.dateTime[1],
  };

  if (state.dateTime[1] - state.dateTime[0] > 6 * 24 * 60 * 60 * 1000) {
    this.$message({
      message: "导出时间范围不能大于7天",
      type: "warning",
    });
  } else {
    let tableHeader = [];
    let tableKey = [];
    // const { data } = await alarm_history(params);
    const data = { ls: state?.tableData || [] };
    if (state.isshowwindturbineName) {
      tableHeader = state.tableHeader.map((item) => item.title);
      tableKey = state.tableHeader.map((item) => item.code);
    } else {
      tableHeader = state.tableHeader1.map((item) => item.title);
      tableKey = state.tableHeader1.map((item) => item.code);
    }
    const stationName =
      stationList.value.find((ele) => {
        return ele.id === state.stationId;
      })?.name || "";
    const fileName = `${stationName} ${state.dateTime[0]} ~ ${state.dateTime[1]} 数据表`;
    outExportExcel(
      tableHeader,
      tableKey,
      data.ls.map((item) => {
        return {
          ...item,
          ts: formatTime(item.ts),
          rank: tableFilter(item.rank),
          alarmTypeName:
            item.alarmType === "booststation"
              ? "升压站"
              : item.alarmType === "windturbine"
              ? "风机"
              : item.alarmType === "inverter"
              ? "光伏"
              : "",
          endtsName: item.endts > 0 ? formatTime(item.endts) : "--",
          isCloseName: item.endts ? "已解除" : "未解除",
          confirmed: item.confirmed ? "是" : "否",
        };
      }),
      fileName
    );
    ElMessage.success(`导出成功!`);
  }
};
// 分页导航
const handlePageChange = (val) => {
  query.page = val;
  getAlarmHistoryt();
};
// 时间格式化
const formatTime = (val) => {
  return dayjs(val).format("YYYY-MM-DD HH:mm:ss");
};
// 格式化
const obj = {
  1: "低级",
  2: "低中级",
  3: "中级",
  4: "中高级",
  5: "高级",
  booststation: "升压站",
  inverter: "光伏",
  windturbine: "风机",
};
const messageTypeObj = {
  1: "触发",
  3: "解除",
};
const tableFilter = (val) => {
  return obj[val];
};
const messageTypeFilter = (val) => {
  return messageTypeObj[val];
};
</script>

<style scoped lang="less">
p {
  padding: 0;
  margin: 0;
}
.history-warning {
  height: calc(100% - 40px);
  padding: 0 20px;
  padding-bottom: 10px;
  .form-wrapper ::v-deep {
    display: flex;
    flex-direction: column;
    padding-top: 10px;
    position: relative;
    .search-wrapper {
      display: flex;
      align-items: center;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #b3b3b3;
      margin-bottom: 10px;
      .search-item {
        display: flex;
        margin-right: 10px;
        max-width: 450px;
        align-items: center;
        .label {
          margin-right: 10px;
          text-align: right;
          white-space: nowrap;
          // width: 60px;
        }
        .search-content {
          flex: 1;
        }
      }
    }

    .btns {
      display: flex;
      justify-content: flex-end;
      margin-right: 10px;
      position: absolute;
      right: 80px;
      top: 13px;
    }

    .buttons {
      background-color: rgba(5, 187, 76, 0.2);
      border: 1px solid #3b6c53;
      color: #b3b3b3;
      font-size: 14px;

      &:hover {
        background-color: rgba(5, 187, 76, 0.5);
        color: #ffffff;
      }
    }
  }
  .table-wrapper {
    height: calc(100% - 60px);
    width: calc(100% - 20px);
    .leftContent {
      width: 242px;
      height: 41px;
      display: flex;
      align-items: center;
      background: url("~@/assets/imgs/title_left_bg.png") no-repeat;

      span {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #05bb4c;
        margin-left: 25px;
      }
    }
    .warpperTable :deep {
      .el-table__body-wrapper{
        height: 100%;
        overflow-y: auto;
      }
    }
    .pagination-wrapper :deep {
      text-align: right;
      margin-top: 10px;
    }
  }
}
</style>
