<template>
  <div>

    <div>
      <div ref="lxHeader">
        <lx-header icon="md-apps" :text="$route.meta.title" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content">
            <div class="searchwrap" style="display: flex;align-items: center;">
              <div>
                <el-input v-model="select.name" placeholder="请输入姓名"></el-input>
              </div>
              <div>
                <el-button type="primary" size="small" @click="getList()">查询</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="editTeacher('add')">新增</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="importTable()">导入</el-button>
                <el-button type="primary" size="small" @click="exportExcel()">导出</el-button>
              </div>
            </div>
          </div>
        </lx-header>
      </div>
    </div>

    <div>
      <xy-table :list="list" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange" :total="total" :table-item="table_item">
        <template v-slot:btns>
          <el-table-column align='center' label="操作" width="180" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="editTeacher('editor',scope.row.id)">编辑</el-button>
              <el-popconfirm style="margin:0 10px" @confirm="deleteList(scope.row.id)" title="确定删除吗？">
                <el-button type="danger" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <add-teacher ref="addTeacher" @refresh="getList()"></add-teacher>
    <imports ref="imports" :table-name="'teachers'" @refresh="getList()"></imports>

  </div>
</template>

<script>
  import addTeacher from './components/addTeacher.vue';
  import {
    index,
    destroy
  } from "@/api/info/teachers.js"
  import imports from "@/views/component/imports.vue"
  import * as XLSX from "xlsx";
  import {
    saveAs
  } from "file-saver";
  export default {
    components: {
      addTeacher,
      imports
    },
    data() {
      return {
        select: {
          name: '',
          page: 1,
          page_size: 10,
        },

        list: [],
        total: 0,
        table_item: [{
          prop: 'name',
          label: '姓名',
          align: 'center',
          width: 180
        }, {
          prop: 'sex',
          label: '性别',
          align: 'center',
          width: 180,
        }, {
          prop: 'introduce',
          label: '介绍',
          align: 'left',
        }]

      }
    },
    created() {
      this.getList()
    },
    methods: {
      pageIndexChange(e){
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e){
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      async getList() {
        const res = await index({
          page: this.select.page,
          page_size: this.select.page_size,
          filter: [{
            key: 'name',
            op: 'like',
            value: this.select.name
          }]
        })
        this.list = res.data
        this.total = res.total

      },
      async exportExcel() {
        let sheetName = `教师${new Date().getTime().toString()}`
        const res = await index({
          filter: [{
            key: 'name',
            op: 'like',
            value: this.select.name
          }],
          page: 1,
          page_size: 9999
        })
        if (res.data) {
          let headers = this.table_item.map(i => {
            return {
              key: i.prop,
              title: i.label
            }
          })
          const data = res.data.map(row => headers.map(header => row[header.key]));
          data.unshift(headers.map(header => header.title));
          const wb = XLSX.utils.book_new();
          const ws = XLSX.utils.aoa_to_sheet(data);
          XLSX.utils.book_append_sheet(wb, ws, sheetName);
          const wbout = XLSX.write(wb, {
            bookType: 'xlsx',
            bookSST: true,
            type: 'array'
          });
          saveAs(new Blob([wbout], {
            type: 'application/octet-stream'
          }), `${sheetName}.xlsx`);
        }
      },
      editTeacher(type, id) {
        if (id) {
          this.$refs.addTeacher.id = id
        }
        this.$refs.addTeacher.type = type
        this.$refs.addTeacher.isShow = true
      },
      deleteList(id) {
        var that = this;
        destroy({
          id: id
        }).then(response => {
          this.$Message.success('删除成功');
          this.getList()

        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
      importTable() {
        this.$refs.imports.show()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .searchwrap {
    display: flex;
    align-items: center;

    &>div {
      display: flex;
      align-items: center;
      margin-right: 10px;

      span {
        min-width: 70px;
      }
    }
  }
</style>
