{include file='public/meta'}
<link rel="stylesheet" href="__LIB__/layui/css/layui.css" media="all">
<body>
<article class="page-container">


	<form class="form form-horizontal" id="form-admin-add">
	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>标题</label>
		<div class="formControls col-xs-8 col-sm-9">
			<input type="text" class="input-text"  placeholder="" id="title" name="title"  >
		</div>
	</div>


	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3">位置</label>
		<div class="formControls col-xs-8 col-sm-9">
			<span class="select-box" style="width:150px;">
		         <select name='place' class='select'>
		           <?php foreach($places as $key=>$val){?>
		           <option value="<?php echo $val['id'];?>"  ><?php echo $val['name'];?></option>
		           <?php }?>
		         </select>
		    </span>
		</div>
	</div>

	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3">类型</label>
		<div class="formControls col-xs-8 col-sm-9">
			<div class="radio-box">
			<input name="display_type" type="radio" id="display_type-1"  value="1" >
			<label for="display_type-1">HTML</label>
			</div>
			<div class="radio-box">
				<input name="display_type" type="radio" id="display_type-2"  value="2"   >
				<label for="display_type-2">代码</label>
			</div>
			<div class="radio-box">
				<input name="display_type" type="radio" id="display_type-3"  value="3" checked=''  >
				<label for="display_type-3">图片</label>
			</div>
		</div>
	</div>

	

	<div class="row cl">
		<label class="form-label col-xs-4 col-sm-3">内容</label>
		<div class="formControls col-xs-8 col-sm-9">
			<div class='panel_form' id='html_form' style="display: none">
	           <script id="html_content" name="html_content" type="text/plain"></script>
        	</div>

        	<div class='panel_form' id='code_form' style="display: none" >
          		<textarea   name="code_content" style='height: 200px;width:400px'></textarea>
        	</div>

        	<div class='panel_form' id='image_form' style="display: block" >
	         <table class="layui-table" lay-size="sm">
	         <tr>
	           <th>图片地址</th>
	           <th>链接地址</th>
	           <th>标题</th>
	           <th><h4 style="font-weight:bold;color:blue;font-size: 1.8em" class="item-add" >+</h4></th>
	         </tr>
	         <tbody id='items'>
	           <tr>
	            <td style="vertical-align: top;">
	              <input type="button"  class="layui-input layui-btn-mini btn-upload" value="上传" id='upload_0' >
	              <span class="hidespan"></span>
	            </td>
	             <td style="vertical-align: top;" ><input type="text" name="bannerurl[]" class="layui-input" ></td>
	             <td style="vertical-align: top;"><input type="text" name="banner_title[]" class="layui-input" ></td>
	             <td style="vertical-align: top;"><h4 style="font-weight:bold;color:red;font-size: 1.8em" class="item-add" onclick="del_item(this)">-</h4></td>
	           </tr>
	         </tbody>
	        </table>
	        </div>

		</div>
	</div>
	
	<input type="hidden" name="id" value="<?php echo intval($row['id']);?>">
	
	<div class="row cl">
		<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
			<input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
		</div>
	</div>

	</form>
</article>

{include file='public/pfooter'}

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__LIB__/layui/layui.js"></script>
<script type="text/javascript">
$(function(){
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});
	
	$("#form-admin-add").validate({
		rules:{
			title:{
				required:true,
			},
		},
		onkeyup:false,
		focusCleanup:true,
		success:"valid",
		submitHandler:function(form){
			$(form).ajaxSubmit({
				type: 'post',
				url: "<?php echo url('admin/adv/publish');?>" ,
				success: function(data){
					layer.msg(data.msg,{icon:1,time:1000});
					if(0==data.error){
						setTimeout(function(){
							location.href="<?php echo url('admin/adv/index');?>";
						},1000)
					}
				},
                error: function(XmlHttpRequest, textStatus, errorThrown){
					layer.msg('服务器端错误',{icon:1,time:1000});
				}
			});
		}
	});

	$("input:radio[name='display_type']").on('click',function(){
		var data= $(this).val();
		console.log(data);
		$('.panel_form').each(function(){
		 	$(this).hide();
		})
		if(1==data){
			$('#html_form').show();
		}
		if(2==data){
			$('#code_form').show();
		}
		if(3==data){
			$('#image_form').show();
		}
	})
});
</script> 
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>

<script type="text/javascript" src="__LIB__/ueditor/1.4.3/ueditor.config.js"></script> 
<script type="text/javascript" src="__LIB__/ueditor/1.4.3/ueditor.all.min.js"> </script> 
<script type="text/javascript" src="__LIB__/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var ue = UE.getEditor('html_content',{
      initialFrameHeight:300
    });

	function del_item(obj){
	   $(obj).parents('tr').remove();
	}


	function createitem(index){
	   var html = '<tr>'+
	        '<td style="vertical-align: top;">'+
	        '<input type="button"  class="layui-input layui-btn-mini btn-upload"  value="上传" id="upload_'+index+'">'+
	        '<span class="hidespan"></span>'+
	        '</td>'+
	        '<td style="vertical-align: top;"><input type="text" name="bannerurl[]" class="layui-input" ></td>'+
	        '<td style="vertical-align: top;"><input type="text" name="banner_title[]" class="layui-input" ></td>'+
	        '<td style="vertical-align: top;"><h4 style="font-weight:bold;color:red;font-size: 1.8em" class="item-add" onclick="del_item(this)">-</h4></td>'+
	      '</tr>';
	  return html;
	}
</script>

<script>
  var sort =1;
  layui.use('upload', function(){
    var upload = layui.upload;
    picupload('#upload_0');
    function picupload(id){
        upload.render({
            elem:  id
            ,url: "{:url('common/upload',['atype'=>'adv'])}" //上传接口
            ,done: function(res,index,upload){
              var obj = this.item;
              console.log(obj);
              //上传完毕回调
              if(0==res.error) {
                $(obj).siblings('.hidespan').empty().append("<img src='"+res.src+"' />").append("<input  type='hidden'  name='banner[]'  value='"+res.id+"' />");
                } else {
                  layer.msg(res.msg);
                }
            }
        });
    }

    $(document).on('click', '.item-add', function() {
      //添加元素
      var item = createitem(sort);
      $('#items').append(item);
      picupload('#upload_'+sort);
      sort +=1;
    })
  })
  
</script>