layui.define(['tool'], function (exports) {
	const layer = layui.layer;
	const form = layui.form;
	const tool = layui.tool;
	const laydate = layui.laydate;
	const obj = {
		load: function (tid) {
			let callback = function (res) {
				if (res.code == 0 && res.data.length > 0) {
					let itemSchedule = '';
					$.each(res.data, function (index, item) {
						let createTime = new Date(item.create_time * 1000).toLocaleString();
						let lineClass = item.is_new == 1 ? 'unread' : 'isread';
						let readStatus = item.is_new == 1 ? '未读' : '已读';
						itemSchedule +=
							`
							
								
								
									${item.name}  在  ${createTime}  发布
								
								内容为:${item.title}
								补充描述:${item.remark}
								
							 
							`;
					});
					$("#schedule_" + tid).html(itemSchedule);
					layer.closeAll();
	
					//  添加点击事件以更改读取状态 
					$(".unread, .isread").on("click", function () {
						let id = $(this).data("id");
						let status = $(this).data("status");
						let newStatus = status == 1 ? 0 : 1;
						let newClass = newStatus == 1 ? 'unread' : 'isread';
						let newStatusText = newStatus == 1 ? '未读' : '已读';
						
						$(this).removeClass().addClass(newClass).text(newStatusText);
						
						$.ajax({
							url: "/api/schedule/update_status",
							method: "POST",
							data: {
								id: id,
								is_new: newStatus
							},
							success: function (res) {
								if (res.code == 0) {
									layer.msg("更新成功");
									location.reload();
								} else {
									layer.msg(res.msg);
								}
							},
							error: function () {
								layer.msg("更新失败");
							}
						});
					});
				}
			};
			tool.post("/api/schedule/get_list", { tid: tid }, callback);
		},
		//保存
		save: function (postData) {
			let callback = function (e) {
				if (e.code == 0) {
					layer.closeAll();
					layer.msg(e.msg);
					if (postData.id > 0) {
						layui.pageTable.reload();
					}
					else {
						tool.load('/task/index/view?id=' + postData.tid);
					}
				} else {
					layer.msg(e.msg);
				}
			}
			tool.post("/schedule/index/add", postData, callback);
		},
		//查看
		view: function (detail) {
			var content = `
							
								
									| 执行员工 | ${detail.name} | 所在部门 | ${detail.department} | 工作类型\ | ${detail.work_cate} | 
								
									| 工作内容 | ${detail.title} | 
								
									| 补充描述 | ${detail.remark} | 
							
							
								
								| 关联项目 | ${detail.project} | 
								
									| 关联任务 | ${detail.task} | 
							
						 `;
			layer.open({
				type: 1,
				title: '工作记录详情',
				area: ['800px', '432px'],
				content: content,
				success: function () {
				},
				btn: ['关闭'],
				btnAlign: 'c',
				yes: function (idx) {
					layer.close(idx);
				}
			})
		},
		//新增
		add: function (tid, schedule) {
			let that = this, detail = {}, title = '添加工作记录';
			if (tid == 0) {
				if(schedule['admin_id'] != GOUGU_DEV.uid){
					layer.msg('不能编辑他人的工作记录');
					return false;
				}
				title = '编辑工作记录';
				detail['id'] = schedule['id'];
				detail['tid'] = schedule['tid'];
				detail['title'] = schedule['title'];
				detail['remark'] = schedule['remark'];
			} else {
				detail['id'] = 0;
				detail['tid'] = tid;
				detail['title'] = '';
				detail['remark'] = '';
			}
			var content = ``;
			layer.open({
				type: 1,
				title: title,
				area: ['724px', '396px'],
				content: content,
				success: function () {
					form.render();
					$('[name="title"]').on('input', function () {
						var _val = $(this).val();
						detail.title = _val;
					});
					$('[form-input="remark"]').on('input', function () {
						var _val = $(this).val();
						detail.remark = _val;
					});
				},
				btn: ['确定提交'],
				btnAlign: 'c',
				yes: function (idx) {
					if (detail.title == '') {
						layer.msg('请填写工作内容');
						return;
					}
					that.save(detail);
					// 发送数据到钉钉机器人
					$.ajax({
						url: "/api/dingdingrobot/sendto",
						method: "POST",
						data: {
							message: "工作记录添加成功!"
						},
						success: function (res) {
							console.log(res);
						},
						error: function () {
							console.log("工作记录添加失败!");
						}
					});
				}
			})
		}
	};
	exports('gouguSchedule', obj);
});