/*
 * 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_cu = function () {

    var hosturl = $('#hosturl').val();
    var path = $('#path').val();
    //specailized vaildation
    var handleSummernote = function () {
        $('.summernote_1').summernote({height: 300});
    };
    //specailized vaildation
    var wizard = function () {
        $('#form_wizard_1').bootstrapWizard();
    };

    var adduserinfo = function () {
        $('.userstuff').live('change', function () {
            var id = $('#headID').val();
            var status = $(this).attr('data-status');
            $.post(hosturl + '/controllers/stuff.php?ac=add', {id: id, status: status}, function (data) {
                if (data) {
                    var shortCutFunction = 'success';
                    var msg = 'Head is add successfully ';
                    var title = 'Success';
                    $('#headsection').html(data);

                } else {
                    var shortCutFunction = 'error';
                    var msg = 'Try again .. something wrong happened';
                    var title = 'Error';

                }
                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-left',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);
            });
        });
    }
    var adduserinfoamin = function () {
        $('.userstuffamin').live('change', function () {
            var id = $('#aminID').val();
            var status = $(this).attr('data-status');
            $.post(hosturl + '/controllers/stuff.php?ac=add', {id: id, status: status}, function (data) {
                if (data) {
                    var shortCutFunction = 'success';
                    var msg = 'Head is add successfully ';
                    var title = 'Success';
                    $('#aminsection').html(data);

                } else {
                    var shortCutFunction = 'error';
                    var msg = 'Try again .. something wrong happened';
                    var title = 'Error';

                }
                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-left',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);
            }
            );
        });
    }
    var editaminandhead = function () {
        $('.btnEdit').click(function () {
            var id = $(this).attr('data-id');
            var userid = $(this).attr('data-userid');
            var status = $(this).attr('data-status');
            $.post(hosturl + '/controllers/stuff.php?ac=edit', {id: id, userid: userid, status: status}, function (data) {
                if (status == 0) {
                    $('#headsection').html(data);
                    $('#headsection').select2();
                }
                if (status == 1) {
                    $('#aminsection').html(data);
                    $('#aminsection').select2();

                }
            });
        });
    }

    var headupdatefn = function () {
        $('.headupdate').live('change', function () {
            var userid = $('#headupID').val();
            var status = $(this).attr('data-status');
            $.post(hosturl + '/controllers/stuff.php?ac=update', {userid: userid, status: status}, function (data) {


                if (data) {
                    var shortCutFunction = 'success';
                    var msg = 'Head is updated successfully ';
                    var title = 'Success';
                    $('#headsection').html(data);

                } else {
                    var shortCutFunction = 'error';
                    var msg = 'Try again .. something wrong happened';
                    var title = 'Error';

                }
                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-left',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);


            });
        });
    }
    var aminupdatefn = function () {
        $('.aminupdate').live('change', function () {
            var userid = $('#aminupID').val();
            var status = $(this).attr('data-status');
            $.post(hosturl + '/controllers/stuff.php?ac=update', {userid: userid, status: status}, function (data) {
                if (data) {
                    var shortCutFunction = 'success';
                    var msg = 'Amin is updated successfully ';
                    var title = 'Success';
                    $('#aminsection').html(data);

                } else {
                    var shortCutFunction = 'error';
                    var msg = 'Try again .. something wrong happened';
                    var title = 'Error';

                }
                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-left',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);
            });
        });
    }

    var addboardstuff = function () {
        var stuffitr = Number($('#boardstuffnum').val()) + 1;
        $('#plusboardstuff').live('click', function () {

            $('#boardstuff').append('<tr>'
                    + '<td class="highlight wds20">'
                    + '<div class="warning">'
                    + '</div>'
                    + '<a href="javascript:;">'
                    + 'User Name </a>'
                    + '</td>'
                    + '<td class="hidden-xs" id="boardstuffsection">'
                    + '<select class="form-control select2me addboardstuff" name="boardstuffadd' + stuffitr + '" data-itr="' + stuffitr + '"  id="boardstuffname' + stuffitr + '" data-status ="2" data-placeholder="Select User Name" required>'
                    + $('#stuffboard').html()
                    + '</select>'
                    + '</td>'
                    + '</div>'
                    + '</tr>'
                    );
            $('#boardstuffname' + stuffitr).select2();
            stuffitr++;
            $('#boardstuffnum').val(stuffitr);
        });
    }
    var addnewuserstuff = function () {
        $('.addboardstuff').live('change', function () {
            var status = $(this).attr('data-status');
            var dataitr = $(this).attr('data-itr');
            var userid = $('#boardstuffname' + dataitr).val();
            var boardstuffnum = $('#boardstuffnum').val();

            $.post(hosturl + '/controllers/stuff.php?ac=addboardstuff', {status: status, itr: dataitr, userid: userid, boardstuffnum: boardstuffnum}, function (data) {

                //alert(data)
                if (data) {
                    var shortCutFunction = 'success';
                    var msg = 'Amin is updated successfully ';
                    var title = 'Success';
                    //$('#boardstuffsection').html(data);
                    window.location.href = hosturl + '/controllers/stuff.php';
                } else {
                    var shortCutFunction = 'error';
                    var msg = 'Try again .. something wrong happened';
                    var title = 'Error';
                }
                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-left',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);
            });
        }
        );
    }
    var deleteboardstuff = function () {
        $('.deleteboard').live('click', function () {
            var itemid = $(this).attr('data-id');

            $.post(hosturl + '/controllers/stuff.php?ac=delboard', {itemid: itemid}, function (data) {
                if (data == 1) {
                    var shortCutFunction = 'success';
                    var msg = 'User was deleted successfully';
                    var title = 'Success';
                } else {
                    var shortCutFunction = 'error';
                    var msg = "Sorry , user can't delete please try again";
                    var title = 'Error';
                }
                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-left',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);

            });
            $(this).parent().parent().remove();
        });
    }
    var editboardstuff = function () {
        $('.btnEditBoard').click(function () {
            var id = $(this).attr('data-id');
            var userid = $(this).attr('data-userid');
            $.post(hosturl + '/controllers/stuff.php?ac=stuffedit', {id: id, userid: userid}, function (data) {
                $("#boardstuffsection" + id).html(data);
            })
        });
    }
    var editbs = function () {
        $('.editbs').live('change', function () {
            var userid = $('#stuffUpdate').val();
            var status = $(this).attr('data-status');
            var id = $(this).attr('data-id');


            //alert(id + '.....' + userid + '.....' + status);
            $.post(hosturl + '/controllers/stuff.php?ac=stuffupdate', {id: id, userid: userid, status: status}, function (data) {
                if (data) {
                    var shortCutFunction = 'success';
                    var msg = 'User was deleted successfully';
                    var title = 'Success';
                    $("#boardstuffsection" + id).html(data);
                } else {
                    var shortCutFunction = 'error';
                    var msg = "Sorry , user can't delete please try again";
                    var title = 'Error';
                }
                toastr.options = {
                    closeButton: $('#closeButton').prop('checked'),
                    debug: $('#debugInfo').prop('checked'),
                    positionClass: $('#positionGroup input:checked').val() || 'toast-top-left',
                    onclick: null
                };
                var $toast = toastr[shortCutFunction](msg, title);
            });
        });
    }
    return {
        //main function to initiate the module
        init: function ()
        {
            handleSummernote();
            wizard();
            adduserinfo();
            adduserinfoamin();
            editaminandhead();
            headupdatefn();
            aminupdatefn();
            addboardstuff();
            addnewuserstuff();
            deleteboardstuff();
            editboardstuff();
            editbs();
        }
    };
}();

