<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<script src="../../../admin/index.js" door type="text/javascript" charset="utf-8"></script>
		<!--下拉选择模板-->
		<script type="text/html" id="selectTpl">
			<option value="">{{d.name}}</option>
		    {{# if(!d.list){d.list = []}}} {{# for(var i=0;i<d.list.length;i++){}} 
				<option value="{{d.list[i].id}}">{{d.list[i].name}}</option>
			{{#}}}
		</script>
	</head>

	<body>
		<form class="layui-form" action="" lay-filter="addForm" style="padding: 20px 30px 0 0">
			<div class="layui-form-item">
				<label class="layui-form-label ui-required">标题</label>
				<div class="layui-input-block">
					<input type="text" name="title" lay-verify="required" maxlength="50" placeholder="请输入标题" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label ui-required">公告</label>
				<div class="layui-input-block">
					<textarea class="layui-textarea" name="content" id="layedit_demo" style="display: none" placeholder="请输入文章内容"></textarea>
				</div>
			</div>
			<div class="layui-form-item layui-hide">
				<button class="layui-btn" lay-submit lay-filter="formSubmit" id="formSubmit">保存</button>
			</div>
		</form>
		<script type="text/javascript">
			layui.config({
				base: "../../../admin/js/layuiPlugins/"
			}).use(["form", 'layer', 'laytpl', 'layedit', ], function() {
				var form = layui.form,
					$ = layui.$,
					layer = layui.layer,
					laytpl = layui.laytpl,
					layedit = layui.layedit;
				var object = {
					id: (JSON.stringify(location.searchObj()) != "{}") ? location.searchObj().id || null : null, //带参传入ID
					currentData: {},
					urls: {
						formUrl: "mesFormUrl",
						byIdUrl: "mesByIdUrl",
					},
					formLayFilter: "addForm", //表单容器
					closeModal: function() { //关闭当前窗口
						parent.layer.close(parent.layer.getFrameIndex(window.name));
					}
				};

				//初始化
				object.main = function() {
					var _this = this;
					if (_this.id) {
						_this.getCurrent(_this.id);
					} else {
						_this.initialization();
					}

					form.on('submit(formSubmit)', function(obj) {
						return _this.mergeData(obj.field).submitInfo();
					});

					//插入图片设置
					layedit.set({
						uploadImage: {
							url: 'imgContentUpload', //接口url 
							type: 'post', //默认post
							accept: 'image',
							acceptMime: 'image/*',
							exts: 'jpg|png|gif|bmp|jpeg',
							size: '10240'
						},
						tool: [
							'strong', 'italic', 'underline', 'del', 'addhr' //添加水平线
							, '|', 'fontFomatt', 'colorpicker' //段落格式，字体颜色
							, 'face', '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'image_alt', 'altEdit', 'anchors' //锚点
							, '|', 'fullScreen'
						]
					});
					//构建一个默认的编辑器
					_this.layeditIndex = layedit.build('layedit_demo', {
						height: 700
					});

					return _this;
				};

				//初始化
				object.initialization = function() {
					var _this = this;
					return _this;
				};
				//合并表单内容
				object.mergeData = function(obj) {
					this.currentData = $.extend(this.currentData, obj);
					return this;
				};
				//获取当前信息
				object.getCurrent = function(id) {
					var _this = this;
					$.ajax({
						url: _this.urls.byIdUrl,
						data: {
							id: id || _this.id
						},
						success: function(res) {
							_this.currentData = res.data[0];
							$('.imgBox img').attr('src', window.hywa.config.href + '/' + res.data[0].imgUrl);
							_this.formVal();
						}
					});
					return this;
				};
				//表单赋值
				object.formVal = function() {
					var _this = this;
					form.val(_this.formLayFilter, _this.currentData);
					layedit.setContent(_this.layeditIndex, _this.currentData.content);
					return _this;
				};
				//提交新增修改
				object.submitInfo = function() {
					var _this = this;
					_this.currentData.content = layedit.getContent(_this.layeditIndex);
					let textData = layedit.getText(_this.layeditIndex);
					if (!(_this.currentData.content)) {
						parent.layer.msg('请编写消息内容', {
							icon: 7
						});
						return false;
					}
					if (textData.length > 1000) {
						parent.layer.msg('消息内容超长，最多1000字', {
							icon: 7
						});
						return false;
					}
					$.ajax({
						type: 'POST',
						url: _this.urls.formUrl,
						data: JSON.stringify(_this.currentData),
						contentType: 'application/json',
						success: function(res) {
							if (res.code == 0) {
								parent.layer.msg(res.msg, {
									icon: 1
								});
								parent.layui.table.reload('tableData');
								_this.closeModal();
							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						}
					});
					return false;
				};
				object.main();

				//验证规则
				form.verify({
					nickname: function(value) {
						if (!new RegExp("^[a-zA-Z][a-zA-Z0-9_]*$").test(value)) {
							return '用户名必须以英文字母开头，只能包含英文字母、数字、下划线';
						}
						if (/(^\_)|(\__)|(\_+$)/.test(value)) {
							return '用户名首尾不能出现下划线\'_\'';
						}
					}
				});
			});
		</script>
	</body>

</html>
