[{include file="headitem.tpl" title="GENERAL_ADMIN_TITLE"|oxmultilangassign}]

<link rel="stylesheet" href="[{$oViewConf->getModuleUrl('ys_shipcloud', 'out/css/ys_shipcloud.min.css')}]">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-simple-datetimepicker@1.12.0/jquery.simple-dtpicker.min.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" integrity="sha256-0rguYS0qgS6L4qVzANq4kjxPLtvnp5nn2nB5G1lWRv4=" crossorigin="anonymous"></script>
<script type="text/javascript" src="[{$oViewConf->getModuleUrl('ys_shipcloud', 'out/js/ys_shipcloud.min.js')}]"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-simple-datetimepicker@1.12.0/jquery.simple-dtpicker.min.js"></script>


[{if $readonly}]
    [{assign var="readonly" value="readonly disabled"}]
    [{else}]
    [{assign var="readonly" value=""}]
    [{/if}]

<form name="transfer" id="transfer" action="[{$oViewConf->getSelfLink()}]" method="post">
    [{$oViewConf->getHiddenSid()}]
    <input type="hidden" name="oxid" value="[{$oxid}]">
    <input type="hidden" name="cl" value="ShipcloudOrder">
    <input type="hidden" name="actshop" value="[{$oViewConf->getActiveShopId()}]">
</form>


[{if $edit->oxorder__oxdelfname->value or $edit->oxorder__oxdellname->value or $edit->oxorder__oxdelstreet->value}]
    [{assign var="daActive" value=1}]
    [{else}]
    [{assign var="daActive" value=0}]
    [{/if}]

<form name="myedit2" id="myedit2" action="[{$oViewConf->getSelfLink()}]" method="post" class="ys--shipcloud">
    [{$oViewConf->getHiddenSid()}]
    <input type="hidden" name="cur" value="[{$oCurr->id}]">
    <input type="hidden" name="cl" value="ShipcloudOrder">
    <input type="hidden" name="fnc" value="changeShipcloudPackage">
    <input type="hidden" name="actshop" value="[{$oViewConf->getActiveShopId()}]">
    <input type="hidden" name="oxid" value="[{$oxid}]">
    <input type="hidden" name="editval[oxshops__oxid]" value="[{$oxid}]">
    <input type="hidden" name="options[shipcloud_id]" value="">
    [{assign var="show_order_articles" value=0}]
    [{capture name="order_articles"}]
    [{foreach from=$oView->getOrderArticles() item=order_article name=order_articles}]
    [{assign var="send" value=$oView->getSend($order_article->oxorderarticles__oxid->value)}]
    [{assign var="amount" value=$order_article->oxorderarticles__oxamount->value}]
    [{assign var="available" value=0}]
    [{math equation="a - b" a=$amount b=$send assign=available}]
    [{if $available > 0}][{assign var="show_order_articles" value=1}]
    <tr>
        <td class="listitem"><input type="checkbox" class="form-control" checked name="order_articles[[{$order_article->oxorderarticles__oxid->value}]][active]"></td>
        <td class="listitem"><input type="number" class="form-control" min=0 max=[{$available}] value=[{$available}] name="order_articles[[{$order_article->oxorderarticles__oxid->value}]][amount]"></td>
        <td class="listitem">[{$send}]/[{$amount}]</td>
        <td class="listitem">[{$order_article->oxorderarticles__oxartnum->value}]</td>
        <td class="listitem">[{$order_article->oxorderarticles__oxtitle->value}]</td>
        <td class="listitem">[{$order_article->oxorderarticles__oxselvariant->value}]</td>
        <td class="listitem"><input type="number" step="0.001" class="form-control" min=0 value=[{$order_article->oxorderarticles__oxweight->value}] name="order_articles[[{$order_article->oxorderarticles__oxid->value}]][weight]"></td>
        <td class="listitem">[{$order_article->oxorderarticles__oxprice->value}] [{$edit->oxorder__oxcurrency->value}]</td>
    </tr>
    [{/if}]
    [{/foreach}]
    [{/capture}]
    [{$debug_session}]
    [{if $show_order_articles}]
    <h2 style="margin-top: -10px;">Noch nicht gepackte Artikel</h2>
    <table class="table--order-articles mb-1" cellspacing="0" cellpadding="0" border="0" width="98%">
        <tr>
            <td class="listheader" width="5%">Im Paket</td>
            <td class="listheader" width="5%">#Artikel</td>
            <td class="listheader" width="15%">Anzahl in Paketen / Gesamt</td>
            <td class="listheader" width="15%">Art.Num</td>
            <td class="listheader" width="15%">Title</td>
            <td class="listheader" width="15%">Variante</td>
            <td class="listheader" width="10%">Gewicht in kg</td>
            <td class="listheader">Preis</td>
        </tr>
        [{$smarty.capture.order_articles}]
    </table>
    <div class="input-group input-group-sm mb-1">
        <select name="options[packagetemplate]" [{$ys_disabled}]>
            <option value="" [{if $options.packagetemplate == ""}]SELECTED[{/if}]>Paketvorlage</option>
            [{foreach from=$oView->getPackageTemplates() item=oPackageTemplate}]
            <option value="[{$oPackageTemplate.name}]" [{if $options.packagetemplate == $oPackageTemplate.name}]SELECTED[{/if}]>[{$oPackageTemplate.name}]</option>
            [{/foreach}]
        </select>
        <div class="input-group-append">
            <button class="btn btn-sm btn-success" onclick="ysSubmitFunction(event,'createNewShipcloudPackage')">[{oxmultilang ident="YS_NEW_PACKAGE"}]</button>
        </div>
    </div>
<hr>
    [{/if}]

    [{assign var="open_shipcloud_form" value=0}]
    [{assign var="show_shipcloud_articles" value=0}]
    [{capture name="shipcloud_articles"}]
    [{foreach from=$oView->getOrderShipcloudPackages() item=shipcloudpackage name=shipcloudPackage}]
    [{assign var="status" value=$shipcloudpackage->getStatus()}]
    [{assign var="show_shipcloud_articles" value=1}]
    [{assign var="shipcloud_oxid" value=$shipcloudpackage->ysshipcloud__oxid->value}]
    [{assign var="yscarrier" value=$shipcloudpackage->ysshipcloud__yscarrier->value}]
    [{assign var="ysservice" value=$shipcloudpackage->ysshipcloud__yscarrierservice->value}]

    [{if $shipcloud_oxid==$options.shipcloud_id}]
    [{assign var="open_shipcloud_form" value=1}]
    [{/if}]
    <tr id="art_[{$smarty.foreach.shipcloudPackage.iteration}]" class="[{if $shipcloud_oxid==$options.shipcloud_id}]active[{/if}]">
        <td class="listitem">
            <button class="btn btn-sm [{if $status==-1}]btn-outline-dark[{elseif $status==0}]btn-warning[{else}]btn-success[{/if}]" onclick="changeEdit(event,'[{$shipcloudpackage->ysshipcloud__oxid->value}]', '#art_[{$smarty.foreach.shipcloudPackage.iteration}]')">[{$smarty.foreach.shipcloudPackage.iteration}]</button>
            [{if $ysservice == 'returns'}]
            <span class="btn btn-sm btn-outline-dark fa fa-reply" data-toggle="tooltip" data-html="true" title="Retourensendung"></span>
            [{/if}]
        </td>
        <td class="listitem">
            <div class="input-group">
                [{if $status==-1}]
                <button class="btn btn-sm btn-danger fa fa-trash" onclick="ysSubmitFunction(event, 'deleteShipcloudPackage', {'options[shipcloud_id]':'[{$shipcloudpackage->ysshipcloud__oxid->value}]'})" data-toggle="tooltip" title="Paket löschen"></button>
                <button class="btn btn-sm btn-warning fas fa-plus" onclick="ysSubmitFunction(event, 'createShipcloudLabel', {'options[shipcloud_id]':'[{$shipcloudpackage->ysshipcloud__oxid->value}]'})" data-toggle="tooltip" title="Shipcloud Label erstellen"></button>
                [{/if}]
                [{if $status>-1}]
                <button class="btn btn-sm btn-danger fa fa-trash-alt" onclick="ysSubmitFunction(event, 'deleteShipcloudLabel', {'options[shipcloud_id]':'[{$shipcloudpackage->ysshipcloud__oxid->value}]'})" data-toggle="tooltip" title="Shipcloud Label löschen"></button>
                [{/if}]
                [{if $status==0}]
                <button class="btn btn-sm btn-warning fas fa-save" onclick="ysSubmitFunction(event, 'createShipcloudLabel', {'options[shipcloud_id]':'[{$shipcloudpackage->ysshipcloud__oxid->value}]'})" data-toggle="tooltip" title="Shipcloud Label aktualisieren"></button>
                [{/if}]
                [{if $status==1}]
                <button class="btn btn-sm btn-success fa fa-envelope" onclick="ysSubmitFunction(event, 'sendShipcloudPackageMail', {'options[shipcloud_id]':'[{$shipcloudpackage->ysshipcloud__oxid->value}]'})" data-toggle="tooltip" title="Versand Mail verschicken"></button>
                [{assign var="pickupStatus" value=$shipcloudpackage->ysshipcloud__yspickuptime->value}]
                [{if $pickupStatus}]
                <span class="btn btn-sm btn-outline-dark fa fa-truck-pickup" data-toggle="tooltip" data-html="true" title="[{$pickupStatus}][{$shipcloudpackage->ysshipcloud__yspickupaddress->value}]"></span>
                [{else}]
                <button class="btn btn-sm btn-success fa fa-truck-pickup" onclick="ysPickupRequest(event, '[{$shipcloudpackage->ysshipcloud__oxid->value}]')" data-toggle="tooltip" title="Pickup Request"></button>
                [{/if}]
                [{if $oView->isReturnAvailable($yscarrier) and $ysservice != 'returns'}]
                <button class="btn btn-sm btn-success fa fa-reply" onclick="ysSubmitFunction(event, 'copyAsRetourLabel', {'options[shipcloud_id]':'[{$shipcloudpackage->ysshipcloud__oxid->value}]'})" data-toggle="tooltip" title="Retouren Label erstellen"></button>
                [{/if}]
                [{/if}]
                [{if $status<1}]
                <button class="btn btn-sm btn-success fa fa-shopping-cart" onclick="ysSubmitFunction(event, 'buyShipcloudLabel', {'options[shipcloud_id]':'[{$shipcloudpackage->ysshipcloud__oxid->value}]'})" data-toggle="tooltip" title="Shipcloud Label kaufen"></button>
                [{/if}]
            </div>
        </td>
        <td class="listitem">
            [{if $status==1}]
            <a href="[{$shipcloudpackage->ysshipcloud__ystrackingurl->value}]" target="_blank" rel="noopener noreferrer" class="btn btn-sm btn-outline-dark fas fa-external-link-alt" data-toggle="tooltip" title="Url in neuem Tab öffnen"></a>
            <button class="onclick-cp btn btn-sm btn-outline-dark fas fa-clipboard" data-toggle="tooltip" title="Url kopieren">
                <input type="text" value="[{$shipcloudpackage->ysshipcloud__ystrackingurl->value}]">
            </button>
            [{/if}]
        </td>
        <td class="listitem">
            [{if $status==1}]
            [{$shipcloudpackage->ysshipcloud__ysshipcloudtrackingnumber->value}]
            <button class="onclick-cp btn btn-sm btn-outline-dark fas fa-clipboard" data-toggle="tooltip" title="Tracking Nummer kopieren">
                <input type="text" value="[{$shipcloudpackage->ysshipcloud__ysshipcloudtrackingnumber->value}]">
            </button>
            [{/if}]
        </td>
        <td class="listitem">
            [{if $status==1 and $shipcloudpackage->ysshipcloud__yspickupnumber->value}]
            [{$shipcloudpackage->ysshipcloud__yspickupnumber->value}]
            <button class="onclick-cp btn btn-sm btn-outline-dark fas fa-clipboard" data-toggle="tooltip" title="Pickup Nummer kopieren">
                <input type="text" value="[{$shipcloudpackage->ysshipcloud__ysshipcloudtrackingnumber->value}]">
            </button>
            [{/if}]
        </td>
        <td class="listitem">
            [{if $status==1}]
            <a href="[{$shipcloudpackage->ysshipcloud__ysshipcloudlabelurl->value}]" target="_blank" rel="noopener noreferrer" class="btn btn-sm btn-outline-dark fas fa-external-link-alt" data-toggle="tooltip" title="Url in neuem Tab öffnen"></a>
            <button class="onclick-cp btn btn-sm btn-outline-dark fas fa-clipboard" data-toggle="tooltip" title="Url kopieren">
                <input type="text" value="[{$shipcloudpackage->ysshipcloud__ysshipcloudlabelurl->value}]">
            </button>
            [{/if}]
        </td>
        <td class="listitem">
            [{if $status==1}]
            [{$shipcloudpackage->ysshipcloud__ysshipclouddeliverycosts->value}] [{$edit->oxorder__oxcurrency->value}]
            [{/if}]
        </td>
        <td class="listitem">
            [{if $status==1}]
            [{$shipcloudpackage->ysshipcloud__yssendmaildate->value}]
            [{/if}]
        </td>
    </tr>
    [{/foreach}]
    [{/capture}]

    [{if $show_shipcloud_articles}]
    <table class="table--shipcloud-articles mb-1" cellspacing="0" cellpadding="0" border="0" width="98%">
        <tr>
            <td class="listheader" width="7%">Paket Status</td>
            <td class="listheader">Aktionen</td>
            <td class="listheader">Shipcloud Link</td>
            <td class="listheader">Tracking Nummer</td>
            <td class="listheader">Pickup Nummer</td>
            <td class="listheader">Shipcloud Label</td>
            <td class="listheader">Kosten</td>
            <td class="listheader">Versand Mail</td>
        </tr>
        [{$smarty.capture.shipcloud_articles}]
    </table>
    [{/if}]
</form>

<div class="editShipcloud">

</div>

<script>[{capture name="ys_shipcloud"}][{strip}]
    var stoken = $('input[name="stoken"]').val();
    var force_admin_sid = $('input[name="force_admin_sid"]').val();
    if (!force_admin_sid){
        force_admin_sid = $('input[name="admin_sid"]').val();
    }
    var active_shop = $('input[name="actshop"]').val();

    var form = $('form[name="myedit2"]');

    $(document).ready(function () {
        $('[data-toggle="tooltip"]').tooltip();
        if (!Modernizr.inputtypes['datetime-local']) {
            $('input[type=datetime-local]').appendDtpicker({locale: "de"});
        }
    });

    function ysSubmitFunction(e, fnc, parameters = {}) {
        e.preventDefault();
        form.children('input[name="fnc"]').val(fnc);
        for (var parameter in parameters) {
            form.children('input[name="' + parameter + '"]').val(parameters[parameter]);
        }
        var shipcloudForm = $('.editShipcloud form[name="myedit"]');
        if (shipcloudForm.length) {
            var data = shipcloudForm.serialize();
            console.log(data);

            $.ajax({
                url: "index.php?cl=ShipcloudFormController&fnc=save&oxid=" + oxid + "&stoken=" + stoken + "&force_admin_sid=" + force_admin_sid + "&actshop=" + active_shop,
                type: "post",
                data: {
                    parameters: data
                },
                success: function (data) {
                    editSuccess(data);
                    if (typeof order_form_messages !== 'undefined') {
                        if (order_form_messages.length == 0) {
                            form.submit();
                        }
                    } else {
                        form.submit();
                    }
                }
            })
        } else {
            form.submit();
        }
    }

    $('.onclick-cp').on('click', function ysCopyToClipboard(event) {
        event.preventDefault();
        var $this = $(this);
        var element = $this.children();
        element.select();
        document.execCommand("copy");
    });

    function editSuccess(data, oxid) {
        $(".editShipcloud").html(data);
        $('.editShipcloud .onChangeSubmit').on('change', function (e) {
            e.preventDefault();
            var form_data = $(this).closest("form").serialize();
            $.ajax({
                url: "index.php?cl=ShipcloudFormController&fnc=save&oxid=" + oxid + "&stoken=" + stoken + "&force_admin_sid=" + force_admin_sid + "&actshop=" + active_shop,
                type: "post",
                data: {
                    parameters: form_data
                },
                success: function (data) {
                    editSuccess(data, oxid)
                },
                error: function () {
                    alert('Unbekannter Fehler');
                }
            })
        });
        $('.editShipcloud .onChangeSubmitAndSave').on('change', function (e) {
            ysSubmitFunction(e, 'save');
        });
        $('.editShipcloud form[name="myedit"]').on('submit', function (e) {
            ysSubmitFunction(e, 'save');
        });
    }

    [{if $open_shipcloud_form==1}]
    var options = [{$options_string}];
    if (options && options['shipcloud_id']) {
        changeEdit(false, options['shipcloud_id']);
    }
    [{/if}]

    function changeEdit(e, oxid = "", element_id = "") {
        if (e) {
            e.preventDefault();
        }

        if (element_id) {
            $('tr[id^="art_"]').removeClass('active');
            $(element_id).addClass('active');
        }

        form.children('input[name="options[shipcloud_id]"]').val(oxid);

        if (oxid) {
            $.ajax({
                url: "index.php?cl=ShipcloudFormController&oxid=" + oxid + "&stoken=" + stoken + "&force_admin_sid=" + force_admin_sid + "&actshop=" + active_shop,
                type: "post",
                success: function (data) {
                    editSuccess(data, oxid);
                },
                error: function () {
                    alert('Unbekannter Fehler');
                }
            });
        }
        else {
            $('tr[id^="art_"]').removeClass('active');
            $(".editShipcloud").html("");
        }
    }

    function ysPickupRequest(e, shipcloud_id) {
        e.preventDefault();
        $('form[name="ys-pickup-request"] input[name="options[shipcloud_id]"]').val(shipcloud_id);
        $('#ysPickupRequest').modal();
    }

    var order_messages = [{$order_message}];
    if (order_messages) {
        order_messages.forEach(function (m) {
            console.log(m);
            var order = m.order;
            if (order) {
                order = 'Order: ' + m.order + '<br>\n';
            } else {
                order = "";
            }
            var info = m.info;
            if (!info) {
                info = "";
            }
            $.notify({
                message: order + m.text + '<br>\n' + info
            }, {
                element: 'body',
                type: m.type,
                delay: m.time,
            });
        });
    }
    [{/strip}][{/capture}]</script>
[{oxscript add=$smarty.capture.ys_shipcloud}]

[{oxscript}]

<div class="modal ys--shipcloud" id="ysErrorModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Modal body text goes here.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>


<form name="ys-pickup-request" action="[{$oViewConf->getSelfLink()}]" method="post" class="ys--shipcloud">
    [{$oViewConf->getHiddenSid()}]
    <input type="hidden" name="cur" value="[{$oCurr->id}]">
    <input type="hidden" name="cl" value="ShipcloudOrder">
    <input type="hidden" name="fnc" value="sendPickupRequest">
    <input type="hidden" name="actshop" value="[{$oViewConf->getActiveShopId()}]">
    <input type="hidden" name="oxid" value="[{$oxid}]">
    <input type="hidden" name="options[shipcloud_id]" value="">
    <div class="modal ys--shipcloud" tabindex="-1" role="dialog" id="ysPickupRequest">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Pickup Request</h5>
                </div>
                <div class="modal-body">
                    <div class="ys--row row">
                        <div class="col-6 form-group">
                            <label class="control-label">[{oxmultilang ident="YS_PICKUP_TIME_FROM"}]</label>
                            <input type="datetime-local" name="pickupTime[from]" class="form-control" value="[{$pickupTime.from}]"/>
                        </div>
                        <div class="col-6 form-group">
                            <label class="control-label">[{oxmultilang ident="YS_PICKUP_TIME_TO"}]</label>
                            <input type="datetime-local" name="pickupTime[to]" class="form-control" value="[{$pickupTime.to}]"/>
                        </div>
                        <div class="col-6 form-group">
                            <label class="control-label">[{oxmultilang ident="YS_PICKUP_ADDRESS"}]</label>
                            <select class="form-control" name="options[pickup_address]" [{$ys_disabled}]>
                                <option value="" [{if !$options.pickup_address}]SELECTED[{/if}]>---</option>
                                [{foreach from=$oView->getPickupAddresses() item=oShipcloudAddress}]
                                <option value="[{$oShipcloudAddress->getId()}]" [{if $options.pickup_address == $oShipcloudAddress->getId()}]SELECTED[{/if}]>[{$oShipcloudAddress->ysshipcloudaddress__addressname->value}]</option>
                                [{/foreach}]
                            </select>
                        </div>

                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-sm btn-success">Send Pickup Request</button>
                    <button type="button" class="btn btn-sm btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</form>