<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="fx-row fx-bet-cen">
          <span>选班设置</span>
          <div>学生当前是否可操作：<i :class="studentEnableClasses"></i></div>
        </div>
      </template>
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="80px" class="mt30">
        <el-form-item label="时间" prop="dateRange">
          <mx-date-range-picker v-model="form.dateRange" emit-all-changes></mx-date-range-picker>
        </el-form-item>
        <el-form-item label="开启" prop="selectEnable">
          <el-switch v-model="form.selectEnable"></el-switch>
        </el-form-item>
      </el-form>
      <div class="mt20 fx-row fx-cen-cen">
        <el-button type="primary" round style="width: 150px" @click="handleSubmit">保存</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  getSchoolParams,
  updateSchoolClass
} from '@/api/webApi/form.js'
import * as ext from '@/utils'

export default {
  name: 'select-class-setting',
  data() {
    return {
      settings: {},
      form: {
        dateRange: [],
        selectEnable: true
      },
      rules: {
        dateRange: [{
          required: true,
          message: '时间必填',
          validator: (r, v, cb) => v?.length == 2 && v.first() && v.last() ? cb() : cb(r.message)
        }]
      }
    }
  },
  computed: {
    studentEnableClasses() {
      return this.settings.allowSelectClass
        ? ['el-icon-check', 'f-primary']
        : ['el-icon-close', 'f-red']
    }
  },
  beforeMount() {
    this.form.dateRange = ext.getDefaultSelectRange()
  },
  mounted() {
    this.loadSchoolSettings()
  },
  methods: {
    loadSchoolSettings() {
      getSchoolParams().then(res => {
        this.settings = res.data
        this.form.dateRange = [res.data.selectBegin, res.data.selectEnd]
        this.form.selectEnable = this.settings.selectEnable == 1
        console.log('settings', this.settings)
      })
    },
    handleSubmit() {
      this.$refs.form.validate().then(data => {
        updateSchoolClass({
          selectBegin: this.form.dateRange.first(),
          selectEnd: this.form.dateRange.last(),
          selectEnable: this.form.selectEnable ? 1 : 0
        }).then(res => {
          this.msgSuccess('保存成功')
          this.loadSchoolSettings()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
