<div class="card panel-info shadow-sm rounded">
    <div class="card-header text-center">
        <span class="fcs-card-title">[% if $day.today %][%$user->gettext('Heute')%][%else%][% $day.dayname %][% /if %]</span>
    </div>
    <div class="card-body">
        [% foreach from=$day.dateday item=calevent %][% include file=$modulePath|cat:"/calevent.tpl" %][% foreachelse %][% /foreach %]
    </div>
</div>

<table class="table table-bordered daycal dayview">
    [% foreach from=$daytimes item=time %]
        [% assign var=count value=$Tools->FCS_Count($day.dates[$time]) %]
        <tr class="row0">
            <th rowspan="[% if $count < 3 %]2[% else %][% $count %][% /if %]">[% $time %]</th>
                [% foreach from=$day.dates[$time] item=date name=lfdnr %]
                    [% if $smarty.foreach.lfdnr.iteration > 1 %]<tr class="row1">
                <div id="[% $date %][% $time %]_1" ondragover="ablegenErlauben(event)" ondrop="ablegen(event)">
                [% /if %]
                <td [% if $day.today %] class="today"[% /if %]>

                    [% assign var=calevent value=$date %][% include file=$modulePath|cat:"/calevent.tpl" %]


                </td>
            </div>                            
        </tr>
    [% foreachelse %]
        <td [% if $day.today %] class="today"[% /if %]>
            <div id="[% $date2 %][% $time %]_1" ondragover="ablegenErlauben(event)" ondrop="ablegen(event)">
                &nbsp;
            </div>                                
        </td>
    </tr>
[% /foreach %]
[% if $count < 2 %]
    <tr class="row1">
        <td [% if $day.today %] class="today"[% /if %] id="[% $date2 %][% $time %]_2" ondragover="ablegenErlauben(event)" ondrop="ablegen(event)">&nbsp;</td>
    </tr>
[% /if %]
[% /foreach %]
</table>

<script>
    function ziehen(ev) {
        if(!ev.target.id){
            ev.target.id = ev.target.parentNode.id;            
        }
        ev.dataTransfer.setData('text', ev.target.id);
    }

    function ablegenErlauben(ev) {
        ev.preventDefault();
    }

    function ablegen(ev) {
        ev.preventDefault();
        var data = ev.dataTransfer.getData('text');
        var dragedObject = document.getElementById(data);
        
        


var TicketID = dragedObject.dataset.ticketid;
var id = ev.target.id;
var matches = id.match(/(\d{4})(\d{2})(\d{2})(\d{2}):(\d{2})/);
if (matches === null) {
    // invalid
} else{
    // now lets check the date sanity
    var year = parseInt(matches[1], 10);
    var month = parseInt(matches[2], 10) - 1; 
    var day = parseInt(matches[3]);
    var hour = parseInt(matches[4], 10);
    var minute = parseInt(matches[5], 10);
    var date = new Date(year, month, day, hour, minute, '00');   

    if (date.getFullYear() !== year
      || date.getMonth() != month
      || date.getDate() !== day
      || date.getHours() !== hour
      || date.getMinutes() !== minute
    ) {
       //alert("Fehler");
    } else {
       TicketDesireDate = addZero(date.getDate()) + "." + addZero(date.getMonth() + 1) + "." + date.getFullYear() + " " + addZero(date.getHours()) +":" + addZero(date.getMinutes());

var url = "index.php?module=ticket&event=RESTUpdateTicketDesireDate";

bootbox.confirm('[% $user->gettext('Möchten Sie das Fälligkeitsdatum ändern?') %]', function(result){
    if(result){
                $.ajax({
                    type: "POST",
                    url: url,
                    data: {tid: TicketID, save: 1, newTicketDesireDate: TicketDesireDate}
                }).done(function (json) {
                            if (json.error) {
                                noty({
                                    text: json.message[0],
                                    type: 'error',
                                    timeout: notifyTimeout
                                });

                            } else {
                                ev.target.appendChild(dragedObject);
                                noty({
                                    text: json.message[0],
                                    type: 'success',
                                    timeout: notifyTimeout
                                });
                            }
                        });
                    }
                })


            }

        }

    }

    function addZero(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }

</script>

<style>
    [draggable=true] {
        cursor: move;
    }
    
.calevent {
    display: inline-block;
    border: 1px solid grey;
    padding: 2px;
    border-radius: 5px;
    background: lightgrey;
}    
</style>
