batchAdd.html 10.1 KB
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('下拉框')" />
	<th:block th:include="include :: select2-css" />
	<th:block th:include="include :: bootstrap-select-css" />
	<th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-menu-add" enctype="multipart/form-data">
<!--			<input id="treeId" name="parentId" type="hidden" th:value="${menu.menuId}" />-->
<!--			部门id-->
			<input name="orgId" type="hidden" id="treeId"/>

			<div class="form-group">
					<label class="col-sm-3 control-label"><span style="color: red; ">*</span>代下单用户部门:</label>
					<div class="col-sm-8">
						<div class="input-group">
							<input name="deptName" onclick="selectDeptTree()" id="treeName" type="text" placeholder="请选择归属部门" class="form-control" 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"><span style="color: red; ">*</span>代下单用户名:</label>
				<div class="col-sm-8">
					<select id="userId" name="userId"  class="form-control m-b" required>
						<option value="">请选择</option>
<!--						<option value="menuItem">页签</option>-->
<!--						<option value="menuBlank">新窗口</option>-->
					</select>
<!--					<span id="userWarm" style="color: red; display: none"> 请先选择用户部门</span>-->
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-3 control-label"><span style="color: red; ">*</span>委托类型:</label>
				<div class="col-sm-8">
					<select id="entrustType" name="entrustType" th:with="type=${@dict.getType('entrust_type')}" class="form-control m-b" required>
						<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"><span style="color: red; ">*</span>报告类型:</label>
				<div class="col-sm-8">
					<select id="reportType" name="reportType" th:with="type=${@dict.getType('background_report_type')}" class="form-control m-b" required>
						<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="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
			<!--上传文件-->
			<div class="form-group">
				<label class="col-sm-3 control-label"><span style="color: red; ">*</span>报告下单公司:</label>
				<div class="col-sm-8">
					<div class="fileinput fileinput-new input-group" data-provides="fileinput">
						<div class="form-control" data-trigger="fileinput"><i class="glyphicon glyphicon-file fileinput-exists"></i> <span class="fileinput-filename" id="fileName"></span></div>
						<span class="input-group-addon btn btn-white btn-file"><span class="fileinput-new">选择文件</span><span class="fileinput-exists">更改</span><input type="file" name="file" id="file"></span>
						<a href="#" class="input-group-addon btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
					</div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: select2-js" />
	<th:block th:include="include :: bootstrap-select-js" />
	<th:block th:include="include :: jasny-bootstrap-js" />
	<script>
        var prefix = ctx + "web/report";
		/**
		 * 提交表单,保存用户下单信息
		 */
        function submitHandler() {

			//代下单用户名
			let userName = $("#userId").find(":selected").text();
			//代下单用户id
			let userId = $("#userId").val();
			//代下单用户部门id
			let orgId = $("#treeId").val();
			//代下单用户部门名称
			let orgName = $("#treeName").val();
			//报告类型
			let reportType = $("#reportType").val();
			//报告名称
			let reportName = $("#reportType").find(":selected").text();
			//委托类型
			let entrustType = $("#entrustType").val();
			//备注
			let remark = $("#remark").val();

			var formData = new FormData();
			formData.append("file", $('#file')[0].files[0]);
			formData.append("userName",userName);
			formData.append("userId",userId);
			formData.append("orgId",orgId);
			formData.append("orgName",orgName);
			formData.append("reportType",reportType);
			formData.append("reportName",reportName);
			formData.append("entrustType",entrustType);
			formData.append("remark",remark);
			//需要校验 报告下单公司 ,其余字段 由jQuery Validation使用 $.validate.form() 校验
	        if ($.validate.form()) {
				$.ajax({
					url: prefix + "/backgroundBatchOrder",
					data: formData,
					cache: false,
					contentType: false,
					processData: false,
					type: 'POST',
					success: function (result) {
						if (result.code == web_status.SUCCESS) {
							$.modal.closeAll();
							$.modal.alertSuccess(result.msg);
							$.table.refresh();
						} else if (result.code == web_status.WARNING) {
							$.modal.enable();
							$.modal.alertWarning(result.msg)
						} else {
							$.modal.enable();
							$.modal.alertError(result.msg);
						}
					}
				});

	        }
	    }


		/**
		 * 委托类型,报告类型 销毁select2 直接下拉即可
		 */
		$(document).ready(function () {
			$("#entrustType").select2('destroy');
			$("#reportType").select2('destroy');
			// $("#entrustType").select2({
			// 	// placeholder: "请输入需要下单的公司",
			// 	searchable: false
			// });
		});


		/**
		 * 报告下单公司,select 模糊搜索(select2 + jQuery)
		 */
		$(document).ready(function () {
			$("#enterpriseName").select2({
				ajax: {
					url: ctx + "web/monitorCompany/getEnterpraseInfo",
					dataType: 'json',
					delay: 500, //延时500毫秒调用接口
					data: function (params) {
						let name = encodeURIComponent(params.term);
						console.log(name);
						return {
							name: name,
							//目前 pageNum,pageSize 后台没有接这两个参数, 天眼查接口默认返回20条数据
							pageNum: 1,
							pageSize: 10
						};
					},
					// 返回数据的回调函数
					processResults: function (data, params) {
						console.log(data);
						console.log(params);
						//解析返回数据
						let code = data.code;
						let content = data.data;
						// console.log(code)
						//定义一个空数组,用于保存 转换后的接口数据
						var result = [];
						//code为2000 数据正常返回
						if (code === '2000'){
							let items = content.items;
							//循环接口返回的数组,并创建一个新的对象,将数据转换成select2能识别的数据格式
							for (var item of items){
								var option = {
									id: item.creditCode,
									text: item.name
								}
								result.push(option);
							}
						//code为2010 接口查无数据
						}else if (code === '2010'){
							$.modal.alert("暂无数据");
						}else {
							$.modal.alertError("天眼查接口查询出错");
						}

						// 返回查询结果给selet2 ,注意:results为固定参数名
						return {
							results: result
						};
					},
					cache: true
				},
				placeholder: "请输入需要下单的公司",
				minimumInputLength: 1, //至少输入1个字符 触发搜索
				allowClear: true,
				//允许在结果中显示 html标签,
				escapeMarkup: function (markup) {
					// console.log(markup);
					return markup;
				},
				// 自定义结果显示样式(可选),其中data为上面自定的option对象,text是option的属性值
				templateResult: function(data) {
					return data.text;
				},
				// 自定义选择后显示的样式(可选),data.text表示:选中之后 select框中的显示的数据为 上面定义的option对象的text属性
				templateSelection: function(data) {
					return data.text;
				}
			});
		})

		/*用户管理-新增-选择部门树*/
		function selectDeptTree() {
			var treeId = $("#treeId").val();
			var deptId = $.common.isEmpty(treeId) ?"23" : $("#treeId").val();
			var url = ctx + "business/frontDept/selectDeptTree/" + deptId;
			console.log(url)
			var options = {
				title: '选择部门',
				width: "380",
				url: url,
				expandLevel: 2,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}

		function doSubmit(index, layero){
			var body = layer.getChildFrame('body', index);
			$("#treeId").val(body.find('#treeId').val());
			$("#treeName").val(body.find('#treeName').val());
			layer.close(index);
			//部门树回调结束,调用接口获取用户信息
			updateDept();
		}


		/**
		 * 代下单用户校验
		 */
		$('#userId').on('select2:open',function () {
			let deptId = $("#treeId").val();
			if (deptId === null || deptId === ''){
				// console.log(111111111111)
				// $("#userWarm").show();
				$.modal.alertError("请先选择部门")
				return;
			}
		});

		/**
		 * 根据 选定的部门id 查询对应部门下的用户,并设置 代下单用户的 option
		 */
		function updateDept() {

			let deptId = $("#treeId").val();
			// console.log(ctx + '/business/frontUser/getFrontUserByOrgId/')
			//调用后端接口,获取用户
			fetch(ctx + 'business/frontUser/getFrontUserByOrgId/' + deptId)
					.then(response => response.json())
					.then(data => {
						console.log(data);

						//先将 select标签置空,防止多次点击 累加option
						const userSelect = document.getElementById('userId');
						userSelect.innerHTML = '';
						data.data.forEach(option => {
							//拼接对应的 option
							console.log(option)
							//'<option value="'+ option.id+'"> '+ option.userName +' </option>'
							let optionElement = document.createElement('option');
							optionElement.textContent = option.userName;
							optionElement.value =  option.id
							userSelect.appendChild(optionElement);
						});

					})
					.catch(error => console.error("Error fetching options:" + error));
		}

    </script>
</body>
</html>