/*
 * This file is part of the Render blade files.
 * */

var $_renderForm = $('#renderForm');
var $_body = $('body');
$_renderForm.hide();

$(document).ready(function () {
    // add new record
    var $_addRecord = renderParam.add;
    $_body.on('click', '#' + $_addRecord.add_btn_id, function (e) {
        $.ajax({
            url: $_addRecord.action,
            type: $_addRecord.method,
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            success: function (response) {
                $_renderForm.html(response);
                $_renderForm.slideDown();
            },

        });
    });

    // edit record
    var $_editRecord = renderParam.edit;
    $_body.on('click', '.' + $_editRecord.edit_btn_id, function (e) {
        var $_id = $(this).data('id');
        var $_editUrl = $_editRecord.action;
        $_editUrl = $_editUrl.replace(':id', $_id);
        $.ajax({
            url: $_editUrl,
            type: $_editRecord.method,
            data: {
                id: $_id
            },
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            success: function (response) {
                $_renderForm.html(response);
                $_renderForm.slideDown();
            },

        });
    });

    // cancel adding new record
    $_body.on('click', '#' + renderParam.cancel_btn_id, function (e) {
        $_renderForm.slideUp();
        $_renderForm.empty();
    });
});
