/**
 * 日历插件JS配置 
 * 
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/ 
 * 
 * @author HuLimin http://blog.u-bo.com
 * @since 0.1
 */
$(document).ready(function() {
    //正整型数据判断
    var type = "^[0-9]*[1-9][0-9]*$";
    var reg = new RegExp(type);
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
    $('#calendar').fullCalendar({
        lazyFetching:false,//关闭缓存
        header: {
            left: 'prev,next,today', //左侧按钮
            center: 'title', //居中信息
            right: 'basicDay,basicWeek,month'//右侧按钮
        },
        timezone: 'local',
        firstDay: 1,
        height: '600', //日历高度
        lang: 'zh-cn',
        weekNumbers: true, //开启显示周次(一年中的第几周)
        editable: false,
        'minTime': '00:00:00',
//        axisFormat: 'H:mm',
        defaultView: 'basicWeek', //默认是month
        eventLimit: true, // allow "more" link when too many events
        businessHours: false,
        firstHour: 0,
        events: function(start, end, timezone, callback) {
            var query_type = $("#query_type").val();
            var query_value = $("#query_value").val();
            var start = dateTime = moment(start).format('YYYY-MM-DD');
            var end = dateTime = moment(end).format('YYYY-MM-DD');
            $.ajax({
                url: getDatasUrl,
                type: 'POST',
                dataType: 'JSON',
                data: {
                    start: start,
                    end: end,
                    _csrf: CSRF,
                    query_type: query_type,
                    query_value: query_value,
                },
                success: function(obj) {
                    callback(obj.data);
                }
            });
        },
        eventMouseover: function(event, jsEvent, view) {
            show(event, jsEvent, 1);
        },
        eventMouseout: function(event, jsEvent, view) {
            show(event, jsEvent, 0);
        },
        dayClick: function(date, jsEvent, view) {
            var dateTime;
            var h = moment(date).format('H');
            var query_value = $("#query_value").val();
            if (isNaN(query_value)) {//非数字
                query_value = 0;
            }
            if (h < 7) {
                dateTime = moment(date).format('YYYY-MM-DD 09:00');
            } else {
                dateTime = moment(date).format('YYYY-MM-DD H:mm');
            }
            if (limitDate == 0) {//不限制
                openDialog(clickUrl + "&date_time=" + dateTime, '', '', '添加');
                return false;
            }
            var time = new Date(y, m, d + 1);
            var delta = date.unix() - moment(time).unix();
            if (delta > 0 && limitDate > 0) {
                art.dialog.tips(tipsMessage, 1.5);
                return false;
            }

            delta = date.unix() - moment(new Date(y, m, d)).unix();
            if (delta < 0 && limitDate < 0) {
                art.dialog.tips(tipsMessage, 1.5);
                return false;
            }

            openDialog(clickUrl + "&date_time=" + dateTime + "&user_id=" + query_value, '', '', '添加');


        },
        eventClick: function(event, jsEvent, view) {
            if (event.view_url.length > 0) {
                openDialog(event.view_url, '', '', '查看/编辑');
            }
        },
        loading: function(bool) {
            $('#loading').toggle(bool);
        }
    });
    //弹窗
    function openDialog(url, width, height, title) {
        width = width || 600;
        height = height || 380;
        art.dialog.open(url, {
            id: 'main-dialog',
            title: title,
            lock: true,
            width: width,
            height: height,
            button: [
                {
                    name: '保存',
                    callback: function() {
                        var iframe = this.iframe.contentWindow;
                        if (!iframe.document.body) {
                            alert('正在加载中...')
                            return false;
                        }
                        iframe.$("#main-form").trigger('submit');
                        return false;
                    },
                    focus: true
                },
                {
                    name: '编辑',
                    callback: function() {
                        var iframe = this.iframe.contentWindow;
                        this.button({name: '删除', disabled: true}).lock();//锁定删除按钮
                        viewUrl = url.replace(/detail/g, 'view');
                        iframe.location = viewUrl;
                        return false;
                    }
                },
                {
                    name: '删除',
                    callback: function() {
                        var iframe = this.iframe.contentWindow;
                        deleteUrl = url.replace(/detail|view/g, 'delete');
                        $.get(deleteUrl, function(res) {
                            if (res.statue) {
                                art.dialog({
                                    title: '成功提示',
                                    time: 1.5,
                                    icon: 'succeed',
                                    content: res.info,
                                    close: function() {
                                        var opener = art.dialog.opener;
                                        var mainDialog = opener.art.dialog.list['main-dialog'];
                                        if (mainDialog != undefined) {
                                            mainDialog.close();
                                        }
                                        return true;
                                    }
                                });
                            } else {
                                art.dialog({
                                    title: '失败提示',
                                    time: 1.5,
                                    icon: 'error',
                                    content: res.info,
                                });
                            }
                        });
                        return false;
                    }
                }
            ],
            close: function() {
                $('#calendar').fullCalendar('refetchEvents');
            },
            cancel: true,
            cancelVal: '放弃'
        });
        return false;
    }

    /**
     * 鼠标滑过显示信息格式
     *
     * @param  obj
     * @param  e
     * @param  type     显示隐藏
     * @returns
     */
    function show(obj, e, type) {
        if (type) {
//            var workTime = new Date(parseInt(obj.work_time) * 1000).toLocaleString().substr(0, 17);
//<span style="color:#CC2104">姓名：' + obj.user_name + '</span><br />
            var paper_clip;
            if (obj.attach_ids) {
                paper_clip = 'icon-paper-clip';
            }
            var title = obj.title;
            if (obj.plan_title != undefined) {
                title = obj.plan_title;
            }
            if (obj.content != undefined) {
                var eInfo = "<i class='" + paper_clip + "'></i>" + title + '<br /><span style="color:#CC2104">关联：' + obj.link_name + '</span><br /><span class="calendar_span" style="color:#CC2104">内容：' + obj.content.replace("</?[^/?(br)|(p)][^><]*>", "").substr(0, 150) + '...</span>';
                $("#info").html(eInfo);
                $('#info').css({'top': (e.pageY), 'left': (e.pageX), 'display': 'block'});
            }
        } else {
            $("#info").hide();
        }
    }

});
