<template>
  <div class="app-warp">
    <div class="detail-structure">
      <el-page-header v-if="parentObj.TYPE === 'add'" content="课程列表" @back="goPrevious"/>
      <div class="detail-container">
        <div class="app-tools">
          <div class="app-function">
            <el-button v-if="parentObj.STATE === '' || parentObj.STATE === '1'" type="primary" icon="el-icon-plus" @click="goToAddCourseBox">新增课程</el-button>
            <el-button v-if="(parentObj.STATE === '' || parentObj.STATE === '1') && TRAINTYPE !== '9cbe83925bbb4d84bc057b5eb5607a53' && show === 1" type="success" icon="el-icon-setting" @click="configureTestPaperDialogShow">配置试卷</el-button>
            <el-button v-if="(!(parentObj.STATE === '' || parentObj.STATE === '1')) && TRAINTYPE !== '9cbe83925bbb4d84bc057b5eb5607a53' && show === 1" type="success" icon="el-icon-watermelon" @click="openPapers">试卷详情</el-button>
          </div>
        </div>
        <div class="app-container">
          <el-table
            v-loading="listLoading"
            :data="varList"
            :header-cell-style="{
              background:'#f5f7fa',
              'font-weight': 'bold',
              'color': '#000'
            }"
            tooltip-effect="dark"
            border
            fit
            highlight-current-row>
            <el-table-column type="index" label="序号" width="50" align="center"/>
            <el-table-column show-overflow-tooltip prop="CURRICULUMNAME" label="课程名称"/>
            <el-table-column show-overflow-tooltip prop="ALL_DEPARTMENT_NAME" label="部门"/>
            <el-table-column show-overflow-tooltip prop="ALL_POST_NAME" label="工种"/>
            <el-table-column show-overflow-tooltip prop="CLASSHOUR" label="要求完成总学时"/>
            <el-table-column show-overflow-tooltip prop="VIDEOTIME" label="视频累计时长">
              <template slot-scope="{row}">
                {{ secondsCount(row.VIDEOTIME) }}
              </template>
            </el-table-column>
            <el-table-column show-overflow-tooltip prop="start_sum" label="上课学员数"/>
            <el-table-column show-overflow-tooltip prop="finsh_num" label="已完成学员数"/>
            <el-table-column label="操作" align="center" width="200">
              <template slot-scope="{row}">
                <el-button type="text" @click="goToSeeClassMinsCtr(row.CLASSCURRICULUM_ID)">查看</el-button>
                <span class="app-divider"/>
                <el-button type="text" @click="goToInvolvedInTraining(row.CLASSCURRICULUM_ID)">涉及培训工种</el-button>
                <span v-if="parentObj.STATE === '' || parentObj.STATE === '1'" class="app-divider"/>
                <el-button v-if="parentObj.STATE === '' || parentObj.STATE === '1'" type="text" @click="handleDelete(row.CLASSCURRICULUM_ID)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="page-btn-group">
            <div>
              <el-button v-if="parentObj.TYPE === 'add'" icon="el-icon-arrow-left" @click="goPrevious">上一步</el-button>
              <el-button v-if="parentObj.TYPE === 'edit'" icon="el-icon-arrow-left" @click="goBack">返回</el-button>
              <el-button v-if="parentObj.STATE === '' || parentObj.STATE === '1'" type="primary" icon="el-icon-check" @click="foComplete">完成</el-button>
            </div>
            <pagination
              :total="total"
              :page.sync="listQuery.page"
              :limit.sync="listQuery.limit"
              @pagination="getClassCurriculumList"/>
          </div>
        </div>
      </div>
    </div>
    <el-dialog :visible.sync="finalExamDialog.visible" title="平台试卷" width="80%" top="60px">
      <el-row type="flex">
        <el-col :span="15" style="border: 1px solid #1890ff;padding:10px;border-radius: 10px;">
          <el-form :model="finalExamDialog.form" label-width="70px">
            <el-row :gutter="12">
              <el-col :span="8">
                <el-form-item label="试卷名称">
                  <el-input v-model="finalExamDialog.form.KEYWORDS" placeholder="请输入试卷名称"/>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label-width="5px">
                  <el-button v-waves type="primary" icon="el-icon-search" @click="getFinalExamDialogList">
                    搜索
                  </el-button>
                  <el-button v-waves icon="el-icon-refresh" class="ml-10" @click="FinalExamDialogListGoKeyReset">
                    重置
                  </el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div class="app-container">
            <el-table
              v-loading="finalExamDialog.listLoading"
              :data="finalExamDialog.varList"
              :header-cell-style="{
                background:'#f5f7fa',
                'font-weight': 'bold',
                'color': '#000'
              }"
              tooltip-effect="dark"
              border
              fit
              highlight-current-row>
              <el-table-column show-overflow-tooltip prop="EXAMNAME" label="试卷名称"/>
              <el-table-column label="操作" width="100px">
                <template slot-scope="{row}">
                  <el-button type="text" @click="examPaperPreviewDialogShow(row.STAGEEXAMPAPERINPUT_ID)">预览</el-button>
                  <span class="app-divider"/>
                  <el-button type="text" @click="finalExamDialogUseTestPaper(row)">使用</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="page-btn-group">
              <div/>
              <pagination
                :total="finalExamDialog.total"
                :page.sync="finalExamDialog.listQuery.page"
                :limit.sync="finalExamDialog.listQuery.limit"
                @pagination="getFinalExamDialogList"/>
            </div>
          </div>
        </el-col>
        <el-col :span="8" :offset="1" style="border: 1px solid #1890ff;padding:10px;border-radius: 10px;">
          <div class="level-title">
            <h1>考试设置</h1>
          </div>
          <div style="color: #606266;font-size: 14px;font-weight: 700;margin-bottom: 18px;">试卷名称 ：{{ finalExamDialog.submitForm.EXAMNAME }}</div>
          <div style="color: #606266;font-size: 14px;font-weight: 700;margin-bottom: 18px;">试卷分数 ：{{ finalExamDialog.submitForm.EXAMSCORE }}</div>
          <div style="color: #606266;font-size: 14px;font-weight: 700;margin-bottom: 18px;">合格分数 ：{{ finalExamDialog.submitForm.PASSSCORE }}</div>
          <el-form :model="finalExamDialog.submitForm">
            <el-form-item label="考试时长（分）" class="is-required">
              <el-input-number v-model="finalExamDialog.submitForm.ANSWERSHEETTIME" :min="0" style="width: 100%;"/>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <span slot="footer">
        <el-button @click="finalExamDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="finalExamDialogSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="examPaperPreviewDialog.visible" title="试卷预览" width="70%" append-to-body top="30px">
      <div class="level-title">
        <h1>试卷基本信息</h1>
      </div>
      <div style="display: flex;justify-content: space-around;">
        <div>试卷名称：{{ examPaperPreviewDialog.info.EXAMNAME }}</div>
        <div>试卷总分：{{ examPaperPreviewDialog.info.EXAMSCORE }}</div>
        <div>通过分数：{{ examPaperPreviewDialog.info.PASSSCORE }}</div>
      </div>
      <div class="level-title" style="margin-top: 20px;">
        <h1>试卷内容信息</h1>
      </div>
      <el-form :model="examPaperPreviewDialog.form" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="题目内容" prop="KEYWORDS">
              <el-input v-model="examPaperPreviewDialog.form.KEYWORDS"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="题目类型" prop="QUESTIONTYPE">
              <el-select v-model="examPaperPreviewDialog.form.QUESTIONTYPE">
                <el-option value="1" label="单选题"/>
                <el-option value="2" label="多选题"/>
                <el-option value="3" label="判断题"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button v-waves type="primary" icon="el-icon-search" @click="getExamPaperPreviewDialogList">
              搜索
            </el-button>
            <el-button v-waves icon="el-icon-refresh" class="ml-10" @click="examPaperPreviewDialogListGoKeyReset">
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <div class="study-papg">
        <dl
          v-for="(item,index) in examPaperPreviewDialog.varList"
          :key="item.PAPER_QUESTION_ID">
          <dt>
            <span v-if="item.QUESTIONTYPE === '1'" class="mark mark-green">(单选题) </span>
            <span v-if="item.QUESTIONTYPE === '2'" class="mark mark-blue">(多选题)</span>
            <span v-if="item.QUESTIONTYPE === '3'" class="mark mark-orange">(判断题)</span>
            <span v-if="item.QUESTIONTYPE === '4'" class="mark mark-purple">(填空题)</span>
            {{ index + 1 }}.{{ item.QUESTIONDRY }}
            <span class="ml-10">(题目分值：{{ item.SCORE }})</span>
          </dt>
          <el-radio-group v-if="item.QUESTIONTYPE === '1'" :disabled="true" v-model="item.ANSWER">
            <dd>
              <el-radio label="A">A.{{ item.OPTIONA }}</el-radio>
            </dd>
            <dd>
              <el-radio label="B">B.{{ item.OPTIONB }}</el-radio>
            </dd>
            <dd>
              <el-radio label="C">C.{{ item.OPTIONC }}</el-radio>
            </dd>
            <dd>
              <el-radio label="D">D.{{ item.OPTIOND }}</el-radio>
            </dd>
          </el-radio-group>
          <el-checkbox-group v-if="item.QUESTIONTYPE === '2'" :disabled="true" v-model="item.checkList" >
            <dd>
              <el-checkbox label="A">A.{{ item.OPTIONA }}</el-checkbox>
            </dd>
            <dd>
              <el-checkbox label="B">B.{{ item.OPTIONB }}</el-checkbox>
            </dd>
            <dd>
              <el-checkbox label="C">C.{{ item.OPTIONC }}</el-checkbox>
            </dd>
            <dd>
              <el-checkbox label="D">D.{{ item.OPTIOND }}</el-checkbox>
            </dd>
          </el-checkbox-group>
          <el-radio-group v-if="item.QUESTIONTYPE === '3'" :disabled="true" v-model="item.ANSWER">
            <dd>
              <el-radio label="A">A.{{ item.OPTIONA }}</el-radio>
            </dd>
            <dd>
              <el-radio label="B">B.{{ item.OPTIONB }}</el-radio>
            </dd>
          </el-radio-group>
          <div class="flex-layout space-between">
            <div>
              <div class="mb-10">
                正确答案：{{ item.ANSWER }}
              </div>
              <div>
                答案解析：{{ item.DESCR }}
              </div>
            </div>
          </div>
        </dl>
      </div>
      <div class="page-btn-group">
        <div/>
        <pagination
          :total="examPaperPreviewDialog.total"
          :page.sync="examPaperPreviewDialog.listQuery.page"
          :limit.sync="examPaperPreviewDialog.listQuery.limit"
          @pagination="getExamPaperPreviewDialogList"/>
      </div>
      <span slot="footer">
        <el-button @click="examPaperPreviewDialog.visible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="involvedInTrainingDialog.visible" title="涉及培训工种">
      <el-form ref="involvedInTrainingDialogForm" :model="involvedInTrainingDialog.form" label-width="70px">
        <el-row class="mb-10">
          <el-col style="text-align: right">
            <el-button v-if="parentObj.STATE === '' || parentObj.STATE === '1'" type="primary" @click="fnAddArr">添加</el-button>
          </el-col>
        </el-row>
        <el-row v-for="(item,index) in involvedInTrainingDialog.form.arr" :key="item.id">
          <el-col :span="9">
            <el-form-item
              :prop="'arr.' + index + '.DEPARTMENT_ID'"
              :label="'部门' + (index + 1)"
              :rules="{
                required: true, message: '部门不能为空', trigger: 'blur'
              }"
            >
              <el-select :disabled="parentObj.STATE !== '' && parentObj.STATE !== '1'" :value="item.DEPARTMENT_ID" filterable placeholder="请选择或输入部门" style="width: 100%;" @change="getPost($event,index,'change')">
                <el-option v-for="item1 in departmentList" :key="item1.DEPARTMENT_ID" :value="item1.DEPARTMENT_ID" :label="item1.NAME"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="9">
            <el-form-item
              :prop="'arr.' + index + '.POST_ID'"
              :label="'工种' + (index + 1)"
              :rules="{
                required: true, message: '工种不能为空', trigger: 'blur'
              }"
            >
              <el-select :disabled="parentObj.STATE !== '' && parentObj.STATE !== '1'" v-model="item.POST_ID" filterable multiple style="width: 100%;" placeholder="请选择或输入工种">
                <el-option v-for="item1 in item.postList" :key="item1.POST_ID" :value="item1.POST_ID" :label="item1.NAME"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="10px">
              <el-button type="primary" @click="selectAll(index)">工种全选</el-button>
              <el-button v-if="(parentObj.STATE === '' || parentObj.STATE === '1') && index !== 0" type="danger" @click="fnDeleteArr(index)">删除</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer">
        <el-button v-if="(parentObj.STATE === '' || parentObj.STATE === '1')" type="primary" @click="involvedInTrainingDialogSubmit">提 交</el-button>
        <el-button @click="involvedInTrainingDialog.visible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="configureTestPaperDialog.visible" title="配置试卷">
      <div >
        <el-button
          type="primary"
          @click="testPaperTypeDialogShowBatch()">批量添加试卷</el-button>
      </div>
      <div class="app-container" style="margin-top: 20px">
        <el-table
          v-loading="configureTestPaperDialog.listLoading"
          ref="multipleTable"
          :data="configureTestPaperDialog.varList"
          :header-cell-style="{
            background:'#f5f7fa',
            'font-weight': 'bold',
            'color': '#000'
          }"
          tooltip-effect="dark"
          border
          fit
          highlight-current-row>
          <el-table-column
            :reserve-selection="true"
            type="selection"
            width="60"
            align="center"/>
          <el-table-column label="序号" width="70px" type="index"/>
          <el-table-column show-overflow-tooltip prop="DEPARTMENT_NAME" label="部门"/>
          <el-table-column show-overflow-tooltip prop="POST_NAME" label="工种"/>
          <el-table-column show-overflow-tooltip prop="CURRICULUMNAME" label="涉及课程"/>
          <el-table-column show-overflow-tooltip label="试卷类型">
            <template slot-scope="{row}">
              {{ row.PAPERSELECTTYPE == '1' ? '平台试卷' : '' }}
              {{ row.PAPERSELECTTYPE == '2' ? '自动生成试卷' : '' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150px">
            <template slot-scope="{row}">
              <el-button
                type="text"
                @click="testPaperTypeDialogShow(row.PAPERSELECTTYPE ? 'edit' : 'add',
                                                row.CLASSPOST_ID,row.PAPERSELECTTYPE ? row.POSTPAPER_ID : '', row.CURRICULUM_IDS)">
                {{ row.PAPERSELECTTYPE ? '编辑试卷' : '新增试卷' }}
              </el-button>
              <span class="app-divider"/>
              <el-button :disabled="!row.PAPERSELECTTYPE" type="text" @click="examPaperPreviewDialogShow(row.STAGEEXAMPAPERINPUT_ID)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="page-btn-group">
          <div/>
          <pagination
            :total="configureTestPaperDialog.total"
            :page.sync="configureTestPaperDialog.listQuery.page"
            :limit.sync="configureTestPaperDialog.listQuery.limit"
            @pagination="getConfigureTestPaperDialogList"/>
        </div>
      </div>
      <span slot="footer">
        <el-button @click="configureTestPaperDialog.visible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="testPaperTypeDialog.visible" title="试卷类型" width="600px">
      <el-form ref="testPaperTypeDialogForm" :model="testPaperTypeDialog.form" :rules="testPaperTypeDialog.rules" label-width="100px">
        <el-form-item label="试卷类型" prop="PAPERSELECTTYPE">
          <el-select v-model="testPaperTypeDialog.form.PAPERSELECTTYPE" style="width: 100%;">
            <el-option value="1" label="平台试卷"/>
            <el-option value="2" label="自动生成试卷"/>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button
          type="primary"
          @click="()=>{
            testPaperTypeDialog.form.PAPERSELECTTYPE === '1' ? finalExamDialogShow() : automaticallyGenerateTestPapersDialogShow()
          }"
        >
          确 定
        </el-button>
        <el-button @click="testPaperTypeDialog.visible = false;addtype = ''">关 闭</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="automaticallyGenerateTestPapersDialog.visible" title="自动生成试卷" top="60px" width="740px">
      <el-form ref="automaticallyGenerateTestPapersDialogForm" :model="automaticallyGenerateTestPapersDialog.form" :rules="automaticallyGenerateTestPapersDialog.rules" label-width="120px">
        <el-form-item v-if="addtype !== 'batch'" label="试卷名称" prop="EXAMNAME">
          <el-input v-model="automaticallyGenerateTestPapersDialog.form.EXAMNAME"/>
        </el-form-item>
        <el-form-item label="试卷分数" prop="EXAMSCORE">
          <el-input v-model="automaticallyGenerateTestPapersDialog.form.EXAMSCORE"/>
        </el-form-item>
        <el-form-item label="合格分数" prop="PASSSCORE">
          <el-input v-model="automaticallyGenerateTestPapersDialog.form.PASSSCORE"/>
        </el-form-item>
        <el-form-item label="单选题数" prop="DANYUANTICOUNT">
          <el-input v-model.number="automaticallyGenerateTestPapersDialog.form.DANYUANTICOUNT" oninput="value=value.replace(/[^\d]+/g,'')">
            <template v-if="addtype !== 'batch'" slot="append">可选题数：{{ automaticallyGenerateTestPapersDialog.form.OPTIONALDANYUANTICOUNT }}</template>
          </el-input>
        </el-form-item>
        <el-form-item label="单选分值" prop="DANXUANTINUMBER">
          <el-input v-model="automaticallyGenerateTestPapersDialog.form.DANXUANTINUMBER"/>
        </el-form-item>
        <el-form-item label="多选题数" prop="DUOXUANTICOUNT">
          <el-input v-model.number="automaticallyGenerateTestPapersDialog.form.DUOXUANTICOUNT" oninput="value=value.replace(/[^\d]+/g,'')">
            <template v-if="addtype !== 'batch'" slot="append">可选题数：{{ automaticallyGenerateTestPapersDialog.form.OPTIONALDUOXUANTICOUNT }}</template>
          </el-input>
        </el-form-item>
        <el-form-item label="多选分值" prop="DUOXUANTINUMBER">
          <el-input v-model="automaticallyGenerateTestPapersDialog.form.DUOXUANTINUMBER"/>
        </el-form-item>
        <el-form-item label="判断题数" prop="PANDUITICOUNT">
          <el-input v-model.number="automaticallyGenerateTestPapersDialog.form.PANDUITICOUNT" oninput="value=value.replace(/[^\d]+/g,'')">
            <template v-if="addtype !== 'batch'" slot="append">可选题数：{{ automaticallyGenerateTestPapersDialog.form.OPTIONALPANDUITICOUNT }}</template>
          </el-input>
        </el-form-item>
        <el-form-item label="判断分值" prop="PANDUITINUMBER">
          <el-input v-model="automaticallyGenerateTestPapersDialog.form.PANDUITINUMBER" />
        </el-form-item>
        <el-form-item label="填空题数" prop="TIANKONGTICOUNT">
          <el-input v-model.number="automaticallyGenerateTestPapersDialog.form.TIANKONGTICOUNT" oninput="value=value.replace(/[^\d]+/g,'')">
            <template v-if="addtype !== 'batch'" slot="append">可选题数：{{ automaticallyGenerateTestPapersDialog.form.OPTIONALTIANKONGTICOUNT }} </template>
          </el-input>
        </el-form-item>
        <el-form-item label="填空分值" prop="TIANKONGTINUMBER" >
          <el-input v-model="automaticallyGenerateTestPapersDialog.form.TIANKONGTINUMBER"/>
        </el-form-item>
        <el-form-item label="考试时长(分钟)" prop="ANSWERSHEETTIME">
          <el-input v-model.number="automaticallyGenerateTestPapersDialog.form.ANSWERSHEETTIME" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')"/>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="automaticallyGenerateTestPapersSubmit">
          确 定
        </el-button>
        <el-button @click="automaticallyGenerateTestPapersDialog.visible = false">关 闭</el-button>
      </span>
    </el-dialog>
    <!--    平台试卷培训类型-->
    <training-type-dialog
      :dialog-form-visible.sync="finalExamDialog.dialogFormVisible"
      @confirm="trainingTypeDialogConfirm1"/>
    <!--    自动生成试卷培训类型-->

    <papers ref="papers" @getChoose = "vectory"/>
    <paperInformation ref="paperInformation" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { requestFN } from '@/utils/request'
import trainingTypeDialog from '@/components/trainingTypeDialog'
import papers from './papers'
import paperInformation from './paperInformation'
import waves from '@/directive/waves'

export default {
  components: {
    Pagination, trainingTypeDialog, papers, paperInformation, waves
  },
  data() {
    return {
      TRAINTYPE: '',
      listLoading: false,
      varList: [],
      batchdata: [{}],
      addtype: '',
      POST_NAME: '',
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      },
      configureTestPaperDialog: {
        visible: false,
        listLoading: false,
        varList: [],
        total: 0,
        listQuery: {
          page: 1,
          limit: 10
        }
      },
      finalExamDialog: {
        visible: false,
        listLoading: false,
        varList: [],
        total: 0,
        listQuery: {
          page: 1,
          limit: 10
        },
        form: {
          KEYWORDS: ''
        },
        submitForm: {
          EXAMNAME: '',
          EXAMSCORE: 0,
          PASSSCORE: 0,
          ANSWERSHEETTIME: 0,
          STAGEEXAMPAPERINPUT_ID: ''
        },
        dialogFormVisible: false
      },
      examPaperPreviewDialog: {
        visible: false,
        info: {},
        form: {
          KEYWORDS: '',
          QUESTIONTYPE: ''
        },
        varList: [],
        total: 0,
        listQuery: {
          page: 1,
          limit: 10
        },
        STAGEEXAMPAPERINPUT_ID: ''
      },
      COURSEWAREIDS: [],
      departmentList: [],
      involvedInTrainingDialog: {
        CLASSCURRICULUM_ID: '',
        visible: false,
        form: {
          arr: []
        }
      },
      testPaperTypeDialog: {
        CLASSPOST_ID: '',
        POSTPAPER_ID: '',
        CURRICULUM_IDS: '',
        addOrEdit: '',
        visible: false,
        form: {
          PAPERSELECTTYPE: ''
        },
        rules: {
          PAPERSELECTTYPE: [
            { required: true, message: '请选择试卷类型', trigger: 'blur' }
          ]
        }
      },
      automaticallyGenerateTestPapersDialog: {
        dialogFormVisible: false,
        visible: false,
        CURRICULUM_IDS: [],
        form: {
          trainAllName: '',
          EXAMNAME: '',
          EXAMSCORE: '',
          PASSSCORE: '',
          OPTIONALDANYUANTICOUNT: 0,
          OPTIONALDUOXUANTICOUNT: 0,
          OPTIONALPANDUITICOUNT: 0,
          OPTIONALTIANKONGTICOUNT: 0,
          DANYUANTICOUNT: '',
          DANXUANTINUMBER: '',
          DUOXUANTICOUNT: '',
          DUOXUANTINUMBER: '',
          PANDUITICOUNT: '',
          PANDUITINUMBER: '',
          TIANKONGTICOUNT: '',
          TIANKONGTINUMBER: '',
          ANSWERSHEETTIME: '60'
          // LABEL_TYPE: ''
        },
        rules: {
          EXAMNAME: [
            { required: true, message: '请填写试卷名称', trigger: 'blur' }
          ],
          trainAllName: [
            { required: true, message: '请选择培训类型', trigger: 'blur' }
          ],
          EXAMSCORE: [
            { required: true, message: '请填写试卷分数', trigger: 'blur' },
            { pattern: /^[0-9]+([.]{1}[0-9]{1})?$/, message: '必须是整数或小数点后一位的数字', trigger: 'blur' }
          ],
          PASSSCORE: [
            { required: true, message: '请填写合格分数', trigger: 'blur' },
            { pattern: /^[0-9]+([.]{1}[0-9]{1})?$/, message: '必须是整数或小数点后一位的数字', trigger: 'blur' }
          ],
          DANYUANTICOUNT: [
            { required: true, message: '请填写单选题数', trigger: 'blur' }
          ],
          DANXUANTINUMBER: [
            { required: true, message: '请填写单选分值', trigger: 'blur' },
            { pattern: /^[0-9]+([.]{1}[0-9]{1})?$/, message: '必须是整数或小数点后一位的数字', trigger: 'blur' }
          ],
          DUOXUANTICOUNT: [
            { required: true, message: '请填写多选题数', trigger: 'blur' }
          ],
          DUOXUANTINUMBER: [
            { required: true, message: '请填写多选分值', trigger: 'blur' },
            { pattern: /^[0-9]+([.]{1}[0-9]{1})?$/, message: '必须是整数或小数点后一位的数字', trigger: 'blur' }
          ],
          PANDUITICOUNT: [
            { required: true, message: '请填写判断题数', trigger: 'blur' }
          ],
          PANDUITINUMBER: [
            { required: true, message: '请填写判断分值', trigger: 'blur' },
            { pattern: /^[0-9]+([.]{1}[0-9]{1})?$/, message: '必须是整数或小数点后一位的数字', trigger: 'blur' }
          ],
          TIANKONGTICOUNT: [
            { required: true, message: '请填写填空题数', trigger: 'blur' }
          ],
          TIANKONGTINUMBER: [
            { required: true, message: '请填写填空分值', trigger: 'blur' },
            { pattern: /^[0-9]+([.]{1}[0-9]{1})?$/, message: '必须是整数或小数点后一位的数字', trigger: 'blur' }
          ],
          ANSWERSHEETTIME: [
            { required: true, message: '请填写考试时长', trigger: 'blur' }
          ]
        }
      },
      show: 0
    }
  },
  inject: ['parentObj', 'setActiveName', 'setCLASSCURRICULUM_ID'],
  created() {
    this.getClassCurriculumList()
    this.getDepartment()
    this.getTraintype()
  },
  methods: {
    getTraintype() {
      requestFN('/class/findById', {
        CLASS_ID: this.parentObj.CLASS_ID
      }).then((data) => {
        this.TRAINTYPE = data.pd.TRAINTYPE
        this.show = data.pd.EXAMINATION
        this.ISSTRENGTHEN = data.pd.ISSTRENGTHEN
      })
    },
    getDepartment() {
      requestFN('/classpost/listDepByClass', {
        CLASS_ID: this.parentObj.CLASS_ID
      }).then((data) => {
        this.departmentList = data.varList
      })
    },
    getPost(e, index, type) {
      if (type === 'change') {
        for (let i = 0; i < this.involvedInTrainingDialog.form.arr.length; i++) {
          if (this.involvedInTrainingDialog.form.arr[i].DEPARTMENT_ID === e) {
            this.$message.warning('不能选择相同部门')
            return
          }
        }
        this.involvedInTrainingDialog.form.arr[index].DEPARTMENT_ID = e
        this.involvedInTrainingDialog.form.arr[index].POST_ID = []
      }
      requestFN('/classpost/listPostByClassDep', {
        DEPARTMENT_ID: e,
        CLASS_ID: this.parentObj.CLASS_ID
      }).then((data) => {
        this.involvedInTrainingDialog.form.arr[index].postList = data.postList
      })
    },
    fnAddArr() {
      this.involvedInTrainingDialog.form.arr.push({
        id: Math.random(),
        DEPARTMENT_ID: '',
        POST_ID: [],
        postList: []
      })
    },
    selectAll(index) {
      if (this.involvedInTrainingDialog.form.arr[index].postList.length === 0) {
        this.$message.warning('当前部门下没有工种')
        return
      }
      let selectArr = []
      if (this.involvedInTrainingDialog.form.arr[index].POST_ID.length !== this.involvedInTrainingDialog.form.arr[index].postList.length) {
        selectArr = this.involvedInTrainingDialog.form.arr[index].postList.reduce((pre, cur) => {
          pre.push(cur.POST_ID)
          return pre
        }, [])
      }
      this.involvedInTrainingDialog.form.arr[index].POST_ID = selectArr
    },
    fnDeleteArr(index) {
      this.involvedInTrainingDialog.form.arr.splice(index, 1)
    },
    getClassCurriculumList() {
      requestFN('/classCurriculum/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page, {
        CLASS_ID: this.parentObj.CLASS_ID
      }).then(res => {
        this.listLoading = false
        this.varList = res.varList
        this.total = res.page.totalResult
      })
    },
    getExamPaperPreviewDialogList() {
      return requestFN(`/paperQuestion/list?showCount=${this.examPaperPreviewDialog.listQuery.limit}&currentPage=${this.examPaperPreviewDialog.listQuery.page}`, {
        STAGEEXAMPAPERINPUT_ID: this.examPaperPreviewDialog.STAGEEXAMPAPERINPUT_ID,
        ...this.examPaperPreviewDialog.form
      }).then((data) => {
        this.examPaperPreviewDialog.varList = data.varList
        this.examPaperPreviewDialog.total = data.page.totalResult
      })
    },
    async examPaperPreviewDialogShow(STAGEEXAMPAPERINPUT_ID) {
      this.examPaperPreviewDialog.STAGEEXAMPAPERINPUT_ID = STAGEEXAMPAPERINPUT_ID
      this.examPaperPreviewDialogListGoKeyReset()
      await this.getExamPaperPreviewDialogInfo()
      this.examPaperPreviewDialog.visible = true
    },
    getExamPaperPreviewDialogInfo() {
      return requestFN(`/stageexampaperinput/goEdit`, {
        STAGEEXAMPAPERINPUT_ID: this.examPaperPreviewDialog.STAGEEXAMPAPERINPUT_ID
      }).then((data) => {
        this.examPaperPreviewDialog.info = data.pd
      })
    },
    examPaperPreviewDialogListGoKeyReset() {
      this.examPaperPreviewDialog.form.KEYWORDS = ''
      this.examPaperPreviewDialog.form.QUESTIONTYPE = ''
      this.batchdata.length = 0
      this.$refs.multipleTable.clearSelection()
      this.getExamPaperPreviewDialogList()
    },
    trainingTypeDialogConfirm1(e) {
      Object.keys(e).forEach(key => {
        this.$set(this.finalExamDialog.form, key, e[key])
      })
    },
    finalExamDialogUseTestPaper(row) {
      this.finalExamDialog.submitForm.STAGEEXAMPAPERINPUT_ID = row.STAGEEXAMPAPERINPUT_ID
      this.finalExamDialog.submitForm.EXAMNAME = row.EXAMNAME
      this.finalExamDialog.submitForm.EXAMSCORE = row.EXAMSCORE
      this.finalExamDialog.submitForm.PASSSCORE = row.PASSSCORE
    },
    async configureTestPaperDialogShow() {
      await this.getConfigureTestPaperDialogList()
      this.configureTestPaperDialog.visible = true
    },
    getConfigureTestPaperDialogList() {
      return requestFN(`classpost/listClassPost?showCount=${this.configureTestPaperDialog.listQuery.limit}&currentPage=${this.configureTestPaperDialog.listQuery.page}`, {
        CLASS_ID: this.parentObj.CLASS_ID
      }).then((data) => {
        console.log(data)
        this.configureTestPaperDialog.varList = data.varList
        this.configureTestPaperDialog.total = data.page.totalResult
      })
    },
    async finalExamDialogShow() {
      this.FinalExamDialogListGoKeyReset()
      this.finalExamDialogUseTestPaper({
        STAGEEXAMPAPERINPUT_ID: '',
        EXAMNAME: '',
        EXAMSCORE: ''
      })
      this.finalExamDialog.submitForm.ANSWERSHEETTIME = 60
      await this.getFinalExamDialogList()
      this.finalExamDialog.visible = true
    },
    getFinalExamDialogList() {
      return requestFN(`/stageexampaperinput/list?showCount=${this.finalExamDialog.listQuery.limit}&currentPage=${this.finalExamDialog.listQuery.page}`, {
        ...this.finalExamDialog.form
      }).then((data) => {
        this.finalExamDialog.varList = data.varList
        this.finalExamDialog.total = data.page.totalResult
      })
    },
    FinalExamDialogListGoKeyReset() {
      for (const key in this.finalExamDialog.form) {
        if (this.finalExamDialog.form.hasOwnProperty(key)) {
          this.finalExamDialog.form[key] = ''
        }
      }
      this.getFinalExamDialogList()
    },
    automaticallyGenerateTestPapersDialogShow() {
      if (typeof (this.testPaperTypeDialog.CURRICULUM_IDS) == 'undefined') {
        this.$message.warning('请配置涉及课程！')
        return
      }
      console.info('batch:' + this.addtype)
      if (this.addtype == 'batch') {
        this.automaticallyGenerateTestPapersDialog.form.trainAllName = ''
        this.automaticallyGenerateTestPapersDialog.visible = true
        this.$nextTick(() => {
          this.$refs.automaticallyGenerateTestPapersDialogForm && this.$refs.automaticallyGenerateTestPapersDialogForm.resetFields()
        })
      } else {
        requestFN('/postpaper/getQuestionCount', {
          CURRICULUM_IDS: this.testPaperTypeDialog.CURRICULUM_IDS,
          CLASS_ID: this.parentObj.CLASS_ID
        }).then((data) => {
          if (data.message) {
            this.$message.warning(data.message)
          } else {
            this.COURSEWAREIDS = []
            this.COURSEWAREIDS = data.coursewareIds.COURSEWAREIDS
            console.info('coursewareids:' + this.COURSEWAREIDS)
            for (let i = 0; i < data.questionCount.length; i++) {
              if (data.questionCount[i].QUESTIONTYPE === '1') this.automaticallyGenerateTestPapersDialog.form.OPTIONALDANYUANTICOUNT = data.questionCount[i].NUM
              if (data.questionCount[i].QUESTIONTYPE === '2') this.automaticallyGenerateTestPapersDialog.form.OPTIONALDUOXUANTICOUNT = data.questionCount[i].NUM
              if (data.questionCount[i].QUESTIONTYPE === '3') this.automaticallyGenerateTestPapersDialog.form.OPTIONALPANDUITICOUNT = data.questionCount[i].NUM
              if (data.questionCount[i].QUESTIONTYPE === '4') this.automaticallyGenerateTestPapersDialog.form.OPTIONALTIANKONGTICOUNT = data.questionCount[i].NUM
            }
            this.automaticallyGenerateTestPapersDialog.form.trainAllName = ''
            this.automaticallyGenerateTestPapersDialog.visible = true
            this.$nextTick(() => {
              this.$refs.automaticallyGenerateTestPapersDialogForm && this.$refs.automaticallyGenerateTestPapersDialogForm.resetFields()
            })
          }
        })
      }
    },

    async finalExamDialogSubmit() {
      const { submitForm, submitForm: { STAGEEXAMPAPERINPUT_ID, ANSWERSHEETTIME }} = this.finalExamDialog
      if (this.ISSTRENGTHEN === '1') {
        let noVideo = false
        await requestFN('/postpaper/verifyStrengthenPaper', {
          STAGEEXAMPAPERINPUT_ID: STAGEEXAMPAPERINPUT_ID
        }).then((data) => {
          if (data.result === 'error') {
            this.$message.warning(data.msg)
            noVideo = true
          }
        })
        if (noVideo) {
          return
        }
      }
      if (!STAGEEXAMPAPERINPUT_ID) {
        this.$message.warning('请选择要使用的试卷！')
        return
      }
      if (!ANSWERSHEETTIME) {
        this.$message.warning('考试时长不能为空！')
        return
      }
      if (this.addtype === 'batch') {
        this.hasTestPaper(submitForm)
        this.addtype = ''
        return
      }
      this.testPaperSubmit(submitForm)
    },
    hasTestPaper(submitForm) {
      requestFN('/postpaper/getData', {
        CLASSPOST_IDS: this.testPaperTypeDialog.CLASSPOST_ID
      }).then((data) => {
        if (data.varList.length > 0) {
          const depart_post = []
          for (let i = 0; i < data.varList.length; i++) {
            depart_post[i] = '[' + data.varList[i].DEPARTMENT_NAME + '-' + data.varList[i].POST_NAME + ']'
          }
          this.$confirm('已选择的工种可能存在试卷，如继续操作可能造成' + depart_post + '试卷被覆盖，是否继续？', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.testPaperSubmit(submitForm)
          }).catch(() => {})
        } else {
          this.testPaperSubmit(submitForm)
        }
      })
    },
    automaticallyGenerateTestPapersSubmit() {
      this.$refs.automaticallyGenerateTestPapersDialogForm.validate(valida => {
        if (valida) {
          const {
            form,
            form: {
              EXAMSCORE,
              PASSSCORE,
              DANYUANTICOUNT,
              OPTIONALDANYUANTICOUNT,
              DANXUANTINUMBER,
              DUOXUANTICOUNT,
              OPTIONALDUOXUANTICOUNT,
              DUOXUANTINUMBER,
              PANDUITICOUNT,
              OPTIONALPANDUITICOUNT,
              PANDUITINUMBER,
              TIANKONGTICOUNT,
              OPTIONALTIANKONGTICOUNT,
              TIANKONGTINUMBER
            }
          } = this.automaticallyGenerateTestPapersDialog
          if (PASSSCORE * 1 > EXAMSCORE * 1) {
            this.$message.warning('合格分数不能大于试卷分数')
            return
          }
          if (this.addtype !== 'batch') {
            if (DANYUANTICOUNT > OPTIONALDANYUANTICOUNT) {
              this.$message.warning('单选题题数不足')
              return
            }
            if (DUOXUANTICOUNT > OPTIONALDUOXUANTICOUNT) {
              this.$message.warning('多选题题数不足')
              return
            }
            if (PANDUITICOUNT > OPTIONALPANDUITICOUNT) {
              this.$message.warning('判断题题数不足')
              return
            }
            if (TIANKONGTICOUNT > OPTIONALTIANKONGTICOUNT) {
              this.$message.warning('填空题题数不足')
              return
            }
            if (((DANYUANTICOUNT * DANXUANTINUMBER) + (DUOXUANTICOUNT * DUOXUANTINUMBER) + (PANDUITICOUNT * PANDUITINUMBER) + (TIANKONGTICOUNT * TIANKONGTINUMBER)) !== EXAMSCORE * 1) {
              this.$message.warning('所有题的分数相加必须等于试卷分数')
              return
            }
            this.testPaperSubmit(form)
          } else {
            if (((DANYUANTICOUNT * DANXUANTINUMBER) + (DUOXUANTICOUNT * DUOXUANTINUMBER) + (PANDUITICOUNT * PANDUITINUMBER) + (TIANKONGTICOUNT * TIANKONGTINUMBER)) !== EXAMSCORE * 1) {
              this.$message.warning('所有题的分数相加必须等于试卷分数')
              return
            }
            requestFN('/postpaper/getData', {
              CLASSPOST_IDS: this.testPaperTypeDialog.CLASSPOST_ID
            }).then((data) => {
              if (data.varList.length > 0) {
                const depart_post = []
                for (let i = 0; i < data.varList.length; i++) {
                  depart_post[i] = '[' + data.varList[i].DEPARTMENT_NAME + '-' + data.varList[i].POST_NAME + ']'
                }
                this.$confirm('已选择的工种可能存在试卷，如继续操作可能造成' + depart_post + '试卷被覆盖，是否继续？', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  this.testPaperBatchSubmit(form)
                }).catch(() => {})
              } else {
                this.testPaperBatchSubmit(form)
              }
            })
          }
        } else {
          this.$message.warning('请补全必填项')
        }
      })
    },
    testPaperSubmit(params) {
      let url = ''
      if (this.testPaperTypeDialog.addOrEdit === 'add') url = '/postpaper/addPostPaper'
      if (this.testPaperTypeDialog.addOrEdit === 'edit') url = '/postpaper/editPostPaper'
      requestFN(url, {
        ...params,
        CLASS_ID: this.parentObj.CLASS_ID,
        PAPERSELECTTYPE: this.testPaperTypeDialog.form.PAPERSELECTTYPE,
        CLASSPOST_ID: this.testPaperTypeDialog.CLASSPOST_ID,
        COURSEWAREIDS: JSON.stringify(this.COURSEWAREIDS),
        POSTPAPER_ID: this.testPaperTypeDialog.POSTPAPER_ID
      }).then(() => {
        this.$message.success('操作成功！')
        this.finalExamDialog.visible = false
        this.testPaperTypeDialog.visible = false
        this.automaticallyGenerateTestPapersDialog.visible = false
        this.$refs.multipleTable.clearSelection()
        this.batchdata.length = 0
        this.getConfigureTestPaperDialogList()
      })
    },
    testPaperBatchSubmit(params) {
      requestFN('/postpaper/batchAddPostPaper', {
        ...params,
        PAPERSELECTTYPE: this.testPaperTypeDialog.form.PAPERSELECTTYPE,
        CLASS_ID: this.parentObj.CLASS_ID,
        TRAINLEVEL_NAME: this.parentObj.TRAINLEVEL_NAME,
        batchdata: JSON.stringify(this.batchdata)
      }).then((res) => {
        if (res.message) {
          this.$confirm(res.message.replace(/\n/g, '<br/>'), {
            dangerouslyUseHTMLString: true,
            type: 'warning'
          }).then(() => {
          })
        } else {
          this.$message.success('操作成功！')
        }
        this.addtype = ''
        this.$refs.multipleTable.clearSelection()
        this.batchdata.length = 0
        this.finalExamDialog.visible = false
        this.testPaperTypeDialog.visible = false
        this.automaticallyGenerateTestPapersDialog.visible = false
        this.getConfigureTestPaperDialogList()
      })
    },
    testPaperTypeDialogShow(addOrEdit, CLASSPOST_ID, POSTPAPER_ID, CURRICULUM_IDS) {
      this.testPaperTypeDialog.addOrEdit = addOrEdit
      this.testPaperTypeDialog.CLASSPOST_ID = CLASSPOST_ID
      this.testPaperTypeDialog.POSTPAPER_ID = POSTPAPER_ID
      this.testPaperTypeDialog.CURRICULUM_IDS = CURRICULUM_IDS
      this.testPaperTypeDialog.visible = true
      this.$nextTick(() => {
        this.$refs.testPaperTypeDialogForm && this.$refs.testPaperTypeDialogForm.resetFields()
      })
    },
    // 批量添加平台试卷或自动生成试卷时
    testPaperTypeDialogShowBatch() {
      this.batchdata.length = 0
      this.addtype = 'batch'
      const selection = this.$refs.multipleTable.selection
      if (selection.length === 0) {
        this.$message.error('请选择要添加的项')
        return
      }
      const batchdatas = []
      const CLASSPOST_IDS = []
      const CURRICULUM_IDS = []
      for (let i = 0; i < selection.length; i++) {
        CLASSPOST_IDS.push(selection[i].CLASSPOST_ID)
        CURRICULUM_IDS.push(selection[i].CURRICULUM_IDS)
        batchdatas[i] = selection[i]
      }
      this.batchdata = batchdatas
      this.testPaperTypeDialog.addOrEdit = 'add'
      this.testPaperTypeDialog.CLASSPOST_ID = CLASSPOST_IDS.join(';')
      this.testPaperTypeDialog.CURRICULUM_IDS = CURRICULUM_IDS
      this.testPaperTypeDialog.visible = true
      this.addtype = 'batch'
      this.$nextTick(() => {
        this.$refs.testPaperTypeDialogForm && this.$refs.testPaperTypeDialogForm.resetFields()
      })
    },
    goToAddCourseBox() {
      this.setActiveName('AddCourseBox')
    },
    handleDelete(CLASSCURRICULUM_ID) {
      this.$confirm('你确定要删除该课程吗？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        requestFN('/classCurriculum/delete', { CLASSCURRICULUM_ID }).then((data) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getClassCurriculumList()
        })
      }).catch(() => {})
    },
    secondsCount(second) {
      if (!second) return 0
      const h = parseInt(second / 60 / 60, 10)
      const m = parseInt(second / 60 % 60, 10)
      const s = parseInt(second % 60, 10)
      if (h) {
        return h + '小时' + m + '分钟' + s + '秒'
      } else {
        if (m) {
          return m + '分钟' + s + '秒'
        } else {
          return s + '秒'
        }
      }
    },
    involvedInTrainingDialogSubmit() {
      this.$refs.involvedInTrainingDialogForm.validate(valid => {
        if (valid) {
          requestFN('/curriculumpost/saveClassCurPost', {
            CLASSCURRICULUM_ID: this.involvedInTrainingDialog.CLASSCURRICULUM_ID,
            deptPostList: JSON.stringify(this.involvedInTrainingDialog.form.arr)
          }).then((data) => {
            this.involvedInTrainingDialog.visible = false
            this.getClassCurriculumList()
          })
        } else {
          this.$message.warning('请补全必填项')
        }
      })
    },
    goToSeeClassMinsCtr(CLASSCURRICULUM_ID) {
      this.setActiveName('SeeClassMinsCtr')
      this.setCLASSCURRICULUM_ID(CLASSCURRICULUM_ID)
    },
    goToInvolvedInTraining(CLASSCURRICULUM_ID) {
      requestFN('/curriculumpost/listClassCurPost', {
        CLASSCURRICULUM_ID
      }).then((data) => {
        this.involvedInTrainingDialog.CLASSCURRICULUM_ID = CLASSCURRICULUM_ID
        this.involvedInTrainingDialog.visible = true
        if (data.varList.length === 0) {
          this.involvedInTrainingDialog.form.arr = []
          this.fnAddArr()
        } else {
          this.involvedInTrainingDialog.form.arr = data.varList
          for (let i = 0; i < data.varList.length; i++) {
            this.$set(this.involvedInTrainingDialog.form.arr[i], 'postList', [])
            this.getPost(data.varList[i].DEPARTMENT_ID, i, 'init')
          }
        }
      })
    },
    foComplete() {
      this.$confirm('完成之后将开班，只能新增学员，不能再进行其它更改，确认继续吗？', '提示', {
        type: 'warning'
      }).then(() => {
        requestFN('/class/finish', {
          CLASS_ID: this.parentObj.CLASS_ID,
          EXAMINATION: this.show
        }).then((data) => {
          this.$message.success('班级添加完成')
          this.goBack()
        })
      }).catch(() => {})
    },
    goBack() {
      this.setActiveName('List')
    },
    goPrevious() {
      this.setActiveName('StudentList')
    },
    openPapers() {
      this.$refs.papers.init({ Id: this.parentObj.CLASS_ID })
    },
    vectory(e) {
      if (e.name === 'papers') {
        if (!e.target.STAGEEXAMPAPERINPUT_ID) {
          this.$message.error('在开班前,试卷没有管理工作，无法查看')
        }
        this.$refs.paperInformation.init({ id: e.target.STAGEEXAMPAPERINPUT_ID })
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
