/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var board = function () {

    var hosturl = $('#hosturl').val();

    var common = function ()
    {
        $('.update').on('change', function ()
        {
            var id = $('#requestId').val();
            var value = $(this).val();
            var colName = $(this).attr("name");
            var tableName = $(this).attr("data-tableName");
            var control = $(this).attr("data-control");

            $.post(hosturl + '/controllers/' + control + '.php?ac=update', {id: id, value: value, tableName: tableName, colName: colName}, function (res)
            {
            });

        });
        $('.cancel').on('click', function ()
        {
            var controllName = $(this).attr("data-controll");
            var doName = $(this).attr("data-do");
            window.location = hosturl + "/controllers/" + controllName + ".php?ac=" + doName + " ";
        });
    }


    var request = function ()
    {
        $('.requestDetails').on('click', function ()
        {
            var requestId = $(this).attr("data-requestid");
            var archived = $(this).attr("data-archived");
            if (archived == 0)
            {
                $.post(hosturl + '/controllers/requests.php?ac=requestDetails', {requestId: requestId}, function (res)
                {
                    $('#details').html(res);
                    $.post(hosturl + '/controllers/requests.php?ac=requestcomments', {requestId: requestId}, function (res)
                    {
                        $('#comments').html(res);
                        $('#request_div').html("");
                        $("#request_model").modal('show');
                    });
                });
            }
            else
            {
                $.post(hosturl + '/controllers/requests.php?ac=requestDetailsArchive', {requestId: requestId}, function (res)
                {
                    $('#request_div').html(res);
                    $("#request_model_archive").modal('show');
                });

            }

        });

        $('#model_close').on('click', function ()
        {
            $('#dcomm').val("");
        });


        $('#xxx').on('click', '#add_comment', function ()
        {
            var comment = $('#dcomm').val();
            var requestId = $('#requestId').val();
            var loginUserName = $('#loginUserName').val();
            var userId = $('#userId').val();
            var related = 0;
            if (comment != "")
            {
                $.post(hosturl + '/controllers/requests.php?ac=addcomment', {comment: comment, related: related, requestId: requestId}, function (res)
                {
                    $('#comments').append('<div class="media" id="comment' + res + '">'
                            + '<a href="javascript:;" class="pull-left blogpapa"></a>'
                            + '<div class="media-body">'
                            + '<h4 class="media-heading bold">' + loginUserName + ''
                            + '<span class="replayrecomment">'
                            + '<label  data-commentId="' + res + '" class="reply"> Reply </label>'
                            + '</span>'
                            + '</h4>'
                            + '<p>' + comment
                            + '<span class="replayrecomment">'
                            + '<label  data-commentId="' + res + '" class="delete"> delete </label>'
                            + '</span>'

                            + '</p><hr>'

                            + '<div id="relatedComments' + res + '">'
                            + '<div id="relatedComments_input' + res + '"></div>'
                            + '</div></div></div>');

                });
                $('#dcomm').val("");
            }
        });

        $('#comments').on('click', '#add_comment_sub', function ()
        {
            var comment = $('#dcomm_sub').val();
            var commentId = $(this).attr("data-commentId");          // main comment
            var requestId = $('#requestId').val();
            var loginUserName = $('#loginUserName').val();

            if (comment != "")
            {
                $('#relatedComments_input' + commentId).remove();
                $.post(hosturl + '/controllers/requests.php?ac=addcomment', {comment: comment, related: commentId, requestId: requestId}, function (res)
                {

                    $('#relatedComments' + commentId).append('<div class="media" id="comment' + res + '">'
                            + '<a href="javascript:;" class="pull-left blogpapa"></a>'
                            + '<div class="media-body">'
                            + '<h4 class="media-heading bold">' + loginUserName + ''
                            + '</h4>'
                            + '<p>' + comment
                            + '<span class="replayrecomment">'
                            + '<label  data-commentId="' + res + '" class="delete"> delete </label>'
                            + '</span>'
                            + '</p>'
                            + '</div>'
                            + '</div><hr>'
                            + '<div id="relatedComments_input' + commentId + '"></div>');

                });
            }
            else
            {
                $('#relatedComments_input' + commentId).html("");
            }
        });

        $('#comments').on('click', '.reply', function ()
        {
            var commentId = $(this).attr("data-commentId");          // main comment
            $('#relatedComments_input' + commentId).html('<div class="post-comment">'
                    + '<div class="chat-form">'
                    + '<div class="input-cont">'
                    + '<textarea class="col-md-10 form-control" rows="1" id="dcomm_sub"></textarea>'
                    + '</div>'
                    + '<div class="btn-cont">'
                    + '<span class="arrow">'
                    + '</span>'
                    + '<a href="#" class="btn blue icn-only" id="add_comment_sub" data-commentId="' + commentId + '">'
                    + '<i class="fa fa-check icon-white"></i></a>'
                    + '</div></div></div>');
            $('#dcomm_sub').focus();
        });

        $('#comments').on('click', '.delete', function ()
        {
            var commentId = $(this).attr("data-commentId");
            var mastercommentId = $(this).attr("data-mastercommentId");
            $.post(hosturl + '/controllers/requests.php?ac=deletecomment', {commentId: commentId}, function (res)
            {
                if (res == 0)        // master comment
                {
                    $('#comment' + mastercommentId).remove();
                }
                else              // sun comment ##comment on comment
                {
                    $('#comment' + commentId).remove();
                }

            });
        });
    }

    var session = function ()
    {
        $('.sessionDetails').on('click', function ()
        {
            var sessionId = $(this).attr("data-sessionid");
            window.location = hosturl + '/controllers/sessions.php?ac=sessiondetails&id=' + sessionId;
        });

        $('#form_wizard_1 .button-submit').click(function ()
        {
            var sessionId = $('#sessionId').val();
            $.post(hosturl + '/controllers/sessions.php?ac=savesession', {sessionId: sessionId}, function (res)
            {
                window.location = hosturl + '' + res;
            });
        }).hide();

        $('.memberAttend').on('change', function ()
        {
            if ($(this).attr("checked") == "checked")
            {
                var memberId = $(this).attr("data-userid");
                var sessionId = $('#sessionId').val();

                $.post(hosturl + '/controllers/sessions.php?ac=addsessionmember', {sessionId: sessionId, memberId: memberId}, function (res)
                {
                });
            }
            else
            {
                var memberId = $(this).attr("data-userid");
                var sessionId = $('#sessionId').val();

                $.post(hosturl + '/controllers/sessions.php?ac=removesessionmember', {sessionId: sessionId, memberId: memberId}, function (res)
                {
                });
            }


        });

        $('.delayRequest').on('change', function ()
        {
            var requestId = $(this).attr("data-requestid");
            if ($(this).attr("checked") == "checked")
            {
                $.post(hosturl + '/controllers/sessions.php?ac=delay', {requestId: requestId}, function (res)
                {

                });
                $(this).parents('tr').css("background-color", "gray");
                $('#x' + requestId).removeClass('requestDetails_s');
            }
            else
            {
                $.post(hosturl + '/controllers/sessions.php?ac=notdelay', {requestId: requestId}, function (res)
                {
                });
                $(this).parents().css("background-color", "");
                $('#x' + requestId).addClass('requestDetails_s');
            }


        });

        $('#tab2').on('click', '.requestDetails_s', function ()
        {
            var requestId = $(this).attr("data-requestid");
            var sessionId = $('#sessionId').val();
            var archived = $(this).attr("data-archived");
            if (archived == 0)
            {
                $.post(hosturl + '/controllers/sessions.php?ac=requestDetails', {requestId: requestId, sessionId: sessionId}, function (res)
                {
                    $('#rDetails').html(res);
                    $('#request').modal('show');
                });
            }
            else
            {
                $.post(hosturl + '/controllers/sessions.php?ac=requestDetailsArchive', {requestId: requestId, sessionId: sessionId}, function (res)
                {
                    $('#rDetails').html(res);
                    $('#session').modal('show');
                });
            }

        });

        $('#rDetails').on('change', '.decision', function ()
        {
            var requestId = $(this).attr("data-requestId");
            var sessionMemberId = $(this).attr("data-sessionMemberId");
            var opinion = $(this).val();
            $.post(hosturl + '/controllers/sessions.php?ac=saveopinion', {opinion: opinion, sessionMemberId: sessionMemberId, requestId: requestId}, function (res)
            {
            });
        });

        $('#rDetails').on('change', '#decision', function ()
        {

            var requestId = $(this).attr("data-requestId");
            var decision = $(this).val();
            $.post(hosturl + '/controllers/sessions.php?ac=savedecision', {decision: decision, requestId: requestId}, function (res)
            {

            });
        });
    }



    return {
        //main function to initiate the module
        init: function ()
        {
            request();
            common();
            session();
        }

    };
}();