<template>
  <div class="leakage-cable">
    <!-- 设备连接状态 -->
    <div class="leakage-top">
      <div style="color: #666666" />
      <div class="operate-btn">
        <delids :multipleSelection2="multipleSelection" @del="toDelete"
          >删除</delids
        >
        <el-button type="primary" @click="refresh">刷新</el-button>
        <el-button type="primary" @click="isQuery = !isQuery">查询</el-button>
        <el-button type="primary" @click="toExport">导出</el-button>
        <el-button
          :type="isPermit == false ? 'primary' : 'info'"
          :disabled="isPermit"
          @click="delAll"
          >清空数据</el-button
        >
      </div>
    </div>
    <div v-if="isQuery">
      <search ref="reset" @search="search" />
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      height="calc(88vh - 150px)"
      :cell-class-name="cellClassFn"
      :header-cell-style="{ background: '#EAF1FE', color: '#666666' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        prop="startPointDeviceName"
        label="网元设备"
        align="center"
      />
      <el-table-column
        prop="pointConnectStatus_text"
        label="连接状态"
        width="150"
        align="center"
      />
      <el-table-column
        prop="endPointDeviceName"
        label="网元设备"
        align="center"
      />
      <el-table-column prop="uploadTime" label="上传时间" align="center" />
      <el-table-column prop="cancelTime" label="取消时间" align="center" />
      <el-table-column
        prop="theInterruptTime"
        label="连接中断时长"
        align="center"
      />
    </el-table>
    <Pagination
      :limit="params.size"
      :page="params.current"
      :total="total"
      class="pagination"
      @pagination="handlePageChange"
    />
  </div>
</template>
<script>
import {
  ConnectStatusList,
  ConnectStatusDelete,
  ConnectStatusDeleteAll
} from '../api';
import { ConnectStatusEnum } from '@/const/index';
import search from './components/search.vue';
import { exportConnectStatusHistory } from '@/api/export';
import download from '@/utils/download';
import { successAlert, warningAlert } from '@/utils/alert';
import { mapState } from 'vuex';

export default {
  components: {
    search
  },
  props: [],
  data() {
    return {
      multipleSelection: [],
      ConnectStatusEnum,
      params: {
        current: 1,
        size: 10
      },
      total: 10,
      tableData: [],
      isQuery: false,
      istrue: 0,
      searchOption: {},
      exids: []
    }
  },
  computed: {
    ...mapState('user', ['userBaseInfo']),
    isPermit() {
      if (this.userBaseInfo.userId === 1) {
        return false;
      } else {
        return true;
      }
    }
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    // 多选操作
    handleSelectionChange(val) {
      let deleteIds = val.map((item) => item.id);
      this.multipleSelection = deleteIds;
    },
    // 分页更改
    handlePageChange(pageData) {
      this.params.size = pageData.size;
      this.params.current = pageData.page;
      this.getTableData();
    },
    // 刷新
    refresh() {
      this.$refs.reset !== undefined
        ? this.$refs.reset.reset()
        : this.getTableData();
    },
    // 查询
    search(option) {
      this.istrue = 1;
      this.searchOption = option;
      this.getTableData();
    },
    // 获取数据
    getTableData() {
      let params = {
        ...this.params,
        ...this.searchOption
      }
      ConnectStatusList(params).then((res) => {
        let list = res.records || []
        list.forEach((item) => {
          item.pointConnectStatus_text =
            this.ConnectStatusEnum[item.pointConnectStatus];
        })
        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;
        }
      })
    },
    // 删除
    toDelete() {
      ConnectStatusDelete({ ids: this.multipleSelection }).then((res) => {
        this.$message.success('删除成功!');
        this.getTableData();
      })
    },
    // 清空
    delAll() {
      this.$confirm('继续操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ConnectStatusDeleteAll().then((res) => {
          this.$message.success('清空成功!');
          this.getTableData();
        })
      }).catch(() => {
        warningAlert('取消删除');
      })
    },
    // 表格背景
    cellClassFn({ row, column, rowIndex, columnIndex }) {
      if (
        row.pointConnectStatus_text === '连接异常' &&
        column.label === '连接状态'
      ) {
        return 'emergency';
      } else if (
        row.pointConnectStatus_text === '连接正常' &&
        column.label === '连接状态'
      ) {
        return 'normal';
      }
      if (rowIndex % 2 === 1) {
        return 'stripe';
      }
    },
    // 导出
    toExport() {
      if (this.exids.length === 0) {
        this.$message.warning('暂无数据');
        return false;
      } else {
        exportConnectStatusHistory({ ids: this.exids }).then((res) => {
          download(res, 'vnd.ms-excel', `设备连接历史状态.xls`);
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.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 .emergency {
    background-color: #f00 !important;
  }
  & ::v-deep .important {
    background-color: #f89850 !important;
  }
  & ::v-deep .normal {
    background-color: green !important;
  }
  .page {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    .current {
      margin: 0 20px;
    }
  }
}
</style>