<template>
  <div class='imc'>
    <el-form :inline="true"
             :model="topForm"
             ref='topForm'
             :rules="topFormRules">
      <el-form-item>
        <div class="addBtn"
             @click="addClass">添加班级</div>
      </el-form-item>
      <el-form-item label="提高班参考数量">
        <el-input disabled
                  v-model="topForm.improventClassesNum"></el-input>
      </el-form-item>
      <el-form-item label="排课数量"
                    prop="count">
        <el-input v-model="topForm.count"></el-input>
      </el-form-item>
      <el-form-item label="排课开始时间"
                    prop="timer">
        <el-date-picker v-model="topForm.timer"
                        align="right"
                        type="date"
                        placeholder="选择日期"
                        value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div class="tableWrap">
      <el-table :data="tableList">
        <el-table-column prop="name"
                         label="提高班名称">
        </el-table-column>
        <el-table-column prop="subjectIdList"
                         label="声部">
          <template slot-scope="scope">
            <div>
              {{ scope.row.subjectIdList | fitterSound }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="userId"
                         label="主教老师">
          <template slot-scope="scope">
            <div>
              {{ scope.row.userId | fitterTeacher }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="courseTimes"
                         label="班级人数">
        </el-table-column>
        <el-table-column prop="date"
                         label="上课时间">
          <template slot-scope="scope">
            <div>
              <p>{{scope.row.startClassTime+'-'+scope.row.endClassTime}}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="date"
                         label="操作">
          <template slot-scope="scope">
            <div>
              <el-button type="text"
                         @click="removeClass(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 确认取消 -->
    <div class="btnWrap"
         style="margin-top:40px;">
      <div class="nextBtn"
           @click='gotoNext'>下一步</div>
      <div class="okBtn">返回</div>
    </div>
    <el-dialog title="添加提高班"
               :visible.sync="raiseVisible"
               width="40%">
      <el-form :model="maskForm"
               ref='maskForm'
               :rules="maskRules"
               :label-position="labelPosition"
               label-width="100px">
        <el-form-item label="提高班名称"
                      prop="name">
          <el-input v-model="maskForm.name"
                    style='width:180px;'></el-input>
        </el-form-item>
        <el-form-item label="提高班声部"
                      prop="sound">
          <el-select v-model="maskForm.sound">
            <el-option v-for="(item,index) in soundList"
                       :key='index'
                       :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主教老师"
                      prop="teacher">
          <el-select v-model="maskForm.teacher">
            <el-option v-for="(item,index) in teacherList"
                       :key="index"
                       :label="item.name"
                       :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级人数"
                      prop="count">
          <el-input v-model="maskForm.count"
                    type="number"
                    style='width:180px;'></el-input>
        </el-form-item>
        <el-form-item label="星期几"
                      prop="week">
          <el-select v-model="maskForm.week">
            <el-option value="1"
                       label="星期一"></el-option>
            <el-option value="2"
                       label="星期二"></el-option>
            <el-option value="3"
                       label="星期三"></el-option>
            <el-option value="4"
                       label="星期四"></el-option>
            <el-option value="5"
                       label="星期五"></el-option>
            <el-option value="6"
                       label="星期六"></el-option>
            <el-option value="7"
                       label="星期天"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间段"
                      prop="startTime">
          <el-time-select placeholder="起始时间"
                          v-model="maskForm.startTime"
                          :picker-options="{
                            start: '06:30',
                            step: '00:15',
                            end: '23:30'
                          }">
          </el-time-select>
          <el-time-select placeholder="结束时间"
                          v-model="maskForm.endTime"
                          :picker-options="{
                            start: '06:30',
                            step: '00:15',
                            end: '23:30',
                            minTime: maskForm.endTime
                          }">
          </el-time-select>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="raiseVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addraiseClass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { findSound, findMusicGroupClassTeacher, getTeacher, getTeamDetail, addHighClass } from '@/api/buildTeam'
let that;
export default {
  props: {
    teamid: {
      type: String,
      required: true
    }
  },
  beforeCreate () {
    that = this;
  },
  data () {
    return {
      labelPosition: 'right',
      topForm: {
        count: '',
        improventClassesNum: '',
        timer: ''
      },
      maskForm: {
        name: '',
        sound: '',
        count: '',
        teacher: '',
        startTime: '',
        endTime: '',
        week: ''
      },
      raiseVisible: false,
      tableList: [],
      soundList: [],
      teacherList: [],
      maskRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' },
        { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
        sound: [{ required: true, message: '请选择声部', trigger: 'blur' },],
        count: [{ required: true, message: '请输入学生数', trigger: 'blur' }],
        teacher: [{ required: true, message: '请选择老师', trigger: 'blur' }],
        startTime: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
        endTime: '',
        week: [{ required: true, message: '请选择星期几', trigger: 'blur' }]
      },
      topFormRules: {
        count: [{ required: true, message: '请输入排课数量', trigger: 'blur' }],
        timer: [{ required: true, message: '请选择开课时间', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    // 获取乐团声部
    findSound({ musicGroupId: this.teamid }).then(res => {
      if (res.code == 200) {
        this.soundList = res.data;
      }
    })
    findMusicGroupClassTeacher({ musicGroupId: this.teamid }).then(res => {
      if (res.code == 200) {
        this.teacherList = res.data
        // console.log(res.data);
      }
    })
    // 获取乐团老师
    // findMusicGroupClassTeacher({ musicGroupId: this.teamid }).then()
    getTeamDetail({ musicGroupId: this.teamid }).then(res => {
      if (res.code == 200) {
        this.topForm.improventClassesNum = res.data.improventClassesNum;
      }
    });
  },
  methods: {
    addraiseClass () {
      this.$refs['maskForm'].validate(res => {
        if (res) {
          // 验证成功组成数组
          let obj = {
            'userId': this.maskForm.teacher, // 老师id
            'dayOfWeek': this.maskForm.week, // 周几
            'startClassTime': this.maskForm.startTime,
            'endClassTime': this.maskForm.endTime,
            'courseTimes': this.maskForm.count,
            'type': 'HIGH',
            'expectStudentNum': this.maskForm.count,
            'musicGroupId': this.teamid,
            'teachMode': 'OFFLINE',
            'name': this.maskForm.name,
            'subjectIdList': this.maskForm.sound,
            'moid': new Date().getTime(),
            'startDate': this.topForm.timer
          }
          this.tableList.push(obj);
          this.raiseVisible = false;
          this.$refs['maskForm'].resetFields();
          this.maskForm.endTime = '';
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    },
    removeClass (row) {
      for (let i in this.tableList) {
        if (this.tableList[i].moid == row.moid) {
          this.tableList.splice(i, 1);
        }
      }
    },
    addClass () {
      this.$refs['topForm'].validate(res => {
        if (res) {
          this.raiseVisible = true;
        } else {
          // this.$message.error('请先输入排课数量')
        }
      })
    },
    gotoNext () {
      addHighClass(this.tableList).then(res => {
        if (res.code == 200) {
          this.$message.success('恭喜您创建成功')
        }
      })
    }
  },
  filters: {
    fitterSound (val) {
      if (that.soundList) {
        for (let i in that.soundList) {
          if (that.soundList[i].id == val) {
            return that.soundList[i].name
          }
        }
      } else {
        return ''
      }
    },
    fitterTeacher (val) {
      if (that.teacherList) {
        for (let i in that.teacherList) {
          if (that.teacherList[i].userId == val) {

            return that.teacherList[i].name
          }
        }
      } else {
        return ''
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.imc {
  box-sizing: border-box;
  padding: 30px 42px;
  background-color: #fff;
  min-height: 80vh;
  .addBtn {
    line-height: 40px;
    text-align: center;
    color: #fff;
    border-radius: 4px;
    margin-right: 20px;
    cursor: pointer;
    width: 120px;
    height: 40px;
    background-color: #4eada7;
  }
}
</style>