<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('新增类目')" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-dept-add">
			<input id="treeId" name="parentCategoryId" type="hidden" th:value="${goodsCategory?.goodsCategoryId}"   />
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">上级类目：</label>
				<div class="col-sm-8">
				    <div class="input-group">
						<input class="form-control" type="text" onclick="selectDeptTree()" id="treeName" readonly="true" th:value="${goodsCategory?.goodsCategoryName}" required>
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
				    </div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">类目名称：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="goodsCategoryName" id="goodsCategoryName" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label is-required">显示排序：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="categorySort" required>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目类别：</label>
				<div class="col-sm-8">
					<div class="radio-box" th:each="dict : ${@dict.getType('goods_category_type')}">
						<input type="radio" th:id="${dict.dictCode}" name="type" th:value="${dict.dictValue}" th:checked="${dict.default}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">类目状态：</label>
				<div class="col-sm-8">
				    <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
						<input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
						<label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-sm-12">
					<div class="form-group">
						<label class="col-xs-2 control-label">保险设定：</label>
						<div class="col-xs-10">
							<label th:each="insurance:${insurances}" class="check-box">
								<input name="insurance" type="checkbox" th:value="${insurance.id}" th:text="${insurance.companyName}">
							</label>
						</div>
					</div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<script type="text/javascript">
		var prefix = ctx + "goods/category";
		
		$("#form-dept-add").validate({
			onkeyup: false,
			rules:{
				deptName:{
					remote: {
		                url: prefix + "/checkGoodsCategoryNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
		                	"parentCategoryId": function() {
		                		return $("input[name='parentCategoryId']").val();
		                    },
		                	"goodsCategoryName" : function() {
		                        return $.common.trim($("#goodsCategoryName").val());
		                    }
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				categorySort:{
					digits:true
				}
			},
			messages: {
		        "goodsCategoryName": {
		            remote: "类目已经存在"
		        }
		    },
		    focusCleanup: true
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	// 方法一：使用saveModal，新增成功后不刷新表格，只显示提示信息
	        	//$.operate.saveModal(prefix + "/add", $('#form-dept-add').serialize());
	        	
	        	// 方法二：使用自定义Ajax保存（如果需要更多控制）
	        	 customSave();
	        }
	    }
	    
	    // 自定义保存方法（可选）
	    function customSave() {
	        $.ajax({
	            url: prefix + "/add",
	            type: "post",
	            dataType: "json",
	            data: $('#form-dept-add').serialize(),
	            beforeSend: function () {
	                $.modal.loading("正在处理中，请稍候...");
	            },
	            success: function(result) {
	                $.modal.closeLoading();
	                if (result.code == web_status.SUCCESS) {
	                    $.modal.alertSuccess(result.msg);
	                    // 如果需要关闭窗口，取消注释下面这行
	                     $.modal.close();
	                } else if (result.code == web_status.WARNING) {
	                    $.modal.alertWarning(result.msg);
	                } else {
	                    $.modal.alertError(result.msg);
	                }
	            }
	        });
	    }
	
		/*部门管理-新增-选择父部门树*/
		function selectDeptTree() {
			var treeId = $("#treeId").val();
			if ($.common.isEmpty(treeId)) {
       			$.modal.alertWarning("请先添加用户所属的类目！");
       			return;
       		}
			var options = {
				title: '类目选择',
				width: "380",
				url: prefix + "/selectGoodsCategoryTree/" + treeId,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		}
	</script>
</body>
</html>
