<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-col-gutter-md">
        <div class="col-3">
          <q-input @input="resetSearch" clearable standout="bg-primary text-white" v-model="msg.TeacherName"
            label="教师名称" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" clearable filled stack-label option-value="SId" option-label="SName"
            v-model="schoolTemp" ref="School_Id" :options="schoolList" label="所属校区" :dense="false" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="msg.IsShow" :options="ShowOpts"
            emit-value map-options label="显示状态" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="msg.AuditStatus" :options="AuditOpts"
            emit-value map-options label="审核状态" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat
        class="sticky-right-column-table sticky-column-table" separator="none" :data="data" :columns="columns"
        row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">教师信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" icon="add" label="新增教师" @click="addObj(null)" />
          </div>
        </template>
        <template v-slot:body-cell-IsShow="props">
          <q-td :props="props">
            <q-badge :color="props.value==0?'negative':'primary'" :label="props.value==0?'隐藏':'显示'" />
          </q-td>
        </template>
        <template v-slot:body-cell-TeacherName="props">
          <q-td :props="props">
            <q-avatar rounded size="sm" v-if="props.row.TeacherHead">
              <img :src="props.row.TeacherHead" />
            </q-avatar>
            <q-avatar rounded size="sm" color="teal-10" text-color="white" v-if="!props.row.TeacherHead">
              {{props.value[0]}}</q-avatar>
            <span class="q-ml-md text-blue cursor-pointer">{{props.value}}</span>
          </q-td>
        </template>
        <template v-slot:body-cell-IsRecommend="props">
          <q-td :props="props">
            <q-badge :color="props.value==0?'negative':'primary'" :label="props.value==0?'不推荐':'推荐'" />
          </q-td>
        </template>
        <template v-slot:body-cell-TeachTag="props">
          <q-td :props="props">
            <q-badge v-for="(x, i) in JSON.parse(props.value)" :key="i" color="red" :label="x" class="q-mr-xs" />
          </q-td>
        </template>
        <template v-slot:body-cell-AuditStatus="props">
          <q-td :props="props">
            <q-badge v-if="props.value==1" color="dark" label="审核中" />
            <div v-if="props.value==3">
              <q-badge color="warning" class="cursor-pointer" label="审核不通过" />
              <q-popup-proxy>
                <q-banner class="bg-dark text-white">
                  {{props.row.AuditRemark}}
                </q-banner>
              </q-popup-proxy>
            </div>
            <div v-if="props.value==2">
              <q-badge v-if="props.value==2" class="cursor-pointer" color="primary" label="审核通过" />
              <q-popup-proxy>
                <q-banner class="bg-dark text-white">
                  {{props.row.AuditRemark}}
                </q-banner>
              </q-popup-proxy>
            </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 v-if="props.row.AuditStatus==1" flat size="xs" icon="iconfont icon-shenhe" color="info"
                style="font-weight:400" class="q-mr-xs" label="审核" @click="showExamine(props.row)" />
              <q-btn v-if="props.row.AuditStatus==2" flat size="xs" icon="iconfont icon-ziyuan" color="warning"
                style="font-weight:400" class="q-mr-xs" label="重置密码" @click="resetPw(props.row.TId)" />
              <q-btn flat size="xs" icon="iconfont icon-shanchu" color="negative" style="font-weight:400"
                class="q-mr-xs" label="删除" @click="deleteUser(props.row.TId)" />
              <q-btn v-if="props.row.AuditStatus==3" flat size="xs" icon="iconfont icon-ziyuan" color="negative"
                style="font-weight:400" class="q-mr-xs" label="重新申请" @click="reApplyTeacher(props.row.TId)" />
              <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
                @click="addObj(props.row)" />
            </div>
          </q-td>
        </template>
      </q-table>
      <teacher-form v-if="persistent" :save-obj="objOption" @close="closeSaveForm" @success="addAfterQuery">
      </teacher-form>
      <teacher-shenhe v-if="persistentExamine" :show-obj="objOption" @close="closeSaveForm" @success="addAfterQuery">
      </teacher-shenhe>
    </div>
  </div>
</template>
<script>
  import {
    getTeacherPage,
    getSchoolDropdown,
    deleteTeacher,
    reApplyTeacher
  } from '../../api/school/index'
  import {
    resetPassword
  } from '../../api/users/user.js'
  import teacherForm from '../../components/school/teacher/teacher-form'
  import teacherShenhe from '../../components/school/teacher/teacher-shenhe'
  export default {
    meta: {
      title: "教师管理"
    },
    components: {
      teacherForm,
      teacherShenhe
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'TeacherName',
            required: true,
            label: '教师名称',
            align: 'left',
            field: row => row.TeacherName
          },
          {
            name: 'TeacherTel',
            label: '教师电话',
            field: 'TeacherTel',
            align: 'left'
          },
          {
            name: 'TeachTag',
            label: '标签',
            field: 'TeachTag',
            align: 'left'
          },
          {
            name: 'SName',
            label: '所属校区',
            field: 'SName',
            align: 'left',
          },
          {
            name: 'DeptName',
            label: '所属部门',
            field: 'DeptName',
            align: 'left',
          },
          {
            name: 'PostName',
            label: '岗位名称',
            field: 'PostName',
            align: 'left',
          },
          {
            name: 'BaseStuNum',
            label: '带班基础人数',
            field: 'BaseStuNum',
            align: 'left',
          },
          {
            name: 'AuditStatus',
            label: '审核状态',
            align: 'left',
            field: 'AuditStatus'
          },
          {
            name: 'IsShow',
            label: '是否显示',
            align: 'left',
            field: 'IsShow'
          },
          {
            name: 'IsRecommend',
            label: '是否推荐',
            align: 'left',
            field: 'IsRecommend'
          },
          {
            name: 'CourseCount',
            label: '课程数量',
            align: 'left',
            field: 'CourseCount'
          },
          {
            name: 'OnlineClassCount',
            label: '网课产品',
            align: 'left',
            field: 'OnlineClassCount'
          },
          {
            name: 'OnlineOrderCount',
            label: '网课订单',
            align: 'left',
            field: 'OnlineOrderCount'
          },
          {
            name: 'CreateTimeStr',
            label: '入职时间',
            align: 'left',
            field: 'CreateTimeStr'
          },
          {
            name: 'TeacherAccount',
            label: '账号',
            align: 'left',
            field: 'TeacherAccount'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'TId'
          }
        ],
        data: [],
        loading: true,
        ShowOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '隐藏',
            value: '0'
          },
          {
            label: '显示',
            value: '1'
          }
        ],
        AuditOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '审核中',
            value: '1'
          },
          {
            label: '审核通过',
            value: '2'
          },
          {
            label: '审核不通过',
            value: '3'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          TeacherName: "",
          TeacherTel: "",
          AuditStatus: "-1",
          IsShow: "-1",
          School_Id: 0
        },
        pageCount: 0,
        persistent: false,
        objOption: null,
        schoolList: [],
        schoolTemp: null,
        persistentExamine: false,
        resetLoading: false
      }
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getSchool()
      this.getTeacher()
    },
    methods: {
      resetSearch() {
        this.msg.pageIndex = 1
        this.loading = true
        if (this.schoolTemp) {
          this.msg.School_Id = this.schoolTemp.SId
        } else {
          this.msg.School_Id = 0
        }
        this.getTeacher()
      },
      resetPw(id) {
        let that = this
        this.$q.dialog({
          title: "重置密码",
          message: "你正在进行重置密码行为，是否确认执行",
          persistent: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认重置",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          that.resetLoading = true
          resetPassword({
            AccountType: 2,
            AccountId: id
          }).then(res => {
            that.resetLoading = false
            that.$q.dialog({
              title: "密码重置成功",
              message: res.Message,
              persistent: true,
              ok: {
                label: "确认",
                flat: true,
                focus: true
              }
            })
          }).catch(() => {
            that.resetLoading = false
          });
        });
      },
      deleteUser(id) {
        let that = this
        this.$q.dialog({
          title: "删除教师",
          message: "你正在进行删除教师行为，一旦执行无法找回，是否确认执行？",
          persistent: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          that.resetLoading = true
          deleteTeacher({
            TId: id
          }).then(res => {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据删除成功！',
              position: 'top'
            })
            this.getTeacher()
          })
        });
      },
      //重新申请审核
      reApplyTeacher(id) {
        reApplyTeacher({
          TId: id
        }).then(res => {
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '重新申请成功!',
            position: 'top'
          })
          this.getTeacher()
        })
      },
      changePage(val) {
        this.msg.Status = this.StatusTemp ? this.StatusTemp.value : '-1'
        this.msg.pageIndex = val;
        this.loading = true;
        this.getTeacher();
      },
      getSchool() {
        getSchoolDropdown({}).then(res => {
          this.schoolList = res.Data
          if (this.msg.School_Id != 0) {
            this.schoolTemp = this.schoolList.filter(x => {
              if (this.objOption.School_Id == x.SId) {
                return x
              }
            })[0]
          }
        })
      },
      getTeacher() {
        getTeacherPage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData
          this.pageCount = res.Data.PageCount
        }).catch(() => {
          this.loading = false
        })
      },
      addAfterQuery() {
        if (!this.objOption) {
          this.msg = {
            pageIndex: 1,
            pageSize: 12,
            rowsPerPage: 12,
            TeacherName: "",
            TeacherTel: "",
            AuditStatus: "-1",
            IsShow: "-1"
          }
        }
        this.getTeacher()
      },
      addObj(obj) {
        if (obj) {
          this.objOption = obj
        } else {
          this.objOption = null
        }
        this.persistent = true
      },
      showExamine(obj) {
        this.objOption = obj
        this.persistentExamine = true
      },
      closeSaveForm() {
        this.persistent = false
        this.persistentExamine = false
      }
    },
  }

</script>

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

</style>
