<?php
namespace app\common\taglib;
use think\template\TagLib;
class Tp extends TagLib {

	protected $tags = array(
        'upload'     => ['attr'=>'id,name,upload_type,upload_path_son,input_text','close'=>0],
        'upload_file'     => ['attr'=>'id,name,upload_type,upload_path_son,input_text','close'=>0],
        'index_file'     => ['attr'=>'id,name,upload_type,upload_path_son,input_text','close'=>0],
        'editor'     => ['attr'=>'id,name,style,width,height,type','close'=>1],
    );
	
    public function tagupload($tag){
        $name       = $tag['name'];
        $id 		= $tag['id'];
        $value	    = $tag['value'];
		//$upload_type      = $tag['upload_type']; 
        $upload_type      = !empty($tag['upload_type'])?$tag['upload_type']:'other';
		$upload_path_son  = !empty($tag['upload_path_son'])?$tag['upload_path_son']:'';
		
		$parseStr='
		<input type="text" name="'.$name.'" id="upimg_'.$name.'" value="'.$value.'" class="layui-input" /> 
		<input type="file" id="for_'.$name.'" name="" onchange="upload_'.$name.'(this)" style="display:none">
		<label for="for_'.$name.'" class="for_label"><p class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon">&#xe67c;</i>上传图片</p></label>
		
		<script>
		function upload_'.$name.'(obj){
			if(obj.value==""){return;}
			var formdata = new FormData();
			formdata.append("file" , $(obj)[0].files[0]);
			$.ajax({
				type :"post",
				url : "{:url("index/Upload/upload")}",
				data : formdata,
				cache : true,
				processData : false,
				contentType : false,
				success : function(date){
					if(date.type==1){
						layer.msg(date.info, {icon:1,time:1500});
						$("#upimg_'.$name.'").val(date.file);
						$(".yulan_'.$name.'").show().attr("data-file",date.file);
					}else{
						layer.msg(date.info, {icon:2,time:1500});
					}
				},
				error : function(){ }
			});
		}			
		</script>';
		
        return $parseStr;
    }

	//上传文件
    public function tagupload_file($tag){
        $name       = $tag['name'];
        $id 		= $tag['id'];
        $value	    = $tag['value'];
		//$upload_type      = $tag['upload_type']; 
        $upload_type      = !empty($tag['upload_type'])?$tag['upload_type']:'other';
		$upload_path_son  = !empty($tag['upload_path_son'])?$tag['upload_path_son']:'';
		
		$parseStr='
		<input type="text" name="'.$name.'" id="upimg_'.$name.'" value="'.$value.'" class="layui-input" /> 
		<input type="file" id="for_'.$name.'" name="" onchange="upload_'.$name.'(this)" style="display:none">
		<label for="for_'.$name.'" class="for_label"><p class="layui-btn layui-btn-normal layui-btn-sm"><i class="layui-icon">&#xe67c;</i>上传文件</p></label>
		
		<script>
		function upload_'.$name.'(obj){
			if(obj.value==""){return;}
			var formdata = new FormData();
			formdata.append("file" , $(obj)[0].files[0]);
			$.ajax({
				type :"post",
				url : "{:url("index/Upload/upload_file")}",
				data : formdata,
				cache : true,
				processData : false,
				contentType : false,
				success : function(date){
					if(date.type==1){
						layer.msg(date.info, {icon:1,time:1500});
						$("#upimg_'.$name.'").val(date.file);
						$(".yulan_'.$name.'").show().attr("data-file",date.file);
					}else{
						layer.msg(date.info, {icon:2,time:1500});
					}
				},
				error : function(){ }
			});
		}			
		</script>';
		
        return $parseStr;
    }

	//前台上传文件
    public function tagindex_file($tag){
        $name       = $tag['name'];
        $id 		= $tag['id'];
        $text 		= $tag['text'];
        $value	    = $tag['value'];
		//$upload_type      = $tag['upload_type']; 
        $upload_type      = !empty($tag['upload_type'])?$tag['upload_type']:'other';
		$upload_path_son  = !empty($tag['upload_path_son'])?$tag['upload_path_son']:'';
		
		$parseStr='
		<input type="hidden" name="'.$name.'" id="upimg_'.$name.'" value="'.$value.'" class="layui-input" /> 
		<input type="file" id="for_'.$name.'" name="" onchange="upload_'.$name.'(this)" style="display:none">
		<label for="for_'.$name.'" class="for_label"><span class="wb_'.$name.'">'.$text.'</span></label>
		
		<script>
		function upload_'.$name.'(obj){
			if(obj.value==""){return;}
			var formdata = new FormData();
			formdata.append("file" , $(obj)[0].files[0]);
			$.ajax({
				type :"post",
				url : "{:url("index/Upload/index_file")}",
				data : formdata,
				cache : true,
				processData : false,
				contentType : false,
				success : function(date){
					if(date.type==1){
						$(".wb_'.$name.'").html("已上传");
						$("#upimg_'.$name.'").val(date.file);
					}else{
						alert(date.info);
					}
				},
				error : function(){ }
			});
		}			
		</script>';
		
        return $parseStr;
    }

	//在线编辑器
    public function tageditor($tag,$content) {
        $id			=	!empty($tag['id'])?$tag['id']: 'editor';
        $name   	=	$tag['name'];
        $style   	    =	!empty($tag['style'])?$tag['style']:'';
        $width		=	!empty($tag['width'])?$tag['width']: '100%';
        $height     =	!empty($tag['height'])?$tag['height'] :'320px';
     //   $content    =   $tag['content'];
        $type       =   $tag['type'] ;

		$parseStr   =  '
		<script type="text/javascript" charset="utf-8" src="/static/tqcms/ueditor/ueditor.config.js"></script>
		<script type="text/javascript" charset="utf-8" src="/static/tqcms/ueditor/ueditor.all.min.js"> </script>
		<script type="text/javascript" charset="utf-8" src="/static/tqcms/ueditor/lang/zh-cn/zh-cn.js"></script>';
				
		$parseStr   =  '
		<script  id="'.$id.'" type="text/plain" style="'.$style.'" name="'.$name.'" >'.$content.'</script>
		<script type="text/javascript">
			var ue_'.$id.' = UE.getEditor("'.$id.'");
		</script>';

        return $parseStr;
    }

}