<template>
  <div class="app-container">
    <div style="margin-top:10px">
      <el-input
        v-model="listQuery.search"
        placeholder="搜索"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleSearch"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-refresh-left"
        @click="resetFilter"
      >刷新重置</el-button>
    </div>
<el-table
      :data="tableData.results"
      style="width: 100%;margin-top:10px;"
      border
      stripe
      fit
      v-loading="listLoading"
      highlight-current-row
      max-height="600"
    >
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column align="left" label="姓名">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column align="left" label="手机号">
        <template slot-scope="scope">{{ scope.row.phone }}</template>
      </el-table-column>
      <el-table-column align="left" label="邮寄地址">
        <template slot-scope="scope">
          {{ scope.row.address }}
          </template>
      </el-table-column>
      <el-table-column align="left" label="检测内容">
        <template slot-scope="scope">{{ scope.row.tests }}</template>
      </el-table-column>
      <el-table-column align="left" label="提交时间">
        <template slot-scope="scope">{{ scope.row.create_time }}</template>
      </el-table-column>
      
      <el-table-column align="center" label="操作" >
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="small"
            @click="handleDelete(scope)"
            icon="el-icon-delete"
            :disabled="!checkPermission(['consult_delete'])"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.count>0"
      :total="tableData.count"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getConsultList, deleteConsult } from "@/api/consult";
import checkPermission from "@/utils/permission";
import Pagination from "@/components/Pagination";

const listQuery = {
  page: 1,
  limit: 20,
  search:''
};
export default {
  components: { Pagination },
  data() {
    return {
      listQuery: Object.assign({}, listQuery),
      tableData: {
          count:0,
          results:[],
      },
      listLoading: true,
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList(query = this.listQuery) {
      this.listLoading = true;
      getConsultList(query).then(response => {
        if(response.data.results){
            this.tableData = response.data;
        }
        this.listLoading = false;
      });
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        limit: 20,
        search:''
      };
      this.getList();
    },
    handleSearch() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleDelete(scope) {
      this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteConsult(scope.row.id).then(response => {
            this.$message({
                type: 'success',
                message: '删除成功!'
              });
          this.getList()
      });
          
        }).catch(() => {      
        });
    },
  }
};
</script>
