<template>
  <div>
    <div class="row col flex q-col-gutter-md">
      <q-field filled label="老师" stack-label class="col-6">
        <template v-slot:control>
          <div>{{ saveObj.TeacherName }}</div>
        </template>
      </q-field>
      <q-field filled label="教室" stack-label class="col-6">
        <template v-slot:control>
          <div>{{ saveObj.RoomName }}</div>
           <q-btn color="accent" size="xs" label="修改" v-if="auth.IsEdit&&saveObj.ClassType==3" @click="modifyRoom" class="q-ml-md" />
        </template>
      </q-field>
      <q-field filled label="课程" stack-label class="col-6">
        <template v-slot:control>
          <div>{{ saveObj.CourseName }}</div>
        </template>
      </q-field>
      <q-field filled label="班级" stack-label class="col-6" v-if="saveObj.ClassType !== 3">
        <template v-slot:control>
          <div>
            {{ saveObj.ClassName }}
            <span v-if="saveObj.ClassType === 2">
              <span v-if="saveObj.ReserveType === 0">(正常试听)</span>
              <span v-if="saveObj.ReserveType === 1">(跟班试听)</span>
            </span>
          </div>
        </template>
      </q-field>
      <q-field filled label="类型" stack-label class="col-6">
        <template v-slot:control>
          <div>
            <span v-if="saveObj.ClassType == 1">跟班</span><span v-if="saveObj.ClassType == 2">试听课</span><span
              v-if="saveObj.ClassType == 3">约课</span>
          </div>
        </template>
      </q-field>
      <q-field filled label="课程时间" stack-label class="col-6">
        <template v-slot:control>
          <div>{{ saveObj.StartTime }}-{{ saveObj.EndTime }}</div>
        </template>
      </q-field>
      <q-field filled label="进度" stack-label class="col-6">
        <template v-slot:control>
          <div>
            <span v-if="saveObj.ClassType == 1">{{ saveObj.Ranks }}/{{ saveObj.TotalPlanNum }}</span>
            <span v-if="saveObj.ClassType == 3">第{{ saveObj.Ranks||0 }}次课</span>
            <span class="q-ml-sm"
              v-if="saveObj.ClassType == 3&&saveObj.CourseGradeName">({{ saveObj.CourseGradeName }})</span>
             <span class="q-ml-md">{{CourseChapterName}}</span> 
          </div>
        </template>
      </q-field>
      <q-field filled label="预约状态：" stack-label class="col-6" v-if="saveObj.ClassType == 3">
        <template v-slot:control>
          <div>
            <span v-if="saveObj.AppointState == 1" style="color:#ef5350">待确认</span><span
              v-if="saveObj.AppointState == 2" style="color:#21ba45">已确认</span>
          </div>
          <q-btn color="accent" size="xs" label="确认" v-if="
              saveObj.AppointState == 1 &&
                saveObj.GuestList.length >= saveObj.ScrollMinNum &&
                auth.IsEdit
            " @click="confirmSub" class="q-ml-md" />
          <q-badge rounded color="red" label="未成班" style="position:absolute;top:0;right:0" class="q-ml-sm" v-if="
              saveObj.AppointState == 1 &&
                isShowTag(saveObj.GuestList, saveObj.ScrollMinNum)
            " />
        </template>
      </q-field>
      <q-field filled label="最小成班人数：" stack-label class="col-6" v-if="saveObj.ClassType == 3">
        <template v-slot:control>
          <div>{{ saveObj.ScrollMinNum }}</div>
        </template>
      </q-field>
      <!-- <q-field
        filled
        label="最大成班人数："
        stack-label
        class="col-6"
        v-if="saveObj.ClassType == 3"
      >
        <template v-slot:control>
          <div>{{ saveObj.ScrollMaxNum }}</div>
        </template>
      </q-field> -->
      <q-field filled label="已约学员：" stack-label class="col-12" v-if="saveObj.ClassType == 3">
        <template v-slot:control>
          <div v-if="saveObj.AppointState == 1 && auth.IsEdit">
            <div class="q-mt-sm flex justify-start">
              <q-btn color="accent" size="xs" label="调课" @click="showChangeCourse" :disable="checkStuType2.length == 0"
                class="q-mr-sm" />
              <q-btn color="accent" size="xs" label="取消预约" @click="cancelSub()" :disable="checkStuType2.length == 0"
                class="q-mr-sm" />
            </div>

            <div class="row">
              <q-checkbox v-model="checkStuType2" v-for="(x, y) in saveObj.GuestList" :key="y" :val="x.AppointmentId">
                {{ x.GuestName }}
                <span v-if="x.AppointType == 2">(补)</span>
                <span v-if="x.AppointType == 3">(重)</span>
              </q-checkbox>
            </div>
          </div>
          <div class="row col-12 relative-position" v-else>
            <div class="absolute" style="top:-20px;right:0" v-if="auth.IsEdit">
              <q-btn color="accent" size="xs" label="取消" @click="showCancelDig = true" />
              <q-btn color="accent" size="xs" label="补录" @click="showMakeUpDig = true" class="q-ml-sm" />

            </div>
            <span v-for="(x, y) in saveObj.GuestList" :key="y">
              {{ x.GuestName }}
              <span v-if="x.AppointType == 2">(补)</span>
              <span v-if="x.AppointType == 3">(重)</span>，
            </span>
          </div>
        </template>
      </q-field>
      <q-field filled label="学员名单：" stack-label class="col-12" v-if="saveObj.ClassType !== 3">
        <template v-slot:control>
          <div>
            <span v-for="(x, y) in saveObj.GuestList" :key="y">
              {{ x.GuestName }}
              <span v-if="x.AppointType == 2">(补)</span>
              <span v-if="x.AppointType == 3">(重)</span>，
            </span>
          </div>
        </template>
      </q-field>
    </div>
    <ChangeCourse v-model="showChangeDig" :saveObj="CourseObj" @success="ChangeCourseSuccessHandle" />
    <MakeUp v-model="showMakeUpDig" :saveObj="saveObj" :date="dateObj.date" @success="makeUpHandle"></MakeUp>
    <CancelSub v-model="showCancelDig" :saveObj="saveObj" :date="dateObj.date" @success="cancelHandle"></CancelSub>
    <Room v-model="showRoomDig" :room="saveObj.RoomId" :saveObj="saveObj" :date="dateObj.date" @success="roomHandle"></Room>
  </div>
</template>
<script>
  import {
    CancelAppointment,
    SetSureAppointment
  } from "../../../api/studyabroad/subscribe.js";
  import ChangeCourse from "./changeCourse";
  import MakeUp from "./makeUp.vue"
  import CancelSub from './deleteStu'
  import Room from "./changeRoom.vue"
  export default {
    components: {
      ChangeCourse,
      MakeUp,
      CancelSub,
      Room
    },
    props: {
      dateObj: {
        type: Object,
        default: null
      },
      saveObj: {
        type: Object,
        default: null
      },
      auth: {
        type: Object,
        default: () => {
          return {
            IsEdit: false
          };
        }
      },
      CourseChapterName:{
        type: String,
        default: ""
      }
    },
    data() {
      return {
        checkStuType2: [], //
        showChangeDig: false,
        showMakeUpDig: false,
        showCancelDig: false,
        showRoomDig:false,
        CourseObj: {}
      };
    },
    methods: {
      //取消预约
      cancelSub() {
        let that = this;
        this.$q
          .dialog({
            title: "提示信息",
            message: "是否要取消预约",
            cancel: true,
            ok: "是",
            cancel: "否"
          })
          .onOk(() => {
            const msg = {
              Date: that.dateObj.date,
              TeacherId: that.saveObj.Tid,
              AppointIds: that.checkStuType2.toString()
            };
            CancelAppointment(msg).then(res => {
              if (res.Code == 1) {
                that.$q.notify({
                  message: "操作成功",
                  position: "top"
                });
                that.checkStuType2.map(e => {
                  that.saveObj.GuestList.map((_e, _i) => {
                    if (e == _e.AppointmentId) {
                      that.saveObj.GuestList.splice(_i, 1);
                    }
                  });
                });
                that.checkStuType2 = [];
                that.$emit("success");
              }
            });
          })
          .onCancel(() => {});
      },

      //确认约课
      confirmSub() {
        if (this.saveObj.EnrollNum < this.saveObj.ScrollMinNum) {
          this.$q.notify({
            message: `最小开班人数为${this.saveObj.ScrollMinNum}`,
            position: "top",
            type: "negative"
          });
          return;
        }
        const msg = {
          Date: this.dateObj.date,
          TeacherId: this.saveObj.Tid,
          ShiftSort: this.saveObj.ShiftSort
        };
        SetSureAppointment(msg).then(res => {
          if (res.Code == 1) {
            this.$q.notify({
              message: "操作成功",
              position: "top",
              type: "positive"
            });
            this.$emit("close");
          }
        });
      },
      //显示调课弹窗
      showChangeCourse() {
        this.CourseObj = {
          date: this.dateObj.date,
          ids: this.checkStuType2.toString()
        };
        this.showChangeDig = true;
      },
      ChangeCourseSuccessHandle() {
        this.$emit("close");
      },
      makeUpHandle(val) {
        let arr = this.saveObj.GuestList || [];
        arr.push(val);
        this.$set(this.saveObj, "GuestList", arr);
        this.$emit("success");
      },
      cancelHandle(val) {
        val.map(e => {
          this.saveObj.GuestList.map((_e, _i) => {
            if (e == _e.AppointmentId) {
              this.saveObj.GuestList.splice(_i, 1);
            }
          });
        });
        this.$emit("success");
      },
      roomHandle(val) {
        this.$set(this.saveObj, "RoomName", val.Name);
        this.$emit("success",val);
      },
      isShowTag(arr, min) {
        let n = 0;
        arr.map(e => {
          if (e.AppointType !== 3) {
            n += 1;
          }
        });
        if (n < min) {
          return true;
        } else {
          return false;
        }
      },
      modifyRoom(){
        this.showRoomDig=true
      }
    }
  };

</script>
