    $('#tambah').on('click', function(){
        SaveDataA();
    })
    
    function SaveDataA(param){
        let $nama   = $('#nama').val();
        let $nim    = $('#nim').val();
        let $iklas  = $('#iklass').val();
        
        // console.log(param);

        /* save data */
         $.ajax({
            type: 'POST',
            dataType: 'json',
            url: baseURL + '/api/savedata',
            data: {
                id      : param,
                inama   : $nama,
                inim    : $nim,
                iklas   : $iklas,
            },
            success: function(result){
                if(result.code == 0){

                    $('#idi').val('');
                    $('#nama').val('');
                    $('#nim').val('');
                    $('#iklass').val('');

                    LoadDadta();
                }else{
                    alert(result.info);
                }
            },
            error: function(xhr) {
                alert(xhr.status+'-'+xhr.statusText);            
            }
        });
    }

    LoadDadta(null);
    function LoadDadta(id){

        /* save data */
         $.ajax({
            type: 'POST',
            dataType: 'json',
            url: baseURL + '/api/loaddata',
            data: {
                id   : id,
            },
            success: function(result){
               
               // $('table.order-search > tbody').empty();
                
                
                if(result.code == 0){
                    
                    let data = result.data;
                    
                    let counter = 0;
                    for(x in data){

                        var newRow = $("<tr>");
                        var cols = "";
                        // onClick="openmyprofile('+reg[x].userid+',\''+reg[x].named+'\')">
                        cols += '<td class="">#'+counter+'</td>';
                        cols += '<td class="tdCenterText bgtd1 tdBorder-left">'+data[x].nama+'</td>';
                        cols += '<td class="tdCenterText bgtd1 tdBorder-left">'+data[x].nim+'</td>';
                        cols += '<td class="tdCenterText bgtd1 tdBorder-left">'+data[x].kelas+'</td>';
                        cols += '<td class="tdCenterText bgtd1 tdBorder-left">'+data[x].create_date+'</td>';
                        cols += '<td class="tdCenterText bgtd1 tdBorder-left"><span class="btn btn-success btn-xs" onclick="loadedit('+data[x].id+')">Edit</span> <span class="btn btn-danger btn-xs"  onclick="Delete('+data[x].id+')">Delete</span></td>';
                        
                        newRow.append(cols);
                        $("table.order-search").append(newRow);
                        counter++;
                        
                        //$('.loaddata').append(''+ x +'<span>'+ data[x].nama + '</span>');
                    }

                }else{
                    alert(result.info);
                }
            },
            error: function(xhr) {
                alert(xhr.status+'-'+xhr.statusText);            
            }
        });
    }

    $('#cancel').on('click', function(){
        $('#idi').val('');
        $('#nama').val('');
        $('#nim').val('');
        $('#iklass').val('');

        $('#tambah').css('display', 'block');
        $('#edit').css('display', 'none');
        $('#cancel').css('display', 'none');
    });

    function loadedit(param){

        $('#tambah').css('display', 'none');
        $('#edit').css('display', 'block');
        $('#cancel').css('display', 'block');

        /* save data */
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: baseURL + '/api/loaddata',
            data: {
                id   : param,
            },
            success: function(result){
                // console.log(result);
                $('.loaddata').empty();
                // console.log(result);
                
                if(result.code == 0){
                    
                    LoadDadta(null);

                    let data = result.data;
                    
                    $('#idi').val(data[0].id);
                    $('#nama').val(data[0].nama);
                    $('#nim').val(data[0].nim);
                    $('#kelas').val(data[0].kelas);
                    $('#iklass').val(data[0].nim);



                }else{
                    alert(result.info);
                }
            },
            error: function(xhr) {
                alert(xhr.status+'-'+xhr.statusText);            
            }
        });
    }

    $('#edit').on('click', function(){
        let $id = $('#idi').val();
        SaveDataA($id);
        $('#tambah').css('display', 'block');
        $('#edit').css('display', 'none');
        $('#cancel').css('display', 'none');
    });

    function Delete(param){
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: baseURL + '/api/deletedata',
            data: {
                id   : param,
            },
            success: function(result){
                
                if(result.code == 0){
                    
                    alert('sukses delete');
                    LoadDadta(null);


                }else{
                    alert('gagal');
                }
            },
            error: function(xhr) {
                alert(xhr.status+'-'+xhr.statusText);            
            }
        });
    }



