<template>
  <div class="content">
    <div class="table-wrapper">
      <div class="title">
        <h6>Ticket List</h6>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="Ticket ID"></el-table-column>
        <el-table-column
          prop="ticketName"
          label="Ticket Name"
        ></el-table-column>
        <el-table-column prop="createBy" label="createBy"></el-table-column>
        <el-table-column
          prop="anomalyFlag"
          label="anomalyFlag"
        ></el-table-column>
        <el-table-column prop="createDate" label="Time">
          <template slot-scope="scope">
            <span>{{ new Date(scope.row.createDate) | formatTime }} </span>
          </template>
        </el-table-column>
        <el-table-column prop="action" label="Action">
          <template slot-scope="scope">
            <span
              class="icon iconfont icon-view 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.id)"
            >
              <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="totalPage > 1"
        :page-size="limit"
        :total="total"
        :current-page.sync="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 50, 100]"
        background
        layout="prev,sizes, pager, next"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { GetTickets, DeleteTickets } from "@/api/vehicle";
export default {
  data() {
    return {
      tableData: [],
      maxHeight: 0,
      page: 1,
      limit: 10,
      total: 0,
      totalPage: 10,
    };
  },
  created() {
    this.getTableData();
    // this.getMaxHeight();
    // window.addEventListener('resize', () => {
    //     this.getMaxHeight();
    // })
  },
  methods: {
    getTableData() {
      const { page, limit } = this;
      GetTickets({ currentPage: page, totalPage: 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);
        }
      });
    },
    detail(item) {
      this.$router.push({
        path: "/issueManagement/detail",
        query: {
          ticketNo: item.id,
        },
      });
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.handleCurrentChange(1)
    },
    toDelete(id) {
      DeleteTickets({ ticketId: id }).then((res) => {
        if (res.httpCode == 200) {
          this.$message.success("delete success");
          this.handleCurrentChange(this.page);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    getMaxHeight() {
      let bodyHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      this.maxHeight = bodyHeight - 194;
    },
  },
};
</script>
