<template>
	<slot name="header"></slot>

	<el-dialog v-bind="$attrs" :title="title" v-model="visible" :width="560" destroy-on-close draggable @closed="visible=false">
		<div class="importBody">
			<el-upload
				class="upload"
				:action="oss.host"
				:data="upload_data"
				:show-file-list="false"
				:on-success="handleSuccess"
				:on-error="handleError"
				:before-upload="beforeUpload"
				:http-request="uploadFile"
				drag
				multiple
				:limit="5"
				:disabled="importDis"
			>
				<div v-if="!importTrue">
					<el-icon class="el-icon--upload"><sc-icon-UploadExcel /></el-icon>
					<div class="el-upload__text">
						<span class="laText">请将Excel拖到此处</span>
						<span class="tipText">，或点击上传</span><em> Excel</em>
					</div>
				</div>
				<div v-else>
					<div class="el-upload__text">
						<el-icon class="trueIcon el-icon&#45;&#45;upload">
							<sc-icon-FormXls />
<!--							<div class="deleteFile" @click="deleteFile"><el-icon class="icon"><el-icon-Close/></el-icon></div>-->
						</el-icon>
						<div class="el-upload__text">文件已选择最大，可以点击立即上传或替换文件。</div>
					</div>
				</div>

				<template #tip>
					<div class="el-upload__tip">
						<span class="tip">最多上传5个文件,单个文件不要超过10M,请上传 xlsx/docx 格式文件</span>
						<span class="btn">
							<slot name="download"></slot>
						</span>
					</div>
					<div class="importBox" v-if="progressShow">
						<div class="item" v-for="item in listProgress" :key="item">
							<div class="name">{{item.type_desc}}</div>
							<el-progress class="exportPopover" :text-inside="true" :stroke-width="12" :percentage="item.rate" />
						</div>
					</div>
				</template>
			</el-upload>
		</div>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button type="primary" @click="save">确 定</el-button>
		</template>
	</el-dialog>
</template>

<script>
import {eventBus} from "@/utils/eventBus"

export default {
	name: "uploadFile",
	emits: ['closed', "uploadFileSuccess"],
	props:{
		size:{type:String, default:'small'},
		title:{type:String, default: ""},
	},
	data(){
		return{
			visible: false,
			text:"",
			listFile:[],
			listProgress:[],

			oss:{
				host:''
			},
			upload_data:{},
			fileShow:false,
			params:{},

			importDis:false,
			importTrue:false,
			progressShow:false,
		}
	},
	setup(){

	},
	watch:{

	},
	mounted() {
		eventBus.$on('sockBack', this.getWsResult);
	},
	unmounted() {
		eventBus.$off('sockBack', this.getWsResult);
	},
	methods:{
		getWsResult(res){
			if(res.data && (res.data.type == 12)){
				let flag = this.listProgress.some(em=>em.file_unique_id == res.data.file_unique_id);
				if(!flag){
					this.listProgress.push(res.data)
				}else{
					this.listProgress.forEach((em,index)=>{
						if(em.file_unique_id == res.data.file_unique_id){
							em.rate = res.data.rate
							if(res.data.status == 1){
								this.listProgress.splice(index,1);
							}
						}
					})
				}
				this.progressShow = true;
				if(this.listProgress.length==0 && res.data.status == 1){
					this.progressShow = false;
				}
				if(res.data.status == 1){
					this.$emit('uploadFileSuccess');
				}
			}
		},

		// 导入文件
		importFile(){
			this.visible = true;
		},
		// 上传前
		beforeUpload(file){
			// const text = file.name.split('.').pop();
			// if(!['xls','xlsx'].includes(text)){
			// 	this.$message.warning(`只能选择的excel文件类型`);
			// 	return false;
			// }
			// this.progressShow = true;
			this.text = file.name;
			this.listFile.push(file);
			return  true
		},
		async uploadFile() {
			try {
				const formData = new FormData();
				for(let i in this.listFile){
					formData.append('files[]', this.listFile[i]);
				}
				for (const key in this.upload_data) {
					formData.append(key, this.upload_data[key]);
				}
				this.params = formData;
				this.importTrue = this.listFile.length>4?true:false;
				this.importDis = this.listFile.length>4?true:false;
			} catch (error) {
				console.log(error)
			}
		},
		deleteFile(){
			this.importTrue = false;
			setTimeout(()=>{
				this.importDis = false;
			},100)
		},
		async save() {
			const res = await this.$API.docs.manager.upload.post(this.params);
			if (res.code == 200) {
				this.$message.success('提交成功,正在上传');
			}
		},
		// 上传成功
		handleSuccess(){

		},
		// 上传失败
		handleError(){
			this.$message.warning('上传失败请重新上传');
		}
	}
}
</script>

<style scoped lang="scss">
.importBody{
	padding: 0 20px;
}
.upload .el-icon--upload{
	width: 54px;height: 54px;
}
.upload ::v-deep .el-upload__text{
	.trueIcon{
		position: relative;
		border: 1px solid var(--el-border-color);
		border-radius: 4px;
	}
	.laText{
		color: var(--el-color-dark);
	}
	.tipText{
		color: var(--el-text-color-placeholder);
	}
	.deleteFile{
		position: absolute;
		right: -6px;
		top: -10px;
		width: 16px;
		height: 16px;
		border-radius: 50%;
		background: var(--el-color-danger);
		display: flex;
		align-items: center;
		justify-content: center;
		.icon{
			width: 12px;height: 12px;
			color: var(--el-color-white);
		}

	}
}
.upload ::v-deep .el-upload__tip{
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-top: 15px;
	color: var(--el-text-color-placeholder);
	.btn{
		color: var(--el-color-primary);
		cursor: pointer;
	}
}
.importBox{
	border: 1px solid var(--el-border-color);
	padding: 10px 5px;
	margin: 15px 0;
	border-radius: 4px;
	.item{margin-bottom: 10px;}
	.item:last-child{margin-bottom: 0;}
	.name{margin-bottom: 5px;font-size: 13px;}
	.exportPopover ::v-deep .el-progress-bar__innerText{
		height: 100%;
		display: flex;align-items: center;justify-content: flex-end;
		font-size: 10px;
	}
}
.errText{
	margin: 15px 0 0 0;
	padding: 10px;
	border-radius: 6px;
	background: var(--el-color-danger-light-9);
	position: relative;
	.name{
		color: var(--el-color-danger);
		font-size: 12px;
		line-height: 20px;
	}
	.deleteErr{
		position: absolute;
		right: 4px;
		top: 4px;
		z-index: 20;
		cursor: pointer;
		.icon{
			font-size: 16px;
		}
	}
}
</style>
