Subversion Repositories SmartDukaan

Rev

Rev 36889 | Rev 36892 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

$(function() {

        $(document).on('click', ".contact-us", function() {
                console.log("Contact Us Button Clicked...")
                contactUs("main-content");
        });

        // ----- Add / Edit / Delete contact entries (authorised users only) -----

        // Open the modal in "add" mode for the clicked section.
        $(document).on('click', ".contact-add-btn", function() {
                var section = $(this).data('section');
                $('#contactUsForm')[0].reset();
                $('#cuId').val(0);
                $('#cuSection').val(section);
                $('#contactUsModalTitle').text(section === 'ESCALATION' ? 'Add Escalation' : 'Add Contact');
                $('#cuBaseAreaLabel').text(section === 'ESCALATION' ? 'Region' : 'Base Area');
                $('#contactUsModal').modal('show');
        });

        // Open the modal pre-filled in "edit" mode.
        $(document).on('click', ".contact-edit-btn", function() {
                var btn = $(this);
                var section = btn.data('section');
                var mobile = String(btn.data('mobile'));
                $('#contactUsForm')[0].reset();
                $('#cuId').val(btn.data('id'));
                $('#cuSection').val(section);
                $('#cuArea').val(btn.data('area'));
                $('#cuBaseArea').val(btn.data('basearea'));
                $('#cuName').val(btn.data('name'));
                $('#cuDesignation').val(btn.data('designation'));
                $('#cuMobile').val(mobile === '-' ? '' : mobile);
                $('#cuEmail').val(btn.data('email'));
                $('#contactUsModalTitle').text('Edit Contact');
                $('#cuBaseAreaLabel').text(section === 'ESCALATION' ? 'Region' : 'Base Area');
                $('#contactUsModal').modal('show');
        });

        // Persist add / edit.
        $(document).on('click', "#contactUsSaveBtn", function() {
                var name = $.trim($('#cuName').val());
                if (name === '') {
                        alert('Name is required.');
                        return;
                }
                var params = {
                        id: $('#cuId').val(),
                        section: $('#cuSection').val(),
                        area: $('#cuArea').val(),
                        baseArea: $('#cuBaseArea').val(),
                        name: name,
                        designation: $('#cuDesignation').val(),
                        mobile: $('#cuMobile').val(),
                        email: $('#cuEmail').val()
                };
                doPostAjaxRequestWithParamsHandler(context + "/contactUs/save", params, function(response) {
                        if (response && response.success) {
                                $('#contactUsModal').modal('hide');
                                contactUs("main-content");
                        } else {
                                alert((response && response.message) ? response.message : 'Could not save contact.');
                        }
                });
        });

        // Soft-delete an entry.
        $(document).on('click', ".contact-delete-btn", function() {
                var name = $(this).data('name');
                if (!confirm('Remove ' + name + ' from the contact list?')) {
                        return;
                }
                doPostAjaxRequestWithParamsHandler(context + "/contactUs/delete", { id: $(this).data('id') }, function(response) {
                        if (response && response.success) {
                                contactUs("main-content");
                        } else {
                                alert((response && response.message) ? response.message : 'Could not remove contact.');
                        }
                });
        });

});

function contactUs(domId){
        doGetAjaxRequestHandler(context+"/contactUs", function(response){
                console.log("doGetAjaxRequest");
                // Drop any previously relocated modal to avoid duplicate #contactUsModal ids.
                $('#contactUsModal').remove();
                $('#' + domId).html(response);
                // The dashboard applies `zoom` to #main-content > .wrapper, which traps a
                // Bootstrap modal behind its body-level backdrop ("screen dims, no popup").
                // Move the modal out to <body> so it renders above the backdrop.
                $('#contactUsModal').appendTo('body');
        });
}