<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          class="filter-input"
          placeholder="All Status"
          clearable
          @change="onClickSearch"
          v-model="filters.pageVo.dataStatus">
          <el-option
            v-for="(item,index) in options.status"
            :key="index"
            :label="item.name"
            :value="item.value"/>
        </el-select>
        <el-input
          class="filter-input"
          v-model="filters.pageVo.criteria"
          placeholder="Search by Type Name"
          prefix-icon="el-icon-search"
          @keyup.enter.native="onClickSearch"
          @change="onClickSearch"
          clearable/>
        <div
          class="filter-flex-button"
          v-if="!$route.meta.onlyView">
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="onClickAdd">
            Create
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="table.list">
      <el-table-column
        align="center"
        label="Article Type"
        prop="articleTypeName"
        min-width="140"/>
      <el-table-column
        align="center"
        label="Related Articles"
        prop="relatedArticles"
        min-width="130"/>
      <el-table-column
        align="center"
        label="Created Date"
        prop="createTime"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Status"
        prop="dataStatus"
        min-width="100"/>
      <el-table-column
        align="center"
        label="Action"
        min-width="100"
        v-if="!$route.meta.onlyView">
        <template v-slot="{ row }">
          <TableAction
            @edit="onClickEdit(row)"
            @delete="onClickDelete(row)"
            v-if="row.dataStatus != 'Inactive'"/>
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="filters.pageNo"
        :limit.sync="filters.pageSize"
        @pagination="getTableData" />
    </div>
    <DialogDetail
      v-bind="dialogAction"
      @hide="hideActionDialog"/>
  </div>
</template>

<script>
import TableAction from '@/components/TableAction.vue'
import Pagination from '@/components/Pagination'
import DialogDetail from './typeDetail.vue'
import charge from '../../http/api/charge'
import api from '../../api/article.js'
export default {
  data() {
    return {
      loading: false,
      filters: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
          dataStatus: ""
        }
      },
      table: {
        list: [],
        total: 0
      },
      options: {
        status: []
      },
      dialogAction: {
        id: "",
        isEdit: false,
        visible: false
      }
    };
  },
  components: { Pagination, TableAction, DialogDetail },
  created() {
    this.getStatusOptions();
    this.onClickSearch();
  },
  methods: {
    onClickSearch() {
      this.filters.pageNo = 1
      this.getTableData()
    },
    getStatusOptions() {
      charge.getStatusOptions().then(res => {
        this.options.status = res.data;
      }).catch(err => {
        this.$message.error(err);
      })
    },
    getTableData() {
      this.loading = true;
      api.getArticleTypePages(this.filters).then(res => {
        if (res.data && res.total) {
          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.loading = false;
      })
    },
    onClickAdd() {
      this.dialogAction.id = "";
      this.dialogAction.isEdit = false;
      this.dialogAction.visible = true;
    },
    onClickEdit(row) {
      this.dialogAction.id = row.articleTypeId;
      this.dialogAction.isEdit = true;
      this.dialogAction.visible = true;
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this article type?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.onDeleteType(row.articleTypeId);
      })
    },
    onDeleteType(id) {
      this.loading = true;
      api.deleteArticleType(id).then(res => {
        this.$message({
          type: 'success',
          message: "Delete success."
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.loading = false;
      })
    },
    hideActionDialog(e) {
      this.dialogAction.id = "";
      this.dialogAction.visible = false;
      if (e) {
        this.getTableData();
      }
    }
  }
}
</script>

<style scoped>
.filter-input {
  min-width: 100px;
  max-width: 300px;
}
</style>