<template>
  <div class="m-container">
    <div class="titlewrap">
      <h2>
        <el-page-header @back="goBack"
                        :content="name"></el-page-header>
      </h2>
      <!-- <p style="margin-bottom:10px; margin-left:30px;">课程有效期:{{ startClassTime|dayjsFormat }} ~ {{endTime |dayjsFormat}}</p> -->
    </div>
    <div class="vipwrap">
      <!-- <div class="newBand"
           v-permission="'vipGroupManage/appendVipGroupCourseSchedules'"
           @click="addCourse">VIP加课</div> -->
      <div class="newBand"
           v-permission="'courseSchedule/vipCourseAdjust'"
           @click="adjustment">批量调整</div>
      <div class="newBand"
           v-permission="'vipGroupManage/updateVipBaseInfo'"
           @click="addrVisible = true">修改教学点</div>
      <div class="newBand"
           v-permission="{parent:'/vipReset',child:'employee/findEducationUsers'}"
           @click="educationalVisible = true">课程信息</div>
      <!-- <div class="newBand"
           v-permission="'vipGroupManage/update'"
           @click="expireVisible = true">有效期调整</div>  -->
    </div>

    <div class="tableWrap">
      <el-table :data="tableList"
                ref="tableList"
                @selection-change="handleSelectionChange"
                :header-cell-style="{background:'#EDEEF0',color:'#444'}">
        <el-table-column type="selection"
                         :selectable="isDisabled"
                         width="55"></el-table-column>
        <el-table-column label="课程名称"
                         align="center"
                         prop="name"></el-table-column>
        <el-table-column label="课程编号"
                         align="center"
                         prop="id">
        </el-table-column>
        <el-table-column label="上课日期"
                         align="center"
                         prop="classDate">
          <template slot-scope="scope">
            <div>{{ scope.row.classDate | formatTimer}}</div>
          </template>
        </el-table-column>
        <el-table-column label="课程状态"
                         align="center"
                         prop="status">
          <template slot-scope="scope">
            <div>{{ scope.row.status | coursesStatus }}</div>
          </template>
        </el-table-column>
        <el-table-column label="是否点名"
                         align="center">
          <template slot-scope="scope">
            <div>{{scope.row.isCallNames?'已点名':'未点名'}}</div>
          </template>
        </el-table-column>
        <el-table-column label="结算状态"
                         align="center">
          <template slot-scope="scope">
            <div>{{scope.row.isSettlement?'已结算':'未结算'}}</div>
          </template>
        </el-table-column>
        <el-table-column label="开始时间"
                         align="center"
                         prop="startClassTimeStr">
          <template slot-scope="scope">
            <div>{{ scope.row.startClassTimeStr | timerForMinFormat }}</div>
          </template>
        </el-table-column>
        <el-table-column label="结束时间"
                         align="center"
                         prop="endClassTimeStr">
          <template slot-scope="scope">
            <div>{{ scope.row.endClassTimeStr | timerForMinFormat }}</div>
          </template>
        </el-table-column>
        <el-table-column label="课程类型"
                         align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.teachMode | teachMode }}</div>
          </template>
        </el-table-column>
        <el-table-column label="教学点"
                         align="center"
                         prop="schoolName">
          <template slot-scope="scope">
            <div>{{scope.row.schoolName?scope.row.schoolName:'网络教室'}}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         width="180"
                         align="center">
          <template slot-scope="scope">
            <div>
              <el-button type="text"
                         v-permission="{parent:'/vipReset',child:'courseSchedule/classStartDateAdjust'}"
                         v-if="!scope.row.isSettlement"
                         @click="resetClass(scope.row)">调整</el-button>
              <el-button type="text"
                         v-permission="{parent:'/vipReset',child:'courseSchedule/batchDelete'}"
                         v-if="!scope.row.isSettlement&&scope.row.status != 'OVER'"
                         @click="removeClass(scope.row)">删除</el-button>
              <!--   -->
              <el-button type="text"
                         v-permission="{parent:'/vipReset',child:'vip/cleanAttendance'}"
                         v-if="!scope.row.isSettlement"
                         @click="clearAttend(scope.row)">清除考勤</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination :total="rules.total"
                  :page.sync="rules.page"
                  :limit.sync="rules.limit"
                  :page-sizes="rules.page_size"
                  @pagination="getList" />
    </div>
    <el-dialog title="课程调整"
               width="400px"
               :before-close="handleClose"
               :visible.sync="courseVisible">
      <el-form :model="maskForm"
               class="maskForm"
               ref="maskForm"
               :rules="maskRules"
               label-position="right"
               label-width="80px"
               :inline="true">
        <el-form-item label="上课日期"
                      prop="date">
          <el-date-picker v-model.trim="maskForm.date"
                          type="date"
                          :picker-options="coursesDate()"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间"
                      prop="startTime">
          <el-time-picker placeholder="起始时间"
                          v-model.trim="startTime"
                          @change="changeStartTime"
                          format='HH:mm'
                          value-format='HH:mm'
                          :picker-options="{
                            selectableRange: '04:30:00 - 23:30:00'
                            }"></el-time-picker>
        </el-form-item>
        <el-form-item label="结束时间"
                      prop="endTime">
          <el-time-picker placeholder="结束时间"
                          format='HH:mm'
                          value-format='HH:mm'
                          v-model="maskForm.endTime"
                          disabled>
          </el-time-picker>
        </el-form-item>
        <!--v-if="maskForm.teachMode=='OFFLINE'"-->
        <el-form-item label="课程类型"
                      prop="courseType">
          <el-select clearable
                     v-model.trim="maskForm.teachMode">
            <el-option label="线上课"
                       value="ONLINE"></el-option>
            <el-option label="线下课"
                       value="OFFLINE"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教学地点"
                      v-if="maskForm.teachMode == 'OFFLINE'">
          <el-select v-model.trim="maskForm.address"
                     filterable
                     clearable>
            <el-option v-for="(item,index) in schoolList"
                       :key="index"
                       :value="item.id"
                       :label="item.name"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="courseVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submitResetClass">确 定</el-button>
      </div>
    </el-dialog>
    <!-- <el-dialog title="vip加课"
               width="1000px"
               :visible.sync="addCourseVisible">

      <el-button type="text"
                 @click="addOneChiose">添加</el-button>
      <el-table :data='addTable'>
        <el-table-column label="上课日期"
                         align="center"
                         prop="name">
          <template slot-scope="scope">
            <div>
              <el-date-picker v-model.trim="scope.row.classDate"
                              style="width:200px!important"
                              type="date"
                              placeholder="选择日期">
              </el-date-picker>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="课程开始时间"
                         align="center"
                         prop="startClassTimeStr">
          <template slot-scope="scope">
            <div>
              <el-time-select v-model.trim="scope.row.startClassTimeStr"
                              style="width:150px!important"
                              :picker-options="{
                                start: '04:30',
                                step: '00:05',
                                end: '23:30'
                              }"
                              placeholder="选择时间">
              </el-time-select>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="课程形态"
                         align="center"
                         prop="name">
          <template slot-scope="scope">
            <div>
              <el-select v-model.trim="scope.row.teachMode">
                <el-option label="线上课"
                           value="ONLINE"></el-option>
                <el-option label="线下课"
                           value="OFFLINE"></el-option>
              </el-select>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作"
                         align="center">
          <template slot-scope="scope">
            <div>
              <el-button type="text"
                         @click="removeMarkCourse(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addCourseVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submitAdd">确 定</el-button>
      </span>
    </el-dialog>-->
    <el-dialog :title="adjustmentName"
               width="800px"
               :visible.sync="adjustmentVisible">
      <el-form :model="adjustmentForm"
               label-position="right"
               label-width="120px"
               ref="adjustmentForm"
               :rules="adjustmentRules"
               :inline="true">
        <el-form-item label="已选择课时数"
                      v-if="!isaddCourse">
          <el-input disabled
                    v-model.trim="adjustmentForm.count"></el-input>
        </el-form-item>
        <el-form-item label="添加课时数"
                      prop="addCount"
                      v-if="isaddCourse">
          <el-input type="number"
                    @mousewheel.native.prevent
                    v-model.trim="adjustmentForm.addCount"></el-input>
        </el-form-item>
        <br />
        <el-form-item label="单课费用"
                      prop="fee"
                      v-if="isaddCourse">
          <el-input v-model.trim="adjustmentForm.fee"></el-input>
        </el-form-item>
        <!--  v-if='isaddCourse' v-if="adjustmentForm.courseType=='OFFLINE'" -->
        <el-form-item label="课程类型"
                      prop="courseType">
          <el-select clearable
                     v-model.trim="adjustmentForm.courseType">
            <el-option label="线上课"
                       value="ONLINE"></el-option>
            <el-option label="线下课"
                       value="OFFLINE"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教学地点"
                      v-if="adjustmentForm.courseType == 'OFFLINE'">
          <el-select v-model.trim="adjustmentForm.address"
                     filterable
                     clearable>
            <el-option v-for="(item,index) in schoolList"
                       :key="index"
                       :value="item.id"
                       :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <br />
        <el-form-item label="排课起始时间"
                      prop="courseTime">
          <el-date-picker v-model.trim="adjustmentForm.courseTime"
                          :picker-options="pickerOptions()"
                          style="width:200px!important;"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日期"></el-date-picker>
          <el-checkbox style="margin-left:10px;"
                       v-model.trim="adjustmentForm.checked">是否跳过节假日</el-checkbox>
        </el-form-item>
      </el-form>
      <div class="WeekWrap">
        <h3 style="margin-bottom:20px;">
          循环次数
          <el-button type="text"
                     style="margin-left:10px;"
                     @click="addWeek">添加</el-button>
        </h3>
        <div class="countWrap"
             style="margin-bottom:10px;">
          <div class="countItem"
               style="margin-bottom:20px;"
               v-for="(item,index) in weekList"
               :key="index">
            <span>循环周期:</span>
            <el-select v-model.trim="item.dayOfWeek"
                       filterable
                       clearable>
              <el-option v-for="(item,index) in weekDateList"
                         :key="index"
                         :label="item.label"
                         :value="item.value"></el-option>
            </el-select>
            <span style="margin-left:10px;">开始时间</span>
            <el-time-picker style="margin-left:10px;"
                            placeholder
                            v-model.trim="item.startClassTime"
                            @change="(val)=>{changeStartTimes(val,item)}"
                            format='HH:mm'
                            value-format='HH:mm'
                            :picker-options="{
                            selectableRange: '04:30:00 - 23:30:00'
                            }"></el-time-picker>
            <span style="margin-left:10px;">结束时间</span>
            <el-time-picker style="margin-left:10px;"
                            placeholder
                            v-model.trim="item.endClassTime"
                            format='HH:mm'
                            value-format='HH:mm'
                            disabled
                            :picker-options="{
                            selectableRange: '04:30:00 - 23:30:00'
                            }"></el-time-picker>
            <el-button style="margin-left:10px;"
                       type="danger"
                       @click="removeWeek(item)"
                       icon="el-icon-delete"
                       circle></el-button>
          </div>
        </div>
      </div>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="adjustmentVisible = false">取 消</el-button>
        <el-button type="primary"
                   v-if="!isaddCourse"
                   @click="submitAdjustment">确 定</el-button>
        <el-button type="primary"
                   v-if="isaddCourse"
                   @click="addCourseSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 批量修改教学点 -->
    <el-dialog title="教学点"
               width="400px"
               :visible.sync="addrVisible">
      <el-form :model="addrForm">
        <el-form-item label="教学点">
          <el-select v-model.trim="addrForm.address"
                     clearable
                     filterable>
            <el-option v-for="(item,index) in schoolList"
                       :key="index"
                       :value="item.id"
                       :label="item.name"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addrVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="resetAddrSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 课程信息老师 -->
    <el-dialog title="课程信息"
               width="400px"
               :visible.sync="educationalVisible">
      <el-form :model="addrForm"
               label-width="120px">
        <el-form-item label="课程分部"
                      prop="organId">
          <el-select v-model.trim="addrForm.organId"
                     filterable
                     @change="onEduTeacher('on')"
                     clearable>
            <el-option v-for="(item,index) in organList"
                       :key="index"
                       :label="item.name"
                       :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="乐团主管"
                      prop="educationalTeacherId">
          <el-select v-model.trim="addrForm.educationalTeacherId"
                     filterable
                     :disabled="!addrForm.organId"
                     clearable>
            <el-option v-for="(item,index) in educationList"
                       :key="index"
                       :label="item.realName"
                       :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="educationalVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="resetEducationalVisible">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="有效期调整"
               width="400px"
               :visible.sync="expireVisible">
      <el-form :model="expireForm"
               ref="expireForm"
               :rules="expireRules"
               label-position="right"
               label-width="80px;"
               :inline="true">
        <el-form-item label="课程结束时间"
                      prop="coursesExpireDate">
          <el-date-picker v-model.trim="expireForm.coursesExpireDate"
                          style="width:200px!important;"
                          type="date"
                          :picker-options="bigin"
                          value-format="yyyy-MM-dd"
                          placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <!-- <div style="padding-left: 15px; color: red;">课程结束时间不得晚于，{{ expireForm.tempCoursesExpireDate }}</div> -->
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="expireVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submitExpireDate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  resetCourse,
  cleanAttendance,
  findEducationUsers,
  getEmployeeOrgan,

} from "@/api/buildTeam";
import { queryEmployByOrganId } from '@/api/systemManage'
// import { getSchool } from '@/api/systemManage'
import pagination from "@/components/Pagination/index";
import {
  findVipGroupCourseSchedules,
  bathDelete,
  appendVipGroupCourseSchedules,
  vipCourseAdjust,
  updateVipBaseInfo,
  batchAppendVipGroupCourses,
  vipGroupManageUpdate
} from "@/api/vipSeting";
import { getTeachSchool } from "@/api/teacherManager";
import { diffTimerFormMinute, addTimerFormMinute } from '@/utils/date'
import dayjs from 'dayjs'
export default {
  components: {
    pagination
  },
  data () {
    return {
      expireVisible: false,
      addrVisible: false,
      adjustmentVisible: false,
      addCourseVisible: false,
      courseVisible: false,
      educationalVisible: false,
      tableList: [],
      addTable: [],
      educationList: [],
      courseTime: "",
      vipid: "",
      addrForm: {
        address: "",
        educationalTeacherId: "",
        organId: ""
      },
      maskForm: {
        date: "",
        startTime: "",
        endTime: "",
        id: "",
        address: "",
        teachMode: ""
      },
      rules: {
        // 分页规则
        limit: 50, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [50, 100] // 选择限制显示条数
      },
      maskRules: {
        date: [{ required: true, message: "请选择上课时间", trigger: "blur" }],
        // startTime: [{ required: true, message: '请选择上课开始时间', trigger: 'blur' },],
        // endTime: [
        //   { required: true, message: "请选择上课结束时间", trigger: "blur" }
        // ]
      },
      schoolList: [],
      Frules: null,
      FsearchForm: null,
      adjustmentForm: {
        count: "",
        courseTime: "",
        checked: false,
        addCount: "",
        courseType: "",
        fee: "",
        address: ""
      },
      adjustmentRules: {
        courseTime: [{ required: true, message: "请选择开始时间" }],
        addCount: [{ required: true, message: "请输入加课次数" }],
        courseType: [{ required: true, message: "请选择课程类型" }],
        fee: [{ required: true, message: "请输入费用" }],
        address: [{ required: true, message: "请选择教学地点" }]
      },
      weekDateList: [
        { value: "1", label: "星期一" },
        { value: "2", label: "星期二" },
        { value: "3", label: "星期三" },
        { value: "4", label: "星期四" },
        { value: "5", label: "星期五" },
        { value: "6", label: "星期六" },
        { value: "7", label: "星期日" }
      ],
      weekList: [
        {
          dayOfWeek: "",
          startTime: "",
          endTime: "",
          moid: new Date().getTime()
        }
      ],
      activeList: [],
      // 改版加课
      isaddCourse: false,
      adjustmentName: "",
      startTime: "",

      chioseVipList: [],
      organList: [],
      name: '',
      endTime: '',
      startClassTime: '',
      expireForm: {
        coursesExpireDate: null,
        tempCoursesExpireDate: null
      },
      bigin: this.beginDate(),
      expireRules: {
        coursesExpireDate: [{ required: true, message: "请选择课程结束时间", trigger: "blur" }]
      },
    };
  },
  created () { },
  mounted () {
    this.__init();
    this.getList();
  },
  activated () {
    this.__init();
    this.getList();
  },
  methods: {
    __init () {
      this.vipid = this.$route.query.id;
      let query = this.$route.query
      let addrForm = this.addrForm
      this.name = this.$route.query.name;
      this.endTime = this.$route.query.endTimeStr
      this.expireForm.coursesExpireDate = this.endTime;
      this.startClassTime = this.$route.query.startTimeStr
      if (query.educationalTeacherId) {
        addrForm.educationalTeacherId = Number(query.educationalTeacherId);
      } else {
        addrForm.educationalTeacherId = null;
      }

      if (query.organId) {
        addrForm.organId = Number(query.organId)
        this.onEduTeacher()
      } else {
        addrForm.organId = null
      }
      if (query.rules) {
        this.Frules = query.rules;
      }
      if (query.searchForm) {
        this.FsearchForm = query.searchForm;
      }
      //  获取乐团主管
      // findEducationUsers().then(res => {
      //   if (res.code == 200) {
      //     this.educationList = res.data;
      //   }
      // });
      getEmployeeOrgan().then(res => {
        if (res.code == 200) {
          this.organList = res.data;
        }
      });

      // 取课程组上指导老师，教学点
      getTeachSchool({
        userId: query.userId
      }).then(res => {
        if (res.code == 200) {
          this.schoolList = res.data;
        }
      })
    },
    getList () {
      findVipGroupCourseSchedules({
        vipGroupId: this.vipid,
        rows: this.rules.limit,
        page: this.rules.page
      }).then(res => {
        if (res.code == 200) {
          this.tableList = res.data.pageInfo.rows;
          this.rules.total = res.data.pageInfo.total;
          this.courseTime = res.data.singleClassMinutes;
        }
      });
    },
    goBack () {
      this.$router.push({
        path: "/business/vipList",
        query: { rules: this.Frules, searchForm: this.FsearchForm }
      });
    },
    resetClass (row) {
      /**
       * maskForm.startTime
       *
       */
      this.maskForm.date = row.classDate.split(' ')[0];
      this.startTime = row.startClassTimeStr.substring(0, 5)
      this.maskForm.endTime = row.endClassTimeStr.substring(0, 5)
      // this.maskForm.endTime = row.endClassTimeStr;
      this.maskForm.id = row.id;
      this.maskForm.address = row.schoolId || null;
      this.maskForm.teachMode = row.teachMode;
      // 修改课时
      this.courseVisible = true;
    },
    handleClose () {
      this.courseVisible = false;
      this.startTime = "";
      this.maskForm = {
        date: "",
        startTime: "",
        endTime: "",
        id: "",
        address: "",
        teachMode: ""
      };
      this.$refs["maskForm"].resetFields();
    },
    submitResetClass () {
      //   endClassTimeStr: this.maskForm.endTime,
      if (!this.startTime) {
        this.$message.error("请填写修改时间");
        return;
      }
      let classTime = this.maskForm.date
        ? new Date(this.maskForm.date)
        : new Date();
      let ymd =
        classTime.getFullYear() +
        "-" +
        (classTime.getMonth() + 1) +
        "-" +
        classTime.getDate();
      let tempStartTime = this.startTime
      if (this.startTime.length <= 5) {
        tempStartTime = tempStartTime + ":00";
      }
      let obj = {
        startClassTime: ymd + " " + this.startTime,
        // startClassTimeStr: this.startTime,
        id: this.maskForm.id,
        classDate: this.maskForm.date,
        schoolId: this.maskForm.address || null,
        teachMode: this.maskForm.teachMode || null,
        groupType: 'VIP'
      };
      resetCourse(obj).then(res => {
        if (res.code == 200) {
          this.$message.success("修改成功");
          this.courseVisible = false;
          this.getList();
        } //else {
        //   this.$message.error(res.msg)
        // }
      });
    },
    removeClass (row) {
      this.$confirm("是否删除该vip课程?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        bathDelete({ courseScheduleIds: row.id }).then(res => {
          if (res.code == 200) {
            this.$message.success("删除成功");
            this.getList();
          }
        });
      });
    },
    // vip 家课
    addCourse () {
      this.adjustmentName = "vip加课";
      this.isaddCourse = true;
      this.adjustmentVisible = true;
      // this.adjustmentForm.count = 0;
      // this.addCourseVisible = true;
    },
    addOneChiose () {
      this.addTable.push({
        classDate: "",
        startClassTimeStr: "",
        endClassTimeStr: "",
        teachMode: "",
        moid: new Date().getTime()
      });
    },
    removeMarkCourse (row) {
      for (let i in this.addTable) {
        if (this.addTable[i].moid == row.moid) {
          this.addTable.splice(i, 1);
        }
      }
    },
    submitAdd () {
      let endClassTimeStr = this.MinutesTest(
        this.addTable[0].startClassTimeStr,
        90
      );
      let some = {
        vipGroupApplyBaseInfo: {
          id: this.vipid
        },
        courseSchedules: []
      };
      for (let i in this.addTable) {
        let obj = {
          classDate: this.addTable[i].classDate,
          startClassTimeStr: this.addTable[i].startClassTimeStr,
          endClassTimeStr: this.MinutesTest(
            this.addTable[i].startClassTimeStr,
            this.courseTime
          ),
          teachMode: this.addTable[i].teachMode
        };
        some.courseSchedules.push(obj);
      }
      // 发请求干活
      appendVipGroupCourseSchedules(some).then(res => {
        if (res.code == 200) {
          this.addCourseVisible = false;
          this.rules.page = 1;
          this.getList();
        }
      });
    },
    isDisabled (row, index) {
      if (row.isSettlement) {
        return false;
      } else {
        return true;
      }
    },
    // 分钟小时相加减
    MinutesTest (str, interval) {
      let houer = str.split(":")[0];
      let min = str.split(":")[1];
      let sdate1 = new Date(1900, 1, 1, houer, min);
      sdate1.setMinutes(sdate1.getMinutes() + parseInt(interval));
      let H = sdate1.getHours();
      let M = sdate1.getMinutes();
      if (H < 10) H = "0" + H;
      if (M < 10) M = "0" + M;
      return H + ":" + M;
    },
    handleSelectionChange (val) {
      this.chioseVipList = val
      this.adjustmentForm.count = val.length;
      this.activeList = val;
    },
    addWeek () {
      // 添加循环周期
      this.weekList.push({
        dayOfWeek: "",
        startClassTime: "",
        endClassTime: "",
        id: new Date()
      });
    },
    // 删除循环周
    removeWeek (item) {
      for (let i in this.weekList) {
        if (this.weekList[i].id == item.id) {
          this.weekList.splice(i, 1);
        }
      }
    },
    adjustment () {
      this.adjustmentName = "批量调整";
      this.isaddCourse = false;
      if (this.adjustmentForm.count <= 0) {
        this.$message.error("请至少勾选一节课");
      } else {
        this.adjustmentVisible = true;
      }
    },
    submitAdjustment () {
      this.$refs["adjustmentForm"].validate(item => {
        if (item) {
          let week = this.weekList;
          if (!week[0] || !week[0].startClassTime || !week[0].dayOfWeek) {
            this.$message.error("至少排一节课");
            return;
          }
          // 开始
          let obj = {};
          obj.courseCreateStartTime = this.adjustmentForm.courseTime;
          let idArr = this.activeList.map(item => {
            return item.id;
          });
          // let tempWeekList = []
          // this.weekList.forEach(item => {
          //   let tempTime = item.startClassTime.length <= 5 ? item.startClassTime + ':00' : item.startClassTime
          //   tempWeekList.push({
          //     dayOfWeek: item.dayOfWeek,
          //     startTime: item.startTime,
          //     endTime: item.endTime,
          //     moid: item.moid,
          //     startClassTime: obj.courseCreateStartTime + ' ' + tempTime,
          //   })
          // })

          let courseScheduleIds = idArr.join(",");
          obj.courseScheduleIds = courseScheduleIds;
          obj.courseTimes = this.weekList;
          obj.holiday = this.adjustmentForm.checked;
          obj.teachMode = this.adjustmentForm.courseType || null;
          obj.groupType = "VIP";
          obj.vipGroupId = this.vipid;
          obj.schoolId = this.adjustmentForm.address || null
          vipCourseAdjust(obj).then(res => {
            if (res.code == 200) {
              this.$message.success("恭喜您修改成功");
              this.adjustmentVisible = false;
              this.getList();
            }
          });
        }
      });
    },
    addCourseSubmit () {
      this.$refs["adjustmentForm"].validate(item => {
        if (item) {
          let week = this.weekList;
          if (!week[0] || !week[0].startClassTime || !week[0].dayOfWeek) {
            this.$message.error("至少排一节课");
            return;
          }
          // 开始
          let obj = {};
          obj.courseCreateStartTime = this.adjustmentForm.courseTime;
          let idArr = this.activeList.map(item => {
            return item.id;
          });
          let courseScheduleIds = idArr.join(",");
          obj.courseScheduleIds = courseScheduleIds;
          obj.courseTimes = this.weekList;
          obj.holiday = this.adjustmentForm.checked;
          obj.vipGroupId = this.vipid;
          obj.courseCount = this.adjustmentForm.addCount;
          obj.teachMode = this.adjustmentForm.courseType;
          obj.singleClassPrice = this.adjustmentForm.fee;
          obj.groupType = "VIP";
          batchAppendVipGroupCourses(obj).then(res => {
            if (res.code == 200) {
              this.$message.success("恭喜您添加成功");
              this.adjustmentVisible = false;
              this.getList();
            }
          });
        }
      });
    },
    resetAddrSubmit () {
      if (!this.addrForm.address) {
        this.$message.error("请选择一个教学点");
        return;
      }
      // 修改教学点
      updateVipBaseInfo({
        id: this.vipid,
        teacherSchoolId: this.addrForm.address,
        educationalTeacherId: this.addrForm.educationalTeacherId
      }).then(res => {
        if (res.code == 200) {
          this.$message.success("修改成功");
          this.addrVisible = false;
          this.getList();
        }
      });
    },
    onEduTeacher (type) {
      this.educationList = []
      let addrForm = this.addrForm
      if (type) { addrForm.educationalTeacherId = null }
      queryEmployByOrganId({ organId: addrForm.organId, rows: 10000 }).then(res => {
        if (res.code == 200) {
          this.educationList = res.data.rows
        }
      })
    },
    resetEducationalVisible () {
      let addrForm = this.addrForm
      if (!addrForm.organId) {
        this.$message.error("请选择课程分部");
        return;
      }
      if (!addrForm.educationalTeacherId) {
        this.$message.error("请选择乐团主管");
        return;
      }
      // 修改乐团主管
      updateVipBaseInfo({
        id: this.vipid,
        organId: addrForm.organId,
        educationalTeacherId: addrForm.educationalTeacherId
      }).then(res => {
        if (res.code == 200) {
          this.$message.success("修改成功");
          this.educationalVisible = false;
          this.getList();
        }
      });
    },
    // 清除考勤
    clearAttend (row) {
      this.$confirm("是否清除考勤记录?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          cleanAttendance({ courseScheduleIds: row.id }).then(res => {
            if (res.code == 200) {
              this.$message.success("清除成功");
              this.getList();
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => { });
    },
    changeStartTime (val) {
      this.$nextTick(res => {
        if (val) {
          this.$set(this.maskForm, 'endTime', addTimerFormMinute(this.maskForm.date, val, this.courseTime))
        } else {
          this.$set(this.maskForm, 'endTime', '')
        }
        if (!this.maskForm.endTime) {
          this.$set(this, 'startTime', '')
        }
      })


    },
    changeStartTimes (val, item) {
      this.$nextTick(res => {
        if (val) {
          let str = dayjs(new Date()).format('YYYY-MM-DD')
          this.$set(item, 'endClassTime', addTimerFormMinute(str, val, this.courseTime))
        } else {
          this.$set(item, 'endClassTime', '')
        }
        if (!item.endClassTime) {
          console.log(item.startClassTime)
          this.$set(item, 'startClassTime', '')
        }
      })
    },
    coursesDate (dateStr) {
      let self = this;
      return {
        firstDayOfWeek: 1,
        disabledDate: time => {
          return time.getTime() + 86400000 <= new Date().getTime()
        }
      };
    },
    pickerOptions (dateStr) {
      return {
        firstDayOfWeek: 1,
        disabledDate (time) {

          return time.getTime() + 86400000 <= new Date().getTime();
        }
      }
    },
    submitExpireDate () {
      this.$refs.expireForm.validate(some => {
        if (some) {
          vipGroupManageUpdate({
            id: this.vipid,
            coursesExpireDate: this.expireForm.coursesExpireDate
          }).then(res => {
            if (res.code == 200) {
              this.$message.success("有效期修改成功")
              this.expireVisible = false
              let str = this.expireForm.coursesExpireDate + ' 00:00:00'
              this.$router.push({
                query: {
                  ...this.$route.query,
                  endTimeStr: str
                }
              })
              // this.$route.query.endTimeStr = str;
              this.endTime = str
              console.log(str)
              this.getList();
            } else {
              this.$message.error(res.msg)
            }
          })
        } else {
          return;
        }
      });

    },
    beginDate () {
      let self = this
      return {
        firstDayOfWeek: 1,
        disabledDate (time) {
          // if (self.expireForm.tempCoursesExpireDate) {
          //   return new Date(self.expireForm.tempCoursesExpireDate).getTime() > time.getTime()
          // } else {
          return time.getTime() <= Date.now()
          //开始时间不选时，结束时间最大值小于等于当天
          // }
        }
      }
    }
  },
  watch: {
    adjustmentVisible (val) {
      if (!val) {
        this.adjustmentForm = {
          count: "",
          courseTime: "",
          checked: false,
          addCount: "",
          courseType: "",
          fee: "",
          address: ""
        }
        this.weekList = [];
        this.$refs['tableList'].clearSelection();
        this.$refs.adjustmentForm.resetFields();

        this.addWeek();
      }
    },
    addrVisible (val) {
      if (!val) {
        this.addrForm.address = "";
      }
    },

  }
};
</script>
<style lang="scss" scoped>
.vipwrap {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  .newBand {
    margin-right: 20px;
  }
}
.resetClassForm {
  /deep/.el-date-editor.el-input,
  /deep/.el-date-editor.el-input__inner {
    width: 180px !important;
  }
}
.countWrap {
  /deep/.el-date-editor.el-input,
  /deep/.el-date-editor.el-input__inner {
    width: 100px !important;
  }
}

.maskForm {
  /deep/.el-input {
    width: 220px !important;
  }
}
.titlewrap {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}
</style>
