<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.ClassName"
            label="班级名称" />
        </div>
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.CourseName"
            label="课程名称" />
        </div>
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.TeacherName"
            label="讲师名称" />
        </div>
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.AssistName"
            label="助教名称" />
        </div>
      </div>
      <div class="page-option">
        <q-btn color="accent" class="q-mr-md" icon="add" label="新增班级" @click="EditClass(null)" />
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat
        class="sticky-right-column-table sticky-tow-column-table" separator="none" title="班级信息" :data="data"
        :columns="columns" row-key="name">
        <template v-slot:body-cell-TeacherHead="props">
          <q-td auto-width :props="props">
            <q-avatar size="md" v-if="props.value">
              <img :src="props.value" />
            </q-avatar>
            <q-avatar size="md" color="teal-10" text-color="white" v-if="!props.value">
              {{props.row.TeacherName}}</q-avatar>
          </q-td>
        </template>
        <template v-slot:body-cell-TeacherName="props">
          <q-td :props="props">
            <div class="text-blue cursor-pointer">{{props.value}}</div>
          </q-td>
        </template>
        <template v-slot:body-cell-AssistIcon="props">
          <q-td auto-width :props="props">
            <q-avatar size="md" v-if="props.value">
              <img :src="props.value" />
            </q-avatar>
            <q-avatar size="md" color="teal-10" text-color="white" v-if="!props.value">
              {{props.row.AssistName}}</q-avatar>
          </q-td>
        </template>
        <template v-slot:body-cell-AssistName="props">
          <q-td :props="props">
            <div class="text-blue cursor-pointer">{{props.value}}</div>
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <div style="min-width:190px;width:100%">
              <q-btn flat size="xs" icon="iconfont icon-shanchu" color="negative" style="font-weight:400"
                class="q-mr-xs" label="删除" @click="deleteClass(props.row)" />
              <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
                @click="EditClass(props.row)" />
            </div>
          </q-td>
        </template>
      </q-table>
      <class-form v-if="isShowEditClassForm" :save-obj="classOption" @close="closeClassForm" @success="refreshPage">
      </class-form>
    </div>
  </div>
</template>
<script>
  import {
    getClassPage,
    deleteClass
  } from '../../api/school/index'
  import classForm from '../../components/school/classmanage/class-form'
  export default {
    meta: {
      title: "班级管理"
    },
    components: {
      classForm
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'ClassName',
            required: true,
            label: '班级名称',
            align: 'left',
            field: row => row.ClassName
          },
          {
            name: 'CourseName',
            label: '所属课程',
            field: 'CourseName',
            align: 'left'
          },
          {
            name: 'TeacherHead',
            label: '',
            field: 'TeacherHead',
            align: 'left',
          },
          {
            name: 'TeacherName',
            label: '讲师',
            field: 'TeacherName',
            align: 'left',
          },
          {
            name: 'AssistIcon',
            label: '',
            align: 'left',
            field: 'AssistIcon'
          },
          {
            name: 'AssistName',
            label: '助教',
            align: 'left',
            field: 'AssistName'
          },
          {
            name: 'StudentCount',
            label: '学员数量',
            align: 'left',
            field: 'StudentCount'
          },
          {
            name: 'JobCount',
            label: '作业数量',
            align: 'left',
            field: 'JobCount'
          },
          {
            name: 'CreateTimeStr',
            label: '创建时间',
            align: 'left',
            field: 'CreateTimeStr'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'ClassId'
          }
        ],
        data: [],
        loading: true,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          ClassName: "",
          CourseName: "",
          AssistName: "",
          TeacherName: "",
        },
        pageCount: 0,
        isShowEditClassForm: false,
        classOption: null,
      }
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.queryClassPage()
    },
    methods: {
      resetSearch() {
        this.msg.pageIndex = 1;
        this.queryClassPage();
      },
      //删除班级
      deleteClass(obj) {
        let that = this;
        this.$q.dialog({
          title: "删除班级",
          message: "你正在进行删除【" + obj.ClassName + "】行为，一旦执行无法找回，是否确认执行？",
          isShowEditClassForm: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          var delMsg = {
            ClassId: obj.ClassId
          };
          that.deleteClass(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.queryClassPage();
            }
          })
        });
      },
      changePage(val) {
        this.msg.pageIndex = val;
        this.queryClassPage();
      },
      queryClassPage() {
        this.loading = true
        getClassPage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData
          this.pageCount = res.Data.PageCount
        }).catch(() => {
          this.loading = false
        })
      },
      refreshPage() {
        if (!this.classOption) {
          this.msg.pageIndex = 1;
          this.msg.ClassName = "";
          this.msg.CourseName = "";
          this.msg.AssistName = "";
          this.msg.TeacherName = "";
        }
        this.queryClassPage()
      },
      EditClass(obj) {
        if (obj) {
          this.classOption = obj
        } else {
          this.classOption = null
        }
        this.isShowEditClassForm = true
      },
      showExamine(obj) {
        this.classOption = obj;
      },
      closeClassForm() {
        this.isShowEditClassForm = false
      }
    },
  }

</script>

<style lang="sass">
  @import url('~assets/css/table.sass');

</style>
