<template>
  <!-- 设备连接告警 -->
  <div class="leakage-cable">
    <el-button-group>
      <el-button
        v-for="item in tabs"
        :key="item.key"
        :type="confirmStatus === item.key ? 'primary' : ''"
        @click="changeType(item)"
        >{{ item.label }}</el-button
      >
    </el-button-group>
    <div class="leakage-top">
      <div style="color: #666666"></div>
      <div class="operate-btn">
        <el-button type="primary" @click="refresh">刷新</el-button>
        <el-button type="primary" @click="isQuery = !isQuery">查询</el-button>
        <el-button type="primary" @click="exportList">导出</el-button>
      </div>
    </div>
    <div v-show="isQuery">
      <search @search="search" ref="reset" />
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      :cell-class-name="cellClassFn"
      :header-cell-style="{ background: '#EAF1FE', color: '#666666' }"
    >
      <el-table-column
        prop="startPointDeviceName"
        label="网元设备"
        align="center"
      />
      <el-table-column
        prop="connectStatusName"
        label="连接状态"
        align="center"
      />
      <el-table-column
        prop="endPointDeviceName"
        label="网元设备"
        align="center"
      />
      <el-table-column prop="uploadTime" label="上传时间" align="center" />
      <el-table-column prop="confirmPerson" label="确认人" align="center" />
      <el-table-column prop="confirmTime" label="确认时间" align="center" />
      <el-table-column fixed="right" label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="
              scope.row.confirmStatus === 1 &&
              scope.row.connectStatusName == '连接正常'
            "
            @click="cancelAlarm(scope.row)"
            >取消</el-button
          >
          <el-button
            type="text"
            v-else-if="scope.row.confirmStatus === 0"
            @click="confirmAlarm(scope.row)"
            >确认</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="告警详情"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <el-row class="flex row_obj">
        <div class="text_header">网元设备：</div>
        <div>{{ dialogInfo.startPointDeviceName }}</div>
      </el-row>
      <el-row class="flex row_obj">
        <div class="text_header">连接状态：</div>
        <div>{{ dialogInfo.connectStatusName }}</div>
      </el-row>
      <el-row class="flex row_obj">
        <div class="text_header">网元设备：</div>
        <div>{{ dialogInfo.endPointDeviceName }}</div>
      </el-row>
      <el-row class="flex row_obj">
        <div class="text_header">上传时间：</div>
        <div>{{ dialogInfo.uploadTime }}</div>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          v-if="
            dialogInfo.confirmStatus === 1 &&
            dialogInfo.connectStatusName === '连接正常'
          "
          @click="cancel(dialogInfo)"
          >取消告警</el-button
        >
        <el-button
          v-else-if="dialogInfo.confirmStatus !== 1"
          type="primary"
          @click="confirm(dialogInfo)"
          >确认告警</el-button
        >
      </span>
    </el-dialog>
    <Pagination
      :limit="params.size"
      :page="params.current"
      :total="total"
      class="pagination"
      @pagination="handlePageChange"
    />
  </div>
</template>

<script>
import { deviceList, deviceConfirm, deviceCancel } from "../api";
import { exportConnet } from "@/api/export";
import search from "./components/search.vue";
import download from "@/utils/download";
import { successAlert, warningAlert } from "@/utils/alert";

export default {
  name:'device',
  components: { search },
  data() {
    return {
      alarmInfo: "",
      dialogInfo: [],
      centerDialogVisible: false,
      confirmStatus: 0,
      params: {
        current: 1,
        size: 10
      },
      total: 10,
      tableData: [],
      tabs: [
        {
          label: "未确认",
          key: 0
        },
        {
          label: "已确认",
          key: 1
        }
      ],
      isQuery: false,
      istrue: 0,
      searchOption: {},
      exids: []
    }
  },
  created() {
    this.getTableData()
    this.searchOption = this.$route.query
  },
  methods: {
    // 表格背景图颜色
    cellClassFn({ row, column, rowIndex, columnIndex }) {
      if (
        row.connectStatusName === "连接正常" &&
        column.property === "connectStatusName"
      ) {
        return "green";
      }
      if (
        row.connectStatusName === "连接异常" &&
        column.property == "connectStatusName"
      ) {
        return "red";
      }
      if (rowIndex % 2 === 1) {
        return "stripe";
      }
    },
    // 刷新
    refresh() {
      this.$refs.reset != undefined
        ? this.$refs.reset.reset()
        : this.getTableData();
    },
    // 分页更改
    handlePageChange(pageData) {
      this.params.size = pageData.size;
      this.params.current = pageData.page;
      this.getTableData();
    },
    // 查询
    search(option) {
      this.istrue = 1;
      this.searchOption = option;
      this.getTableData();
    },
    // 获取数据
    getTableData() {
      let param = {
        confirmStatus: this.confirmStatus,
        ...this.params,
        ...this.searchOption
      };
      deviceList(param).then((res) => {
        let list = res.records || [];
        this.tableData = list;
        this.total = res.total;
        this.exids = list.map((i) => i.id);
        if (this.istrue == 1) {
          if (this.tableData.length != 0) {
            successAlert("操作成功");
          } else {
            warningAlert("查询结果为空");
          }
          this.istrue = 0;
        }
      });
    },
    // 表格确认告警
    confirmAlarm(row) {
      this.centerDialogVisible = true;
      this.dialogInfo = row;
    },
    // 编辑确认告警
    confirm(dialogInfo) {
      let id = dialogInfo.id;
      deviceConfirm({ id: id }).then((res) => {
        this.getTableData();
        this.$store.dispatch('alarm/getDevice');
      });
      this.centerDialogVisible = false;
    },
    // 表格取消告警
    cancelAlarm(row) {
      this.centerDialogVisible = true;
      this.dialogInfo = row;
    },
    // 编辑取消告警
    cancel(dialogInfo) {
      let query = {
        connectStatusName: dialogInfo.connectStatusName,
        id: dialogInfo.id
      };
      deviceCancel(query).then((res) => {
        this.getTableData();
      });
      this.centerDialogVisible = false;
    },
    // 切换是否确认告警
    changeType(item) {
      this.confirmStatus = item.key;
      this.params.current = 1;
      this.getTableData();
    },
    // 导出
    exportList() {
      if (this.exids.length == 0) {
        this.$message.warning("暂无数据");
        return false;
      } else {
        exportConnet({ ids: this.exids }).then((res) => {
          download(res, "vnd.ms-excel", `设备连接告警.xls`);
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.row_obj {
  text-align: center;
  margin-top: 20px;
  div {
    font-size: 16px;
    font-weight: 700;
    color: #333333;
  }
  .text_header {
    width: 200px;
    font-weight: 700;
    color:#666666
  }
}
.leakage-cable {
  .leakage-top {
    margin-bottom: 20px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }
  & ::v-deep .cell {
    color: #333333;
  }
  & ::v-deep .stripe {
    background-color: #eaf1fe;
  }
  & ::v-deep .red {
    background-color: #f00;
  }
  & ::v-deep .green {
    background-color: green;
  }
  .page {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    .pageNum {
      margin: 0 20px;
    }
  }
}
.tips {
  width: 400px;
  height: 80px;
  background: #fff;
  box-shadow: 0px 2px 14px #888888;
  border-radius: 5px;
  padding: 0 20px 20px 20px;
  position: absolute;
  text-align: center;
  top: calc(50% - 40px);
  left: calc(50% - 200px);
}
</style>
