<template>
  <div class="content">
    <div class="flex-layout">
      <div class="table-wrapper">
        <div class="title">
          <h6>Group List</h6>
        </div>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="groupId" label="Group ID"></el-table-column>
          <el-table-column
            prop="groupName"
            label="Group Name"
          ></el-table-column>
          <el-table-column prop="autoUpdate" label="Automatic updates">
            <template slot-scope="scope">
              <span v-if="scope.row.autoUpdate == '0'">true</span>
              <span v-else>false</span>
            </template>
          </el-table-column>
          <el-table-column prop="createBy" label="createBy"></el-table-column>
          <el-table-column prop="action" label="Action" width="250px">
            <template slot-scope="scope">
              <span
                class="icon iconfont icon-edit cursor table-acion"
                @click="detail(scope.row)"
              ></span>
              <el-popconfirm
                confirm-button-text="Yes"
                cancel-button-text="No"
                icon="el-icon-info"
                icon-color="red"
                title="Are you sure to delete this?"
                @confirm="toDelete(scope.row.groupId)"
              >
                <span
                  class="icon iconfont icon-shanchu cursor table-acion"
                  slot="reference"
                >
                </span>
              </el-popconfirm>
            </template>
          </el-table-column>
          <template slot="empty" v-if="tableData.length == 0">
            <span>No data available</span>
          </template>
        </el-table>
        <el-pagination
          v-if="total > 0"
          :page-size="limit"
          :current-page.sync="page"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          background
          :page-sizes="[10, 20, 50, 100]"
          layout="prev, sizes, pager, next"
        ></el-pagination>
      </div>
      <div class="chart-wrapper flex-column">
        <div class="flex-row">
          <div class="chart">
            <h6>Command sending success rate</h6>
            <pie-chart />
          </div>
          <div class="chart">
            <h6>Statistics of single group Command Send Status</h6>
            <bar-chart />
          </div>
        </div>
        <div class="chart">
          <h6>Logs</h6>
          <el-table :data="logData" style="width: 100%"  height="250">
            <el-table-column prop="Time" label="Time"></el-table-column>
            <el-table-column prop="Operator" label="Operator"></el-table-column>
            <el-table-column prop="logs" label="logs"></el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import pieChart from "./components/pieChart";
import barChart from "./components/barChart";
import { GetGroups, DeleteGroup } from "@/api/vehicle";
export default {
  components: { pieChart, barChart },
  data() {
    return {
      tableData: [],
      logData:[
        {Time:'2022-01-01 08:15:2',Operator:'Ding shuo',logs:'Send to subscription'},
        {Time:'2022-01-01 08:15:2',Operator:'Ding shuo',logs:'Send to subscription'},
        {Time:'2022-01-01 08:15:2',Operator:'Ding shuo',logs:'Send to subscription'},
        {Time:'2022-01-01 08:15:2',Operator:'Ding shuo',logs:'Send to subscription'},
        {Time:'2022-01-01 08:15:2',Operator:'Ding shuo',logs:'Send to subscription'},
        {Time:'2022-01-01 08:15:2',Operator:'Ding shuo',logs:'Send to subscription'},
        ],
      maxHeight: 0,
      page: 1,
      limit: 10,
      total: 0,
    };
  },
  created() {
    this.getTableData();
    // this.getMaxHeight();
    // window.addEventListener('resize', () => {
    //     this.getMaxHeight();
    // })
  },
  methods: {
    getTableData() {
      const { page, limit } = this;
      GetGroups({ currentPage: page, pageSize: limit }).then((res) => {
        const { data } = res;
        this.tableData = data.records;
        this.total = data.total;
        if (page > 0 && data.records.length == 0) {
          this.handleCurrentChange(page - 1);
        }
      });
    },
    toDelete(id) {
      DeleteGroup({ groupId: id }).then((res) => {
        if (res.httpCode == 200) {
          this.$message.success("delete success");
          this.handleCurrentChange(this.page);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    detail(item) {
      this.$router.push({
        path: "/groupManagement/detail",
        query: {
          groupId: item.groupId,
        },
      });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.handleCurrentChange(1);
    },
    getMaxHeight() {
      let bodyHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      this.maxHeight = bodyHeight - 194;
    },
  },
};
</script>

<style lang="scss" scoped src="../../assets/css/group-management.scss" />
