<?php 
	$this->display(tpl('head',true));
?>
<style>
	.layui-table thead tr th{
		text-align: center;
	}
</style>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  	<legend>登陆方式管理</legend>
</fieldset>
<table class="layui-table layui-form" cellspacing="0" cellpadding="0" border="0" >
	<thead>
		<tr>
			<th width="100">平台名称</th>
			<th>平台描述</th>
			<th width="100">版本</th>
			<th width="100">图标</th>
			<th width="100">开关状态</th>
			<th width="130">操作</th>
		</tr>
	</thead>
	<tbody>
		<?php foreach($data AS $key=>$row) { ?>
		<tr>
			<td align="center"><?php echo $row['name'] ?></td>
			<td><?php echo $row['description'] ?></td>
			<td align="center"><?php echo $row['version'] ?></td>
			<td align="center" id="thumbin_<?php echo $row[id] ?>" lay-filter="thumb" lay-submit iid="<?php echo $row[id] ?>">
				<?php if( $row['thumb']) { ?>
					<img id="thumb_<?php echo $row[id] ?>" src="<?php echo get_img_url($row['thumb']) ?>" />
				<?php } ?>
			</td>
			<td align="center">
				<?php if($row[is_install]=='1') { ?>
				<input <?php echo $row['is_open'] ? 'checked' : '' ?> lay-skin="switch" lay-filter="state" id="<?php echo $row[id] ?>" lay-text="开启|关闭" type="checkbox" value="1">
				<?php } ?>
			</td>
			<td align="center">
				<?php if(!$row[is_install]) { ?>
					<a class="layui-btn layui-btn-sm layui-btn-warm" href="<?php echo admin_plugin_url ?>more/install/dir_<?php echo $row[initial_name] ?>">安装</a>
				<?php }else{ ?>
					<?php if($row['initial_name'] !='wechat' && $row[is_setting]=='1') { ?>
				<a class="layui-btn layui-btn-sm layui-btn-warm" href="<?php echo admin_plugin_url ?>more/edit/id_<?php echo $row[id] ?>">设置</a>
					<?php } ?>
				<a class="layui-btn layui-btn-sm layui-btn-danger" href="<?php echo admin_plugin_url ?>more/uninstall/id_<?php echo $row[id] ?>">卸载</a>
				
				<?php } ?>
			</td>
		</tr>
		<?php } ?>
	</tbody>
</table>

<table class="layui-table" id="upthumb" cellspacing="0" cellpadding="0" border="0" style="display: none;">
	<tbody>
		<tr>
			<td>图标地址</td>
			<td>
				<input id="upthumbinput" name="thumb" placeholder=""/>
				<button id="select_pic" lay-filter="select_pic">选择图标上传</button>
			</td>
		</tr>
		<tr>
			<td colspan="2" align="center">
				<button class="layui-btn layui-btn-sm"  lay-filter="postthumb" lay-submit>确定</button>
			</td>
		</tr>
	</tbody>
</table>
<script>
	var upid='';
	var index=Object;
	layui.use(['table','form', 'layer','upload'], function() {
		var table = layui.table,form=layui.form,layer = layui.layer,upload = layui.upload;
		form.on('switch(state)', function(obj) {
			if(obj.elem.checked == true) {
				var state = 1;
			} else {
				var state = 0;
			}
			edit_state({'is_open': state,'id': obj.elem.id,'json':true});

			if(obj.elem.checked == true) {
				layer.tips("已开启", obj.othis);
			} else {
				layer.tips("已关闭", obj.othis);
			}
		});
		index=form.on('submit(thumb)', function(data){
			upid=data.elem.attributes.iid.value;
			layer.open({
				title:'上传图标',
				area:['460px'],
			  	type: 1, 
			  	content: $('#upthumb'), 
			  	cancel: function(index, layero){ 
				  $('#upthumbinput').val('');
				}
			});
			return false; 
		});
		form.on('submit(postthumb)', function(data){
			var thumb= $('#upthumbinput').val();
			if(!thumb){
				layer.tips('图标地址不可为空','#upthumbinput',{tips: [1, '#3595CC'],time: 4000});
				return false;
			}

			$.ajax({
				url: "<?php echo  admin_plugin_url  ?>/more/edit_thumb/",
				type: 'POST',
				data:{'id':upid,'thumb':thumb,'json':true},
				dataType: 'json',
				success: function(res) {
					if(res.code != '0'){
						layer.alert(res.text);
						return false;
					}
					layer.closeAll();

					if($('#thumb_'+upid).length>0){
						$('#thumb_'+upid).attr('src',res.thumb);
					}else{
						var html="<img id='thumb_"+upid+"' src='"+res.thumb+"' />";
						console.log(html)
						$('#thumbin_'+upid).html(html);
					}
					$('#upthumbinput').val('');
					
				}
			})
			return false;
		});
		//选完文件后不自动上传
		upload.render({
		    elem: '#select_pic',
		    url: '/admin/upfile/up/json_true/',
		    auto: true,
		    bindAction: '#star_upload',
		    before: function(obj){
		    	files = obj.pushFile(); //将每次选择的文件追加到文件队列
		    	loading = layer.load();
		    },
		    done: function(res, index, upload){
		    	console.log(res);
		    	layer.close(loading);
			    if(res.Result==0){
			    	layer.msg(res.content);
			    }else{
			      	$('#upthumbinput').val(res.url); 
			    }
			    delete files[index]; //删除对应的文件
		    }
		});
	})
	
	function edit_state(data){
		$.ajax({url: "<?php echo  admin_plugin_url  ?>/more/edit_state/",type: 'POST',data:data,dataType: 'json',success: function(res) {if(res.code != '0'){layer.alert(res.text);}}})
	}
</script>
<?php 
	$this->display(tpl('foot',true));
?>
