<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.RoomName" label="教室名称"
            @clear="resetSearch" maxlength="30" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" option-value="SId" option-label="SName"
            v-model="msg.School_Id" :options="schoolList" emit-value map-options label="关联校区" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="msg.Status" :options="StatusOpts"
            option-value="value" option-label="label" label="状态" emit-value map-options />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat
        class="sticky-column-table" separator="none" title="教室信息" :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="EditClassRoom(null)" />
          </div>
        </template>
        <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>
              <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
                @click="EditClassRoom(props.row)" />
              <template v-if="props.row.Status==0">
                <q-btn flat size="xs" icon="iconfont icon-shanchu" color="negative" style="font-weight:400"
                  class="q-mr-xs" label="删除" @click="removeClassRoom(props.row,1)" />
              </template>
              <template v-if="props.row.Status==1">
                <q-btn flat size="xs" icon="iconfont icon-shanchu" color="negative" style="font-weight:400"
                  class="q-mr-xs" label="恢复" @click="removeClassRoom(props.row,0)" />
              </template>
            </div>
          </q-td>
        </template>
      </q-table>
      <classroom-form v-if="isShowEditClassRoomForm" :save-obj="classRoomOption" @close="closeClassRoomForm"
        @success="refreshPage">
      </classroom-form>
    </div>
  </div>
</template>
<script>
  import {
    queryClassRoomPage,
    getSchoolDropdown,
    deleteClassRoomInfo
  } from '../../api/school/index'
  import classroomForm from '../../components/school/classroom/classroom-form'
  export default {
    meta: {
      title: "教室管理"
    },
    components: {
      classroomForm
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'RoomName',
            label: '教师名称',
            align: 'left',
            field: 'RoomName',
          },
          {
            name: 'SchoolName',
            label: '所属校区',
            field: 'SchoolName',
            align: 'left'
          },
          {
            name: 'StatusStr',
            label: '状态',
            align: 'left',
            field: 'StatusStr'
          },
          {
            name: 'UpdateByName',
            label: '操作人',
            align: 'left',
            field: 'UpdateByName'
          },
          {
            name: 'UpdateTimeStr',
            label: '操作时间',
            align: 'left',
            field: 'UpdateTimeStr'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'RoomId'
          }
        ],
        data: [],
        loading: true,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          RoomName: "",
          School_Id: 0, //校区编号
          Status: '-1', //状态
        },
        pageCount: 0,
        isShowEditClassRoomForm: false,
        classRoomOption: null,
        schoolList: [], //校区列表
        StatusOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '正常',
            value: '0'
          },
          {
            label: '删除',
            value: '1'
          }
        ],
      }
    },
    created() {
      this.getSchool();
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getClassRoomPage()
    },
    methods: {
      //获取校区列表
      getSchool() {
        getSchoolDropdown({}).then(res => {
          if (res.Code == 1) {
            var tempArray = res.Data;
            if (!tempArray) {
              tempArray = [];
            }
            tempArray.unshift({
              SId: 0,
              SName: "不限"
            })
            this.schoolList = tempArray;
          }
        })
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getClassRoomPage();
      },
      //删除教室
      removeClassRoom(obj, status) {
        let that = this;
        var message = "";
        if (status == 1) {
          message = "您正在进行删除【" + obj.RoomName + "】行为，一旦执行无法找回，是否确认执行？";
        } else {
          message = "您正在进行恢复【" + obj.RoomName + "】，是否确认执行？";
        }
        this.$q.dialog({
          title: "删除教室",
          message: message,
          isShowEditClassRoomForm: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          var delMsg = {
            RoomId: obj.RoomId,
            Status: status
          };
          deleteClassRoomInfo(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.getClassRoomPage();
            }
          })
        });
      },
      //分页改变
      changePage(val) {
        this.msg.pageIndex = val;
        this.getClassRoomPage();
      },
      //获取教室分页列表
      getClassRoomPage() {
        this.loading = true
        queryClassRoomPage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData;
          this.pageCount = res.Data.PageCount;
        }).catch(() => {
          this.loading = false
        })
      },
      //刷新页面
      refreshPage() {
        if (!this.classRoomOption) {
          this.msg.pageIndex = 1;
          this.msg.RoomName = "";
          this.msg.School_Id = 0;
        }
        this.getClassRoomPage()
      },
      //新增修改教室
      EditClassRoom(obj) {
        if (obj) {
          this.classRoomOption = obj;
        } else {
          this.classRoomOption = null;
        }
        this.isShowEditClassRoomForm = true;
      },
      //关闭新增修改弹窗
      closeClassRoomForm() {
        this.isShowEditClassRoomForm = false;
      }
    },
  }

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

</style>
