<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          class="filter-view-item"
          v-model="listQuery.pageVo.typeOfFeedback"
          placeholder="Type of Feedbacks"
          @change="handleFilter"
          clearable>
          <el-option
            v-for="(item,index) in typeOptions"
            :key="index"
            :label="item.name"
            :value="item.value" />
        </el-select>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="tableList"
      style="width: 100%;">
      <el-table-column
        label="ID"
        align="center"
        min-width="60">
          <template slot-scope="{row}">
            <a class="link-type" href="javascript:void(0);" @click="viewFeedback(row)">{{ row.feedbackPk }}</a>
          </template>
      </el-table-column>
      <el-table-column
        label="Type of Feedback"
        align="center"
        min-width="150">
          <template slot-scope="{row}">
            <span>{{ row.typeOfFeedback }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Feedback"
        align="center"
        min-width="180">
          <template slot-scope="{row}">
            <span>{{ row.feedback }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Status"
        align="center"
        min-width="120">
          <template slot-scope="{row}">
            <span :class="{readYes: row.readStatus}">{{ row.readStatus ? "Read" : "Unread" }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Reply Count"
        align="center"
        min-width="120">
          <template slot-scope="{row}">
            <span>{{ "0" }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Feedback Time"
        align="center"
        min-width="140">
          <template slot-scope="{row}">
            <span>{{ row.feedbackTime }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Action"
        align="center"
        min-width="80">
          <template slot-scope="{row}">
            <TableAction
              :showEdit="false"
              @delete="deleteFeedback(row)"/>
          </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <Pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.pageNo"
        :limit.sync="listQuery.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import TableAction from '@/components/TableAction.vue'
  import feedback from '../../http/api/feedback'
  export default {
    components: { Pagination, TableAction },
    data() {
      return {
        listLoading: false,
        tableList: [],
        total: 0,
        listQuery: {
          pageNo: 1,
          pageSize: 10,
          pageVo: {
            criteria: "",
            typeOfFeedback: ""
          }
        },
        typeOptions: []
      }
    },
    created() {
      this.getTypeOptions();
      this.getList();
    },
    methods: {
      handleFilter() {
        this.listQuery.pageNo = 1;
        this.getList();
      },
      getTypeOptions() {
        feedback.getFeedbackTypes().then(res => {
          if (res.data) {
            this.typeOptions = res.data
          }
        }).catch(error => {
          this.$message({
            message: error,
            type: 'error'
          })
        })
      },
      getList() {
        this.listLoading = true;
        feedback.getFeedbackPages(this.listQuery).then(res => {
          this.listLoading = false;
          this.total = res.total;
          this.tableList = res.data
        }).catch(error => {
          this.listLoading = false;
          this.total = 0;
          this.tableList = [];
          this.$message({
            message: error,
            type: 'error'
          })
        })
      },
      viewFeedback(row) {
        //this.$store.commit('provider/SET_Feedback', row)
        this.$router.push({path: '/support-management/feedback/'+row.feedbackPk})
      },
      deleteFeedback(row) {
        this.$confirm('Are you sure you want to delete this feedback?', 'Delete', {
          confirmButtonText: 'OK',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(res => {
          this.handleDeleteFeedback(row.feedbackPk)
        })
      },
      handleDeleteFeedback(id) {
        feedback.deleteFeedback(id).then(res => {
          this.$message({
            message: 'Delete feedback successfully',
            type: 'success'
          });
          this.listLoading = true;
          this.getList();
        }).catch(err => {
          this.$message({
            message: err,
            type: 'error'
          })
        })
      }
    }
  }
</script>

<style lang="scss" scoped="scoped">
  @import '../../styles/variables.scss';
  .readYes {
    color: $panGreen;
  }
</style>