<?php include _include(ADMIN_PATH.'view/htm/header.inc.htm');?>

<div class="row">
	<div class="col-lg-10 mx-auto">
		<div class="card">
			<div class="card-body">
				<form action="<?php echo url('plugin-setting-xn_friendlink');?>" method="post" id="form">
					<h4 class="card-title">友情链接列表</h4>
					<div class="table-responsive arrlist">
						<table class="table" style="min-width: 800px">
							<thead>
								<tr align="center">
									<th width="80">ID</th>
									<th width="100">名称</th>
									<th>链接</th>
									<th width="100">排序</th>
									<th width="100" class="text-center">删除</th>
								</tr>
								<thead>
									<tbody>
										<?php foreach($linklist as $arr) { ?>
										<?php $rowid = $arr['linkid'];?>
										<tr align="center" rowid="<?php echo $rowid; ?>">
											<td><input type="text" class="form-control" name="rowid[<?php echo $rowid;?>]" value="<?php echo $rowid; ?>" placeholder="版块ID" /></td>
											<td><input type="text" class="form-control" name="name[<?php echo $rowid;?>]" value="<?php echo $arr['name']; ?>" placeholder="名称" /></td>
											<td><input type="text" class="form-control" name="url[<?php echo $rowid;?>]" value="<?php echo $arr['url']; ?>" placeholder="链接" /></td>
											<td><input type="text" class="form-control" name="rank[<?php echo $rowid;?>]" value="<?php echo $arr['rank']; ?>" placeholder="顺序" /></td>
											<td><a class="btn row_delete" role="btn">删除</a></td>
										</tr>
										<?php } ?>
										<tr align="center" rowid="<?php echo !empty($rowid) ? $rowid : 0; ?>" class="d-none">
											<td><input type="text" class="form-control" name="rowid[0]" value="" placeholder="ID" /></td>
											<td><input type="text" class="form-control" name="name[0]" value="" placeholder="名称" /></td>
											<td><input type="text" class="form-control" name="url[0]" value="" placeholder="链接" /></td>
											<td><input type="text" class="form-control" name="rank[0]" value="" placeholder="顺序" /></td>
											<td><a class="btn row_delete" role="btn">删除</a></td>
										</tr>
									</tbody>
						</table>
					</div>
					<p><a role="button" class="btn row_add">[+]增加一行</a></p>
					<p class="text-center">
						<button type="submit" class="btn btn-primary" id="submit" data-loading-text="<?php echo lang('submiting');?>..."><?php echo lang('confirm');?></button>
					</p>
				</form>
			</div>
		</div>
	</div>
</div>

<?php include _include(ADMIN_PATH.'view/htm/footer.inc.htm');?>

<script>
	var maxid = <?php echo $maxid;?>;

	var jform = $("#form");
	var jsubmit = $("#submit");
	var jarrlist = $('.arrlist');
	jform.base64_encode_file(); // 对文件进行 base64 编码，处理文件上传，很方便

	jform.on('submit', function() {
		jform.reset();
		var postdata = jform.serialize();
		jsubmit.button('loading');
		$.xpost(jform.attr('action'), postdata, function(code, message) {
			if (code == 0) {
				$.alert(message);
				jsubmit.text(message).delay(3000).location();
				return;
			} else {
				alert(message);
				jsubmit.button('reset');
			}
		});
		return false;
	});

	// 删除
	var jdelete = $('a.row_delete');
	jarrlist.on('click', 'a.row_delete', function() {
		var jthis = $(this);
		var jtr = jthis.parents('tr');
		var rowid = jtr.attr('rowid');
		if (jform.find('tbody').find('tr').length <= 2) {
			$.alert('不能删除系统保留的版块。');
			return;
		}
		jtr.remove();
		return false;
	});
	// 增加
	var jadd = $('a.row_add');
	jadd.on('click', function() {
		var jclone = jarrlist.find('tr').last().clone();
		jclone.insertAfter(jarrlist.find('tr').last()).removeClass('d-none');
		var jrowid = jclone.find('input[name^="rowid"]');
		//var rowid = xn.intval(jrowid.val()) + 1;
		var rowid = ++maxid;
		jrowid.val(rowid);
		jclone.attr('rowid', rowid);

		// 清空值
		jclone.find('input').not('[name^="rowid"]').val('');

		// 修改 [] 中的值为 rowid
		jclone.find('input').attr_name_index(rowid);

		return false;
	});

	$('#nav_pc li.plugin, #nav_mobile li.plugin').addClass('active');
</script>