<?php
$filter = ['id' => $_GET['id']];
$order = $auth->getApiDataFilter("get/orders", $filter);
$order = $order[0];
echo "<pre>";
//print_r($order);
echo "</pre>";
$editable = true;
foreach ($order->body as $b) {
    if ($b->status != 0)
        $editable = false;
}
//var_dump($editable);
if (!$editable) :
?>
    <form id="sendForm" class="row w-100" method="POST">
        <div class="col-lg-4 col-sm-12">
            <div class="card">
                <div class="card-header">
                    <h4><?php echo "#PASS-{$filter['id']}" ?></h4>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-lg-4 div-sm-6">
                            <h6>Megrendelő</h6>
                        </div>
                        <div class="col-lg-8 div-sm-6">
                            <strong><?php echo $order->customer->companyName ?></strong>
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-lg-4 div-sm-6">
                            <h6>Megjegyzés</h6>
                        </div>
                        <div class="col-lg-8 div-sm-6">
                            <?php echo (isset($order->orderComment)) ? $order->orderComment : ''; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-4 col-sm-12">
            <div class="card customerParameters">
                <div class="card-header">
                    <h4>Megrendelés paraméterei</h4>
                </div>
                <div class="card-body">
                    <div>
                        <h5>Számlázási adatok:</h5>
                        <?php
                        $tempCustomer = $auth->getApiDataFilter("get/partner/short", ['id' => $order->customer->id]);
                        $tempCustomer = $tempCustomer[0];
                        $customer = $tempCustomer;
                        ?>
                        <div><?php echo $customer->companyName ?></div>
                        <div><?php echo $customer->companyZip . " " . $customer->companyCity . ", " . $customer->companyAdressLocation ?></div>
                        <div><?php echo (isset($customer->companyVat)) ? $customer->companyVat : 'Nincs megadva Adószám' ?> </div>
                        <div><?php echo (isset($customer->companyVatEU)) ? $customer->companyVatEU : 'Nincs megadva EU adószám' ?> </div>
                        <div><?php echo (isset($order->total)) ? "Összesen: " . $order->total . " Ft" : '' ?> </div>
                    </div>
                    <?php if ($customer->companyTLocation2 != "") : ?>
                        <hr>
                        <div>
                            <h5>Szállítási adatok:</h5>
                            <div><?php echo $customer->companyLocationZip . " " . $customer->companyLocationCity . ", " . $customer->companyTLocation2 ?></div>
                        </div>
                    <?php endif; ?>
                    <?php if ($customer->companyPostalLocation2 != "") : ?>
                        <hr>
                        <div>
                            <h5>Postázási adatok:</h5>
                            <div><?php echo $customer->companyPostalZip . " " . $customer->companyPostalCity . ", " . $customer->companyPostalLocation2 ?></div>
                        </div>
                    <?php endif; ?>
                    <div>
                        <h5>Kapcsolattartás:</h5>
                        <?php
                            $full = $auth->getApiDataFilter("get/partner", ['id' => $order->customer->id]);
                            $full = (isset($full[0]))?$full[0]:[];
                        ?>
                        <div><?php echo (isset($full->contactName))?$full->contactName:''?></div>
                        <div><?php echo (isset($full->contactTel))?"<a href=\"tel:{$full->contactTel}\">{$full->contactTel}</a>":''?></div>
                        <div><?php echo (isset($full->contactEmail))?"<a href=\"mailto:{$full->contactEmail}\">{$full->contactEmail}</a>":''?></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-4 col-sm-12">
            <div class="card">
                <div class="card-header">
                    <h4>Megrendelés egyéb</h4>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-4">
                            Értékesítő</br>
                            <small>XXX</small>
                        </div>
                        <div class="col-8">
                            <?php
                            if (isset($order->salesPersonName) && $order->salesPersonName != null) {
                                echo $order->salesPersonName;
                            }
                            ?>
                        </div>
                    </div>
                    <hr>
                    <div class="row">
                        <div class="col-4">
                            Fizetési határidő:</br>
                        </div>
                        <div class="col-8">
                            <?php echo (isset($order->paymentDueDate)) ? $order->paymentDueDate : ''; ?>
                        </div>
                    </div>
                    <hr>
                    <?php
                    if ($user->accesslevel < 2) {
                    ?>
                        <div class="row">
                            <div class="col-4">
                                Engedélyezés</br>
                                <small><?php
                                        if ($order->orderAcceptedBy != null) {
                                            echo $order->orderAcceptedBy;
                                        }
                                        ?></small>
                            </div>
                            <div class="col-8">
                                <?php
                                $selected = (isset($order->orderAccept)) ? $order->orderAccept : 0;
                                switch ($selected) {
                                    case 0:
                                        echo "Nincs";
                                        break;
                                    case 1:
                                        echo "Elutasítva";
                                        break;
                                    case 2:
                                        echo "Engedélyezve";
                                        break;
                                }
                                ?>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col-4">
                                Fizetési állapot</br>
                                <small>XXX</small>
                            </div>
                            <div class="col-8">
                                <?php
                                $selected = (isset($order->orderStatus)) ? $order->orderStatus : 0;
                                switch ($selected) {
                                    case 0:
                                        echo "Nincs";
                                        break;
                                    case 1:
                                        echo "Számlázva";
                                        break;
                                    case 2:
                                        echo "Fizetve";
                                        break;
                                }
                                ?>
                            </div>
                        </div>
                    <?php
                    }
                    ?>
                </div>
            </div>
        </div>

        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    Termékek
                </div>
                <div class="card-body productList2">
                    <table class="table">
                        <?php
                        if (isset($order->body)) {
                            echo "<thead>
                        <th>#</th>
                        <th>Termék neve</i></div>
                        <th>Mennyiség</th>
                        <th>Kedvez. %</th>
                        <th class=\"text-right\">E.Ár Ft</th>
                        <th class=\" text-right\">Ár Ft</th>
                        <th>Szállítási dátum</th>
                        <th>TP</th>
                        <th>Státusz</th>
                      </thead><tbody>";
                            foreach ($order->body as $p) {
                                $productprice = (isset($p->productPrice)) ? attendNum($p->productPrice) : 0;
                                $total = (isset($p->total)) ? attendNum($p->total) : 0;
                                $status = selectStatus($p->status);
                                echo "<tr data-product-order=\"{$p->id}\">
                                    <td>{$p->id}</td>
                                    <td>{$p->productName} <i class=\"fas fa-info productInfo\" data-product-id=\"{$p->id}\"></i></td>
                                    <td>{$p->quantitity} db</td>
                                    <td>{$p->discount} %</td>
                                    <td class=\"text-right\">{$productprice} Ft</td>
                                    <td class=\"text-right\">{$total} Ft</td>
                                    <td>{$p->delivery}</td>
                                    <td>{$p->ekaer}</td>
                                    <td>{$status}</td>
                                </tr>";
                            }
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </form>
    <div class="shadow" style="background-color: rgba(0,0,0,0.7);cursor:pointer;display:none;z-index:9999999;position:fixed;width:100%;height:100%;top:0;left:0;">
        <div class="card popup" style="position:fixed;top:10%;left:10%;width:80%;height:80%">
            <div class="card-header bg-gradient-success">
                <h3></h3>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-3"><strong>Termék leírás</strong></div>
                    <div class="productDesc col-9"></div>
                </div>
                <div class="row">
                    <div class="col-3"><strong>Termék egység ára:</strong></div>
                    <div class="productPrice col-9"></div>
                </div>
            </div>
            <div class="card-footer">
                <button class="btn btn-block bg-gradient-danger closeWindow">Ablak bezárása</button>
            </div>
        </div>
    </div>
    <script>
        let orderLoad = JSON.parse(`<?php echo json_encode($order); ?>`);
        let productList = JSON.parse(`<?php echo json_encode($order->body); ?>`);
        let user = <?php echo $user->id ?>;
        let currentOrder = <?php echo $_GET['id'] ?>;
    </script>
<?php
    addJS("orderupdate.js");
else :
    header("Location: " . SITEURL . "/orders/order/orderedit?id=" . $_GET['id']);
endif;
?>