<template>
	<div class="app-container">
		<form-search :searchformShow="{'type':'','subjectid':'','slipid':'','gradeid':'','edition':'',province:''}"
		             @handleQuery="handleQuery"></form-search>
		<el-card class="box-card" style="margin-bottom:30px">
			<el-row :gutter="24">
				<el-col :span="4">
					<h3 style="padding-left: 11px;border-left: 2px solid #e6a700;color: #e6a700;">章节目录</h3>
					<div class="head-container">
						<el-input
							v-model="name"
							placeholder="请输入章节名称"
							clearable
							size="small"
							prefix-icon="el-icon-search"
							style="margin-bottom: 20px"
						/>
					</div>
					<div class="head-container">
						<el-tree
							:data="deptOptions"
							:props="defaultProps"
							:expand-on-click-node="false"
							:filter-node-method="filterNode"
							ref="tree"
							default-expand-all
							@node-click="handleNodeClick"
						/>
					</div>
				</el-col>
				<el-col :span="20">
					<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="['system:resources:add']"
						  >新增</el-button>
						</el-col>  -->
						<el-col :span="1.5">
							<el-button
								plain
								icon="el-icon-upload2"
								size="small"
								@click="upToCloudResourse"
								:disabled="multiple"
							>上传到云库
							</el-button>
						</el-col>
						<el-col :span="1.5">
							<el-button
								plain
								icon="el-icon-upload2"
								size="small"
								@click="downToSchoolResourse"
								:disabled="multiple"
							>上传到校本资源库
							</el-button>
						</el-col>
						<el-table v-loading="loading" :data="resourcesList" @selection-change="handleSelectionChange">
							<el-table-column type="selection" width="55" align="center"/>
							<el-table-column label="文件" align="center" prop="resourcesName">
								<template slot-scope="scope">
									<div  >{{scope.row.resourcesName}}</div>
								</template>
							</el-table-column>
							<el-table-column label="发布人" align="center" prop="createBy"/>
							<el-table-column label="时间" align="center" prop="createTime"/>
							<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)"
										v-hasPermi="['system:resources:remove']"
									>删除
									</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-row>
				</el-col>
			</el-row>
		</el-card>
		<!-- 添加或修改云库对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="资源名称" prop="resourcesName">
					<el-input v-model="form.resourcesName" placeholder="请输入资源名称"/>
				</el-form-item>
				<el-form-item label="资源路径" prop="resourcesUrl">
					<el-input v-model="form.resourcesUrl" placeholder="请输入资源路径"/>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" placeholder="请输入备注"/>
				</el-form-item>
			</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>
		<upload-dialog v-if="dialogVisible"  :title="title"  :dialogVisible="dialogVisible" @handleClose="handleClose"></upload-dialog>
	</div>
</template>

<script>
	import { list, add, update, exportList} from '@/api/webApi/school'
	import { upToCloudResourse ,downToSchoolResourse,getInfo,remove} from '@/api/webApi/person'
	import { treeList } from '@/api/webApi/system'

	export default {
		components: {},
		data() {
			return {
				dialogVisible:false,
				img: {
					ship: require('@/assets/images/ship.png')
				},
				defaultProps: {
					children: 'children',
					label: 'name'
				},
				deptOptions: undefined,
				deptName: '',
				// 遮罩层
				loading: true,
				// 导出遮罩层
				exportLoading: false,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 云库表格数据
				resourcesList: [],
				// 弹出层标题
				title: '',
				// 是否显示弹出层
				open: false,
				// 资源类型字典
				typeOptions: [],
				// 资源分类字典
				categoryOptions: [],
				// 是否删除字典
				delFlagOptions: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					resourcesName: null,
					type: null,
					category: null,
					province: null,
					city: null,
					county: null,
					chapterId: null,
					resourcesUrl: null,
					bookId: null,
					subjectId: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					resourcesName: [
						{ required: true, message: '资源名称不能为空', trigger: 'blur' }
					],
					resourcesUrl: [
						{ required: true, message: '资源路径不能为空', trigger: 'change' }
					],
				},
				viewName: '',
				name: ''
			}
		},
		created() {
			this.viewName = this.$route.name
			this.getList()
            // this.getTreeselect();
			this.getDicts('resource_type').then(response => {
				this.typeOptions = response.data
			})
			this.getDicts('category').then(response => {
				this.categoryOptions = response.data
			})
			this.getDicts('bool_flag').then(response => {
				this.delFlagOptions = response.data
			})
		},
		watch: {
			// 根据名称筛选部门树
			name(val) {
				this.$refs.tree.filter(val)
			}
		},
		methods: {
			/** 查询个人资源库列表 */
			getList() {
				this.loading = true
        		this.queryParams.category = '课件资源'

				list(this.queryParams).then(response => {
					this.resourcesList = response.rows
					this.total = response.total
					this.loading = false
				})
			},
			// 资源类型字典翻译
			typeFormat(row, column) {
				return this.selectDictLabel(this.typeOptions, row.type)
			},
			// 资源分类字典翻译
			categoryFormat(row, column) {
				return this.selectDictLabel(this.categoryOptions, row.category)
			},
			// 是否删除字典翻译
			delFlagFormat(row, column) {
				return this.selectDictLabel(this.delFlagOptions, row.delFlag)
			},
			handleClose(type){
				let _this = this

				if(type && type==1){
					_this.ids.forEach((item, index) => {
						upToCloudResourse(item).then(() => {
							if (index == _this.ids.length - 1) {
								_this.msgSuccess('操作成功')
								_this.dialogVisible = false

							}
						})
					})
				}else{
					this.dialogVisible = false

				}
			},
			upToCloudResourse() {
				let _this = this
				const resourcesIds = this.ids.join(',')
				this.title = '上传至云库'
				this.dialogVisible = true ;
			},
			downToSchoolResourse() {
				let _this = this
				const resourcesIds = this.ids.join(',')
				this.title = '上传校本资源库'
				this.dialogVisible = true ;
			},
			// 取消按钮
			cancel() {
				this.open = false
				this.reset()
			},
			// 表单重置
			reset() {
				this.form = {
					resourcesId: null,
					resourcesName: null,
					sourceId: null,
					category: null,
					chapterId: null,
					knowledgeBasicId: null,
					resourcesUrl: null,
					type: null,
					linkWay: null,
					bookId: null,
					customerId: null,
					subjectId: null,
					delFlag: null,
					createBy: null,
					createTime: null,
					remark: null
				}

			},
			/** 搜索按钮操作 */
			handleQuery(data) {
				this.queryParams.pageNum = 1
				Object.assign(this.queryParams, data)
				this.getList()
				this.getTreeselect()
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm('queryForm')
				this.handleQuery()
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.resourcesId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset()
				this.open = true
				this.title = '添加个人资源库'
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset()
				const resourcesId = row.resourcesId || this.ids
				getInfo(resourcesId).then(response => {
					this.form = response.data
					this.open = true
					this.title = '修改个人资源库'
				})
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs['form'].validate(valid => {
					if (valid) {
						if (this.form.resourcesId != null) {
							update(this.form).then(response => {
								this.msgSuccess('修改成功')
								this.open = false
								this.getList()
							})
						} else {
							add(this.form).then(response => {
								this.msgSuccess('新增成功')
								this.open = false
								this.getList()
							})
						}
					}
				})
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const resourcesIds = row.resourcesId || this.ids
				this.$confirm('是否确认删除个人资源库编号为"' + resourcesIds + '"的数据项?', '警告', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					return remove(resourcesIds)
				}).then(() => {
					this.getList()
					this.msgSuccess('删除成功')
				})
			},
			/** 导出按钮操作 */
			handleExport() {
				const queryParams = this.queryParams
				this.$confirm('是否确认导出所有个人资源库数据项?', '警告', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.exportLoading = true
					return exportList(queryParams)
				}).then(response => {
					this.download(response.msg)
					this.exportLoading = false
				})
			},

			/** 查询章节下拉树结构 */
			getTreeselect() {
				if (!this.queryParams.slipid) {
					this.deptOptions = []
					return
				}
				treeList(this.queryParams).then(response => {
					this.deptOptions = response.data
				})
			},
			// 筛选节点
			filterNode(value, data) {
				if (!value) return true
				return data.name.indexOf(value) !== -1
			},
			// 节点单击事件
			handleNodeClick(data) {
				this.queryParams.chapterId = data.id
				this.getList()
			}
		}
	}
</script>
