<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head 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-knowledgeOss-add">
			<input type="hidden" id="menuId" name="menuId">
			<div class="form-group">	
				<label class="col-sm-3 control-label">文件名：</label>
				<div class="col-sm-8">
					<input id="fileName" name="fileName" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">文件后缀名：</label>
				<div class="col-sm-8">
					<input id="fileSuffix" name="fileSuffix" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">文件地址：</label>
				<div class="col-sm-8">
					<input id="url" name="url" class="form-control" required="required" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">菜单：</label>
				<div class="col-sm-8">
					<input id="menuName" name="menuName" class="form-control" onclick="menu()" required="required" readonly="readonly" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">文件来源：</label>
				<div class="col-sm-8">
					<!-- <input id="extend1" name="extend1" class="form-control" type="text"> -->
					<select id="extend1" class="form-control m-b" th:with="type=${@dict.getType('knowledge_source')}">
						<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
					</select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<!-- <input id="extend2" name="extend2" class="form-control" type="text"> -->
					<textarea id="extend2" name="extend2" class="form-control"></textarea>
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "knowledge/knowledgeOss";
		$("#form-knowledgeOss-add").validate({
			rules:{
				xxxx:{
					required:true,
				},
			}
		});

        function menu() {
            var menuId = 0;
            var url = ctx + "knowledge/knowledgeMenu/selectList/" + menuId;
            var options = {
                title: '选择菜单',
                width: "800",
                url: url,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            var data = body.find('.selected')[0];
            if(data != "undefined" && data != "" && data != null){
                $("#menuName").val(body.find('.selected')[0].children[2].innerHTML);
                var menuId = data.children[1].innerHTML;
                $("#menuId").val(menuId);
            }else{
                layer.msg("请选择一个菜单");
            }
            layer.close(index);
        }
		
		function submitHandler() {
	        if ($.validate.form()) {
	            //$.operate.save(prefix + "/add", $('#form-knowledgeOss-add').serialize());
                add();
	        }
	    }

        function add() {
            var menuId = $("input[name='menuId']").val();
            var fileName = $("input[name='fileName']").val();
            var fileSuffix = $("input[name='fileSuffix']").val();
            var url = $("input[name='url']").val();
            var extend1 = $("#extend1 option:selected").val();
            var extend2 = $("input[name='extend2']").val();

            var match = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
            //console.log(match.test(url));
            if(match.test(url)){
                $.ajax({
                    cache : true,
                    type : "POST",
                    url : prefix + "/add",
                    data : {
                        "menuId":menuId,
                        "fileName":fileName,
                        "fileSuffix":fileSuffix,
                        "url":url,
                        "extend1":extend1,
                        "extend2":extend2
                    },
                    async : false,
                    error : function(request) {
                        $.modal.alertError("系统错误");
                    },
                    success : function(data) {
                        $.operate.successCallback(data);
                    }
                });
			}else {
                layer.alert("请输入正确的url");
			}
        }
	</script>
</body>
</html>
