<template>
  <div ref="subjectSign">
    <el-card :style="{'background-image':backimg}" style="padding:30px;margin:10px 0;background-color:white;background-repeat: no-repeat;background-position: bottom right;">
      <p style="color:#A6A6A6;font-size:24px;font-weight:bold;">COURSE PICK</p>
      <p style="color:#414141;font-size:24px;font-weight:bold;">选科报名-<span>{{subjectDetail.type}}</span></p>
      <hr class="layui-bg-orange" style="width:40px;height:4px;margin-top:10px;" />
    </el-card>
    <!--内容区域-->
    <el-card>
      <el-row class="container">
        <div style="margin:20px;">
          <h2>注意事项：</h2>
          <p style="color:red;">1. 选科报名将在学校开启后可以使用，如不能使用，请等待学校开启。</p>
          <p style="color:red;">2. 报名关闭前，学生可随时更改当前的选科报名结果，报名关闭后将不允许更改。</p>
        </div>
        <el-row :gutter="20">
          <el-col :span="14">
            <el-row :gutter="10" v-loading="loading" v-if="subjectDetail.batchId">
              <el-col :span="12">
                <div style="border:1px dashed #1890FF;padding:20px;">
                  <p style="color:#D07A35;">* 请从以下2门首选科目中任意选择1门（必选）</p>
                  <div style="margin-top:40px;">
                    <div style="padding:10px;" v-for="item in subject1Arr" :key="item">
                      <div class="text1" :style="{'backgroundColor':subject1===item?'#D4EAFF':'#FBFBFB'}" @click="subject1=item">
                        {{item}}
                        <input v-if="subject1===item" type="checkbox" class="duigou" />
                      </div>
                    </div>
                  </div>
                </div>
              </el-col>
              <el-col :span="12">
                <div style="border:1px dashed #1890FF;padding:20px;">
                  <p style="color:#D07A35;">* 请从以下4门可选科目中任意点击选中2个</p>
                  <el-row style="margin-top:40px;">
                    <el-col :span="12" style="padding:10px;" v-for="item in subject2Arr" :key="item.id">
                      <!--<div class="text1" :style="{'backgroundColor':hasSubject(item.id)?'#D4EAFF':'#FBFBFB','cursor':!hasSubject(item.id)&&subject2Res.length===2?'not-allowed':'',}"-->
                      <!--@click="!hasSubject(item.id)&&subject2Res.length===2?'':selectSubject2(item.id)">-->
                      <div class="text1" :style="{'backgroundColor':hasSubject(item)?'#D4EAFF':'#FBFBFB','cursor':disabled(item)?'not-allowed':'','color':disabled(item)?'#C9C9C9':'#000000'}" @click="disabled(item)?'':selectSubject2(item)">
                        {{item}}
                        <input v-if="hasSubject(item)" type="checkbox" class="duigou" />
                      </div>
                    </el-col>
                  </el-row>
                </div>

              </el-col>
            </el-row>
            <div style="margin-top: 20px;" v-if="subjectDetail.batchId">
              <el-button type="primary" size="medium" style="margin-right: 10px;" @click="saveSujectInfo">提交选科报名</el-button>
              <!--<span style="color:#999999;">开始日期：2021年04月09日</span>-->
              <span style="color:#999999;">开始日期：{{openTime}}</span>
            </div>
            <div v-else :style="{'color':theme}" style="padding-left: 50px;margin-top: 40px;font-size:22px;">您学校暂未开启选科报名</div>
          </el-col>
          <el-col :span="10">
            <div>
              <el-image style="width: 100%;height:100%;" :src="require('@/assets/images/career/img_dEmAZ_5698.png')"></el-image>
            </div>
          </el-col>
        </el-row>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { selectSyzyXkbm, saveSyzyXkbm } from '@/api/webApi/subjectSign';
export default {
  name: "index",
  data() {
    return {
      backimg: 'url(' + require('@/assets/images/career/icon_colleges.png') + ')',
      subject1: '历史',
      subject2Res: [],
      subject1Arr: [],
      subject2Arr: [],
      subjectDetail: {},
      openTime: '',
      time: false,
      loading: false
    }
  },
  computed: {
    disabled() {
      return function (id) {
        return !this.hasSubject(id) && this.subject2Res.length === 2;
      }

    },
    ...mapState({ theme: state => state.settings.theme }),
    hasSubject() {
      return function (id) {
        const flag = this.subject2Res.some(item => {
          return item === id
        })
        return flag
      }
    }
  },
  watch: {
    theme: {
      immediate: true,
      handler(val) {
        this.$nextTick(() => {
          this.$refs.subjectSign.style.setProperty('--themeColor', val)
        })
      }
    },
    subject1: {
      handler(val) {
        this.subject2Res = []
      }
    }
  },
  created() {
    this.getSyzyXkbm()
  },
  methods: {
    saveSujectInfo() {
      if (this.time === false) {
        const params = {
          batchId: this.subjectDetail.batchId,
          course1: this.subject1,
          course2: this.subject2Res.toString()
        }
        saveSyzyXkbm(params).then(res => {
          this.time = true
          this.msgSuccess('提交成功')
        })
        setTimeout(() => {
          this.time = false
        }, 3000)
      } else {
        this.msgInfo('不可重复提交')
      }
    },
    getSyzyXkbm() {
      this.loading = true
      //获取报名批次信息
      selectSyzyXkbm().then(res => {
        console.log(res.data)
        this.subjectDetail = res.data
        this.subject1Arr = res.data.course1;
        this.subject2Arr = res.data.course2;
        this.subject1 = res.data.userCourse1;
        this.subject2Res = res.data.userCourse2;
        const date = new Date(this.subjectDetail.openTime);
        this.loading = false
        this.openTime = (date.getFullYear()) + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日';
      })
    },
    selectSubject2(id) {
      const flag = this.subject2Res.some(item => {
        return item === id
      })
      if (flag) {
        this.subject2Res.splice(this.subject2Res.indexOf(id), 1)
      } else {
        this.subject2Res.push(id)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.layui-bg-orange {
  background-color: var(--themeColor);
  margin-left: 0;
}
.container {
  font-size: 16px;
  padding: 5px;
  font-family: PingFangSC-Regular, PingFang SC, Helvetica, serif;
  background-color: #ffffff;
  .text1 {
    font-size: 24px;
    font-weight: 600;
    line-height: 89px;
    text-align: center;
    position: relative;
    .textImg {
      width: 16px;
      height: 16px;
      position: absolute;
      bottom: 0;
      right: 0;
      background-color: var(--themeColor);
    }
    .duigou {
      /*display: none;*/
      cursor: pointer;
      width: 14px;
      height: 14px;
      font-size: 14px;
      position: absolute;
      right: 0px;
      bottom: 0px;
      &::after {
        position: absolute;
        top: 0;
        color: #fff;
        width: 15px;
        height: 15px;
        display: inline-block;
        visibility: visible;
        padding-left: 0px;
        text-align: center;
        content: "✓";
        font-size: 12px;
        font-weight: bold;
        background-color: var(--themeColor);
      }
    }
  }
}
</style>
