<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="计划名称" prop="TrainName">
				<el-input v-model="queryParams.TrainName" placeholder="请输入计划名称" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="培训类别" prop="ZcsZhuanYe">
				<el-select v-model="queryParams.ZcsZhuanYe" placeholder="请选择培训类别" clearable>
					<el-option v-for="dict in dict.type.jxjy_zhuanye" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="审核状态" prop="ShenHe">
				<el-select v-model="queryParams.ShenHe" placeholder="请选择审核状态" clearable>
					<el-option  label="未审核" value="0" />
					<el-option  label="已审核" value="1" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['jxjy:Training:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['jxjy:Training:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['jxjy:Training:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['jxjy:Training:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="TrainingList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="期次" align="center">
				<template slot-scope="scope">
					<span>{{scope.row.QiciYear+'年'+scope.row.QiCi+'期次' }}</span>
				</template>
			</el-table-column>
			<el-table-column label="培训类别" align="center" prop="ZcsZhuanYe">
				<template slot-scope="scope">
					<div style="display: flex;align-items: center;">
						<dict-tag :options="dict.type.jxjy_zhuanye" :value="scope.row.ZcsZhuanYe" />
						<span>
							{{scope.row.ZhuanYeType === "" ?'':'('+scope.row.ZhuanYeType+')'}}
						</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="计划名称" align="center" prop="TrainName" width="400px" />
			<el-table-column label="培训开始时间" align="center" prop="StartDate" >
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.StartDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="培训结束时间" align="center" prop="EndDate" >
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.EndDate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>  
			<el-table-column label="现报人数" align="center" prop="BmNum" />
			<el-table-column label="报名上限" align="center" prop="BmNumMax" />
			<el-table-column label="审核状态" align="center" prop="ShenHe">
				<template slot-scope="scope">
					<div>
						<!-- {{scope.row.ShenHe == 0?'未审核':'已审核'}} -->
						<span v-if="scope.row.ShenHe == 0" >
							<span style="color: #F56C6C;margin-right: 10px;">
								未审核
							</span>
							<el-button type="text" @click="changeType(scope.row,'1')">[设置]</el-button>
						</span>
						<span v-else>
							<span style="margin-right: 10px;">
								已审核
							</span>
							<el-button type="text" @click="changeType(scope.row,'0')">[设置]</el-button>
						</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						>修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						>删除</el-button>
				</template>
			</el-table-column>
			<el-table-column label="报名人员" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)"
						>查看</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改继续教育培训计划对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="80vw" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="160px">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="培训类别" prop="ZcsZhuanYe">
							<el-select v-model="form.ZcsZhuanYe" disabled placeholder="请选择培训类别">
								<el-option v-for="dict in dict.type.jxjy_zhuanye" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="培训专业" prop="ZhuanYeType">
							<!-- <el-input v-model="form.ZhuanYeType" type="textarea" placeholder="请输入内容" /> -->
							<el-select v-model="form.ZhuanYeType" placeholder="请选择培训专业">
								<el-option v-for="dict in dict.type.jxjy_pxzy" :key="dict.value" :label="dict.label"
									:value="dict.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="期次" prop="QiCi">
							<div style="display: flex;align-items: center;">
								<el-input v-model="form.QiciYear" readonly style="width: 80px;" /> 年 第 
								<el-input v-model="form.QiCi" style="width: 60px;" /> 期
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="学时" prop="XueShi">
							<el-input v-model="form.XueShi" placeholder="请输入学时" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="每学时" prop="Minute">
							<div style="display: flex;align-items: center;">
								<el-input v-model="form.Minute" placeholder="请输入学时" style="width: 80%;" />分钟
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="缴费金额" prop="Undef3">
							<div style="display: flex;align-items: center;">
								<el-input v-model="form.Undef3"  placeholder="请输入内容" style="width: 80%;" />元
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="24"> 
						<el-form-item label="培训计划名称" prop="TrainName">
							<el-input v-model="form.TrainName" placeholder="请输入培训计划名称" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="配置课程" prop="Undef1">
							<el-input v-model="form.Undef1" placeholder="请输入内容" style="width: 60%;" >
								<el-button slot="append" readonly @click="chooseClass">配置课程</el-button>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="配置考试" prop="Undef2">
							<el-input v-model="form.Undef2" readonly placeholder="请输入内容" style="width: 60%;">
								<el-button slot="append"  @click="chooseSj">配置考试</el-button>
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="培训分院范围及参训人员分配" prop="TContent">
							<el-input v-model="form.TContent" type="textarea" placeholder="请输入内容" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报名开始时间" prop="StartDate">
							<el-date-picker clearable v-model="form.StartDate" type="date" value-format="yyyy-MM-dd"
								placeholder="请选择报名开始时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报名结束时间" prop="EndDate">
							<el-date-picker clearable v-model="form.EndDate" type="date" value-format="yyyy-MM-dd"
								placeholder="请选择报名结束时间">
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报名最大人数" prop="BmNumMax">
							<el-input v-model="form.BmNumMax" placeholder="请输入报名最大人数" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="报名人数" prop="BmNum">
							<el-input v-model="form.BmNum" placeholder="请输入报名人数" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="是否审核" prop="ShenHe">
							<!-- <el-input v-model="form.ShenHe" placeholder="请输入通知审核 0未审核  1审核" /> -->
							<el-select v-model="form.ShenHe" placeholder="请选择审核状态" clearable>
								<el-option  label="未审核" value="0" />
								<el-option  label="已审核" value="1" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
		<el-dialog title="课程列表" :visible.sync="classShow" width="50vw" >
			<el-table ref="classList" v-loading="classLoading" :data="classList" @selection-change="classChange">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="课程名称" align="center" prop="ClassName" width="350px" />
				<el-table-column label="注册专业分类" align="center">
					<template slot-scope="scope"> 
						<dict-tag :options="dict.type.jxjy_zhuanye" :value="scope.row.ZYId" /> 
					</template>
				</el-table-column>
				<el-table-column label="学时" align="center">
					<template slot-scope="scope">
						<span style="color: #F56C6C">
							{{scope.row.XueShi}}
						</span>
					</template>
				</el-table-column>
				<el-table-column label="必(选)修" align="center">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.jxjy_kclx" :value="scope.row.ClassSort" />
					</template>
				</el-table-column>
				<el-table-column label="添加日期" align="center" prop="XueShi">
					<template slot-scope="scope">
						{{parseTime(scope.row.createTime, '{y}-{m}-{d}')}}
					</template>
				</el-table-column> 
			</el-table>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" @click="classConfiem">确 定</el-button>
				<el-button @click="classShow = false">取 消</el-button>
			</span>
		</el-dialog>
		<el-dialog title="试卷列表" :visible.sync="sjShow" width="50vw" >
			<el-table v-loading="sjLoading" ref="sjList" :data="sjList" @selection-change="sjChange">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="试卷名称" align="center" prop="PaperName" width="300px" />
				<!-- <el-table-column label="注册专业分类" align="center" prop="ProduceWay">
					<template slot-scope="scope">
						<dict-tag :options="dict.type.jxjy_zhuanye" :value="scope.row.SubjectID" />
					</template>
				</el-table-column> -->
				<el-table-column label="几个分数" align="center" prop="PassMark" />
				<el-table-column label="试卷总分" align="center" prop="PaperMark" />
				<el-table-column label="创建日期" align="center" prop="CreateDate" width="180">
					<template slot-scope="scope">
						<span>{{ parseTime(scope.row.CreateDate, '{y}-{m}-{d}') }}</span>
					</template>
				</el-table-column>
			</el-table>
			<span slot="footer" class="dialog-footer">
				<el-button type="primary" :disabled="selectSjIds.length>1" @click="sjConfiem">确 定</el-button>
				<el-button @click="sjShow = false">取 消</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listTraining,
		getTraining,
		delTraining,
		addTraining,
		updateTraining
	} from "@/api/xiehui/jxjy/Training";
	import {
		listClass
	} from "@/api/xiehui/jxjy/kcgl";
	import {
		listPaperInfo
	} from "@/api/xiehui/jxjy/PaperInfo";
	export default {
		name: "Training",
		dicts: ['jxjy_zhuanye','jxjy_pxzy','jxjy_kclx'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 继续教育培训计划表格数据
				TrainingList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					ECId: null,
					ZYIdList: null,
					ClassIdList: null,
					TrainName: null,
					ZcsZhuanYe: null,
					ZhuanYeType: null,
					QiciYear: null,
					QiCi: null,
					XueShi: null,
					StartDate: null,
					EndDate: null,
					TContent: null,
					BmEndDate: null,
					BmNum: null,
					BmNumMax: null,
					Bzq: null,
					OverFlg: null,
					ShenHe: null,
					AddDate: null,
					BeiZhu: null,
					TongZhi: null,
					Minute: null,
					Undef1: null,
					Undef2: null,
					Undef3: null,
					Undef4: null,
					Undef5: null,
					money: null,
					className: null,
					ksName: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					QiCi:{
						required: true,
						message: '请输入期次',
						trigger: ['blur', 'change']
					},
					XueShi:{
						required: true,
						message: '请输入学时',
						trigger: ['blur', 'change']
					},
					Minute:{
						required: true,
						message: '请输入每学时多少分钟',
						trigger: ['blur', 'change']
					},
					Undef3:{
						required: true,
						message: '请输入缴费金额',
						trigger: ['blur', 'change']
					},
					TrainName:{
						required: true,
						message: '请输入培训计划名称',
						trigger: ['blur', 'change']
					},
					Undef1:{
						required: true,
						message: '请配置课程',
						trigger: ['blur', 'change']
					},
					Undef2:{
						required: true,
						message: '请配置考试',
						trigger: ['blur', 'change']
					},
					StartDate:{
						required: true,
						message: '请选择报名开始时间',
						trigger: ['blur', 'change']
					},
					EndDate:{
						required: true,
						message: '请选择报名结束时间',
						trigger: ['blur', 'change']
					},
					BmNumMax:{
						required: true,
						message: '请输入报名上限',
						trigger: ['blur', 'change']
					},
					BmNum:{
						required: true,
						message: '请输入现报人数',
						trigger: ['blur', 'change']
					},
					ShenHe:{
						required: true,
						message: '请选择是否审核',
						trigger: ['blur', 'change']
					}
					
				},
				//课程列表
				classLoading:false,
				classList:[],
				selectIds:[],//被选中的课程列表
				selectNames:[],//被选中的课程列表
				classShow:false,
				//试卷列表
				sjLoading:false,
				sjList:[],
				selectSjIds:[],//被选中的课程列表
				selectSjNames:[],//被选中的课程列表
				sjShow:false,
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询继续教育培训计划列表 */
			getList() {
				this.loading = true;
				listTraining(this.queryParams).then(response => {
					this.TrainingList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					TrainId: null,
					ECId: null,
					ZYIdList: null,
					ClassIdList: null,
					TrainName: null,
					ZcsZhuanYe: 8,
					ZhuanYeType: null,
					QiciYear: new Date().getFullYear(),
					QiCi: null,
					XueShi: null,
					StartDate: null,
					EndDate: null,
					TContent: null,
					BmEndDate: null,
					BmNum: null,
					BmNumMax: null,
					Bzq: null,
					OverFlg: null,
					ShenHe: null,
					AddDate: null,
					BeiZhu: null,
					TongZhi: null,
					Minute: null,
					Undef1: null,
					Undef2: null,
					Undef3: null,
					Undef4: null,
					Undef5: null,
					money: null,
					className: null,
					ksName: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null, 
					ClassIdList:null,
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.TrainId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加继续教育培训计划";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const TrainId = row.TrainId || this.ids
				getTraining(TrainId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改继续教育培训计划";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.TrainId != null) {
							updateTraining(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addTraining(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const TrainIds = row.TrainId || this.ids;
				this.$modal.confirm('删除培训会连同该培训已报名人员一并删除，您确认删除吗？').then(function() {
					return delTraining(TrainIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('jxjy/Training/export', {
					...this.queryParams
				}, `Training_${new Date().getTime()}.xlsx`)
			},
			//修改状态
			changeType(row,type){
				this.reset();
				const TrainId = row.TrainId || this.ids
				getTraining(TrainId).then(response => {
					this.form = response.data;
					this.form.ShenHe = type
					this.$confirm('您要改变当前审核状态吗?', '提示', {
					  confirmButtonText: '确定',
					  cancelButtonText: '取消',
					  type: 'info'
					}).then(() => {
						updateTraining(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.getList();
						}); 
					}).catch(() => {
				          
					});
				});
			},
			//配置课程
			chooseClass(){
				this.classLoading = true;
				this.selectIds = []
				this.selectNames = []
				listClass({pageNum:1,pageSize:100,ShenHe:1}).then(response => {
					this.classList = response.rows;
					this.classShow = true
					if(this.form.ClassIdList != null && this.form.ClassIdList.length >0 ){
						setTimeout(()=>{
							this.form.ClassIdList.split(",").forEach(item=>{
								let index = this.classList.findIndex(obj=> obj.ClassId == item  )
								console.log(index)
								this.$refs.classList.toggleRowSelection(this.classList[index]);
							})
						},500)
						
					}
					this.classLoading = false;
				});
			},
			classChange(selection){
				this.selectIds = selection.map(item => item.ClassId)
				this.selectNames = selection.map(item => item.ClassName)
			},
			classConfiem(){
				if(this.selectIds.length>0){
					this.form.ClassIdList = this.selectIds.join(",")
					this.form.Undef1 = this.selectNames.join(",")
				}
				this.$refs["form"].validateField("Undef1")
				this.classShow = false
			},
			//试卷配置
			chooseSj(){
				this.sjLoading = true;
				this.selectSjIds = []
				this.selectSjNames = []
				listPaperInfo({pageNum:1,pageSize:100}).then(response => {
					this.sjList = response.rows;
					this.sjShow = true
					if(this.form.ECId !== null && this.form.ECId !== "" ){
						setTimeout(()=>{
							let index = this.sjList.findIndex(obj=> obj.PaperID == this.form.ECId)
							this.$refs.sjList.toggleRowSelection(this.sjList[index]);
						},500)
						
					}
					this.sjLoading = false;
				});
			},
			sjChange(selection){
				this.selectSjIds = selection.map(item => item.PaperID)
				this.selectSjNames = selection.map(item => item.PaperName)
			},
			sjConfiem(){
				if(this.selectSjIds.length>0){
					this.form.ECId = this.selectSjIds.join(",")
					this.form.Undef2 = this.selectSjNames.join(",")
				}
				this.$refs["form"].validateField("Undef2")
				this.sjShow = false
			}
		}
	};
</script>

<style lang="scss" scoped>
	::v-deep .el-input.is-disabled .el-input__inner {
		background-color: #fff !important;
		color: #303133 !important
	}
	::v-deep .el-upload{
		display: block !important;
	}
</style>