<template>
  <div class="message-box-head">
    <el-tabs v-model="chooseMenu" type="card">
      <el-tab-pane :label="$t('message.messageList.messageStatistics')" name="1">
        <div :style="{width: '28.75rem', height: '18.75rem'}" ref="areaMesChart">
        </div>
      </el-tab-pane>
      <el-tab-pane :label="$t('message.messageList.messageDetails')" name="2">
        <el-table :data="messagebox.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
                  style="width: 100%"
                  height="400">
          <el-table-column prop="shipName"
                           :label="$t('message.messageList.shipName')"
                           width="150"
                           align="center"
                           show-overflow-tooltip
                           sortable>
          </el-table-column>
          <el-table-column sortable
                           show-overflow-tooltip
                           prop="areaName"
                           :label="$t('message.messageList.port')"
                           width="100"
                           align="center">
          </el-table-column>
          <el-table-column prop="eventType"
                           :label="$t('message.messageList.status')"
                           width="70"
                           align="center">
            <template slot-scope="scope">
              {{ status(scope.row.status) }}
            </template>
          </el-table-column>
          <el-table-column sortable
                           prop="eventTime"
                           label="更新时间"
                           align="center">
            <template slot-scope="scope">
              {{ scope.row.updateTime }}
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="pageSizeChange"
          @current-change="pageCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="messagebox.length">
        </el-pagination>
      </el-tab-pane>
    </el-tabs>

  </div>
</template>

<script lang="ts">
import {Component, Vue, Watch} from 'vue-property-decorator'
import * as Echarts from "echarts";

@Component({
  props: {
    message: {
      type: Array,
      required: true
    }
  }
})
export default class MessageList extends Vue {
  private tableData: any[] = []
  private sourceData: any[] = []
  private messagebox: any[] = []
  private mesMapEchartData: any[] = []
  private areaChartECharts: any
  private isLeave = false
  private chooseMenu = '1'
  private currentPage = 1
  private pageSize = 10

  @Watch('message', {deep: true, immediate: true})
  messageData(data: any) {
    if (data) {
      this.messagebox = data
      this.mesMapEchartData = this.getMeslist(data);
    }
  }

  public mounted() {
    this.areaChartECharts = Echarts.init(this.$refs.areaMesChart as HTMLElement);
    this.initEcharts()
    window.addEventListener('setItemEvent', (e: any) => {
      if (this.chooseMenu === '1' && e.key === 'language') {
        this.areaChartECharts.clear()
        this.initEcharts()
      }
    })
  }

  private pageSizeChange(val: any) {
    this.pageSize = val
  }

  private pageCurrentChange(val: any) {
    this.currentPage = val
  }

  private initEcharts() {
    const options = {
      legend: {},
      tooltip: {},
      dataset: {
        dimensions: ['areaName', this.$t('message.messageList.arrive'), this.$t('message.messageList.leave2')],
        source: this.mesMapEchartData
      },
      xAxis: {
        type: 'category',
        axisLabel: {
          rotate: 45
        }
      },
      yAxis: {
        show: true,
        minInterval: 1
      },
      series: [{type: 'bar'}, {type: 'bar'}]
    }
    this.areaChartECharts.setOption(options);
  }

  private getMeslist(meslist: any) {
    // areaName eventType 1:到达 2离开
    let mesMapEchartData: any[] = [];
    let mesMap = new Map();
    meslist.forEach((item: any) => {
      let itemMap = mesMap.get(item.areaName);
      let itemArriveNum = 0;
      let itemLeaveNum = 0;
      if (item.status === 1) {
        itemArriveNum = 1;
      } else {
        itemLeaveNum = 1;
      }
      if (itemMap) {
        mesMap.set(item.areaName, {
          areaName: item.areaName,
          arriveNum: itemMap.arriveNum + itemArriveNum,
          leaveNum: itemMap.leaveNum + itemLeaveNum
        })
      } else {
        mesMap.set(item.areaName, {
          areaName: item.areaName,
          arriveNum: itemArriveNum,
          leaveNum: itemLeaveNum
        })
      }
    });
    mesMap.forEach((value, key) => {
      mesMapEchartData.push([value.areaName, value.arriveNum, value.leaveNum])
    });
    return mesMapEchartData;
  }

  private status(val: any) {
    return val === 1 ? '到达' : '离开';
  }

  private leaveArea() {
    this.tableData = this.sourceData.filter((e) => e.eventType === 2)
    this.isLeave = true
  }

  private cancel() {
    console.log()
  }
}
</script>

<style scoped lang="scss">
.message-box-head {
  .header-btn {
    height: 2.5rem;
    display: flex;
    align-items: center;
    margin-bottom: 1.25rem;

    button {
      height: 100%;
      width: 7.5625rem;
      margin-left: 1.25rem;
      background-color: #FFFFFF;
      color: var(--aside-gbc);
      border: 0.625rem solid var(--aside-gbc);
    }

    .MeasuringTools-box-botton {
      display: inline-block;
      line-height: 1;
      white-space: nowrap;
      cursor: pointer;
      background-color: #2c469b;
      width: 7.5625rem;
      border: #2c469b 0.625rem solid;
      color: #ffffff;
      text-align: center;
      box-sizing: border-box;
      outline: 0;
      margin: 0 1.25rem;
      transition: 0.1s;
      font-weight: 500;
      padding: 0.75rem 1.25rem;
      font-size: 0.875rem;
      border-radius: 0.1875rem;
    }

    .MeasuringTools-box-change {
      display: inline-block;
      line-height: 1;
      white-space: nowrap;
      cursor: pointer;
      background-color: #ffffff;
      width: 7.5625rem;
      border: #2c469b 0.625rem solid;
      color: #2c469b;
      text-align: center;
      box-sizing: border-box;
      outline: 0;
      margin: 0 1.25rem;
      transition: 0.1s;
      font-weight: 500;
      padding: 0.75rem 1.25rem;
      font-size: 0.875rem;
      border-radius: 0.1875rem;
    }
  }
}
</style>
