<link href="<?php echo asset_url('css/fileinput.min.css')?>" media="all" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo asset_url('js/fileinput.min.js') ?>"></script>
<script type="text/javascript" src="<?php echo asset_url('js/fileinput_locale_zh.js') ?>"></script>

<style type="text/css">
.form-group{width: 60%} 
#container_map{height: 300px}
.file-drop-disabled{clear: both;}
.kv-file-upload {display: none;}
.cursor{overflow: visible; position: absolute; z-index: 1000;  cursor: url("http://api.map.baidu.com/library/MarkerTool/1.2/src/images/transparent.cur"), default;}
</style>
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
<script src="http://cache.amap.com/lbs/static/es5.min.js"></script>
<script src="http://webapi.amap.com/maps?v=1.3&key=93cd0129276159ec3ff4c38a8831492a&plugin=AMap.Geocoder"></script>




<div class="col-md-10" role="main" id="bnb">
		<nav class="breadcrumb"></i> 五折卡模块<span class="c-gray en">&gt;</span>商家管理</nav>
		<div class="xgsj_wbox">											
			    <div class="title-box" style="border:0;padding: 20px 0px;">
			        <p class="t-p" style="width: 500px;display: inline-block;">修改商家</p>
			        <button type="button" onclick="javascript:history.go(-1)" class="sjxg_fh" data-dismiss="modal" style="margin-top:8px;">返回</button>
			    </div>
				<div class="bs-example">
					<form action="<?php echo site_url('admin/shop/store/'.$info['sid'])?>" method="post" class="registerForm regiswidth" enctype="multipart/form-data">
						<input name='loc' type="hidden" value="">
						<div class="form-group">
							<span class="dianm sjxg_tle_span">*店名：</span>
							<div class="examp">
								<input type="text" maxlength="15" value="<?php echo $info['name']?>" class="form-control" name="shopname" >				   
				            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
			                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
			                         <span style="font-size:12px;">提示：请输入不超过15个汉字含符号。</span>
                				</div>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<span class="dianm sjxg_tle_span">*标题：</span>
				            <div class="examp">
				            	<input type="text" name="title" value="<?php echo $info['title']?>" class="form-control" maxlength="20">
				            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
			                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
			                         <span style="font-size:12px;">提示：请输入店铺优惠标题，不超过20个汉字符号。</span>
                				</div>
				        	</div>
			        	</div>
			        	<div class="form-group form-phone">
					        <span class="dianm sjxg_tle_span">*商家电话：</span>
				            <div class="examp">
				            	<?php 
									
									foreach($info['tel'] as $k => $row){
				            	?>
				            	<div class="qusq_in_box">							
									<input type="text" value="<?php echo $row['tel']?>" name="phone[]" placeholder="0517-88532778" maxlength="13" required data-bv-notempty-message="电话号码不能为空">
									<div class="qusq_in_btn qusq_in_off"> <img src="<?php echo asset_url('images/lajitong.png');?>">
							        </div>
								</div>
								<?php }?>
				            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
			                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
			                         <span style="font-size:12px;">提示：最多填写5个电话号码。</span>
                				</div>
				            </div>
			       		</div>
			       		<div class="form-group" >
				            <span class="dianm sjxg_tle_span">*地址：</span>
				            <div class="examp">
				            	<input type="text" name="address" value="<?php echo $info['address']?>" maxlength="20" class="form-control">
				            	
				            	<div id="container_map"></div> 
				            	
				            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
			                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
			                         <span style="font-size:12px;">提示：请输入不超过20个汉字符号。如果地图地址显示不准确，单击地图重新标示。</span>
                				</div>
				        	</div>
			        	</div>
			        	<div class="form-group">
				        	<span class="jianjie sjxg_tle_span">*简介：</span>
				            <div class="examp">
				            	<textarea name="shop_des" class="form-control" id="shoptext" cols="30" maxlength="30" rows="3"><?php echo $info['shop_des']?></textarea>
				      
				            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
			                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
			                         <span style="font-size:12px;">提示：请输入店铺普通优惠简介，不超过30个汉字符号。</span>
                				</div>
				            </div>
			        	</div>
			        	<div class="form-group"  style="padding-bottom:15px">
				            <span class="classification jianjie sjxg_tle_span">*商家分类：</span>
				            <div class="examp">
				            	<select name="category" class="selectmr sjxg_selt" style="width:108px" name="country" data-bv-notempty data-bv-notempty-message="请选择一项" >
						           <?php foreach($category as $k => $v) { ?>
						           		<option <?php if($k == $info['category']) { ?> selected="selected" <?php } ?> value="<?php echo $k?>"><?php echo $v?></option>
						           <?php } ?>
						   
					            </select>
				            </div>
			        	</div>
		                <div class="form-group">
		                    <span class="classification jianjie sjxg_tle_span">区域/商圈：</span>
		                    <div class="examp">
		                        <select name="aid" class="selectmr sjxg_selt"  style="width:108px" data-bv-notempty data-bv-notempty-message="请选择一项" >
		                            <option value="0">全部</option>
		                        <?php foreach ($areaInfo  as $v):?>        
		                            <option <?php if($v['id'] == $info['aid']) { ?> selected="selected" <?php } ?> value="<?php echo $v['id']?>"><?php echo $v['area_name']?></option>
		                        <?php endforeach;?>
		                        </select>
		                    </div>
		                </div>            
			        	<div class="form-group form-group2">
				            <span class="smallimg jianjie sjxg_tle_span">*商家小图：</span>
				            <div class="examp" id="small_container">
				            	<p class="shaopupload sjxg_img_btn">
				            		<input type="file" name="avatar" id="small" multiple="" class="brefile">
					            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
				                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
				                         <span style="font-size:12px;">提示：商家列表展示图片，请上传202*148像素的图片。</span>
                					</div>
				            	</p>
				            </div>
			        	</div>
			        	<div class="form-group form-group2" style="width:60%">
				            <span class="cover jianjie sjxg_tle_span">*商家封面：</span>
				            <div class="examp" id="pic_container">
				            	<p class="shaopupload shaopupload1 sjxg_img_btn">
				            		<input type="file" name="avatar" id="pic" multiple="" class="brefile">					            	
					            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
				                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
				                         <span style="font-size:12px;">商家详情头部图片，请上传720*403像素的图片，最多上传5张图片。</span>
                					</div>
				            	</p>
				            </div>
				            
			        	</div>
			        	<div class="form-group form-group2" style="width:60%" id="message_container">
				            <span class="cover jianjie sjxg_tle_span">*详情图：</span>
				            <div class="examp">
				            	<p class="shaopupload shaopupload1 sjxg_img_btn">
				            		<input type="file" name="avatar" id="message_pic" multiple="" class="brefile">
			
					            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
				                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
				                         <span style="font-size:12px;">商家详情内容图片，请上传小于宽度670像素的图片，最多上传5张图片。</span>
                					</div>
				            	</p>
				            </div>
			        	</div>
			        	<div class="form-group form-group2" style="width:60%">
			            <span class="rule jianjie sjxg_tle_span">*商家详情及使用规则：</span>
			            <div class="examp">
			            	<textarea class="form-control" style="height:180px" id="summernote" name="message" cols="60"><?php echo $info['message']?></textarea>

			            	<div class="lr_ts" style="float:none;margin: 10px 0px;">
				                         <img src="<?php echo asset_url('images/tips.png');?>" alt="" style="width: 14px;">
				                         <span style="font-size:12px;">请输入商家的优惠详细内容及优惠使用时间、用法等详细规则。</span>
                			</div>
			            	
			            </div>
			            </div>
			            <button class="edit_btn" id="save" type="submit">保存修改</button>
		                    
		                
		                <?php foreach($info['top_pic'] as $k => $row) { ?>
		                	<input type="hidden" value="<?php echo $row['pic'].'|'.$row['width'].'|'.$row['height']?>" name="pics[]">
		                <?php } ?>
		                <?php foreach($info['message_pic'] as $k => $row) { ?>
		                	<input type="hidden" value="<?php echo $row['pic'].'|'.$row['width'].'|'.$row['height']?>" name="message_pics[]">
		                <?php } ?>
		                <?php foreach($info['pic'] as $k => $row) { ?>
		                	<input type="hidden" value="<?php echo $row['pic'].'|'.$row['width'].'|'.$row['height']?>" name="small_pics[]">
		                <?php } ?>
		            </form>    
		        </div>
        </div>
	</div>

	<script type="text/javascript">
		$(function(){
			// 添加电话 
			var addnum=$('.qusq_in_box').length;
			//初始化
			var obj = $('.qusq_in_box').last();
			obj.children('div').removeClass('qusq_in_off').addClass('qusq_in_add');
			obj.children('div').children('img').attr('src',"<?php echo asset_url('images/lALORKum1Cgo_40_40.png');?>");
			$(document).on('click', '.qusq_in_add', function(event) {
				if(addnum<5){
					var bqs='<div class="qusq_in_box"><input type="text" name="phone[]" required data-bv-notempty-message="电话号码不能为空" maxlength="13" placeholder="0517-88532778"><div class="qusq_in_btn qusq_ad_btn qusq_in_add"><img src="<?php echo asset_url("images/lALORKum1Cgo_40_40.png")?>"></div></div>';
					$(this).parent().after(bqs);
					$(this).find('img').attr('src','<?php echo asset_url("images/lajitong.png");?>');
					$(this).removeClass('qusq_in_add');
					$(this).addClass('qusq_in_off');
					addnum=addnum+1;
					$('.registerForm').bootstrapValidator('addField', 'phone[' + phonenum + ']', tel_valid)
				}
				
			})

			$(document).on('click', '.qusq_in_off', function(event) {
				$(this).parent().remove();
				addnum=addnum-1;
			})

		})	
	</script>
	
<script type="text/javascript">
jQuery(document).ready(function($) {
    $("button[type=button]").on('click', function(event) {
        event.preventDefault();
        /* Act on the event */
        location.href="javascript:history.go(-1)";
    });   
});
var small_files = 0;
var message_pic_files = 0;
var pic_files = 0;
function init_upload(target, min, max, name,initialPreview ,initialPreviewConfig,mode){

	var ele = $(target);
	var eee = ele.fileinput({
		minFileCount : min,
		dropZoneEnabled : false,
		maxFileCount : max, 
		validateInitialCount: true,
    	overwriteInitial: false,
		uploadUrl : '<?php echo site_url('admin/shop/upload');?>'+'/'+mode,
		language: "zh",
		showUpload : false,
		previewSettings : {
		    image: {width: "130px", height: "auto"},
		    html: {width: "213px", height: "160px"},
		    text: {width: "160px", height: "136px"},
		    video: {width: "213px", height: "160px"},
		    audio: {width: "213px", height: "80px"},
		    flash: {width: "213px", height: "160px"},
		    object: {width: "213px", height: "160px"},
		    other: {width: "160px", height: "160px"}
		},
		initialPreview: initialPreview,
		initialPreviewConfig: initialPreviewConfig,

	});


	ele.on('fileuploaded', function(event, data, previewId, index){ 

		var input = '<input type="hidden" value="'+data.response.url+'" name="'+name+'"/>'
		$(input).appendTo('form');
	});

	ele.on('filebatchselected', function(event, files, extra) {

			console.log($(this).fileinput('getFileStack').length);
			$("#save").attr('disabled', false);
	});



	ele.on('filedeleted', function(event, key) {
    	$("input[name='"+name+"']").eq(key).remove();
	});

}



function bd_decrypt( bd_lat,  bd_lon)
{
    x = bd_lon - 0.0065, y = bd_lat - 0.006;
    z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);

    gg_lon = z * Math.cos(theta);
    gg_lat = z * Math.sin(theta);
    console.log('d');
}


$(function(){

	var pic_initialPreview = [
				<?php foreach($info['top_pic'] as $row) { ?>
		    "<img src='<?php echo $row['pic']?>' class='file-preview-image' style='width:130px;height:auto' alt='<?php echo $row['id']?>' title='<?php echo $row['id']?>'>",
		    <?php } ?>
		];
	var message_initialPreview = [
				<?php foreach($info['message_pic'] as $row) { ?>
		    "<img src='<?php echo $row['pic']?>' class='file-preview-image' style='width:130px;height:auto' alt='<?php echo $row['id']?>' title='<?php echo $row['id']?>'>",
		    <?php } ?>
		];
	var small_initialPreview = [
				<?php foreach($info['pic'] as $row) { ?>
		    "<img src='<?php echo $row['pic']?>' class='file-preview-image' style='width:130px;height:auto' alt='<?php echo $row['id']?>' title='<?php echo $row['id']?>'>",
		    <?php } ?>
		];
	var pic_initialPreviewConfig = [
		<?php foreach($info['top_pic'] as $k => $row) { ?>
	        {caption: "<?php echo $row['id']?>", width: "130px", url: "<?php echo site_url('admin/shop/pic_delete');?>", key: <?php echo $k?>},
	<?php } ?>
	];
	var message_initialPreviewConfig = [
		<?php foreach($info['message_pic'] as $k => $row) { ?>
	        {caption: "<?php echo $row['id']?>", width: "130px", url: "<?php echo site_url('admin/shop/pic_delete');?>", key: <?php echo $k?>},
	<?php } ?>
	];
	var small_initialPreviewConfig = [
		<?php foreach($info['pic'] as $k => $row) { ?>
	        {caption: "<?php echo $row['id']?>", width: "130px", url: "<?php echo site_url('admin/shop/pic_delete');?>", key: <?php echo $k?>},
	<?php } ?>
	];
	init_upload('#pic', 0, 5, 'pics[]', pic_initialPreview,pic_initialPreviewConfig,'pic')
	init_upload('#small', 0, 1, 'small_pics[]',small_initialPreview,small_initialPreviewConfig,'small_pics')
	init_upload('#message_pic', 0, 5, 'message_pics[]',message_initialPreview, message_initialPreviewConfig,'message_pics')




  var map = new AMap.Map('container_map', {
        resizeEnable: true,
        zoom:15
    });



 	
    
   
    map.on('click', function(e) {
    	console.log(e.lnglat.getLng(), e.lnglat.getLat())
    	addMarker(e.lnglat.getLng(), e.lnglat.getLat())
   });

    addMarker(<?php echo $info['loc']?>);
    map.setFitView();



    



    var marker = null;
	function addMarker(x, y){
		if(marker){
    		marker.setMap(null);
    	}
		marker = new AMap.Marker({
			icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
			position: [x, y]
		});
        marker.setMap(map);
        // 设置label标签
	    marker.setLabel({//label默认蓝框白底左上角显示，样式className为：amap-marker-label
	        offset: new AMap.Pixel(20, -20),//修改label相对于maker的位置
	        content: $("input[name='shopname']").val()
	    });

	    $("input[name='loc']").val(x+','+y)

	}


	$("input[name='address']").focusout(function(){
		var address = $(this).val();

		if(address == ''){
			return false;
		}
		convertAddress(address)
		 
	})


	function convertAddress(address){

		var geocoder = new AMap.Geocoder({});
		geocoder.getLocation(address, function(status, result) {
            if (status === 'complete' && result.info === 'OK') {
            	var geocodes = result.geocodes
            	geocode = geocodes[0];
            	addMarker(geocode.location.getLng(),geocode.location.getLat())
    	      	map.setFitView();
            	
            }
        });

	}


     




})
	var tel_valid = {
            message: '电话号码不能为空',
            validators: {
                notEmpty: {
                    message: '电话号码不能为空',
                    container: '.qusq_in_box'
                },
                regexp: {
                    regexp: /(^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$)|(^(\d{3,4}|\d{3,4}-|\s)?\d{8}$)/i,
                    message: '电话号码不正确',

                }                    
            },
            group : '.qusq_in_box',
        };
	var tel_index = 0;
$(function(){

	$('.registerForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh',
            excluded : ':disabled'
        },
        
        
        fields: {
        	
        	shopname: {
                message: '商家名称无效',
                validators: {
                    notEmpty: {
                        message: '商家名不能是空的'
                    },
                    stringLength: {
                        max: 15,
                        message: '商家名不能大于15个字符'
                    }
                }
            },
            password1: {
                message: '密码无效',
                validators: {
                    notEmpty: {
                        message: '密码不能是空的'
                    },
                    stringLength: {
                    	min: 6,
                        max: 18,
                        message: '密码必须6-18位字符'
                    }
                }
            },
            password2: {
                message: '密码无效',
                validators: {
                    notEmpty: {
                        message: '密码不能是空的'
                    },
                    identical: {
                        field: 'password1',
                        message: '密码不相等'
                    }
                }
            },
            'phone[0]': tel_valid,
            address: {
                message: '地址无效',
                validators: {
                    notEmpty: {
                        message: '地址不能为空'
                    },
                    stringLength: {
                        max: 20,
                        message: '地址必须小于20字符'
                    }
                }
            },
            shop_des: {
                message: '商家简介无效',
                validators: {
                    notEmpty: {
                        message: '商家简介不能为空'
                    },
                    stringLength: {
                        max: 30,
                        message: '商家简介必须小于30字符'
                    }
                }
            },
            title  : {
                message: '标题无效',
                validators: {
                    notEmpty: {
                        message: '标题不能为空'
                    },
                    stringLength: {
                        max: 20,
                        message: '标题必须小于20字符'
                    }
                }
            }           
        }
    })
	.on('success.form.bv',function(e){
			e.preventDefault();
            $('textarea').each(function() {
                replace = $(this).val();

                replace=replace.replace(/\n/g,'<br/>');
                $(this).val(replace);
                
            });
			var small_num = $('#small_container').find('.file-preview-thumbnails .file-preview-frame').length;
			var pic_num = $('#pic_container').find('.file-preview-thumbnails .file-preview-frame').length;
			var message_num = $('#message_container').find('.file-preview-thumbnails .file-preview-frame').length;



			var loc = $("input[name='loc']").val();

			if(loc == ''){
				alert('未能自动识别地址，请在地图上标注');
				return false;
			}
			if(small_num < 1){
				alert('请上传商家小图');
				window.location.href = '#small_container'
				return false;
			}

			if(pic_num < 1){
				alert('请上传商家封面');
				window.location.href = '#pic_container'
				return false;
			}

			if(pic_num < 1){
				alert('请上传商家封面');
				window.location.href = '#pic_container'
				return false;
			}

			if(message_num < 1){
				alert('请上传详情图');
				window.location.href = '#message_container'
				return false;
			}

			var form = this;
			e.preventDefault();

			$('#message_pic').fileinput('getFileStack').length;
			$('#message_pic').on('filebatchuploadcomplete', function(event, files, extra) {
    			form_submit(form);
			});

			$('#small').on('filebatchuploadcomplete', function(event, files, extra) {
				form_submit(form);
			});
			

			$('#pic').on('filebatchuploadcomplete', function(event, files, extra) {
		    	form_submit(form);
			});

			$('#small').fileinput('upload');
			$('#pic').fileinput('upload');
			$('#message_pic').fileinput('upload');

			form_submit(form);

	})


	function form_submit(form){
		var message_pic_upload_num = $('#message_pic').fileinput('getFileStack').length;
		var small_upload_num = $('#small').fileinput('getFileStack').length;
		var pic_upload_num = $('#pic').fileinput('getFileStack').length;
		if(message_pic_upload_num == 0 && small_upload_num == 0 && pic_upload_num == 0){
			form.submit();
		}

	}
	
})

</script>


