$View={};
$View.link=function(dom,type='src')
{
	var list=dom.querySelectorAll(`[view-${type}]`);
	for (var index = 0; index < list.length; index++) 
	{
		list[index][type]=list[index].getAttribute(`view-${type}`);
	}
}

$View._style=function(dom)
{
	dom=$M.find(dom)
	if(!dom)return
	let tag_style=dom.getAttribute('_style')
			
	if(!tag_style)
	{
		tag_style=$M.style(dom).display
		dom.setAttribute('_style',tag_style)
	}

}


$View.getView= function(obj)
{
	var that=this;
	this.obj=obj;
	this.count=0;
	this.request=obj.request?obj.request:{};
	var curdom=$M.find(obj.id);
	this.dom=curdom;
	if(!curdom)return false;

	if(obj.new)curdom._html=curdom.innerHTML;
	else curdom._html=curdom._html?curdom._html:curdom.innerHTML;

	$View._style(curdom)
	if(obj.loadDom)$View._style(obj.loadDom)

	
	if(!obj.show && !obj.load && !obj.loadInner)$M.hide(curdom)


	if(!obj._data)
	{
		obj._data={};	
		Object.defineProperty(obj,"_data",{set:async function(v){this.data=v;await cal()}})
		if(obj.request)
		{
			obj._request={};	
			Object.defineProperty(obj,"_request",{set:function(v){this.request=v;calq()}})			
		}
	}

	function after(d)
	{
		requestAnimationFrame
		(
			function()
			{
				curdom.innerHTML=d;
				if(obj.fun instanceof Function)
				{
					obj.fun(this.count)
				}
			}
		)
		
		this.count+=1;
		//加载链接
		$View.link(curdom,'src');
		//如果元素被隐藏，恢复显示
		$M.show(curdom);
		$M.fade(curdom);
	}

	async function cal()
	{
		if(obj.worker)
		{	
			alert('暂不支持')	 
		}
		else
		{
			after(await $Render.display(curdom._html,obj["data"]));
		}
	}

	async function calq()
	{
		if(!obj.request || !obj.request.url)
		{
			return false;
		}
		var m=(obj.request.method=='post')?obj.request.method:'get';
		
		function requestExec(d)
		{
			if(d && d.code==0)
			{
				that.set(d.param);
				that.requestResult=d.param;
			}
			else
			{
				if(obj.request.errorBeforeRequest)
				{
					that.set(that.requestResult?that.requestResult:{});
				}
				else
				{
					//使用默认的data值
					that.set();
				}
				$M.info('【view】请求失败',d)
			}
		}

		if(obj.load)$Route.call(obj.load,{dom:obj.id,type:'route'},obj.id)
		if(obj.loadInner)curdom.innerHTML=obj.loadInner
		if(obj.loadDom)
		{
			obj.loadDom=$M.find(obj.loadDom)
			$M.show(obj.loadDom)
		}
		if(obj.loadTime)this.$loadTimeStart=Date.now()

		let check=obj.request.check?obj.request.check:requestExec;
		let resultTag;
		let result=await $Ajax[m](obj.request).catch((err)=>{
			if(err && err.type=='filter')
			{
				$M.info('【view】请求，被过滤器拦截，默认操作将不再执行',err)
			}
			else
			{
				resultTag=true;
				$M.info('【view】请求错误',err)
			}
		});

		if(obj.loadTime)
		{
			let now=Date.now()-this.$loadTimeStart-obj.loadTime
			if(now<0)
			{
				await $M.sleep(curdom,-now)
			}
		}
		
		if(obj.loadDom)$M.hide(obj.loadDom)

		if(result!==undefined || resultTag)await check(result);
	}
}

$View.getView.prototype.param=function(ke)
{
	if(ke)
	{
		return this.obj['request'][ke];
	}
	else
	{
		return this.obj['request'];
	}
}

$View.getView.prototype.query=function(ke)
{
	if(ke)
	{
		this.obj['request']['param']=ke
	}
	this.obj['_request']=this.obj['request']
}

$View.getView.prototype.get=function(ke)
{
	if(ke)
	{
		return this.obj['data'][ke];
	}
	else
	{
		return this.obj['data'];
	}
}

$View.getView.prototype.new=function(html)
{
	if(!this.obj.show && !this.obj.load && !this.obj.loadInner)$M.hide(this.dom)
	if(html) this.dom._html=html
	else this.dom._html=this.dom.innerHTML
}

$View.getView.prototype.set=function(objd,orv)
{
	if(!(objd instanceof Object) && objd && orv)
	{
		this.obj['data'][objd]=orv;
		var res=this.obj['data'];
		this.obj._data=res;
	} 
	else if(objd instanceof Object)
	{
		this.obj._data=objd;
	}
	else
	{
		this.obj._data=this.obj.data;
	}
}
