<?php 
	$this->display(tpl('head',true)); 
	$display[$edit['ad_type']]=" style='display: block'";
	$ad_type[$edit['ad_type']]="checked";
	$is_sell[intval($edit['is_sell'])]="checked";
	if(!$edit[id]){
		$display['text']=" style='display: block'";
		$ad_type['text']="checked";
	}else{
		$setting=unserialize($edit['setting']);
	}
	$time[$edit['time']]="selected";
?>

<style>
	.list{
		display: none;
	}
	.layui-input-block p{
		margin-top: 10px;
		color: #aaa;
	}
</style>
<script src="/Public/global/js/colpick.js" type="text/javascript" charset="utf-8"></script>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>创建/修改广告</legend>
</fieldset>
<form class="layui-form" method="post" action="">
	<div class="layui-form-item">
		<label class="layui-form-label">广告位名称</label>
		<div class="layui-input-block">
			<input name="post[name]" class="layui-input" type="text" value="<?php echo $edit['name'] ?>">
			<p>作为相关文章的索引,如多个用空格隔开</p>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">广告位标签</label>
		<div class="layui-input-block">
			<input name="post[tag]" class="layui-input" type="text" value="<?php echo $edit['tag'] ?>" <?php echo $edit ? 'readonly':''  ?> >
			<p>此值不可重复,添加后不可修改,不可大于32字符,不可为中文,不填写默认会生成.</p>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">广告位属性</label>
		<div class="layui-input-block">
			<input name="post[ad_type]" lay-filter="ad_type" class="layui-input" type="radio" value="text" title="文字广告位" <?php echo $ad_type['text'] ?> >
			<input name="post[ad_type]" lay-filter="ad_type" class="layui-input" type="radio" value="images" title="图片广告位"	<?php echo $ad_type['images'] ?> >
			<input name="post[ad_type]" lay-filter="ad_type" class="layui-input" type="radio" value="code" title="代码广告位" <?php echo $ad_type['code'] ?> >
		</div>
	</div>
	
	<div id="is_show">
		<div class="layui-form-item">
			<label class="layui-form-label">是否出售</label>
			<div class="layui-input-block">
				<input type="radio" name="post[is_sell]" {{$is_sell[0]}} value="0" title="不出售" />
				<input type="radio" name="post[is_sell]" {{$is_sell[1]}} value="1" title="出售" />
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">广告位价格
			<p style="margin-top: 10px;"><a style="background-color: #009688;color: #fff;padding: 2px 5px;border-radius: 5px;"  href="JavaScript:" onclick="add_price()">添加价格</a></p></label>
			<div class="layui-input-block" id="price_list">
				<?php 
					$price=unserialize($edit['price']);
					$pricenum=count($price['price']);
					$pricenum = $pricenum ? $pricenum  : 1;
					for ($i=0;$i<$pricenum;$i++){
				?>
				<li style="float: left;width: 100%;margin-bottom: 10px;" id="price_li_{{$i}}">
				<div class="left">
					<p style="float: left; margin-right: 10px;">价格</p>
					<input name="post[price][price][]" class="layui-input" style="width: 100px;" type="text" value="<?php echo $price['price'][$i] ?>">
				</div>
				<p style="float: left; margin-left: 10px;">元</p>
				<div class="left">
					<p style="float: left; margin-right: 10px; margin-left: 10px;">时间</p>
					<input name="post[price][time][]" class="layui-input" style="width: 100px;" type="text" value="<?php echo $price['time'][$i] ?>">
				</div>
				<p style="float: left; margin-left: 10px;">天</p>
				<p style="float: left; margin-left: 10px;"><a href="JavaScript:" onclick="del_price({{$i}})" style="background-color: #E6162D;color: #fff;padding: 2px 3px;border-radius: 5px;">删除</a></p>
				</li>
				<?php } ?>	
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">最大积分抵扣量</label>
			<div class="layui-input-block">
				<div class="left">
				<input name="post[integral]" class="layui-input" type="text" value="<?php echo $edit['integral'] ?>" >
				</div><p  class="left" style="margin-left: 10px;">为0则不可以积分抵扣,大于0需要在参数中心开启才可用</p>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">广告数量</label>
			<div class="layui-input-block">
				<div class="left">
				<input name="post[sellnum]" id="sellnum" class="layui-input" type="text" value="<?php echo $edit['sellnum'] ?>" >
				</div><p  class="left" style="margin-left: 10px;">个</p>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">模板代码</label>
			<div class="layui-input-block">
				<textarea class="layui-textarea" name="post[tpl]">{{$edit['tpl']}}</textarea>
				<p>文字广告位如:&lt;a href=&quot;网址&quot;&gt;标题&lt;/a&gt;</p>
				<p>图片广告位如:&lt;a href=&quot;网址&quot;&gt;&lt;img src=&quot;图片&quot;&gt;&lt;/a&gt; </p>
				<p>加上代码后,程序会自动替换，自动循环</p>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">广告说明</label>
			<div class="layui-input-block">
				<textarea class="layui-textarea" name="post[msg]">{{$edit['msg']}}</textarea>
				<p>广告说明，比如限制多少字以内，或者图片高宽</p>
			</div>
		</div>
	</div>
	
	<div class="list text" <?php echo $display['text'] ?> >
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
		  <legend>默认文字广告设置</legend>
		</fieldset>
		<div id="list_text">
			<?php
				$title_num= count($setting['text']['title']);
				$title_num = $title_num ? $title_num  : 1;
				for ($i=0;$i<$title_num;$i++){
			php?>
			<li>
				<div class="layui-form-item">
					<label class="layui-form-label">文字内容</label>
					<div class="layui-input-block">
						<input name="post[html][text][title][]" class="layui-input" type="text" value="<?php echo $setting['text']['title'][$i] ?>">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">文字链接地址</label>
					<div class="layui-input-block">
						<input name="post[html][text][weburl][]" class="layui-input" type="text" value="<?php echo $setting['text']['weburl'][$i] ?>">
					</div>
				</div>
			</li>
			<?php } php?>
		</div>
	</div>
	<div class="list code" <?php echo $display['code'] ?>>
		<div class="layui-form-item">
			<label class="layui-form-label">代码内容</label>
			<div class="layui-input-block">
				<textarea  name='post[html][code]' id="code" style="width: 100%; height: 200px;"><?php echo $setting['code'] ?></textarea>
			</div>
		</div>
	</div>
	<div class="list images" <?php echo $display['images']?>>
	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
	  <legend>默认图片广告设置</legend>
	</fieldset>
		<div id="list_images">
			<?php
				$images_num= count($setting['images']['imgurl']);
				$images_num = $images_num ? $images_num  : 1;
				for ($i=0;$i<$images_num;$i++){
			php?>
			<li>
				<div class="layui-form-item">
					<label class="layui-form-label">图片地址</label>
					<div class="layui-input-block">
						<input name="post[html][images][imgurl][]"  value="<?php echo $setting['images']['imgurl'][$i] ?>" id="img_url_{{$i}}" class="layui-input" type="text" style="width: 50%;float: left;">
						<button style="float: left; margin-left: 50px;" type="button" class="layui-btn layui-btn-normal" onclick="upload_click({{$i}})">选择图片上传</button>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">图片链接地址</label>
					<div class="layui-input-block">
						<input name="post[html][images][weburl][]" value="<?php echo $setting['images']['weburl'][$i] ?>" class="layui-input" type="text">
					</div>
				</div>
			</li>
			<?php } php?>
		</div>
		
	</div>
	<div class="layui-form-item">
		<div class="layui-input-block my_button">
			<input type="hidden" name="id" value="<?php echo $edit['id']?>" />
			<input type="hidden" name="step" value="post" />
			<button class="layui-btn" lay-submit="" lay-filter="post">立即提交</button>
		</div>
	</div>
	<div style="display: none;">
		<button type="button" id="select_img">上传监听按钮</button>
	</div>
</form>

<script>
var ad_type='<?php echo $edit['ad_type'] ? $edit['ad_type'] : 'text' ?>';
var pricenum ={{$pricenum}};
function add_price(){
	pricenum++;
	var html="";
	html+='<li style="float: left;width: 100%;margin-bottom: 10px;" id="price_li_'+pricenum+'">'
	html+='<div class="left">'
	html+='	<p style="float: left; margin-right: 10px;">价格</p><input name="post[price][price][]" class="layui-input" style="width: 100px;" type="text">'
	html+='</div>'
	html+='<p style="float: left; margin-left: 10px;">元</p>'
	html+='<div class="left">'
	html+='	<p style="float: left; margin-right: 10px; margin-left: 10px;">时间</p><input name="post[price][time][]" class="layui-input" style="width: 100px;" type="text">'
	html+='</div>'
	html+='<p style="float: left; margin-left: 10px;">天</p>'
	html+='<p style="float: left; margin-left: 10px;"><a href="JavaScript:" onclick="del_price('+pricenum+')" style="background-color: #E6162D;color: #fff;padding: 2px 3px;border-radius: 5px;">删除</a></p>'
	html+='</li>'
	$('#price_list').append(html);
}
function del_price(i){
	$('#price_li_'+i).remove();
}


$("#sellnum").focusout(function() {
	insert_html(this.value)
});

function insert_html(num){
	
	if(ad_type=='text'){
		var leng=$('#list_text li').length;
		if(leng > num){
			var _leng=  leng - num;
			for(var i=1;i <=_leng;i++){
				$('#list_text li')[leng - i].remove();
			}
			return;
		}
		
		for(var i=0;i<num-leng;i++){
			var html="";
			html+='<li><div class="layui-form-item">'
			html+='	<label class="layui-form-label">文字内容</label>'
			html+='	<div class="layui-input-block">'
			html+='		<input name="post[html][text][title][]" class="layui-input" type="text">'
			html+='	</div>'
			html+='</div>'
			html+='<div class="layui-form-item">'
			html+='	<label class="layui-form-label">文字链接地址</label>'
			html+='	<div class="layui-input-block">'
			html+='		<input name="post[html][text][weburl][]" class="layui-input" type="text">'
			html+='	</div>'
			html+='</div></li>'
			$('#list_text').append(html);
		}
	}else if(ad_type=='images'){
		
		var leng=$('#list_images li').length;
		if(leng > num){
			var _leng=  leng - num;
			for(var i=1;i <=_leng;i++){
				$('#list_images li')[leng - i].remove();
			}
			return;
		}
		
		
		for(var i=0;i<num-leng;i++){
			var ii=num-leng+i+1;
			var html="";
			html+='<li><div class="layui-form-item">'
			html+='	<label class="layui-form-label">图片地址</label>'
			html+='	<div class="layui-input-block">'
			html+='		<input name="post[html][images][imgurl][]"  id="img_url_'+ii+'" class="layui-input" type="text" style="width: 50%;float: left;">'
			html+='		<button style="float: left; margin-left: 50px;" type="button" class="layui-btn layui-btn-normal"  onclick="upload_click('+ii+')">选择图片上传</button>'
			html+='	</div>'
			html+='</div>'
			html+='<div class="layui-form-item">'
			html+='	<label class="layui-form-label">图片链接地址</label>'
			html+='	<div class="layui-input-block">'
			html+='		<input name="post[html][images][weburl][]" class="layui-input" type="text">'
			html+='	</div>'
			html+='</div></li>';
			$('#list_images').append(html);
		}
	}
	
}
if_html_show(ad_type)
function if_html_show(t){
	if(t=='text'){
		$('#is_show').show(500);
		$(".text").show(500);
		$(".images").hide(500);
		$(".code").hide(500);
		ad_type='text';
	}else if(t=='code'){
		$('#is_show').hide(500);
		$(".text").hide(500);
		$(".images").hide(500);
		$(".code").show(500);
		ad_type='';
	}else{
		ad_type='images';
		$('#is_show').show(500);
		$(".text").hide(500);
		$(".code").hide(500);
		$(".images").show(500);
	}
}
layui.config({
	version: '<?php echo NOWTIME ?>'
});
var upload;
var imgi;
layui.use(['upload','form'], function(){
  	var $ = layui.jquery;
  	upload = layui.upload;
	var form = layui.form;
	form.on('radio(ad_type)', function(data){
		if_html_show(data.value);
	});
	//选完文件后不自动上传
	upload.render({
		elem: '#select_img',
		url: '/admin/upfile/up',
	    auto: true,
	    before: function(obj){
	    	files = obj.pushFile(); //将每次选择的文件追加到文件队列
	    	loading = layer.load();
	    },
	    done: function(res, index, upload){
	    	layer.close(loading);
		    if(res.Result==0){
		    	layer.msg(res.content);
		    }else{
		      	$('#img_url_'+imgi).val(res.url); 
		    }
		    delete files[index]; //删除对应的文件
	    }
	});
});

	 function upload_click(i){
		 imgi=i;
		$('#select_img').click();
	 }
</script>
<?php 
$this->display(tpl('foot',true)); 
?>
