<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="select.page=1,getList()">查询</el-button>
                <el-button type="primary" size="small" @click="resetSelect">重置</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 :showIndex="false" :list="list" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange" :total="total" :table-item="table_item">
        <template v-slot:course_contents>
          <el-table-column align='center' label="排课历史" width="960" header-align="center">
            <template slot="header" slot-scope="scope">
              <div style="display: flex; justify-content: space-between; align-items: center;">
                <span>排课历史</span>
              </div>
            </template>
            <el-table-column prop="course.name" label="课程名称" width="240" align="left">
              <template slot-scope="scope">
                <div class="cc-item" v-for="(item, index) in scope.row.course_contents" :key="index">
                  <el-tooltip :content="item.course ? item.course.name : ''" placement="top" :disabled="!item.course || item.course.name.length <= 20">
                    <span>{{ item.course ? item.course.name : '' }}</span>
                  </el-tooltip>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="theme" label="课程主题" width="240" align="left">
              <template slot-scope="scope">
                <div class="cc-item" v-for="(item, index) in scope.row.course_contents" :key="index">
                  <el-tooltip :content="item.theme || ''" placement="top" :disabled="!item.theme || item.theme.length <= 20">
                    <span>{{ item.theme || '' }}</span>
                  </el-tooltip>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="direction" label="课程方向" width="240" align="left">
              <template slot-scope="scope">
                <div class="cc-item" v-for="(item, index) in scope.row.course_contents" :key="index">
                  <el-tooltip :content="item.direction_detail ? item.direction_detail.value : ''" placement="top" :disabled="!item.direction_detail || item.direction_detail.value.length <= 20">
                    <span>{{ item.direction_detail ? item.direction_detail.value : '' }}</span>
                  </el-tooltip>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" width="240" align="left">
              <template slot-scope="scope">
                <div class="cc-item" v-for="(item, index) in scope.row.course_contents" :key="index">
                  <el-tooltip :content="item.remark || ''" placement="top" :disabled="!item.remark || item.remark.length <= 20">
                    <span>{{ item.remark || '' }}</span>
                  </el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
        </template>
        <template v-slot:btns>
          <el-table-column align='center' fixed="right" 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: [{
          type: 'index',
          align: 'center',
          width: 80,
          fixed: 'left'
        },{
          prop: 'name',
          label: '授课老师',
          align: 'center',
          width: 180,
          fixed: 'left'
        },{
          prop: 'introduce',
          label: '老师简介',
          align: 'left',
          width: 240,
        }, {
          prop: 'sex',
          label: '性别',
          align: 'center',
          width: 80,
        }, {
          prop: 'mobile',
          label: '联系方式',
          align: 'center',
          width: 180,
        },{
          prop: 'course_contents',
          label: '排课历史',
          align: 'center',
          width: 240,
        },]

      }
    },
    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()
      },
      resetSelect(){

        this.select.name = ''
        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;
      }
    }
  }

  /* 排课历史子列每条数据的分隔样式 */
  .cc-item {
    padding: 6px 0;
    border-bottom: 1px dashed #dcdfe6;
    line-height: 1.2;
    height: 24px; /* 固定每行高度 */
    overflow: hidden; /* 隐藏超出部分 */
    text-overflow: ellipsis; /* 超出显示省略号 */
    white-space: nowrap; /* 不换行 */
  }
  .cc-item:last-child {
    border-bottom: none;
  }
</style>
