<!--
based on 
 https://ext.dcloud.net.cn/plugin?id=672
 https://github.com/steffenx/uniapp_popup
  
 *数组形式传值
 *type: success warn info fail loading（string），默认info
 *content: 内容（string）
 *duration: 消失时间（Number），默认2000
 *distance: 弹窗间距，默认128，可设为0代表重叠
 *clickable: 是否点击消失（Boolean），默认false
 -->
<template>
	<view class="popup_list">
		<view v-for="(toast,index) of popup_list" :id="toast.uuid" :key="toast.uuid" >
			<view class="mpopup" :style="{ background: toast.color ,top:index*toast.distance+50+'px'}" :class="[toast.animator,'mpopup-'+toast.type]" @click="close(toast.uuid,index)">
				<view class="icon" ></view>
				<text class="text" >{{ toast.content }}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				popup_list:[],//弹窗数组
			}
		},
		props:{
		},
		methods:{
			open:function(toast){
				//生成uuid
				let uuid=this.guid();
				toast.uuid=uuid;
				//添加动画
				toast.animator='fade_Down';
        
				toast.clickable = toast.clickable || false //判断是否可点击消失/可控制消失
        toast.duration = toast.duration || 1500
				toast.distance = toast.distance || 128 // 弹窗间距
        toast.type = toast.type || 'info'
        
				//添加进数组
				this.popup_list.push(toast);
		
				if(!toast.clickable){
					this.disappear(toast.uuid,toast.duration);
				}//可点击消失
				else{
					this.$emit('uuidCallback',toast.uuid);
				}
				
			},
			//自动消失
			disappear:function(uuid,duration){
				//退出动画之后，短暂延迟后移除本元素
				this.fade_out_animator(uuid,duration).then(res=>{
					setTimeout(()=>{
						for(let i=0;i<this.popup_list.length;i++){
							if(this.popup_list[i].uuid==res){
								//移除本元素
								this.popup_list.splice(i,1);
								this.$forceUpdate()
							}
						}
					},250)
				});
			},
			fade_out_animator:function(uuid,duration){
				//duration秒后退出
				if(!duration||typeof(duration)!='number'){duration=1500;}
				return new Promise(res=>{
					setTimeout(()=>{
						for(let i=0;i<this.popup_list.length;i++){
							if(this.popup_list[i].uuid==uuid){
								//添加退出动画
								this.popup_list[i].animator='fade_Top';
								res(uuid);
							}
						}
					},duration)
				})
			},
			//可控制关闭的弹出框
			close:function(uuid,ind){
				if(ind){
					if(!this.popup_list[ind].clickable){return}
				}
				this.remove_element(uuid).then((res)=>{
					setTimeout(()=>{
						for(let i=0;i<this.popup_list.length;i++){
							if(this.popup_list[i].uuid==res){
								//移除本元素
								this.popup_list.splice(i,1);
								this.$emit('closeCallback',uuid);
								this.$forceUpdate()
							}
						}
					},250)
				})
			},
			//控制移除元素
			remove_element:function(uuid){
				return new Promise(res=>{
					for (var i = 0; i < this.popup_list.length; i++) {
						if(this.popup_list[i].uuid==uuid){
							this.popup_list[i].animator='fade_Top';
							res(uuid)
							break;
						} 
					}
				})
				
			},
			//更新
			update:function(update_list){
				for (var i = 0; i < this.popup_list.length; i++) {
					if(this.popup_list[i].uuid==update_list.uuid){
						this.popup_list[i].type=update_list.type;
						this.init(this.popup_list[i]);
						this.popup_list[i].content=update_list.content;
						break;
					} 
				}
			},
			//生成uuid
			guid:function() {
			    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
			        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
			        return v.toString(16);
			    });
			}
		}
	}
</script>

<style lang="scss">
	.mpopup{
		display: flex;
		flex-direction: column;
		text-align: center;
		justify-content: center;
		align-items: center;
		min-height: 45px;
		max-width: 80%;
		transition :all .5s;
		position: fixed;
    right:0;
    left:0;
		margin: 0 auto;
		border-radius: 5px;	
		z-index:998;
    @media screen and (min-width: 751px) { max-width: 500px }
		
    .icon{
			display: flex;
			text-align: center;
			justify-content: center;
			width: 32px;
			height: 32px;
			margin: 20px;
			background: no-repeat center center / contain;
		}
		.text{
			margin: 0px 20px 20px 20px;
			font-size: 16px;
		}
	}
	.mpopup-success{
		background: #f0f9eb;
		border: 1px solid #e1f3d8;
		color: #67c23a;
		.icon{
			background-image: url('./cToast.success.png');
		}
	}
	.mpopup-fail{
		background: #fef0f0;
		border: 1px solid #fde2e2;
		color: #f56c6c;
		.icon{
			background-image: url('./cToast.fail.png');
		}
	}
	.mpopup-warn{
		background: #fdf6ec;
		border: 1px solid #faecd8;
		color: #e6a23c;
  	.icon{
			background-image: url('./cToast.warn.png');
		}

	}
	.mpopup-info{
		background: #edf2fc;
		border: 1px solid #ebeef5;
		color: #909399;
		.icon{
			background-image: url('./cToast.info.png');
		}
	}
	.mpopup-loading{
		background: #e2f5ff;
		border: 1px solid #ceeeff;
		color: #5cbaff;
		.icon{
			background-image: url('./cToast.loading.png');
			animation: rotate360 1.5s ease infinite;
		}
	}
	.fade_Down{
		animation: fadeInDown 0.6s both;
	}
	.fade_Top{
		animation: fadeInTop 0.5s forwards;
	}
	/*从上到下*/
	@keyframes fadeInDown
	{
	    from {
	        opacity: 0;
	        -webkit-transform: translate(0,-100px); 
	        transform: stranslate(0,-100px); 
	    }
	    to {
	        opacity:1;
	        -webkit-transform: translate(0,10px);
	        transform: stranslate(0,10px);
	    }
	}
	/*从下到上*/
	@keyframes fadeInTop
	{
	    from {
	        opacity:1;
	        -webkit-transform: translate(0,10px); 
	        transform: stranslate(0,10px);
	    }
	    to {
	
			opacity: 0;
			-webkit-transform: translate(0,-100px);
			transform: stranslate(0,-100px);
	    }
	}
	@keyframes rotate360
	{
		from {
			transform: rotate(0);
		}
		to{
			transform: rotate(360deg);
		}
	}
</style>
