$(document).ready(function() {
    
    "use strict";
    
    
    // Datatables
    
    //$('#example').dataTable();

    /*$('#data-table-auction-objects').DataTable( {
        "pageLength": 15,
        "lengthMenu": [ 15, 30, 50, 75, 100 ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "fnInitComplete": function( settings, json ) {
            $('#filterContainer').prependTo('.dataTables_wrapper');
        },
        "processing": true,
    } );*/

    /*var filterStatus = ($.urlParam('status') == '') ? null : $.urlParam('status');
    var filterLanguage = ($.urlParam('lang') == '') ? null : $.urlParam('lang');*/

    /*$('#data-table-auction-objects').DataTable({
        processing: true,
        serverSide: true,
        ordering: true,
        "pageLength": 10,
        "lengthMenu": [ 10, 20, 50, 100 ],
        "ajax": {
            "url": "/ajax/ajax-auction-objects-overview",
            "type": "POST",
            "headers": {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
            "dataSrc": "data",
            "data": {
                'status': filterStatus,
                'lang': filterLanguage,
            },
        },
        "deferRender": true,
        "columns": [
            {
                "mRender": function ( data, type, row ) {
                    return '<a class="action-icon" href="/objekt-bearbeiten/'+row.id+'"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                },
                className: "col-action"
            },
            {
                "data": "status",
                "name": "ao.status",
                "searchable": false,
                "render": function (val, type, row) {
                    if(val == 1) {
                        return 'Exportbereit';
                    } else if(val == 2) {
                        return 'Exportiert';
                    } else if(val == 3) {
                        return 'Prüffall';
                    } else if(val == 4)  {
                        return 'Fehlerhaft';
                    }  else {
                        return '';
                    }
                }
            },
            { "data": "lang_code", "name": "l.lang_code", "searchable": true },
            { "data": "auction_name", "name": "ao.auction_name", "searchable": true },
            { "data": "source_type_name", "name": "source_type_name", "searchable": false },
            { "data": "artist_firstname", "name": "ao.artist_firstname", "searchable": true },
            { "data": "artist_lastname", "name": "ao.artist_lastname", "searchable": true },
            { "data": "artist_born_loc", "name": "ao.artist_born_loc", "searchable": false },
            { "data": "artist_born_date", "name": "artist_born_date", "searchable": false },
            { "data": "artist_died_date", "name": "artist_died_date", "searchable": false },
            { "data": "artist_died_loc", "name": "ao.artist_died_loc", "searchable": false },
            { "data": "medium_category", "name": "medium_category", "searchable": false },
            { "data": "object_medium", "name": "ao.object_medium", "searchable": false },
            {
                "mRender": function ( data, type, row ) {
                    return '<a href="'+row.object_url+'" target="_blank">'+row.object_url+'</a>';
                }
            },
            { "data": "auction_no", "name": "ao.auction_no", "searchable": true },
            { "data": "lot_no", "name": "ao.lot_no", "searchable": true },
            { "data": "object_title", "name": "ao.object_title", "searchable": true },
            { "data": "object_date_pre", "name": "ao.object_date_pre", "searchable": false },
            { "data": "object_date", "name": "ao.object_date", "searchable": false },
            { "data": "object_date_post", "name": "ao.object_date_post", "searchable": false },
            { "data": "object_est_from", "name": "ao.object_est_from", "searchable": false },
            { "data": "object_est_to", "name": "ao.object_est_to", "searchable": false },
            { "data": "object_est_curr", "name": "ao.object_est_curr", "searchable": false },
            { "data": "object_result", "name": "ao.object_result", "searchable": false },
            { "data": "object_result_curr", "name": "ao.object_result_curr", "searchable": false },
            { "data": "object_indicators", "name": "ao.object_indicators", "searchable": false },
            { "data": "object_height", "name": "ao.object_height", "searchable": false },
            { "data": "object_width", "name": "ao.object_width", "searchable": false },
            { "data": "object_depth", "name": "ao.object_depth", "searchable": false },
            { "data": "object_size_unit", "name": "ao.object_size_unit", "searchable": false },
            {
                "mRender": function ( data, type, row ) {
                    return '<a href="'+row.object_image_url+'" target="_blank">'+row.object_image_url+'</a>';
                }
            },
            { "data": "auction_loc", "name": "ao.auction_loc", "searchable": false },
            { "data": "auction_date", "name": "auction_date", "searchable": false },
            { "data": "source_type", "name": "ao.source_type", "searchable": true },
            { "data": "object_id", "name": "ao.object_id", "searchable": false },
            { "data": "id", "name": "ao.id", className: "col-id", "searchable": false },
            { "data": "created_at", "name": "created_at", "searchable": false },
            { "data": "crawled_at", "name": "crawled_at", "searchable": false },
        ],
        "columnDefs": [

        ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json",
            "processing": "<img src='/images/loading.gif'>",
        },
        "fnInitComplete": function (settings, json) {
            $('#filterContainer').prependTo('.dataTables_wrapper');
            $('#buttonLine').prependTo('.dataTables_wrapper');
            $('#filterContainer').show();
        },
    });*/

    /*$('#data-table-assign-artists').DataTable({
        "pageLength": 10,
        "lengthMenu": [ 10, 20, 50, 100 ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "fnInitComplete": function (settings, json) {
            $('#filterContainer').prependTo('.dataTables_wrapper');
            $('#buttonLine').prependTo('.dataTables_wrapper');
            $('#filterContainer').show();
            $('#buttonLine').show();
        },
        "processing": true,
    });*/

    /*$('#data-table-artists').DataTable({
        processing: true,
        serverSide: true,
        ordering: true,
        "pageLength": 10,
        "lengthMenu": [ 10, 20, 50, 100 ],
        //iDisplayLength: 10,
        "ajax": {
            "url": "/ajax/ajax-artists-overview",
            "type": "GET",
            "dataSrc": "data",
            "data": {
                'status': filterStatus
            },
        },
        "deferRender": true,
        "columns": [
            {
                "mRender": function ( data, type, row ) {
                    return '<a class="action-icon" href="/kuenstler-bearbeiten/'+row.id+'"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                },
                className: "col-action"
            },
            {
                "data": "status",
                "render": function (val, type, row) {
                    if(val == 0) {
                        return 'Neu';
                    } else if(val == 1) {
                        return 'Geprüft';
                    } else if(val == 2) {
                        return 'Nicht verwenden';
                    } else {
                        return '';
                    }
                }
            },
            { "data": "born_date" },
            { "data": "died_date" },
            { "data": "firstname_en" },
            { "data": "lastname_en" },
            { "data": "firstname_de" },
            { "data": "lastname_de" },
            { "data": "firstname_fr" },
            { "data": "lastname_fr" },
            { "data": "born_location_en" },
            { "data": "died_location_en" },
            { "data": "born_location_de" },
            { "data": "died_location_de" },
            { "data": "born_location_fr" },
            { "data": "died_location_fr" },
            { "data": "id", className: "col-id" },
        ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "fnInitComplete": function (settings, json) {
            $('#filterContainer').prependTo('.dataTables_wrapper');
            $('#buttonLine').prependTo('.dataTables_wrapper');
            $('#filterContainer').show();
            $('#buttonLine').show();
        },
    });*/

    /*$('#data-table-source-types').DataTable({
        "pageLength": 15,
        "lengthMenu": [ 15, 30, 50, 75, 100 ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "fnInitComplete": function (settings, json) {
            $('#buttonLine').prependTo('.dataTables_wrapper');
            $('#buttonLine').show();
        },
        "processing": true,
    });*/



    /*var editor; // use a global for the submit and return data rendering in the examples

    editor = new $.fn.dataTable.Editor({
        ajax: "/ajax/ajax-source-type-overview",
        table: "#data-table-source-types",
        idSrc:  'id',
        fields: [{
            name: "name"
        }, {
            name: "location"
        }, {
            name: "url"
        }]
    });

    // Activate an inline edit on click of a table cell
    $('#data-table-source-types').on('click', 'tbody td:not(:first-child)', function (e) {
        editor.inline(this);
    });


    $('#data-table-source-types').DataTable({
        dom: "Bfrtip",
        processing: true,
        serverSide: true,
        ordering: true,
        "pageLength": 10,
        "lengthMenu": [ 10, 20, 50, 100 ],
        //iDisplayLength: 10,
        "ajax": {
            "url": "/ajax/ajax-source-type-overview",
            "type": "GET",
            "dataSrc": "data",
        },
        "deferRender": true,
        "columns": [
            {
                "mRender": function ( data, type, row ) {
                    return '<a class="action-icon" href="/auktionshaus-bearbeiten/'+row.id+'"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                },
                className: "col-action"
            },
            { "data": "name" },
            { "data": "location" },
            { "data": "url" },
            { "data": "id", className: "col-id" },
        ],
        "select": {
            style:    'os',
            selector: 'td:first-child'
        },
        "buttons": [
            { extend: "create", editor: editor },
            { extend: "edit",   editor: editor },
            { extend: "remove", editor: editor }
        ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "fnInitComplete": function (settings, json) {
            $('#buttonLine').prependTo('.dataTables_wrapper');
        },
    });*/


    /*$('#data-table-mediums').DataTable({
        "pageLength": 15,
        "lengthMenu": [ 15, 30, 50, 75, 100 ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "fnInitComplete": function (settings, json) {
            $('#filterContainer').prependTo('.dataTables_wrapper');
            $('#buttonLine').prependTo('.dataTables_wrapper');
            $('#filterContainer').show();
            $('#buttonLine').show();
        },
        "processing": true,
    });*/

    /*$('#data-table-mediums').DataTable({
        processing: true,
        serverSide: true,
        ordering: true,
        "pageLength": 10,
        "lengthMenu": [ 10, 20, 50, 100 ],
        //iDisplayLength: 10,
        "ajax": {
            "url": "/ajax/ajax-mediums-overview",
            "type": "GET",
            "dataSrc": "data",
        },
        "deferRender": true,
        "columns": [
            {
                "mRender": function ( data, type, row ) {
                    return '<a class="action-icon" href="/grobtechnik-bearbeiten/'+row.id+'"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                },
                className: "col-action"
            },
            { "data": "searchword_en" },
            { "data": "searchword_de" },
            { "data": "searchword_fr" },
            { "data": "category" },
            { "data": "id", className: "col-id" },
        ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "fnInitComplete": function (settings, json) {
            $('#filterContainer').prependTo('.dataTables_wrapper');
            $('#buttonLine').prependTo('.dataTables_wrapper');
            $('#filterContainer').show();
            $('#buttonLine').show();
        },
    });*/

    /*$('#data-table-assign-mediums').DataTable({
        processing: true,
        ordering: false,
        "pageLength": 15,
        "lengthMenu": [ 15, 30, 50, 75, 100 ],
        //iDisplayLength: 10,
        "ajax": {
            "url": "/ajax/ajax-mediums-overview",
            "type": "GET",
            "dataSrc": "",
        },
        "columns": [
            {
                "mRender": function ( data, type, row ) {
                    return '<a href="javascript:void(0)" title="Grobtechnik zuweisen" onclick="selectMedium('+row.id+')"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>';
                },
                className: "col-action"
            },
            { "data": "searchword_en" },
            { "data": "searchword_de" },
            { "data": "searchword_fr" },
            { "data": "category" },
            { "data": "id", className: "col-id" },
        ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
    });*/

    /*$('#data-table-users').DataTable({
        "pageLength": 15,
        "lengthMenu": [ 15, 30, 50, 75, 100 ],
        "language": {
            "url": "/plugins/datatables/i18n/German.json"
        },
        "deferRender": true,
        "fnInitComplete": function (settings, json) {
            $('#filterContainer').prependTo('.dataTables_wrapper');
            $('#buttonLine').prependTo('.dataTables_wrapper');
        },
        "processing": true,
    });*/
    
    var table = $('#example2').DataTable({
        "columnDefs": [
            { "visible": false, "targets": 2 }
        ],
        "order": [[ 2, 'asc' ]],
        "displayLength": 25,
        "drawCallback": function ( settings ) {
            var api = this.api();
            var rows = api.rows( {page:'current'} ).nodes();
            var last=null;
 
            api.column(2, {page:'current'} ).data().each( function ( group, i ) {
                if ( last !== group ) {
                    $(rows).eq( i ).before(
                        '<tr class="group"><td colspan="5">'+group+'</td></tr>'
                    );
 
                    last = group;
                }
            } );
        }
    } );
 
    // Order by the grouping
    $('#example2 tbody').on( 'click', 'tr.group', function () {
        var currentOrder = table.order()[0];
        if ( currentOrder[0] === 2 && currentOrder[1] === 'asc' ) {
            table.order( [ 2, 'desc' ] ).draw();
        }
        else {
            table.order( [ 2, 'asc' ] ).draw();
        }
    } );
    
    $.fn.isValid = function(){
        return this[0].checkValidity()
    }
    
    var t = $('#example3').DataTable();
 
    $('#add-row').on( 'click', function () {
        if($("#add-row-form").isValid()) {
            var name = $('#name-input').val(),
                position = $('#position-input').val(),
                age = $('#age-input').val(),
                date = $('#date-input').val(),
                salary = $('#salary-input').val();
            t.row.add( [
                name,
                position,
                age,
                date,
                '$' + salary
            ] ).draw();
            
            $('.modal').modal('hide');
            
            return false;
        }
    });
    
    $('.date-picker').datepicker({
        orientation: "top auto",
        autoclose: true
    });
});

/*$.urlParam = function(name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results==null){
        return null;
    }
    else{
        return results[1] || 0;
    }
}*/