<template>
  <!-- 漏缆实时状态 -->
  <div class="leakage-cable">
    <div class="leakage-top">
      <div></div>
      <div class="operate-btn">
        <el-button type="primary" @click="getTableData">刷新</el-button>
        <el-button type="primary">查询</el-button>
        <el-button type="primary" @click="toExport">导出</el-button>
      </div>
    </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="startPointConnectStatus_text" label="连接状态" align="center" />
      <el-table-column prop="endPointDeviceName" label="网元设备" align="center" />
      <el-table-column prop="endPointConnectStatus_text" label="连接状态" align="center" />
      <el-table-column prop="lastPointDeviceName" label="网元设备" align="center" />
      <el-table-column prop="uploadTime" label="上传时间" align="center" />
    </el-table>
    <Pagination
      :limit="params.size"
      :page="params.current"
      :total="total"
      class="pagination"
      @pagination="handlePageChange"
    />
  </div>
</template>

<script>
import { DeviceStatusList, DeviceStatusExport } from './api'
import { ConnectStatusEnum } from '@/const/index'

export default {
  data() {
    return {
      ConnectStatusEnum,
      params: {
        current: 1,
        size: 10
      },
      total: 10,
      tableData: [],
    };
  },
  methods: {
    // 表格背景图颜色
    cellClassFn({ row, column, rowIndex, columnIndex }) {
      if ((row.startPointConnectStatus_text == '连接正常' && column.property == 'startPointConnectStatus_text')) {
        return 'green'
      }
      if ((row.startPointConnectStatus_text == '连接异常' && column.property == 'startPointConnectStatus_text')) {
        return 'red'
      }
      if ((row.endPointConnectStatus_text == '连接正常' && column.property == 'endPointConnectStatus_text')) {
        return 'green'
      } else if ((row.endPointConnectStatus_text == '连接异常' && column.property == 'endPointConnectStatus_text')) {
        return 'red'
      }
      if (rowIndex % 2 == 1) {
        return 'stripe'
      }
    },
    handlePageChange(pageData) {
      this.params.size = pageData.size;
      this.params.current = pageData.page;
      this.getTableData()
    },
    getTableData() {
      DeviceStatusList(this.params).then(res => {
        let list = res.records || []
        list.forEach(item => {
          item.startPointConnectStatus_text = this.ConnectStatusEnum[item.startPointConnectStatus]
          item.endPointConnectStatus_text = this.ConnectStatusEnum[item.endPointConnectStatus]
        });
        this.tableData = list
        this.total = res.total
      })
    },
    toExport() {
      DeviceStatusExport().then(res => {})
    }
  },
  mounted() {
    this.getTableData()
  },
};
</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 .red {
    background-color: #f00;
  }
  & ::v-deep .green {
    background-color: green;
  }
  .page {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    .current {
      margin: 0 20px;
    }
  }
}
</style>
