<!-- 上传文档组件，by wuyang -->
<template>
	<div class="directorySelection">
		<div class="upload">
			<p class="slectfile">请选择文件类型 :
				<el-select v-model="fileType" placeholder="请选择" size="small" required>
					<el-option v-for="item in typeList" :key="item.dicKey" :label="item.dicValue" :value="item.dicKey">
					</el-option>
				</el-select>
			</p>
			<p class="slectfile" v-if="titleList!=undefined">请选择目录存放:
				<el-select v-model="catalogId" placeholder="请选择" size="small" required>
					<el-option v-for="item in titleList" :key="item.catalogName" :label="item.catalogName" :value="item.catalogId">
					</el-option>
				</el-select>
			</p>
			<p class="slectfile">请选择您要上传的文档 :</p>
			<div v-loading="isUp">

				<el-upload class="upload-demo" :before-remove="beforeRemove" drag :file-list="fileList" action="" ref="upload"
				 :http-request="uploadpic" :before-upload="befroeSave" multiple>
					<i class="el-icon-upload"></i>
					<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
				</el-upload>
			</div>
		</div>
		<p class="dialogcenter">
			<lbButton name="开始上传" @clicks="createFile" type="1" :loading="isUp" size="1"></lbButton>
		</p>

	</div>
</template>

<script>
	import {
		postApi,
		getApi,
		getMessage
	} from '../../api/commonApi'
	import lbButton from '../material/button/lbButton.vue'
	export default {
		name: "directorySelection",
		data() {
			return {
				documentlist: [],
				nextlist: [],
				List: [],
				isSelect: '0',
				catalogValue: '-1',
				isSelctDic: '-1',
				uploadFileList: [],
				fileIndex: '-1',
				isUp: false,
				typeList: [], //文档类型,
				currentCatalogId: '',
				fileType: '',
				fileList: [],
				catalogId: this.titleC,
				isUps: false

			};
		},
		props: ['checkInfo', 'fileFlag', 'titleList', 'titleC', 'caseId'],
		components:{
			lbButton
		},
		watch: {
			titleC(val) {
				if (val == '-100') {
					this.catalogId = ''
				} else {
					this.catalogId = val
				}

			}
		},
		created() {
			if (this.titleC == '-100') {
				this.catalogId = ''
			} else {
				this.catalogId = this.titleC
			}
			this.getDocument();
			this.getFileType();
		},
		mounted() {},
		methods: {

			//文档类型
			getFileType() {
				let that = this;
				let caseClass = sessionStorage.getItem("level1");
				let documentType = caseClass == "诉讼" ? that.$documentLitType : caseClass == "非诉讼" ? that.$documentNonType : that.$documentType;
				getApi('system/sysDictionary/list', {
					companyId: localStorage.getItem("companyId"),
					type: documentType
				}, function(data) {
					that.typeList = data.list;
				}, function(data) {});
			},
			//获得目录结构
			getDocument() {
				let that = this;
				var catalogType;
				if (that.$route.query.selects == undefined)
					catalogType = ""
				else if (sessionStorage.getItem("level1") == '诉讼')
					catalogType = 'l' + that.$route.query.selects;
				else if (sessionStorage.getItem("level1") == '非诉讼')
					catalogType = 'n' + that.$route.query.selects;
				else if (that.$route.query.caseClass != null || that.$route.query.caseClass != undefined) {
					let caseClass = that.$route.query.caseClass;
					catalogType = caseClass == "1" ? "l" + that.$route.query.selects : "n" + that.$route.query.selects;
				} else
					catalogType = ""

				getApi('document/fileCatalog/list', {
						userId: localStorage.getItem('userId'),
						catalogType: catalogType
					},
					function(data) {
						that.documentlist = data.list;
						that.getNext(that.documentlist[0].value, 0, that.documentlist[0].id);
						that.currentCatalogId = that.documentlist[0].id;
					},
					function(data) {});
			},
			//点击获取当前目录的子目录
			getNext(catalogNo, index, id) {
				this.currentCatalogId = id;
				this.isSelect = 0;
				this.catalogValue = catalogNo;

				this.List.splice(index + 1);
				let that = this;
				getApi('/document/fileCatalog/firstList', {
						userId: localStorage.getItem('userId'),
						catalogNo: catalogNo
					},
					function(data) {
						that.nextlist = data.list
						if (that.nextlist.length > 0) {
							that.List.push(that.nextlist)
						}

					},
					function(data) {});
			},
			befroeSave(file) {
				const isLt2M = file.size / 1024 / 1024 < 99;
				if (!isLt2M) {
					getMessage('上传文件不能超过100M', 'warning')
				}
				return isLt2M;
			},
			//上传前
			beforeAvatarUpload(file) {
				this.isUps = false
				let isDoc = file.type === 'application/msword';
				console.log(isDoc)
				if (isDoc) {
					this.$confirm('您上传的是doc格式, 是否转换为docx格式?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						this.isUps = true;
					}).catch(() => {
						getMessage('已取消', '')
						this.isUps = true;
					});

				} else {
					this.isUps = true;
				}
				return this.isUps

			},
			//上传文件
			uploadpic(item) {
				let that = this;
				that.isUp = true
				let formData = new FormData()
				let isDoc = item.file.type === 'application/msword';
// 				const isLt2M = item.file.size / 1024 / 1024 < 1;
// 				if (!isLt2M) {
// 					alert(isLt2M)
// 					getMessage('上传文件不能超过100M', 'warning')
// 					that.isUp = false
// 					return
// 				}
				if (isDoc) {
					this.$confirm('您上传的是doc格式，无法在线编辑, 是否转换为docx格式?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					}).then(() => {
						formData.append('file', item.file)
						formData.append('toDocxFlag', true)
						that.fileList.push(item.file.name + 'x')
						postApi('/file/upload', formData, function(data) {
							if (data.fileUrl == null || data.fileUrl == '') {
								getMessage('添加失败', 'error')
								that.isUp = false;
								//上传失败 删除该附件
								that.fileList.splice(that.fileList.length - 1, 1)
								return
							}
							let obj = {}
							if (that.checkInfo != undefined) {
								obj = that.createNewFile();
							} else {
								obj = that.createNewFile2();
							}

							obj.fileUrl = data.fileUrl;
							obj.htmlUrl = data.htmlUrl;
							obj.fileName = item.file.name + 'x';
							obj.fileSize = data.fileSize;
							that.uploadFileList.push(obj);
							getMessage('添加成功', 'success')
							that.isUp = false
						}, function(data) {});
					}).catch(() => {
						getMessage('已取消', '')
						formData.append('file', item.file)
						that.fileList.push(item.file.name)
						postApi('/file/upload', formData, function(data) {
							if (data.fileUrl == null || data.fileUrl == '') {
								getMessage('添加失败', 'error')
								that.isUp = false;
								//上传失败 删除该附件
								that.fileList.splice(that.fileList.length - 1, 1)
								return
							}
							let obj = {}
							if (that.checkInfo != undefined) {
								obj = that.createNewFile();
							} else {
								obj = that.createNewFile2();
							}

							obj.fileUrl = data.fileUrl;
							obj.htmlUrl = data.htmlUrl;
							obj.fileName = item.file.name;
							obj.fileSize = data.fileSize;
							that.uploadFileList.push(obj);
							getMessage('添加成功', 'success')
							that.isUp = false
						}, function(data) {});
					});

				} else {
					formData.append('file', item.file)
					that.fileList.push(item.file.name)
					postApi('/file/upload', formData, function(data) {
						if (data.fileUrl == null || data.fileUrl == '') {
							getMessage('添加失败', 'error')
							that.isUp = false;
							//上传失败 删除该附件
							that.fileList.splice(that.fileList.length - 1, 1)
							return
						}
						let obj = {}
						if (that.checkInfo != undefined) {
							obj = that.createNewFile();
						} else {
							obj = that.createNewFile2();
						}

						obj.fileUrl = data.fileUrl;
						obj.htmlUrl = data.htmlUrl;
						obj.fileName = item.file.name;
						obj.fileSize = data.fileSize;
						that.uploadFileList.push(obj);
						getMessage('添加成功', 'success')
						console.log(that.uploadFileList)
						that.isUp = false
					}, function(data) {});
				}
			},

			beforeRemove(file, uploadFileList) {
				let that = this;
				console.log(file, uploadFileList)
				that.uploadFileList.splice(that.uploadFileList.indexOf(file), 1);
				// getMessage('已删除', 'success')
			},

			createNewFile() {
				return {

					caseId: this.caseId,
					taskDetailId: this.checkInfo.taskDetailId || '', //子任务Id
					webCatalogId: this.catalogId,
					companyId: localStorage.getItem('companyId'),
					fileName: "",
					webFlag: '1',
					fileType: this.fileType,
					fileFlag: this.fileFlag,
					fileUrl: "",
					htmlUrl: "",
					fileSize: 0,
					logId: this.checkInfo.logId || '',
					sort: '',
					suffix: "",
					userId: localStorage.getItem('userId'),
					userName: localStorage.getItem('userName')
				}
			},
			createNewFile2() {
				return {

					caseId: this.caseId,
					taskDetailId: '', //子任务Id
					webCatalogId: this.catalogId,
					webFlag: '2',
					companyId: localStorage.getItem('companyId'),
					fileName: "",
					fileType: this.fileType,
					fileFlag: this.fileFlag,
					fileUrl: "",
					htmlUrl: "",
					fileSize: 0,
					logId: '',
					sort: '',
					suffix: "",
					userId: localStorage.getItem('userId'),
					userName: localStorage.getItem('userName')
				}
			},

			//保存文件列表并上传
			createFile() {
				let that = this;
				that.isUp = true
				if (that.fileType == null || that.fileType == '') {
					getMessage('请选择文档类型', 'warning')
					that.isUp = false
					return
				}
				if (that.uploadFileList == null || that.uploadFileList.length == 0) {
					getMessage('请上传文件', 'warning')
					that.isUp = false
					return
				}
				postApi('/document/fileInfo/saveList', that.uploadFileList, function(data) {
					getMessage('上传成功', 'success')
					that.isUp = false
					var temp = {
						flag: "true",
						fileList: data.fileList
					}
					that.$emit("closeFlag", temp);
					that.$refs.upload.clearFiles();
					that.resetFile();
				}, function(data) {
					that.isUp = false
					getMessage(data.msg, 'error')
					var temp = {
						flag: "true",
						fileList: data.fileList
					}
					that.$emit("closeFlag", temp);
					that.$refs.upload.clearFiles();
					that.resetFile();
				});
			},
			resetFile() {
				this.fileType = ''
				this.uploadFileList = []
				this.fileList = []
			}
		}
	};
</script>
<style scoped>
	@import "../directorySelection/directorySelection.css";
	@import "../uploadFile/uploadFile.css";
</style>
<style>
	.directorySelection .el-upload {
		width: 100%;
	}

	.directorySelection .el-upload-dragger {
		width: 100% !important;
	}
</style>
