layui.define(['tool','editormd'], function (exports) {
	const layer = layui.layer, tool = layui.tool,editor = layui.editormd;
	const obj = {
		addLink: function (id, topic_id, module, url, desc) {
			let that = this;
			layer.open({
				title: '添加链接',
				type: 1,
				area: ['580px', '240px'],
				content: '
',
				btnAlign: 'c',
				btn: ['提交发布'],
				yes: function () {
					let callback = function (e) {
						if(e.code==0){
							layer.closeAll();
							layer.msg(e.msg);
							if(module == 'project'){
								setTimeout(function(){
									location.reload();
								},2000)
							}
							else{
								tool.load('/' + module + '/index/view/id/' + topic_id);
							}
						}
						else{
							layer.msg(e.msg);
						}
					}
					let url = $('#box_url').val();
					let desc = $('#box_desc').val();
					if (url == '') {
						layer.msg('请输入URL');
						return false;
					}
					if (desc == '') {
						layer.msg('请输入链接说明');
						return false;
					}
					let postData = { id: id, topic_id: topic_id, module: module, url: url, desc: desc };
					tool.post("/api/appendix/add_link", postData, callback);
				}
			})
		},
		log: function (topic_id, module) {
			let callback = function (res) {
				if (res.code == 0 && res.data.length > 0) {
					let itemLog = '';
					$.each(res.data, function (index, item) {
						if (item.field == 'content') {
							itemLog += `
							
								
								${item.name}
								 ${item.action}了${item.title} ${item.times}
							
						`;
						}
						else if (item.field == 'file' || item.field == 'link' || item.field == 'user') {
							itemLog += `
								
									
									${item.name}
									 ${item.action}了${item.title}${item.new_content}${item.times}
								
							`;
						} else if (item.field == 'new' || item.field == 'delete') {
							itemLog += `
								
									
									${item.name}
									 ${item.action}了${item.title}${item.times}
								
							`;
						}
						else if (item.field == 'document') {
							if (item.action == '修改') {
								itemLog += `
									
										
										${item.name}
										 ${item.action}了${item.title}${item.remark} ${item.times}
									
								`;
							}
							else {
								itemLog += `
									
										
										${item.name}
										 ${item.action}了${item.title}${item.remark}${item.times}
									
								`;
							}
						}
						else {
							itemLog += `
							
								
								${item.name}
								 将${item.title}从 ${item.old_content} ${item.action}为${item.new_content}${item.times}
							
						`;
						}
					});
					$("#log_" + module + "_" + topic_id).html(itemLog);
				}
			}
			tool.get("/api/log/get_list", { tid: topic_id, m: module }, callback);
		},
		load: function (topic_id, module) {
			let callback = function (res) {
				if (res.code == 0 && res.data.length > 0) {
					let itemComment = '';
					$.each(res.data, function (index, item) {
						let pAdmin = '', ops = '';
						if (item.padmin_id > 0) {
							pAdmin = '@' + item.pname + '
';
						}
						if (item.admin_id == GOUGU_DEV.uid) {
							ops = '编辑删除';
						}
						itemComment += `
							
						`;
					});
					$("#comment_" + module + "_" + topic_id).html(itemComment);
					layer.closeAll();
				}
			}
			tool.post("/api/comment/get_list", { tid: topic_id, m: module }, callback);
		},
		add: function (id, topic_id, pid, padmin_id, module, content, md_content) {
			let that = this;
			let callback = function (res) {
				that.load(topic_id, module);
			}
			if (content == '') {
				layer.msg('请完善评论内容');
				return false;
			}
			let postData = { id: id, topic_id: topic_id, pid: pid, padmin_id: padmin_id, module: module, content: content, md_content: md_content };
			tool.post("/api/comment/add", postData, callback);
		},
		del: function (id, topic_id, module) {
			let that = this;
			layer.confirm('确定删除该评论吗?', {
				icon: 3,
				title: '提示'
			}, function (index) {
				let callback = function (e) {
					layer.msg(e.msg);
					if (e.code == 0) {
						that.load(topic_id, module);
					}
				}
				tool.delete("/api/comment/delete", { id: id }, callback);
				layer.close(index);
			});
		},
		//编辑器
		editor: function (id, topic_id, pid, padmin_id, module, txt) {
			let that = this,edit;
			layer.open({
				closeBtn: 2,
				title: false,
				type: 1,
				area: ['730px', '360px'],
				content: '',
				success: function () {
					edit = editor.render('editorBox', {
						markdown: txt,
						imageUploadURL: "/api/index/md_upload",
						lineNumbers: false,
						watch: false,
						toolbarIcons: function () {
							return [
								"undo", "redo", "bold", "del", "italic", "quote", "h3",
								"list-ul", "list-ol", "hr", "link", "reference-link", "image", "code", "table", "watch"
							];
						},
						height: 300,
						width:725,
					});
				},
				btnAlign: 'c',
				btn: ['提交发布'],
				yes: function () {
					that.add(id, topic_id, pid, padmin_id, module, edit.getHTML(), edit.getMarkdown());
				}
			})
		},
		// 添加联系人
		addContact:function (business_id,module, contact, post, phone, email, wechat,qq) {
			let that = this;
			layer.open({
				title: '添加联系人',
				type: 1,
				area: ['580px', '580px'],
				content: '',
				btnAlign: 'c',
				btn: ['提交发布'],
				yes: function () {
					let callback = function (a) {
						layer.closeAll();
						layer.msg(a.msg);
						tool.load("/business/index/view?id=" + business_id);
					}
					let contact = $('#box_contact').val();
					let post = $('#box_post').val();
					let phone = $('#box_phone').val();
					let email = $('#box_email').val();
					let wechat = $('#box_wechat').val();
					let qq = $('#box_qq').val();
					if (contact == '') {
						layer.msg('请输入姓名');
						return false;
					}
					if (post == '') {
						layer.msg('请输入职位');
						return false;
					}
					let postData = {  module: module,business_id:business_id, contact: contact, post: post, phone: phone, email: email, wechat: wechat, qq: qq };
					tool.post("/api/business/add_contact", postData, callback);
				}
			})
		},
		//编辑联系人
		editContact:function (id,topic_id,business_id,module, contact, post, phone, email, wechat,qq) {
			let that = this;
			layer.open({
				title: '编辑联系人',
				type: 1,
				area: ['580px', '580px'],
				content: '',
				btnAlign: 'c',
				btn: ['提交修改'],
				yes: function () {
					let contact = $('#box_contact').val();
					let post = $('#box_post').val();
					let phone = $('#box_phone').val();
					let email = $('#box_email').val();
					let wechat = $('#box_wechat').val();
					let qq = $('#box_qq').val();
					if (contact == '') {
						layer.msg('请输入姓名');
						return false;
					}
					if (post == '') {
						layer.msg('请输入职位');
						return false;
					}
					let callback = function (e) {
						layer.msg(e.msg);
						tool.load("/business/index/view?id=" + business_id);
					}
					let postData = { id:id, module: module,business_id:business_id, contact: contact, post: post, phone: phone, email: email, wechat: wechat, qq: qq };
					tool.post("/api/business/edit_contact", postData, callback);
					console.log(postData)
					layer.closeAll();
				}
			})
		},
		//删除联系人
		delContact:function (id, topic_id, module) {
			let that = this;
			layer.confirm('确定删除该联系人吗?', {
				icon: 3,
				title: '提示'
			}, function (index) {
				let callback = function (e) {
					layer.msg(e.msg);
					if (e.code == 0) {
						that.load(topic_id, module);
					}
				}
				tool.delete("/api/business/del_contact", { id: id }, callback);
				layer.close(index);
			});
		}
	};
	exports('gouguComment', obj);
});