<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>
			<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"> 
					<dict-tag :options="dict.type.jxjy_zhuanye" :value="scope.row.ZYId" /> 
				</template>
			</el-table-column>
			<el-table-column label="课程名称" align="center" prop="ClassName" width="400px" />
			<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" />
			<el-table-column label="审核" align="center" prop="ShenHe">
				<template slot-scope="scope">
					<div>
						<span v-if="scope.row.ShenHe == 0" >
							<span style="color: #F56C6C;margin-right: 10px;">
								未审核
							</span>
						</span>
						<span v-else>
							<span style="margin-right: 10px;">
								已审核
							</span>
						</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>

		<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="100px">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="培训类别" prop="ZYId">
							<el-select v-model="form.ZYId" 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="6">
						<el-form-item label="课程类型" prop="ClassSort">
							<el-select v-model="form.ClassSort" placeholder="请选择培训类别">
								<el-option v-for="dict in dict.type.jxjy_kclx" :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="XueShi">
							<div style="display: flex;align-items: center;">
								<el-input v-model="form.XueShi" placeholder="请输入学时" style="width: 80%;" />个
							</div>
						</el-form-item>
					</el-col>
					<el-col :span="24"> 
						<el-form-item label="课程名称" prop="ClassName">
							<el-input v-model="form.ClassName" placeholder="请输入培训计划名称" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="是否审核" prop="ShenHe">
							<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-col :span="2">
						<div style="float: right;height: 36px;line-height: 36px;font-weight: bold;">
							添加视频
						</div>
					</el-col>
					<el-col :span="22">
						<el-row :gutter="20" v-for="(v,i) in form.videos">
							<el-col :span="6">
								<el-form-item :label="'视频'+(i+1)" label-width="60px"  >
									<el-upload action="uploadUrl" :action="uploadUrl"  :limit="1"  :file-list="fileList[i]" :on-success="function(file){return handleAvatarSuccess(file,i)}" :on-remove="function(file){return handleRemove(file,i)}" :on-error="function(file){return haveError(file,i)}">
										<el-button size="small" type="primary" v-if="v.VPath == null">点击上传</el-button>
									</el-upload>
								</el-form-item>
							</el-col>
							<el-col :span="10">
								<el-form-item label="视频名称" label-width="80px" >
									<el-input v-model="v.VName" placeholder="请输入培训计划名称" />
								</el-form-item>
							</el-col>
							<el-col :span="4">
								<el-form-item label="视频总时长(分钟)"  label-width="120px">
									<el-input v-model="v.Undef1" placeholder="请输入培训计划名称" readonly />
								</el-form-item>
							</el-col>
							<el-col :span="3">
								<div style="display: flex;align-items: center;justify-content: space-between;">
									<el-button type="info">预览</el-button>
									<el-button type="primary" v-if="i==0" @click="addVideos">新增视频</el-button>
									<el-button type="danger" v-if="i!=0" @click="delVideos(i)">删除视频</el-button>
								</div>
							</el-col>
						</el-row>
					</el-col>
					</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>
	</div>
</template>

<script>
	import {
		listClass,
		getClass,
		addClass,
		updateClass,
		delClass,
	} from "@/api/xiehui/jxjy/kcgl";

	export default {
		name: "KechengGl",
		dicts: ['jxjy_zhuanye',"jxjy_kclx"],
		data() {
			return {
				uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload2", //上传路径
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 继续教育培训计划表格数据
				TrainingList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
				
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					ClassSort:{
						required: true,
						message: '请选择课程类型',
						trigger: ['blur', 'change']
					},
					XueShi:{
						required: true,
						message: '请填写学时',
						trigger: ['blur', 'change']
					},
					ClassName:{
						required: true,
						message: '请填写课程名称',
						trigger: ['blur', 'change']
					},
					ShenHe:{
						required: true,
						message: '请选择是否审核',
						trigger: ['blur', 'change']
					}
					
				},
				fileList:[],//图片列表
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询继续教育培训计划列表 */
			getList() {
				this.loading = true;
				listClass(this.queryParams).then(response => {
					this.TrainingList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					ZcsZhuanYe:'8',
					ClassName:null,
					ClassSort:null,
					XueShi:null,
					ShenHe:null,
					videos:[]
				};
				this.fileList = []
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.ClassId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.form.videos = [{VName:null,Undef1:null,VPath:null}]
				this.fileList = [[]]
				this.open = true;
				this.title = "添加继续教育培训计划";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const ClassId = row.ClassId || this.ids
				getClass(ClassId).then(response => {
					this.form = response.data;
					if(response.data.videos.length >0){
						this.fileList = response.data.videos.map( item => {
							let obj = [{ name:item.VPath,url:item.VPath}]
							return obj
						})
					}else{
						this.form.videos = [{VName:null,Undef1:null,VPath:null}]
						this.fileList = [[]]
					}
					
					this.open = true;
					this.title = "修改继续教育培训计划";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.ClassId != null) {
							updateClass(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addClass(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ClassIds = row.ClassId || this.ids;
				this.$modal.confirm('您确认删除吗？？').then(function() {
					return delTraining(ClassIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			//修改状态
			changeType(row,type){
				this.reset();
				const ClassId = row.ClassId || this.ids
				getClass(ClassId).then(response => {
					this.form = response.data;
					this.form.ShenHe = type
					this.$confirm('您要改变当前审核状态吗?', '提示', {
					  confirmButtonText: '确定',
					  cancelButtonText: '取消',
					  type: 'info'
					}).then(() => {
						updateClass(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.getList();
						}); 
					}).catch(() => {
				          
					});
				});
			},
			//
			handleAvatarSuccess(file,index){
				this.form.videos[index].VPath = file.fileName
				this.form.videos[index].Undef1 = file.minute+Math.ceil((file.second/60)* 100) / 100
			},
			handleRemove(file,index){
				this.form.videos[index].VPath = null
				this.form.videos[index].Undef1 = null
			},
			haveError(filr,index){
				this.form.videos[index].VPath = null
				this.fileList[i] = []
			},
			//添加视频
			addVideos(){
				this.fileList.push([])
				this.form.videos.push({VName:null,Undef1:null,VPath:null})
			},
			//删除视频
			delVideos(i){
				this.fileList.splice(i,1)
				this.form.videos.splice(i,1)
			}
		}
	};
</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>