<?php
    /* Print Order Details */
    if($_SERVER["REQUEST_METHOD"] != "POST" && !isset($_POST['selected_orders'])){
        header("Location: /admin/orders/list");
        exit();
    }
    /* Print Order Details */

    $selected_orders = $_POST['selected_orders'];
?>
<html>
    <head>
        <title>Sipariş Ayrıntıları</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <style>
            *{
                font-family: "Arial" !important;
                font-size: 12px;
                -webkit-print-color-adjust: exact;
                margin: 0;
                padding: 0;
            }
            *, ::after, ::before {
                box-sizing: border-box;
            }
            table{
                border-collapse: separate;
                border-spacing: 0;
                width: 100%;
                margin-bottom: 1rem;
                background-color: transparent;
                border: 1px solid #777777;
                margin: 10px 0;
            }
            table > thead {
                background-color: rgba(0, 0, 0, 0.05);
            }
            table > thead > tr > th, table > thead > tr > td {
                border-bottom: 1px solid #777777;
                border-top: 0;
                border-right-width: 0;
                color: #777777;
            }
            table > thead > tr > th:first-child, table > thead > tr > td:first-child {
                border-left-width: 0;
            }
            table > tbody > tr:last-child > th, table > tbody > tr:last-child > td {
                border-bottom-width: 0;
            }
            table thead > tr > th, table tfoot > tr > th {
                border-top: 0;
                font-weight: 700;
                font-size: 12px;
                color: #333;
                letter-spacing: 0.5px;
            }
            table th, table td {
                border-right-width: 0;
                border-top-width: 0;
                padding: 10px;
                vertical-align: middle;
                border: 1px solid #777777;
                color: #333;
            }
            table > tbody > tr > th, table > tbody > tr > td {
                border-right-width: 0;
                border-top-width: 0;
                vertical-align: middle;
            }
            table > tbody > tr > th:first-child, table > tbody > tr > td:first-child {
                border-left-width: 0;
            }


            .container {
                width: 100%;
                margin-right: auto;
                margin-left: auto;
            }
            .row {
                display: -ms-flexbox;
                display: flex;
                -ms-flex-wrap: wrap;
                flex-wrap: wrap;
                margin-right: -10px;
                margin-left: -10px;
            }
            .col-6 {
                -ms-flex: 0 0 50%;
                flex: 0 0 50%;
                max-width: 50%;
                position: relative;
                width: 100%;
                padding-right: 10px;
                padding-left: 10px;
            }
            .col-12 {
                -ms-flex: 0 0 100%;
                flex: 0 0  100%;
                max-width:  100%;
                position: relative;
                width: 100%;
                padding-right: 10px;
                padding-left: 10px;
            }

            .text-center {
                text-align: center;
            }
            .title {
                text-align: center;
                font-size: 16px;
                font-weight: 500;
                margin-bottom: 15px;
            }
            .text-danger {
                color: #e61010;
            }

            /*@media print {
                @page { margin: 0; }
                body { margin: 1.6cm; }
            }*/

            @media print {
                .products 
                {
                    page-break-after:always;
                }
            }
        </style>
    </head>
    <body>
        <?php foreach ($selected_orders as $val):
                $order_details       = $order->getOrder($val);
                $order_items         = $order->getOrderItems($order_details['id']);
                $payment_type        = $order->getPaymentMethod($order_details['payment_type']);
                $order_status        = $order->getOrderStatus($order_details['status']);
         ?>
            <div class="container">
                <div class="row">
                    <div class="col-12 ">
                        <div class="title">
                            <strong><?php echo $order_details['order_id']; ?></strong> Sipariş Ayrıntıları
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-6">
                        <table>
                            <thead>
                                <tr>
                                    <th colspan="2">Teslimat Bilgileri</th>
                                </tr>
                            </thead>
                            <tbody width="100%">
                                <tr>
                                    <td width="80px">Adı</td>
                                    <td><?php echo $order_details['name']; ?></td>
                                </tr>
                                <tr>
                                    <td>Telefon</td>
                                    <td><?php echo $order_details['phone']; ?></td>
                                </tr>
                                <tr>
                                    <td>Adres</td>
                                    <td><?php echo $order_details['address']. "<br>". $order_details['town']. " - ". $order_details['city']; ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-6">
                        <table>
                            <thead>
                                <tr>
                                    <th colspan="2">Fatura Bilgileri</th>
                                </tr>
                            </thead>
                            <tbody width="100%">
                                <tr>
                                    <td width="80px">Adı</td>
                                    <td><?php echo !empty($order_details['tckno']) ? $order_details['name'] : $order_details['company']; ?></td>
                                </tr>
                                <tr>
                                    <td>Vergi No</td>
                                    <td><?php echo !empty($order_details['tckno']) ? $order_details['tckno'] : $order_details['tax_no']; ?></td>
                                </tr>
                                <tr>
                                    <td>Vergi Da.</td>
                                    <td><?php echo !empty($order_details['tckno']) ? '' : $order_details['tax_office']; ?></td>
                                </tr>
                                <tr>
                                    <td>Adres</td>
                                    <td><?php echo $order_details['address']. "<br>". $order_details['town']. " - ". $order_details['city']; ?></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-6">
                        <table>
                            <thead>
                                <tr>
                                    <th colspan="2">Sipariş Bilgileri</th>
                                </tr>
                            </thead>
                            <tbody width="100%">
                                <tr>
                                    <td width="100px">Ödeme Yöntemi</td>
                                    <td><?php echo $payment_type; ?></td>
                                </tr>
                                <tr>
                                    <td>Süreç</td>
                                    <td><?php echo $order_status; ?></td>
                                </tr>
                                <tr>
                                    <td>Sipariş Tarihi</td>
                                    <td><?php echo $order_details['date_added']; ?></td>
                                </tr>
                                <tr>
                                    <td>Fatura No</td>
                                    <td><?php echo $order_details['inv_no']; ?></td>
                                </tr>
                                <tr>
                                    <td>Sipariş Notu</td>
                                    <td><?php echo $order_details['order_note']; ?></td>
                                </tr>
                                <tr>
                                    <td>Hediye mi?</td>
                                    <td><?php echo $order_details['giftwrap'] == 1 ? "Evet" : "Hayır"; ?></td>
                                </tr>
                                <tr>
                                    <td>Hediye Notu</td>
                                    <td><?php echo $order_details['gift_note']; ?></td>
                                </tr>
                                <tr>
                                    <td>Kaynak</td>
                                    <td><strong class="text-danger"><?php echo $order_details['source']; ?></strong></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-6">
                        <table>
                            <thead>
                                <tr>
                                    <th colspan="2">Sipariş Toplamları</th>
                                </tr>
                            </thead>
                            <tbody width="100%">
                                <tr>
                                    <td width="80px">Toplam</td>
                                    <td><?php echo $utils->moneyFormatter($order_details['gross_total']); ?> TL</td>
                                </tr>
                                <tr>
                                    <td>İndirim Toplamı</td>
                                    <td><?php echo $utils->moneyFormatter($order_details['discount_total']); ?> TL</td>
                                </tr>
                                <tr>
                                    <td>Teslimat Bedeli</td>
                                    <td><?php echo $utils->moneyFormatter($order_details['shipping']); ?> TL</td>
                                </tr>
                                <tr>
                                    <td>Genel Toplam</td>
                                    <td><?php echo $utils->moneyFormatter($order_details['order_total']); ?> TL</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="products">
                <table>
                    <thead>
                        <tr>
                            <th>Ürün</th>
                            <th>Adet x Fiyat</th>
                            <th>Tutar</th>
                        </tr>
                    </thead>
                    <tbody width="100%">
                        <?php foreach ($order_items as $item): ?>
                            <tr>
                                <td><?php echo $item["product_title"]; ?></td>
                                <td><?php echo $item["quantity"] . " x " . $utils->moneyFormatter($item["product_price"]); ?> TL</td>
                                <td><?php echo $utils->moneyFormatter($item["total_price"]); ?> TL</td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endforeach; ?>
        <script>
            window.onafterprint = window.close;
            window.print();
        </script>
    </body>
</html>