<?php
// +----------------------------------------------------------------------
// | yershop网店管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | 版权申明：yershop网店管理系统不是一个自由软件，是贝云网络官方推出的商业源码，严禁在未经许可的情况下
// | 拷贝、复制、传播、使用yershop网店管理系统的任意代码，如有违反，请立即删除，否则您将面临承担相应
// | 法律责任的风险。如果需要取得官方授权，请联系官方http://www.yershop.com
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
/**
 * 后台模板生成器控制器
 */
class Template extends Home{
     /* 模板生成器查询首页 */
	public function index(){     
      if($_POST){
		   
	//$value="$"."info['$name']";
	$name=$_POST["name"];$title=$_POST["title"];
	$type=$_POST["type"];
	$value="$"."info['$name']|default=''";
	$v="$"."info['$name']";$lists="$"."info['$name']";
	$path='';
	if($type=="picture"){
		
		$path=get_cover_path($value);
	}
    // TODO 可以加入系统配置
     $data= array(
        'text'    => ' <div class="form-item">
									                 <label>'.$title.'<span></span></label>
													 <div class="form-input">
									                     <input type="text" name="'.$name.'" value="{'.$value.'}">
													  </div>
								                 </div>',
        'textarea'  => '  <div class="form-item">
									                 <label>'.$title.'<span></span></label>
													 <div class="form-input">
									                    <textarea name="'.$name.'"/>{'.$value.'}</textarea>
													 </div>
								                 </div>',
        'date'      => '	<div class="form-item">
									<label>'.$title.'<span>('.$title.')</span></label>
									<div class="form-input">
										<input type="text"name="'.$name.'" id="'.$name.'" class="laydate-icon"
									    value="{notempty name="info.id"}{$info.'.$name.'|time_format}{/notempty}"/>
									    <script type="text/javascript" src="__COMMON__/laydate/laydate.js"></script>		
										<script>
											laydate({
											  elem: "#'.$name.'",
											  format: "YYYY-MM-DD hh:mm:ss", 
											  min: laydate.now(), //设定最小日期为当前日期//目标元素。
											  
											  event:"focus" //响应事件。如果没有传入event，则按照默认的click
											});
										</script>
									</div>
							   </div>
',
		'radio'     => '单选',
		'select'     => '<div class="form-item">
														<label>'.$title.'<span></span></label> 
													<div class="form-input">
										 <select name="brand_id" class="input_box types" > 
										   <option value="0">无</option> 
										    <option value="1">是</option>
											  <option value="2">否</option>
										  </select>
									</div>

														<script type="text/javascript">  													
															 $("select[name="'.$name.'"]").val("{'.$value.'}");													
														</script>	
                                                  </div>',
        'picture'   => ' <div class="form-item">
														<label>'.$name.'<span>(图片名称)</span></label> 
														<input type="hidden" name="'.$name.'"  value="{'.$value.'}">
				                                     <a id="'.$name.'" href="javascript:void(0);" class="btn_upload">
													<input type="file" name="file" class="file" id="'.$name.'" style=""/>选择文件
													</a>
													   <div class="upload_preview">
															 {notempty name="config.value"}  
																  <div class="upload-pre-item"> 
																	<img class="'.$name.'" src="'.$path.'"/>
																	</div>
															  {/notempty}
														</div>

														<script type="text/javascript">  
															var field="'.$name.'";	
															
															upload(field);
														</script>	
                                                  </div>',
												  
									  
		'color'     => '拾色器',
		'editor'     => ' <div class="form-item">
									    <label>'.$title.'<span>(详细内容)</span></label>
										<div class="form-input w100">
												<textarea class="am-validate" id="'.$name.'" name="'.$name.'" required>{$info.'.$name.'|default=""}</textarea>	
												<link rel="stylesheet" href="__COMMON__/kindeditor/default/default.css" />
												<script charset="utf-8" src="__COMMON__/kindeditor/kindeditor-min.js"></script>
												<script charset="utf-8" src="__COMMON__/kindeditor/zh_CN.js"></script>
												<script type="text/javascript">
													var editor_'.$name.';
													KindEditor.ready(function(K) {
														editor_'.$name.' = K.create("textarea[name=\''.$name.'\']", {
															allowFileManager : false,
															themesPath: K.basePath,
															width: "90%",
															height: "350",
															resizeType:1,
															pasteType : 2,
															urlType : "absolute",
															fileManagerJson :"{:url("fileManagerJson")}",
															uploadJson : "{:url("Uploads/ke_upimg")}"
														});
													});
													$(function(){
														$("textarea[name=\''.$name.'\']").closest("form").submit(function(){
															editor_'.$name.'.sync();
														});
													})
											    </script> 
										</div>
                                   </div>',
        'photo'     => '  
							  <div class="form-item">
									<label>'.$title.'<span>('.$title.')</span></label>
									<input type="hidden" name="'.$name.'"  value="{'.$value.'}"	>
								   <a id="'.$name.'" href="javascript:void(0);" class="btn_upload">
													<input type="file" name="file" class="file" id="'.$name.'" style=""/>选择文件
													</a>
									  <div class="upload_preview">
											    {notempty name="'.$v.'"}
											     {php}$list=explode(",",'.$lists.');{/php}
												{foreach name="list" item="vo" }  
												<div class="upload-pre-item"> <img src="{$vo|get_cover_path}" data-id="{$vo}"/>
												   <span class="close"  onclick="delimage(this,"'.$name.'");"></span>
											 	</div>
												 {/foreach}	
												{/notempty} 
									   </div>
									 <script type="text/javascript">  
								             var field="'.$name.'";
						     	             uploads(field);
                                       </script>
							</div>',
    );
	$html=str_replace("\r\n","",$data[$type]);
	$html=str_replace("\t","",$data[$type]);
    $res["html"]=trim($html);
     $res["code"]=1;return $res;
        }
       
		$this->meta_title="模板生成器管理";
		$this->assign('meta_title', $this->meta_title);
	    return $this->fetch("template/index"); 
	}


    /* 编辑模板生成器 */
	public function edit($id){   
	    if($_POST){
		   $Template = new \app\admin\model\Template;
           $res=$Template->allowField(true)->validate(true)->save($_POST,['id' => $_POST['id']]);
	       if($res){
			   addUserLog("edit_template",session_uid());
		      $this->success("更新成功！",cookie("__forward__"));
		   }else{
			  $error=$Template->getError()?$Template->getError():"更新失败！";
			  $this->error($error);
		   } 
	   }
	   else{
		 $map['id']=$id;
		 $info=db("Template")->where($map)->find();
	     $this->assign('info', $info);
		
		   cookie("__forward__",input('server.HTTP_REFERER'));
		 $this->meta_title="编辑模板生成器";
		 $this->assign('meta_title', $this->meta_title);
	     return $this->fetch("template/edit");
	   }
	}
	 /* 增加模板生成器 */
	public function add($id=""){  
	   if($_POST){
		  $Template = new \app\admin\model\Template;
            // 过滤post数组中的非数据表字段数据
          $res=$Template->validate(true)->allowField(true)->save($_POST);
	      if($res){
			  addUserLog("add_template",session_uid());
		      $this->success("新增成功！",cookie("__forward__"));
		  }else{
			  $error=$Template->getError()?$Template->getError():"新增失败！";
			  $this->error($error);
		  } 
	 }
	  else{
		
		  $this->meta_title="新增模板生成器";
		    cookie("__forward__",input('server.HTTP_REFERER'));
		  $this->assign('meta_title', $this->meta_title);
	      return $this->fetch("template/edit");
	  }
	}
	 /* 删除模板生成器*/
	public function del(){   
	    $id=input("id");
	    $map['id']=["in",$id];
		if(!$map["id"]){
			 $this->error("未选择数据！");
		}
		
		$res=db("template")->where($map)->delete();
		
		if($res){
		   addUserLog("del_template",session_uid());
		   $this->success("删除成功！");
		}else{
	           $this->error("删除失败！");
		}
	}
	
}
