var userID = "";
var srvID = "";
var ifrm;
window.jQuery || document.write("<script src='../Scripts/BootStrap/js/jquery.min.js'>" + "<" + "/script>");
if ('ontouchstart' in document.documentElement) document.write("<script src='../Scripts/BootStrap/js/jquery.mobile.custom.min.js'>" + "<" + "/script>");
var ff;
jQuery(function ($) {
    $('#external-events div.external-event').each(function () {
        var eventObject = {
            title: $.trim($(this).text())
        };
        $(this).data('eventObject', eventObject);
        $(this).draggable({
            zIndex: 999,
            revert: true,
            revertDuration: 0
        });
    });
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
    LoadData(1, 1);
});
try { ace.settings.check('main-container', 'fixed') } catch (e) { }
function resize_r() {
    var s = 0;
    var ss = $(window.parent.document).find('iframe[id=' + ifrm + ']');
    var l = $(window.parent.document).height() - ss.offset().top;
    var t = $(document).height();
    if (l > t) {
        s = l;
    }
    else {
        s = $(document).height();
    }
    $($(window.parent.document).find('iframe[id=' + ifrm + ']')).height(s);
    window.parent.sidebar_r();
}
var opid;
function calendartoo(data) {
    var calendar = $('#calendar').fullCalendar({
        buttonText: {
            prev: '<i class="ace-icon fa fa-chevron-left"></i>',
            next: '<i class="ace-icon fa fa-chevron-right"></i>'
        },
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        events: data,
        editable: true,
        droppable: true,
        drop: function (date, allDay) {
            var originalEventObject = $(this).data('eventObject');
            var $extraEventClass = $(this).attr('data-class');
            var copiedEventObject = $.extend({}, originalEventObject);
            copiedEventObject.start = date;
            copiedEventObject.allDay = allDay;
            if ($extraEventClass) copiedEventObject['className'] = [$extraEventClass];
            $('#calendar').fullCalendar('renderEvent', copiedEventObject, true);
            if ($('#drop-remove').is(':checked')) {
                $(this).remove();
            }
        },
        selectable: true,
        selectHelper: true,
        select: function (start, end, allDay) {
            bootbox.prompt("添加备忘录：", function (title) {
                if (title !== null) {
                    calendar.fullCalendar('renderEvent',
            					{
            					    title: title,
            					    start: start,
            					    end: end,
            					    allDay: allDay
            					},
            					true
            				);
                    var ks = Todate(start);
                    var js = Todate(end);
                    var bz = title;
                    bz = encodeJson_News(bz);
                    var l = layer.load("正在提交...");
                    $.ajax({
                        type: "post",
                        url: "../Handlers/IndividualCenter/WorkingLog.aspx?Method=Add&srvid=" + srvID + "&userID=" + userID + "&ks=" + encodeURI(encodeURI(ks)) + "&js=" + encodeURI(encodeURI(js)) + "&bz=" + encodeURI(encodeURI(bz)),
                        cache: false,
                        success: function (msg) {
                            layer.close(l);
                            if (msg == "sucess") {
                                layer.msg('添加成功！', 3, 1);

                                location.reload();
                            }
                            else {
                                layer.close(l);
                                layer.msg('添加失败！', 3, 5);
                            }
                        },
                        error: function () {
                            layer.close(l);
                            layer.msg('网络请求错误!', 3, 5);
                        }
                    });
                }
            });
            calendar.fullCalendar('unselect');
        },
        eventClick: function (calEvent, jsEvent, view) {
            opid = calEvent.id;
            var modal =
            			'<div class="modal fade">\
            			  <div class="modal-dialog">\
            			   <div class="modal-content">\
            				 <div class="modal-body">\
            				   <button type="button" class="close" data-dismiss="modal" style="margin-top:-10px;">&times;</button>\
            				   <form class="no-margin">\
            					  <label>修改备忘录内容：</label>\
                                  <input class="middle" autocomplete="off" type="hidden" value="' + calEvent.id + '" style="width:200px;" />\
                                  <textarea id="bz" name="bz" class="middle" autocomplete="off" placeholder="请输入日志信息" style="width: 425px; height: 99px;" >' + calEvent.title + '</textarea><button type="submit" class="btn btn-sm btn-success"><i class="ace-icon fa fa-check"></i> 保存</button></form></div><div class="modal-footer"><button type="button" class="btn btn-sm btn-danger" data-action="delete" ><i class="ace-icon fa fa-trash-o"></i>删除</button><button type="button" class="btn btn-sm" data-dismiss="modal"><i class="ace-icon fa fa-times"></i>关闭</button></div></div></div></div>';
            var modal = $(modal).appendTo('body');
            modal.find('form').on('submit', function (ev) {
                ev.preventDefault();
                calEvent.title = $(this).find("textarea").val();
                calEvent.id = $(this).find("input[type=hidden]").val();
                calendar.fullCalendar('updateEvent', calEvent);
                calEvent.title = encodeJson_News(calEvent.title);
                $.ajax({
                    type: "post",
                    url: "../Handlers/IndividualCenter/WorkingLog.aspx?Method=Update&id=" + calEvent.id + "&bz=" + encodeURI(encodeURI(encodeJson_News(calEvent.title))),
                    cache: false,
                    success: function (msg) {
                        if (msg == "sucess") {
                            layer.msg('修改成功！', 3, 1);
                            location.reload();
                        }
                        else {
                            layer.msg('修改失败！', 3, 5);
                        }
                    },
                    error: function () {
                        layer.msg('网络请求错误!', 3, 5);
                    }
                });
                modal.modal("hide");
            });
            modal.find('button[data-action=delete]').on('click', function () {
                layer.confirm("确定删除吗？", function () {
                    $.ajax({
                        type: "post",
                        url: "../Handlers/IndividualCenter/WorkingLog.aspx?Method=Del&id=" + opid, //;ev._id,
                        cache: false,
                        success: function (msg) {
                            if (msg == "sucess") {
                                layer.msg('删除成功！', 3, 1);
                                location.reload();
                            }
                            else {
                                layer.msg('删除失败！', 3, 5);
                            }
                        },
                        error: function () {
                            layer.msg('网络请求错误!', 3, 5);
                        }
                    });
                });
                modal.modal("hide");
            });
            modal.modal('show').on('hidden', function () {
                modal.remove();
            });
        }
    });
}
function LoadData(pagesize, page) {
    var l = layer.load('正在加载数据......');
    var coks = document.cookie.substring(document.cookie.indexOf('userinf_tcwl'));
    var ary = coks.split(';')[0].split('=');
    if (!ary[2]) {
        $('html').empty();
        return;
    }
    userID = ary[2].split('&')[0];
    srvID = ary[3];
    var s = window.location.href.split('=');
    if (s.length == 4) {
        ifrm = s[3];
    }
    resize_r();
    var href = window.location.href.split("=");
    var mid = href[3];
    var url = "../Handlers/IndividualCenter/WorkingLog.aspx?Method=All&srvid=" + srvID + "&userID=" + userID + "&page=" + page + "&rows=" + pagesize + "";
    if (href.length > 3) {
        url += "&check=" + mid + "";
    }
    $.ajax({
        url: url,
        type: "post",
        cache: false,
        dataType: "json",
        success: function (data) {
            var dt = '';
            for (var i = 0; i < data.rows.length; i++) {
                if (i == data.rows.length - 1) {
                    var starttime = data.rows[i].StartTime;
                    var startnian = starttime.split('-')[0];
                    var startyue = starttime.split('-')[1];
                    var startri = starttime.split('-')[2];
                    var endtime = data.rows[i].EndTime;
                    var endnian = endtime.split('-')[0];
                    var endyue = endtime.split('-')[1];
                    var endri = endtime.split('-')[2];
                    dt += "{id:\"" + data.rows[i].ID + "\",title:\"" + data.rows[i].Content + "\",start:\"" + new Date(startnian + "," + startyue + "," + startri) + "\",end:\"" + new Date(endnian + "," + endyue + ", " + endri) + "\"}";
                } else {
                    var starttime = data.rows[i].StartTime;
                    var startnian = starttime.split('-')[0];
                    var startyue = starttime.split('-')[1];
                    var startri = starttime.split('-')[2];
                    var endtime = data.rows[i].EndTime;
                    var endnian = endtime.split('-')[0];
                    var endyue = endtime.split('-')[1];
                    var endri = endtime.split('-')[2];
                    dt += "{id:\"" + data.rows[i].ID + "\",title:\"" + data.rows[i].Content + "\",start:\"" + new Date(+startnian + "," + startyue + "," + startri) + "\",end: \"" + new Date(endnian + "," + endyue + "," + endri) + "\"},";
                }
            }
            ff = dt;
            layer.close(l);
            calendartoo(eval('[' + dt + ']'));
            resize_r();
        },
        error: function () {
            layer.msg('网络请求错误!', 10, 5);
        }
    });
}
function getCanldear() {
}
function Todate(num) {
    num = num + "";
    var date = "";
    var month = new Array();
    month["Jan"] = 1; month["Feb"] = 2; month["Mar"] = 3; month["Apr"] = 4;
    month["May"] = 5; month["Jun"] = 6; month["Jul"] = 7; month["Aug"] = 8;
    month["Sep"] = 9; month["Oct"] = 10; month["Nov"] = 11; month["Dec"] = 12;
    var week = new Array();
    week["Mon"] = "一"; week["Tue"] = "二"; week["Wed"] = "三"; week["Thu"] = "四";
    week["Fri"] = "五"; week["Sat"] = "六"; week["Sun"] = "日";
    str = num.split(" ");
    date = str[3] + "-";
    date = date + month[str[1]] + "-" + str[2];
    return date;
} 