<template>
  <NavHeader/>
  <bread-crumb/>
  <div class="w-1200px m-auto">
    <div class="relative -mt-40px flex justify-end">
      <button type="button" class="back-btn" @click="linkTo('process')">返回</button>
    </div>
    <div class="mt-10px w-full min-h-860px bg-hex-FFF pt-25px">
      <h3 class="text-18px text-center">新建考试计划</h3>
      <div class="mt-60px pl-250px">
        <el-form ref="ruleFormRef" :rules="rules" :model="createForm" label-width="120px" size="large" status-icon>
          <el-form-item label="考试名称" prop="ksmc" style="width: 620px">
            <el-input v-model="createForm.ksmc" placeholder="请输入考试名称" />
          </el-form-item>
          <el-form-item label="考试时间" prop="kssj" style="width: 620px">
            <el-date-picker  v-model="createForm.kssj" type="daterange" range-separator="至" start-placeholder="考试开始时间"
                            end-placeholder="考试结束时间" value-format="YYYY-MM-DD" />
          </el-form-item>
          <el-form-item label="考试类型" prop="kslx" style="width: 620px">
            <el-radio-group v-model="createForm.kslx">
              <el-radio v-for="item in type_list" :label="item.value">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="createForm.kslx === '7'" label="联考学校" prop="lkxx">
            <el-select v-model="createForm.lkxx" style="width: 500px" multiple placeholder="请选择联考学校">
              <el-option
                v-for="item in school_list"
                :label="item.schoolname"
                :value="{value:item.schoolid,label:item.schoolname}"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="考试年级" prop="ksnj">
            <el-select v-model="createForm.ksnj" style="width: 500px" placeholder="请选择考试年级" @change="getSubjectList">
              <el-option
                v-for="item in grade_list"
                :label="item.gradename"
                :value="{label:item.gradename,value:item.grade}"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="考试科目" prop="kskm">
            <el-select v-model="createForm.kskm" style="width: 500px" multiple placeholder="请选择考试科目">
              <el-option
                v-for="item in subject_list"
                :label="item.subject_name"
                :value="{label:item.subject_name,value:item.subject_id}"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="阅卷流程" prop="yjlc" style="width: 620px">
            <el-radio-group v-model="createForm.yjlc">
              <el-radio v-for="item in process_type" :label="item.value">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <div class="mt-50px pl-50px">
              <el-button class="mr-20px" @click="linkTo('process')"><span class="px-40px">取消</span></el-button>
              <el-button type="primary" color="#003eee" @click="handleSubmit(ruleFormRef)"><span class="px-40px">确定</span></el-button>
            </div>


          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
  <commonFooter/>
</template>
<route lang="json">
{
"meta":{
"title":"新建考试计划",
"breadcrumb": true
}
}
</route>
<script setup>
import { useRouter } from "vue-router";
import request from "~/utils/request";
import {user} from "~/store";
const router = useRouter();
const linkTo = (name) => {
  router.push({ name });
};
const rules = $ref({
  ksmc:[{ required: true, message: '请输入考试名称', trigger: 'blur' }],
  kssj:[{ required: true, message: "请选择考试时间", trigger: "blur" }],
  kslx: [{required: true, message: '请选择考试类型', trigger: 'change'}],
  ksnj: [{required: true, message: '请选择一个年级', trigger: 'change'}],
  kskm: [{required: true, message: '请至少选择一个科目', trigger: 'change'}],
  yjlc: [{required: true, message: '请选择阅卷流程', trigger: 'change'}],
  lkxx: [{required: true, message: '请至少选择一个学校', trigger: 'change'}],
})
const ruleFormRef = ref(null);
let createForm = $ref({
  ksmc:'',
  kssj:'',
  kslx:'1',
  ksnj:'',
  kskm:[],
  yjlc:'1',
  lkxx:''
})
let type_list = [{
  value: '1',
  label: '周考'
}, {
  value: '2',
  label: '月考'
}, {
  value: '3',
  label: '期中'
},{
  value: '4',
  label: '期末'
}, {
  value: '5',
  label: '作业'
}, {
  value: '6',
  label: '测试'
},{
  value: '7',
  label: '联考'
}]
let grade_list = $ref([])
let subject_list = $ref([])
let process_type = [{
  value: '1',
  label: '先上传后划块'
},{
  value: '2',
  label: '先划块后上传'
}]
let school_list = $ref([])
const getSchool= () => {
  request({
    url:window.GLOBAL_CONFIG.uc + "/index.php",
    data: {
      mod:'school',
      action:'main',
      do:'index',
      page:'1',
      limit:'999'
    },
  }).then((res) => {
    if (res.code === "1") {
      school_list = res.data.page_data;
    }
  });
}
getSchool();
function getGrade() {
  let data = {
    for_mistake:'1'
  }
  if(user.user_role_id < 72) {
    data.sm_id = user.sm_info.sm_id
  }
  request({
    url: window.GLOBAL_CONFIG.uc + '/index.php?mod=school&action=main&do=base_grade',
    data: data,
  }).then((res) => {
    if (res.code === "1") {
      grade_list = res.data;
    }
  });
}
getGrade();
const getSubjectList = () => {
  for(let i in grade_list) {
    if(grade_list[i].grade === createForm.ksnj.value) {
      createForm.kskm = [];
      subject_list = grade_list[i].subjects;
    }
  }
}
const handleCancel = () => {
  router.back();
}

const handleSubmit = async (formEl) => {
  if (!formEl) return;

  formEl.validate(async (valid, fields) => {
    if (valid) {
      let kskm_label = [];
      let kskm_value = [];
      for(let i in createForm.kskm) {
        kskm_label.push(createForm.kskm[i].label);
        kskm_value.push(createForm.kskm[i].value);
      }

      let data = {
        issubmit:'1',
        yzy_ksjh:{
          ykj_ksrwmc:createForm.ksmc,
          ykj_ksrq:createForm.kssj[0],
          ykj_jsrq:createForm.kssj[1],
          ykj_kslx:createForm.kslx,
          ykj_ksnj:createForm.ksnj.value,
          ykj_ksnj_name:createForm.ksnj.label,
          ykj_kskm:kskm_value.join(','),
          ykj_kskm_name:kskm_label.join(','),
          ykj_yjlc:createForm.yjlc
        }
      }
      if(createForm.kslx === '7') {
        let lkxx_value = [];
        let lkxx_label = [];
        for(let j in createForm.lkxx) {
          lkxx_label.push(createForm.lkxx[j].label);
          lkxx_value.push(createForm.lkxx[j].value);
        }
        data.yzy_ksjh.ykj_lkxx = lkxx_value.join(',');
        data.yzy_ksjh.ykj_lkxx_name  = lkxx_label.join(',');
      }
      request({
        url: '/yzy/ksjh/liankao_add',
        data: data,
      }).then((res) => {
        if (res.code === "1") {
          ElMessage.success("考试计划创建成功！");
          linkTo('process');
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
}
</script>

<style scoped>

</style>
