<#include "/common/defaultEngine.html"/>
<@pageTheme mark="${config.optimize()?string('true', 'false')}">
<@header title="修改菜单" bodyClass="white-bg" libs=["bootstrapSelect"]/>
<div class="wrapper wrapper-content ibox-content">
<@f.form id="form-menu-edit" class="form-horizontal">
	<@f.hidden name="id" value="${menu.id?default('')}"/>
	<div class="form-group">
		<label class="col-sm-3 control-label "><span style="color: red; ">*</span><@ctx.i18n text = "上级菜单"/>：</label>
		<div class="col-sm-8">
			<@f.treeselect id="treeId" value="${menu.parentId}" name="parentId" boxTitle="上级菜单选择" url="sys/menu/menuTreeData"
			labelValue="${menu.parentName?default('主目录')}"/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><span style="color: red; ">*</span><@ctx.i18n text = "菜单类型"/>：</label>
		<div class="col-sm-8">
			<@f.radio name="type" dictType="sys_menu_type" value="${menu.type}"/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><span style="color: red; ">*</span><@ctx.i18n text = "菜单名称"/>：</label>
		<div class="col-sm-8">
			<input class="form-control" type="text" name="name" id="name" value="${menu.name?default("")}" required>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><span style="color: red; ">*</span><@ctx.i18n text = "所属模块"/>：</label>
		<div class="col-sm-8">
			<@f.checkbox name="moduleCodes" items=modules itemLabel="moduleName" value="${menu.moduleCodes}" itemValue="moduleCode" required=true/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><@ctx.i18n text = "请求地址"/>：</label>
		<div class="col-sm-8">
			<@f.input name="url" value="${menu.url?default('#')}" helpTip="Controller类中Mapping 如：@RequestMapping(\"/sys/demo\") 填写:sys/demo + 具体方法@GetMapping"/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><@ctx.i18n text = "目标"/>：
			<a tabindex="0" role="button" data-toggle="popover" data-trigger="focus" data-placement="bottom"
			   data-content="菜单已什么形式打开">
				<i class="fa fa-question-circle-o" style="color: orange"></i></a>
		</label>
		<div class="col-sm-8">
			<@f.select name="target" dictType="sys_menu_target" value="${menu.target?default('')}"/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><@ctx.i18n text = "权限标识"/>：
		</label>
		<div class="col-sm-8">
			<@f.input name="perms" value="${menu.perms?default('')}" helpTip="控制器中定义的权限标识，如：<@shiro.hasPermission name=\"sys:menu:add\">"/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><span style="color: red; ">*</span><@ctx.i18n text = "排序"/>：</label>
		<div class="col-sm-8">
			<@f.input name="orderNum" value="${menu.orderNum?default(0)}" required=true/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><@ctx.i18n text = "图标"/>：
		</label>
		<div class="col-sm-8">
			<@f.iconselect name="icon" placeholder="选择图标" value="${menu.icon?default('')}"/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><@ctx.i18n text = "是否显示(新)"/>：
		</label>
		<div class="col-sm-8">
			<@f.radio name="isNew" dictType="sys_yes_no" value="${menu.isNew?default('')}"/>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-3 control-label"><@ctx.i18n text = "菜单状态"/>：</label>
		<div class="col-sm-8">
			<@f.radio name="hide" value="${menu.hide?c}" dictType="sys_show_hide"/>
		</div>
	</div>
</@f.form>
<@footer>
 <script>
	var prefix = baseURL + "sys/menu";

	$(function() {
		var menuType = $('input[name="type"]:checked').val();
		menuVisible(menuType);
	});

	$("#form-menu-edit").validate({
		onkeyup: false,
		rules:{
			menuType:{
				required:true,
			},
			name:{
				remote: {
					url: prefix + "/checkMenuNameUnique",
					type: "POST",
					dataType: "html",
					data: {
						"id": function() {
							return $("#id").val();
						},
						"parentId": function() {
							return $("input[name='parentId']").val();
						},
						"name": function() {
							return opt.common.trim($("#name").val());
						}
					},
					dataFilter: function(data, type) {
						return opt.validate.unique(data);
					}
				}
			},
			orderNum:{
				digits:true
			},
		},
		messages: {
			"name": {
				remote: "菜单已经存在"
			}
		},
		focusCleanup: true
	});

	function submitHandler() {
		if (opt.validate.form()) {
			var data;
			if($('input[name="type"]:checked').val() === "1"){
				var options = {
					id:'form-menu-edit',
					del:['moduleCodes']/*需要删除提交表单数据*/
				};
				data = opt.form.dataUp(options);
			}else if($('input[name="type"]:checked').val() === "0"){
				var options = {
					id:'form-menu-edit',
					del:['perms','url','target','moduleCodes']/*需要删除提交表单数据*/
				};
				data = opt.form.dataUp(options);
			}else{
				var options = {
					id:'form-menu-edit',
					del:['url','target','icon','moduleCodes']/*需要删除提交表单数据*/
				};
				data = opt.form.dataUp(options);
			}
			var moduleCodes = "";
			$('input[name="moduleCodes"]:checked').each(function () {
				moduleCodes += $(this).val() + ",";
			})
			moduleCodes = moduleCodes.substring(0,moduleCodes.length-1);
			data.push({"name": "moduleCodes", "value": moduleCodes});
			//console.log(data);
			opt.operate.save(prefix + "/edit", data);
		}
	}
	function resultExe(r){
		if (r.code == opt.variable.web_status.SUCCESS) {
				opt.modal.success('操作成功', function(){
					//refreshMenu(); /*刷新菜单*/
					parent.$.treeTable.refresh();
					parent.opt.modal.closeAll();
				});
		} else if (r.code == opt.variable.web_status.WARNING) {
			opt.modal.warning(r.msg);
		}  else {
			opt.modal.error(r.msg);
		}
	}


	$(function() {
		$('input[name="type"]').on('ifChecked',
		function(event) {
			var menuType = $(event.target).val();
			menuVisible(menuType);
		});
	});

	function menuVisible(menuType) {
		if (menuType == "0") {
			$("#url").parents(".form-group").hide();
			$("#perms").parents(".form-group").hide();
			$("#target").parents(".form-group").hide();
			$("#icon").parents(".form-group").show();
			$("input[name='hide']").parents(".form-group").show();
			$("input[name='isNew']").parents(".form-group").show();
		} else if (menuType == "1") {
			$("#url").parents(".form-group").show();
			$("#target").parents(".form-group").show();
			$("#perms").parents(".form-group").show();
			$("#icon").parents(".form-group").show();
			$("input[name='hide']").parents(".form-group").show();
			$("input[name='isNew']").parents(".form-group").show();
		} else if (menuType == "2") {
			$("#url").parents(".form-group").hide();
			$("#perms").parents(".form-group").show();
			$("#icon").parents(".form-group").hide();
			$("#target").parents(".form-group").hide();
			$("input[name='hide']").parents(".form-group").hide();
			$("input[name='isNew']").parents(".form-group").hide();
		}
	}

</script>
</@footer>
</@pageTheme>
