@extends('layouts.main')
@section('title', 'Kunstobjekte verwalten')
@section('headline', 'Kunstobjekte verwalten')
@section('content')

    {{--<div class="page-title">
        <h3 class="breadcrumb-header">@yield('headline')</h3>
    </div>--}}
    <div id="main-wrapper">
        <div class="row">
            <div class="col-md-12">
                @if($alert == 'successUpdate')
                    <div class="alert alert-success" role="alert">
                        Das Kunstobjekt wurde erfolgreich aktualisiert!
                    </div>
                @endif

                <div class="alert alert-info" id="hint-select-data" style="display: none;">Bitte treffen Sie zunächst eine Auswahl, um die Daten anzuzeigen.</div>

                <div class="panel panel-white">
                    <div class="panel-heading clearfix">
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <div class="filter-container" id="filterContainer">
                                {!! Form::open(['action' => 'AuctionObjects\AuctionObjectsOverviewController@index', 'class' => 'form-horizontal', 'id' => 'filterAuctionObjectForm', 'method' => 'get', 'role' => 'form', 'files' => true]) !!}
                                    <input name="filtered" id="filtered" type="hidden" value="1">
                                    <input name="page" id="page" type="hidden" value="{{$page}}">
                                    <div id="customSearch" style="float: right; margin-left: 20px;">
                                        <label style="float: left; padding: 7px 5px 0;">Suchen:</label>
                                        <select class="form-control" id="selectFilterObjectSearchColumn" name="sc" style="float: left; width: 150px; margin-right: 5px;">
                                            <option value="all" @if($selectedFilterSearchColumn == 'all' || $selectedFilterSearchColumn == '') selected @endif>Alle</option>
                                            @foreach(trans()->get('auction_object_table_columns') as $key => $value)
                                                @if($key != 'status' && $key != 'lang_code' && $key != 'source_type_name')
                                                    <option value="{{ $key }}" @if($selectedFilterSearchColumn == $key) selected @endif>{{ $value }}</option>
                                                @endif
                                            @endforeach
                                            {{--@foreach($searchColumns as $searchColumn)
                                                <option value="{{$searchColumn}}" @if($selectedFilterSearchColumn == $searchColumn) selected @endif>{{$searchColumn}}</option>
                                            @endforeach--}}
                                        </select>
                                        <input class="form-control" name="s" id="inputSearch" type="text" max="999" maxlength="" value="@if(!is_null($selectedFilterSearch)){{stripslashes($selectedFilterSearch)}}@endif" style="float: left; width: 150px;">
                                    </div>
                                    <div id="pageLen" style="float: right; margin-left: 20px;">
                                        <input class="form-control" name="len" id="inputPageLen" type="text" max="999" maxlength="3" value="@if($pageLength){{$pageLength}}@else{{auth()->user()->table_page_length}}@endif" style="float: left; width: 50px;"> <label style="float: right; padding: 7px 5px 0;">Einträge anzeigen</label>
                                    </div>
                                    <select class="form-control" id="selectFilterObjectSourceType" onchange="submitFilterForm(3)" name="st" style="width: 210px; float: right;">
                                        <option value="">- Auktionshaus wählen -</option>
                                        @foreach($sourceTypes as $sourceType)
                                            <option value="{{ $sourceType->id }}" @if($selectedFilterSourceType == $sourceType->id) selected @endif>{{ $sourceType->name }}</option>
                                        @endforeach
                                    </select>
                                    <select class="form-control" id="selectFilterObjectStatus" onchange="submitFilterForm(2)" name="status" style="margin-right: 20px; width: 175px; float: right;">
                                        <option value="" @if(is_null($selectedFilterStatus)) selected @endif>- Filter wählen -</option>
                                        @foreach($statuses as $status)
                                            <option value="{{$status->status}}" @if($selectedFilterStatus == $status->status && !is_null($selectedFilterStatus)) selected @endif>{{$status->status_name}}</option>
                                        @endforeach
                                        {{--<option value="1" @if($selectedFilterStatus == 1) selected @endif>Exportbereit</option>
                                        <option value="2" @if($selectedFilterStatus == 2) selected @endif>Exportiert</option>
                                        <option value="3" @if($selectedFilterStatus == 3) selected @endif>Prüffall</option>--}}
                                    </select>
                                    <select class="form-control" id="selectFilterObjectLanguage" onchange="submitFilterForm(1)" name="lang" style="margin-right: 20px; width: 175px; float: right;">
                                        <option value="" @if(is_null($selectedFilterLanguage)) selected @endif>- Sprache wählen -</option>
                                        @foreach($languages as $language)
                                            <option value="{{ $language->id }}" @if($selectedFilterLanguage == $language->id) selected @endif>{{ $language->lang_code }}</option>
                                        @endforeach
                                    </select>
                                    <label style="float: right; padding: 7px 5px 0;">Filtern:</label>
                                {!! Form::close() !!}

                            </div>

                            {!! Form::open(['action' => 'Ajax\AuctionObjectsOverviewController@bulkEditStatus', 'class' => 'form-horizontal', 'id' => 'frm-bulk-edit', 'method' => 'post', 'role' => 'form', 'files' => true]) !!}
                            <table id="data-table-auction-objects" class="cell-border data-table display table table-data-width nowrap hover">
                                <thead>
                                    <tr>
                                        <th class="col-bulk">
                                            <input id="dt-check-all" type="checkbox">
                                        </th>
                                        <th></th>
                                        {{--<th>Med.Kat. ID</th>--}}
                                        <th>Status</th>
                                        <th>Spr.</th>
                                        <th>Auktion</th>
                                        <th>Auktionshaus</th>
                                        <th class="col-artist-fixed">Künstler</th>
                                        <th>Künstler VN</th>
                                        <th>Künstler NN</th>
                                        <th>Geb.Ort</th>
                                        <th>Geb.</th>
                                        <th>Gest.</th>
                                        <th>Gest.Ort</th>
                                        <th>Grobtechnik</th>
                                        <th>Technik</th>
                                        <th>Pfad</th>
                                        <th>Auktionsnr.</th>
                                        <th>Lot Nr.</th>
                                        <th>Titel</th>
                                        <th>Entst.Txt 1</th>
                                        <th>Entstehung</th>
                                        <th>Entst.Txt 2</th>
                                        <th>SP von</th>
                                        <th>SP bis</th>
                                        <th>SP Währ.</th>
                                        <th>Erlös</th>
                                        <th>Erlös Währ.</th>
                                        <th>Katalog.</th>
                                        <th>Höhe</th>
                                        <th>Breite</th>
                                        <th>Tiefe</th>
                                        <th>Einheit</th>
                                        <th>Bildpfad</th>
                                        <th>Auktionsort</th>
                                        <th>Auktionsdat.</th>
                                        <th>Auktionshaus</th>
                                        <th>Objektnr.</th>
                                        <th class="col-id">ID</th>
                                        <th>Anlagedat.</th>
                                        <th>Auslesedat.</th>
                                        @if(Auth::user()->user_role_id == 1)
                                            <th>Status Nachr.</th>
                                        @endif
                                    </tr>
                                </thead>
                            </table>

                            <div class="row" style="padding-bottom: 20px;">
                                <div class="col-md-12">
                                    <label for="bulk_action" class="control-label pull-left" style="padding: 0 20px; font-size: 24px;"><i class="fa fa-level-up" aria-hidden="true" style="transform: rotate(90deg);"></i></label>
                                    <select class="form-control pull-left" name="bulk_action" id="bulk_action" style="margin-right: 20px; width: 200px;">
                                        <option value="">- Aktion wählen -</option>
                                        <option value="1">Status: Exportbereit</option>
                                        <option value="2">Status: Exportiert</option>
                                        <option value="3">Status: Prüffall</option>
                                    </select>
                                    <button type="submit" id="btnBulkAction" class="btn btn-primary">Absenden</button>
                                </div>
                            </div>
                            {!! Form::close() !!}

                            <script type="text/javascript">
                                //var filterStatus = ($.urlParam('status') == '') ? null : $.urlParam('status');
                                //var filterLanguage = ($.urlParam('lang') == '') ? null : $.urlParam('lang');
                                //var filterSourceType = ($.urlParam('st') == '') ? null : $.urlParam('st');

                                var filterStatus = @if(!is_null($selectedFilterStatus)) {{$selectedFilterStatus}} @else null @endif;
                                var filterLanguage = @if($selectedFilterLanguage) {{$selectedFilterLanguage}} @else null @endif;
                                var filterSourceType = @if($selectedFilterSourceType) {{$selectedFilterSourceType}} @else null @endif;
                                var filterSearchColumn = @if($selectedFilterSearchColumn) '{{$selectedFilterSearchColumn}}' @else null @endif;
                                var filterSearch = @if($selectedFilterSearch) '{{$selectedFilterSearch}}' @else null @endif;

                                var pageParam = ($.urlParam('page') == '') ? null : $.urlParam('page');
                                var lenParam = ($.urlParam('len') == '') ? 10 : $.urlParam('len');
                                var sortcParam = ($.urlParam('sortc') == null || $.urlParam('sortc') == '') ? 38 : $.urlParam('sortc');
                                var sortdParam = ($.urlParam('sortd') == null || $.urlParam('sortd') == '') ? 'desc' : $.urlParam('sortd');
                                var displayStart = 0;

                                if (pageParam != null) {

                                    displayStart = (pageParam - 1) * lenParam;
                                }

                                var table = $('#data-table-auction-objects').DataTable({
                                    fixedHeader: {
                                        header: true,
                                        headerOffset: $('.page-header').outerHeight()
                                    },
                                    processing: true,
                                    serverSide: true,
                                    ordering: true,
                                    autoWidth: false,
                                    //scrollY: '60vh',
                                    //scrollCollapse: false,
                                    //'dom': 'Blfrtip',
                                    'dom': 'Blrtip',
                                    "displayStart": displayStart,
                                    "pageLength": {{ $pageLength }},
                                    "lengthMenu": [ 10, 25, 50, 100 ],
                                    "lengthChange": false,
                                    "order": [[ sortcParam, sortdParam ]],
                                    "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,
                                            'st': filterSourceType,
                                            'sc': filterSearchColumn,
                                            's': filterSearch,
                                        },
                                    },
                                    "deferRender": true,
                                    "columns": [
                                        {
                                            "data": "id",
                                            "name": "ao.id",
                                            "searchable": false,
                                            "orderable": false,
                                            className: "col-bulk",
                                            "mRender": function ( data, type, row ) {
                                                return '<input class="dt-checkboxes" name="dt_checkbox[]" type="checkbox" value="'+row.id+'">';
                                            },
                                        },
                                        {
                                            "mRender": function ( data, type, row ) {
                                                return '<a class="action-icon" href="/objekt-bearbeiten/'+row.id+'/?status={{$selectedFilterStatus}}&lang={{$selectedFilterLanguage}}&st={{$selectedFilterSourceType}}&sc={{$selectedFilterSearchColumn}}&s={{$selectedFilterSearch}}&page='+(table.page.info().page+1)+'&len='+(table.page.info().length)+'&sortc='+table.order()[0][0]+'&sortd='+table.order()[0][1]+'"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                                            },
                                            className: "col-action",
                                            orderable: false,
                                            searchable: false,
                                        },
                                        //{ "data": "t_medium_category_id", "name": "t_medium_category_id", "searchable": false },
                                        {
                                            "data": "status",
                                            "name": "ao.status",
                                            "mRender": function ( data, type, row ) {
                                                var dropdown = '';

                                                dropdown += '<select class="" name="" onchange="dynamicAssignStatus(\''+row.id+'\', this)" style="width: 75px;">';
                                                dropdown += '<option value="">- Bitte wählen -</option>';
                                                dropdown += '<option value="1" '+ (row.status == 1 ? 'selected' : '' ) +'>Exportbereit</option>';
                                                dropdown += '<option value="2" '+ (row.status == 2 ? 'selected' : '' ) +'>Exportiert</option>';
                                                dropdown += '<option value="3" '+ (row.status == 3 ? 'selected' : '' ) +'>Prüffall</option>';
                                                dropdown += '</select>';

                                                return dropdown;
                                            },
                                            width: 100,
                                            "searchable": false,
                                        },
                                        { "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_fullname",
                                            "name": "artist_fullname",
                                            "mRender": function ( data, type, row ) {

                                                var artist_fullname = '';

                                                artist_fullname += '<a class="assign-icon" onclick="dynamicAssignArtist(\''+row.id+'\')"><i class="fa fa-search-plus" aria-hidden="true"></i></a>';
                                                artist_fullname += '<span class="assign-artist-label" id="label_'+row.id+'">'+ (data!=null ? '<a class="artist-name" href="/kuenstler-bearbeiten/'+row.t_artist_id+'" target="_blank">'+data+'</a> <a class="delete-assignment-icon" onclick="removeArtistAssignment(\''+row.id+'\')"><i class="fa fa-times-circle" aria-hidden="true"></i></a>' : '' )+'</span>';

                                                return artist_fullname;
                                            },
                                            orderable: true,
                                            "searchable": false,
                                            class: 'col-artist-fixed',
                                        },
                                        { "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": true },
                                        {
                                            "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": true },
                                        {
                                            "data": "medium_category",
                                            "render": function ( data, type, row ) {

                                                    if(type === 'display') {

                                                        var dropdown = '';

                                                        dropdown += '<select class="" name="" onchange="dynamicAssignMediumCategory(\'' + row.id + '\', this)" style="width: 140px;">';
                                                        dropdown += '<option value="">- Bitte wählen -</option>'
                                                        @foreach($mediumCategories as $mediumCategory)
                                                            dropdown += '<option value="{{$mediumCategory->id}}" ' + (row.t_medium_category_id == {{$mediumCategory->id}} ? 'selected' : '') + '>{{ $mediumCategory->name }}</option>';
                                                        @endforeach
                                                            dropdown += '</select>';

                                                        return dropdown;
                                                    } else {
                                                        return data;
                                                    }
                                            },
                                            width: 150,
                                            "searchable": false,
                                            orderable: true
                                        },
                                        { "data": "object_medium", "name": "ao.object_medium", "searchable": true },
                                        {
                                            "mRender": function ( data, type, row ) {
                                                return '<a href="'+row.object_url+'" target="_blank">'+row.object_url+'</a>';
                                            },
                                            searchable: false,
                                        },
                                        { "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": true },
                                        { "data": "object_width", "name": "ao.object_width", "searchable": true },
                                        { "data": "object_depth", "name": "ao.object_depth", "searchable": true },
                                        { "data": "object_size_unit", "name": "ao.object_size_unit", "searchable": true },
                                        {
                                            "mRender": function ( data, type, row ) {
                                                return '<a href="'+row.object_image_url+'" target="_blank">'+row.object_image_url+'</a>';
                                            },
                                            searchable: false,
                                        },
                                        { "data": "auction_loc", "name": "ao.auction_loc", "searchable": true },
                                        { "data": "auction_date", "name": "auction_date", "searchable": false },
                                        { "data": "source_type", "name": "ao.source_type", "searchable": false },
                                        { "data": "object_id", "name": "ao.object_id", "searchable": true },
                                        { "data": "id", "name": "ao.id", "searchable": true, "orderable": true },
                                        { "data": "created_at", "name": "created_at", "searchable": false },
                                        { "data": "crawled_at", "name": "crawled_at", "searchable": false },
                                        @if(Auth::user()->user_role_id == 1)
                                            { "data": "status_message", "name": "status_message", "searchable": false },
                                        @endif
                                    ],
                                    "columnDefs": [
                                        /*{
                                            targets: 0,
                                            'checkboxes': {
                                                'selectRow': true,
                                                'stateSave': true,
                                            },
                                        },*/
                                        {
                                            targets: 4, // Auktion
                                            render: $.fn.dataTable.render.ellipsis( 20, false )
                                        },
                                        {
                                            targets: 7, // Kuenstler VN
                                            render: $.fn.dataTable.render.ellipsis( 20, true )
                                        },
                                        {
                                            targets: 8, // Kuenstler NN
                                            render: $.fn.dataTable.render.ellipsis( 20, true )
                                        },
                                        {
                                            targets: 14, // Technik
                                            render: $.fn.dataTable.render.ellipsis( 30, true )
                                        },
                                        {
                                            targets: 18, // Titel
                                            render: $.fn.dataTable.render.ellipsis( 30, true )
                                        },
                                        {
                                            targets: 27, // Katalog.
                                            render: $.fn.dataTable.render.ellipsis( 30, true )
                                        },
                                        {
                                            targets: 33, // Auktionsort.
                                            render: $.fn.dataTable.render.ellipsis( 30, true )
                                        },
                                        {
                                            targets: 35, // Auktionshaus
                                            render: $.fn.dataTable.render.ellipsis( 30, true )
                                        }
                                    ],
                                    'stateSave': false,
                                    'select': 'multiple',
                                    "language": {
                                        "url": "/plugins/datatables/i18n/German.json",
                                        "processing": "<img src='/images/loading.gif'>",
                                    },
                                    "fnInitComplete": function (settings, json) {
                                        $('#filterContainer').insertAfter('#data-table-auction-objects_filter');
                                        $('#buttonLine').insertAfter('#data-table-auction-objects_filter');
                                        $('#filterContainer').show();
                                    },
                                    "drawCallback": function() {
                                        if(this.fnGetData().length > 0) {
                                            $('#hint-select-data').hide();
                                            $('#data-table-auction-objects_paginate').show();
                                        } else {
                                            $('#hint-select-data').show();
                                            $('#data-table-auction-objects_paginate').hide();
                                        }
                                    },
                                });

                                $('.table-responsive').on('scroll', function(e) {
                                    var left = $(this).scrollLeft();
                                    var left = left - 17;
                                    $('.fixedHeader-floating').css('left', -left);
                                });

                                $('.table-responsive').on( 'order.dt', function () {
                                    // This will show: "Ordering on column 1 (asc)", for example
                                    var order = table.order();
                                    console.log('Ordering on column '+order[0][0]+' '+order[0][1]+'');
                                } );

                                // Bulk edit
                                $('#frm-bulk-edit').on('submit', function(e){

                                    e.preventDefault();

                                    var form = this;

                                    /*var rows_selected = table.column(0).checkboxes.selected();

                                    // Iterate over all selected checkboxes
                                    $.each(rows_selected, function(index, rowId){
                                        // Create a hidden element
                                        $(form).append(
                                            $('<input class="selectedRow">')
                                                .attr('type', 'hidden')
                                                .attr('name', 'id[]')
                                                .val(rowId)
                                        );
                                    });*/

                                    // Ajax request
                                    $.ajax({
                                        url: '/ajax/auction-object-bulk-edit-status',
                                        type: 'POST',
                                        beforeSend: function (xhr) {
                                            var token = $('meta[name="csrf_token"]').attr('content');

                                            if (token) {
                                                return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                                            }
                                        },
                                        data: $('#frm-bulk-edit').serialize(),
                                        success:function(data){
                                            table.ajax.reload(null, false);
                                            //table.column(0).checkboxes.deselect(); // deselect all checkboxes
                                            $('#dt-check-all').prop('checked', false);
                                            $('#bulk_action').val(''); // reset select field
                                            $('.selectedRow').remove(); // remove temporary hidden fields
                                        },
                                        error:function(e){
                                            console.log(e);
                                            console.log('Ajax Error while assigning');
                                        }
                                    });
                                });

                                $(document).on('change', '#dt-check-all', function(e){

                                    if(this.checked){
                                        $('.dt-checkboxes').each(function(){
                                            this.checked = true;
                                        });
                                    }else{
                                        $('.dt-checkboxes').each(function(){
                                            this.checked = false;
                                        });
                                    }
                                });

                                var lastChecked = null;

                                $(document).on('mousedown', '.dt-checkboxes', function(e){


                                    if(!lastChecked) {
                                        lastChecked = this;
                                        return;
                                    }

                                    if(e.shiftKey) {
                                        var from = $('.dt-checkboxes').index(this);
                                        var to = $('.dt-checkboxes').index(lastChecked);

                                        var start = Math.min(from, to);
                                        var end = Math.max(from, to) + 0;

                                        $('.dt-checkboxes').slice(start, end)
                                            .filter(':not(:disabled)')
                                            //.prop('checked', lastChecked.checked).parent().parent().addClass('selected')
                                            .prop('checked', true)
                                            .parent().parent().addClass('selected');

                                        console.log(table.column(0).checkboxes.selected());

                                    }

                                    lastChecked = this;

                                    if(e.altKey){

                                        $('.dt-checkboxes')
                                            .filter(':not(:disabled)')
                                            .each(function () {
                                                var $checkbox = $(this);
                                                $checkbox.prop('checked', !$checkbox.is(':checked'));
                                            });
                                    }

                                });

                                // cut column values
                                $.fn.dataTable.render.ellipsis = function ( cutoff, wordbreak, escapeHtml ) {
                                    var esc = function ( t ) {
                                        return t
                                            .replace( /&/g, '&amp;' )
                                            .replace( /</g, '&lt;' )
                                            .replace( />/g, '&gt;' )
                                            .replace( /"/g, '&quot;' );
                                    };

                                    return function ( d, type, row ) {
                                        // Order, search and type get the original data
                                        if ( type !== 'display' ) {
                                            return d;
                                        }

                                        if ( typeof d !== 'number' && typeof d !== 'string' ) {
                                            return d;
                                        }

                                        d = d.toString(); // cast numbers

                                        if ( d.length < cutoff ) {
                                            return d;
                                        }

                                        var shortened = d.substr(0, cutoff-1);

                                        // Find the last white space character in the string
                                        if ( wordbreak ) {
                                            shortened = shortened.replace(/\s([^\s]*)$/, '');
                                        }

                                        // Protect against uncontrolled HTML input
                                        if ( escapeHtml ) {
                                            shortened = esc( shortened );
                                        }

                                        return '<span class="ellipsis" title="'+esc(d)+'">'+shortened+'&#8230;</span>';
                                    };
                                };

                                $('#inputSearch, #inputPageLen').on( 'keyup', function (e) {

                                    var keycode = (e.keyCode ? e.keyCode : e.which);
                                    if(keycode == '13'){
                                        $('#filterAuctionObjectForm').submit();
                                    }
                                });

                                // page Lenght input field
                                /*var dataTableAuctionObjects = $('#data-table-auction-objects').DataTable();

                                $('#inputPageLen').on( 'keyup', function (e) {

                                    var pageLen = $('#inputPageLen').val(); // get input value

                                    if (pageLen == 0) {
                                        pageLen = 10; // set standard value
                                    }

                                    dataTableAuctionObjects.page.len( pageLen ).draw(); // redraw table after keyup
                                });*/

                                // assign status
                                function dynamicAssignStatus(id, selectObject) {

                                    var auctionObjectId = id;
                                    var status = selectObject.value;

                                    // Ajax request
                                    $.ajax({
                                        url: '/ajax/post-ajax-auction-object-assignment-status',
                                        type: 'POST',
                                        beforeSend: function (xhr) {
                                            var token = $('meta[name="csrf_token"]').attr('content');

                                            if (token) {
                                                return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                                            }
                                        },
                                        data: {
                                            auction_object_id : auctionObjectId,
                                            status : status
                                        },
                                        success:function(data){
                                            //window.location = data.redirectUrl;
                                            table.ajax.reload(null, false);
                                            //alert('Der Status wurde erfolgreich zugewiesen!');
                                        },error:function(e){
                                            console.log(e);
                                            console.log('Ajax Error while assigning');
                                        }
                                    });
                                }

                                // assign artist
                                function dynamicAssignArtist(id, thisObject) {

                                    var auctionObjectId = id;

                                    $('#auction_object_id').val(auctionObjectId);

                                    $('.assign-artist-modal').modal('show');

                                }

                                // assign artist to row
                                function assignArtistToRow(id) {

                                    var artistId = id;
                                    var auctionObjectId = $('#auction_object_id').val();

                                    // Ajax request
                                    $.ajax({
                                        url: '/ajax/post-ajax-auction-object-assignment-artist',
                                        type: 'POST',
                                        beforeSend: function (xhr) {
                                            var token = $('meta[name="csrf_token"]').attr('content');

                                            if (token) {
                                                return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                                            }
                                        },
                                        data: {
                                            auction_object_id : auctionObjectId,
                                            artist_id : artistId,
                                        },
                                        success:function(data){
                                            //$('#label_'+auctionObjectId).text(data).append('<a class="delete-assignment-icon" onclick="removeArtistAssignment(\''+auctionObjectId+'\')"><i class="fa fa-times-circle" aria-hidden="true"></i></a>');
                                            table.ajax.reload(null, false);
                                            //alert('Der Künstler wurde erfolgreich zugewiesen!');
                                        },error:function(e){
                                            console.log(e);
                                            console.log('Ajax Error while assigning');
                                        }
                                    });

                                    $('.assign-artist-modal').modal('hide');
                                }

                                // remove artist assignment
                                function removeArtistAssignment(id) {

                                    var auctionObjectId = id;

                                    // Ajax request
                                    $.ajax({
                                        url: '/ajax/post-ajax-delete-auction-object-artist-assignment',
                                        type: 'POST',
                                        beforeSend: function (xhr) {
                                            var token = $('meta[name="csrf_token"]').attr('content');

                                            if (token) {
                                                return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                                            }
                                        },
                                        data: {
                                            auction_object_id : auctionObjectId
                                        },
                                        success:function(data){
                                            //$('#label_'+auctionObjectId).text('');
                                            table.ajax.reload(null, false);
                                            //alert('Der Künstlerzuweisung wurde erfolgreich entfernt!');
                                        },error:function(e){
                                            console.log(e);
                                            console.log('Ajax Error while assigning');
                                        }
                                    });
                                }

                                // assign medium category
                                function dynamicAssignMediumCategory(id, selectObject) {

                                    var auctionObjectId = id;
                                    var mediumCategoryId = selectObject.value;

                                    // Ajax request
                                    $.ajax({
                                        url: '/ajax/post-ajax-auction-object-assignment',
                                        type: 'POST',
                                        beforeSend: function (xhr) {
                                            var token = $('meta[name="csrf_token"]').attr('content');

                                            if (token) {
                                                return xhr.setRequestHeader('X-CSRF-TOKEN', token);
                                            }
                                        },
                                        data: {
                                            auction_object_id : auctionObjectId,
                                            medium_category_id : mediumCategoryId
                                        },
                                        success:function(data){
                                            //window.location = data.redirectUrl;
                                            table.ajax.reload(null, false);
                                            //alert('Die Grobtechnik wurde erfolgreich zugewiesen!');
                                        },error:function(e){
                                            console.log(e);
                                            console.log('Ajax Error while assigning');
                                        }
                                    });
                                }

                                $(document).on('change', '.dt-checkboxes-select-all input', function(e){

                                    if ($(this).prop('checked')) {
                                        $('.dt-checkboxes').parent().parent().addClass('selected');
                                    } else {
                                        $('.dt-checkboxes').parent().parent().removeClass('selected');
                                    }
                                });

                                $(document).on('change', '.dt-checkboxes', function(e){

                                    if ($(this).prop('checked')) {
                                        $(this).parent().parent().addClass('selected');
                                    } else {
                                        $(this).parent().parent().removeClass('selected');
                                    }
                                });



                                /*$('#data-table-auction-objects tbody').on( 'mouseenter', 'td', function () {
                                    var colIdx = table.cell(this).index().column;

                                    $( table.cells().nodes() ).removeClass( 'highlight' );
                                    $( table.column( colIdx ).nodes() ).addClass( 'highlight' );
                                });

                                $('#data-table-auction-objects tbody').on( 'mouseleave', 'td', function () {
                                    $( table.cells().nodes() ).removeClass( 'highlight' );
                                });*/


                            </script>

                            <div class="modal fade assign-artist-modal" tabindex="-1" role="dialog" aria-labelledby="assignArtistModalLabel" aria-hidden="true">
                                <div class="modal-dialog modal-lg">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                            <h4 class="modal-title" id="artistModalTitle">Künstler auswählen und zuweisen</h4>
                                        </div>
                                        <div class="modal-body">
                                            <div class="table-responsive">
                                                <input type="hidden" id="auction_object_id">
                                                <table id="data-table-assign-artist" class="data-table display table table-data-width nowrap">
                                                    <thead>
                                                    <tr>
                                                        <th class="col-action">Aktionen</th>
                                                        <th>Status</th>
                                                        <th>Geb.Datum</th>
                                                        <th>Gest. Datum</th>
                                                        <th class="col-min-120">Vorname EN</th>
                                                        <th class="col-min-120">Nachname EN</th>
                                                        <th class="col-min-120">Vorname DE</th>
                                                        <th class="col-min-120">Nachname DE</th>
                                                        <th class="col-min-120">Vorname FR</th>
                                                        <th class="col-min-120">Nachname FR</th>
                                                        <th class="col-min-120">ID</th>
                                                    </tr>
                                                    </thead>
                                                </table>

                                                <script type="text/javascript">
                                                    $('#data-table-assign-artist').DataTable({
                                                        processing: true,
                                                        serverSide: true,
                                                        ordering: true,
                                                        "autoWidth": false,
                                                        "pageLength": 10,
                                                        "lengthMenu": [ 10, 20, 50, 100 ],
                                                        //'stateSave': false,
                                                        "ajax": {
                                                            "url": "/ajax/ajax-assign-artist-overview",
                                                            "type": "GET",
                                                            "dataSrc": "data",
                                                        },
                                                        "deferRender": true,
                                                        "columns": [
                                                            {
                                                                "mRender": function ( data, type, row ) {
                                                                    return '<a href="javascript:void(0)" title="Künstler zuweisen" onclick="assignArtistToRow('+row.id+')"><i class="fa fa-plus-circle" aria-hidden="true"></i></a>';
                                                                },
                                                                className: "col-action",
                                                                "width": "90px"
                                                            },
                                                            {
                                                                "data": "status",
                                                                "name": "a.status",
                                                                "render": function (val, type, row) {
                                                                    if(val == 0) {
                                                                        return 'Neu';
                                                                    } else if(val == 1) {
                                                                        return 'Geprüft';
                                                                    } else if(val == 2) {
                                                                        return 'Nicht Künstler';
                                                                    } else if(val == 3) {
                                                                        return 'Online-Geprüft';
                                                                    } else {
                                                                        return '';
                                                                    }
                                                                }
                                                            },
                                                            { "data": "born_date", "name": "a.born_date", "searchable": false },
                                                            { "data": "died_date", "name": "a.died_date", "searchable": false },
                                                            { "data": "firstname_en", "name": "a.firstname_en", "searchable": true },
                                                            { "data": "lastname_en", "name": "a.lastname_en", "searchable": true },
                                                            { "data": "firstname_de", "name": "a.firstname_de", "searchable": true },
                                                            { "data": "lastname_de", "name": "a.lastname_de", "searchable": true },
                                                            { "data": "firstname_fr", "name": "a.firstname_fr", "searchable": true },
                                                            { "data": "lastname_fr", "name": "a.lastname_fr", "searchable": true },
                                                            { "data": "id", "name": "a.id", "width": "120px", "searchable": true }
                                                        ],
                                                        "language": {
                                                            "url": "/plugins/datatables/i18n/German.json"
                                                        },
                                                    });

                                                    function submitFilterForm(fieldKey) {

                                                        if(fieldKey == 1) {
                                                            // reset status dropdown
                                                            $('#selectFilterObjectStatus').prop('selectedIndex', 0);

                                                            // reset auction dropdown
                                                            $('#selectFilterObjectSourceType').prop('selectedIndex', 0);
                                                        }
                                                        else if(fieldKey == 2) {
                                                            // reset auction dropdown
                                                            $('#selectFilterObjectSourceType').prop('selectedIndex', 0);
                                                        }

                                                        $('#filterAuctionObjectForm')[0].submit();
                                                    }
                                                </script>
                                            </div>

                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-info" data-dismiss="modal">Schließen</button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>


            </div>
        </div><!-- Row -->
    </div><!-- Main Wrapper -->


@stop
