/**
 * Created by Khalid.Alaraj on 10-October-2016.
 */
$.fn.dataTable.ext.errMode = 'throw';
$(document).ready(function() {
    var table = $('#users-table').DataTable({
        // "processing": true,
        // "serverSide": true,
        "ajax": "load_users.php",
        "columns": [
            {"data": "un"},
            {"data": "active"},
            {
                "orderable": false,
                "targets": -1,
                "data": null,
                "defaultContent": '<i data-toggle="modal" data-target="#det-modal" class="glyphicon glyphicon-edit click-view edit"></i> <i class="glyphicon glyphicon-trash click-view delete"></i>'
            }
        ]
    });

    $('#users-table tbody').on('click', '.edit', function () {
        var data = table.row($(this).parents('tr')).data();
        $('#service-action').val('edit');
        $('#users-un').val(data['un']);
        //$('#users-pw').val(data['pw']);
        $('#users-active').val(data['active']);
        $('#users-id').val(data['id']);

    });
    $('#users-table tbody').on('click', '.delete', function () {
        var data = table.row($(this).parents('tr')).data();
        if (confirm('Are you sure to delete this record ?')) {
            $("#body-content").load("edit.php", {delete: 1, id: data['id']}, function () {
            });
            /*$.post('load_det.php',{delete:1,id:data['id']}).done(function(response){
             $("body-content").load( "/resources/load.html #body-content" );
             });*/
        }

    });

    $(document).on('click','#add-user',function(e){
        $('#service-action').val('create');
        $('#users-un').val('');
        $('#users-pw').val('');
        $('#users-active').val('1');
    });
});