<template>
  <div class="app-container">
    <div class="filter-container filter-view">
      <el-select
        class="filter-view-item"
        v-model="filter.pageVo.brand"
        placeholder="Brand"
        @change="toSearch"
        clearable>
        <el-option
          v-for="item in brandOptions"
          :key="item"
          :label="item"
          :value="item" />
      </el-select>
      <div class="filter-flex-button">
        <el-button
          @click="onClickAdd"
          icon="el-icon-plus"
          type="primary">
          Create
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.list"
      class="no-border"
      style="width: 100%;">
      <el-table-column
        align="center"
        label="Brand"
        prop="brand"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Model"
        prop="model"
        min-width="120"/>
      <el-table-column
        align="center"
        label="No. of Users"
        prop="userCount"
        min-width="130"/>
      <el-table-column
        align="center"
        label="Action"
        min-width="120"
        v-if="!$route.meta.onlyView">
        <template slot-scope="{ row }">
          <TableAction
            :showDel="row.userCount == 0"
            @edit="onClickEdit(row)"
            @delete="onClickDelete(row)"/>
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="filter.pageNo"
        :limit.sync="filter.pageSize"
        @pagination="getTableList" />
    </div>
    <detail
      v-bind="action"
      @hide="hideDialog"/>
  </div>
</template>

<script>
import api from '../../api/vehicles.js';
import Pagination from '@/components/Pagination';
import TableAction from '@/components/TableAction';
import detail from './detail.vue';
export default {
  data() {
    return {
      filter: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          brand: "",
          model: "",
          criteria: ""
        }
      },
      table: {
        list: [],
        total: 0,
        loading: false
      },
      brandOptions: [],
      action: {
        item: {},
        visible: false
      }
    }
  },
  components: {detail,Pagination,TableAction},
  created() {
    this.toSearch();
    this.getBrandOprions();
  },
  methods: {
    toSearch() {
      this.filter.pageNo = 1;
      this.getTableList();
    },
    getBrandOprions() {
      api.getBrandsOptions().then(res => {
        if (res.data) {
          this.brandOptions = res.data
        }
      }).catch(error => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    getTableList() {
      this.table.loading = true;
      api.getVehicleModelsPage(this.filter).then(res => {
        if (res.data) {
          this.table.total = res.total
          this.table.list = res.data
        } else {
          this.table.total = 0
          this.table.list = []
        }
      }).catch(err => {
        this.$message({
          type: "error",
          message: err
        })
        this.table.total = 0
        this.table.list = []
      }).finally(() => {
        this.table.loading = false;
      })
    },
    onClickAdd() {
      this.action.item = {};
      this.action.visible = true;
    },
    onClickEdit(row) {
      this.action.item = row;
      this.action.visible = true;
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this model?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteModel(row.vehicleModelId);
      })
    },
    deleteModel(id) {
      this.table.loading = true;
      api.deleteVehicleModel(id).then(res => {
        this.$message({
          type: 'success',
          message: "Delete success."
        })
        this.getTableList()
      }).catch(err => {
        this.table.loading = false;
        this.$message({
          type: 'error',
          message: err
        })
      })
    },
    hideDialog(e) {
      this.action.id = "";
      this.action.visible = false;
      if (e) {
        this.getTableList();
      }
    }
  }
}
</script>

<style>
</style>