<?php
    ini_set('max_execution_time', 0);
    set_time_limit(0);
    ini_set('memory_limit', -1);
    include("../utility.php");
    extract($_REQUEST);
    switch ($requestType) {
        case 'placeOrder':
            $I4 = $_COOKIE['I4'] ?? '0'; //Kiinteä yksilasinen ikkuna
            $KOR = $_COOKIE['kOR'] ?? '0'; //Kääntyvä tai liukuva terassilasi
            $OP = $_COOKIE['OP'] ?? '0'; //Kääntyvä parvekelasi
            $LK = (isset($_COOKIE['LK']) && !empty($_COOKIE['LK']) ? ($_COOKIE['LK'] == 1 ? "K" : "E") : 'E'); //Lasinen terassikaide
            $LSK = $_COOKIE['LSK'] ?? '0'; //Parvekkeen alalasitus
            $P = $_COOKIE['P'] ?? '0'; //Avattava kaksilasinen ikkuna
            $T = $_COOKIE['T'] ?? '0'; //Avattava ristikollinen ikkuna
            $TI = $_COOKIE['TI'] ?? '0'; //Kahvallinen tuuletusikkuna
            $TK = $_COOKIE['TK'] ?? '0'; //Avattava kolmilasinen ikkuna
            $pro1 = $_COOKIE['i'] ?? '0'; //Ovi- tai peililasi
            $pro2 = $_COOKIE['ii'] ?? '0'; //Portaikon lasikaide
            $pro3 = $_COOKIE['iii'] ?? '0'; //Kiinteä ikkuna 3-6 metrissä
            $hintar = $_COOKIE['ashi'] ?? '0'; //ashi
            $vero = $_COOKIE['vro'] ?? '0'; //Price without tax
            $veroton = $_COOKIE['vhi'] ?? '0'; //tax
            $product_arr = [
                ["name" => "Kiinteä yksilasinen ikkuna", "quantity" => $I4],
                ["name" => "Kääntyvä tai liukuva terassilasi", "quantity" => $KOR],
                ["name" => "Kääntyvä parvekelasi", "quantity" => $OP],
                ["name" => "Lasinen terassikaide", "quantity" => $LK],
                ["name" => "Parvekkeen alalasitus", "quantity" => $LSK],
                ["name" => "Avattava kaksilasinen ikkuna", "quantity" => $P],
                ["name" => "Avattava ristikollinen ikkuna", "quantity" => $T],
                ["name" => "Kahvallinen tuuletusikkuna", "quantity" => $TI],
                ["name" => "Avattava kolmilasinen ikkuna", "quantity" => $TK],
                ["name" => "Ovi- tai peililasi", "quantity" => $pro1],
                ["name" => "Portaikon lasikaide", "quantity" => $pro2],
                ["name" => "Kiinteä ikkuna 3-6 metrissä", "quantity" => $pro3]
            ];
            $ship_htm = '';
            foreach ($product_arr as $key => $value) {
                $ship_htm .= '
                            <tr>
                                <td align="left">' . $value["name"] . '</td>
                                <td>' . $value["quantity"] . '</td>
                            </tr>
                        ';
            }
    
            $consig_htm = '';
            foreach ($product_arr as $key => $value) {
                if ($value["quantity"] > '0') {
                    $consig_htm .= '
                            <tr>
                                <td align="left">' . $value["name"] . '</td>
                                <td>' . $value["quantity"] . '</td>
                            </tr>
                        ';
                }
            }
    
            $emailhead = '
    
                        <html>
    
                        <head>
    
                        <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
                        <title>Uusi Varaus</title>
    
                        </head>
    
                        <body>
                        <img src="https://kiiltavatpinnat.fi/logo.svg" width="100px">
                        ';
    
            $consiTitle = '
    
                        <p>Dear <b>'.$user_name.',</b></p>
    
                        <p>Tilauksesi on sijoitettu</p>
    
                        ';
    
            $shipTitle = '
                        
                     
                        <h1>Uusi Varaus</h1>
                        
                       
                           <p>Olet saanut seuraavan tilauksen <b>'.$user_name.' </b> ' . date('F d, Y h:i:s', strtotime($booking_date)) . '</p>
                        ';
                        
    
            $shipBody = '
    
                        <hr>
    
                        <p><b>kohteen tiedot laskurista:</b></p>
    
                        <table  border="1" bordercolor="grey" width="100%" cellspacing="0" cellpadding="15">
    
                        <tr>
    
                        <th align="left">tuotteen nimi</th>
    
                        <th align="left">määrä</th>
    
                        </tr>
    
                        ' . $ship_htm . '
    
                        </table>
    
                        ';
            $consigBody = '
    
                        <hr>
    
                        <p><b>kohteen tiedot laskurista:</b></p>
    
                        <table  border="1" bordercolor="grey" width="100%" cellspacing="0" cellpadding="15">
    
                        <tr>
    
                        <th align="left">tuotteen nimi</th>
    
                        <th align="left">määrä</th>
    
                        </tr>
    
                        ' . $consig_htm . '
    
                        </tr>
                        </table>
    
                        ';
    
            $consigdetail = '
    
                        <br>
    
                        <hr>
    
                        <p><b>asiakkaan tiedot:</b></p>
    
                        <table border="1" bordercolor="grey" width="100%" cellspacing="0" cellpadding="15">
    
                        <tr>
    
                        <th align="left">Nimi </th>
    
                        <td colspan="2">' . $user_name . '</td>
    
                        </tr>
    
                        <tr>
    
                        <th align="left">Sähköpostiosoite </th>
                        <td colspan="2">' . $user_email . '</td>
    
                        </tr>
    
                        <tr>
    
                        <th align="left">Puhelinnumero </th>
    
                        <td colspan="2">' . $user_phone . '</td>
    
                        </tr>
    
                        <tr>
    
                        <th align="left">Osoite ja postinumero </th>
    
                        <td colspan="2">' . $address . '</td>
    
                        </tr>
                        <tr>
    
                        <th align="left">Kellonaikatoiveet ja mahdolliset lisätiedot </th>
    
                        <td colspan="2">' . $user_remarks . '</td>
    
                        </tr>
    
                        </table>';
    
            $emaifoot = '
                        <p><strong>Asiakkan hinta: </strong> ' . $hintar . ' &euro;</p>
    
                        </body>
    
                    </html>';
            //=======================Customer Email=========================
            $customerBody = $emailhead . $consiTitle . $consigBody . $emaifoot;
            $email = $user_email;
            $subject = "Kiitos varauksestasi!";
            $payload = [
                "notification" => "email",
                "email" => $email,
                "subject" => $subject,
                "message" => $customerBody
            ];
            systemNotification($payload);
            //======================Admin Email======================
            $shipperBody = $emailhead . $shipTitle . $consigdetail . $shipBody . $emaifoot;
            $email = "booking@kiiltavatpinnat.fi";
            $subject = "Kiitos varauksestasi!";
            $payload = [
                "notification" => "email",
                "email" => $email,
                "subject" => $subject,
                "message" => $shipperBody
            ];
            systemNotification($payload);
            $url = PRODUCTION_API."booking/booking.php";
            $request = [
                "name"=> $user_name,
                "email"=> $user_email,
                "phone"=> $user_phone,
                "address"=> $address,
                "remarks"=> $user_remarks,
                "booking_date"=> date('Y-m-d', strtotime($booking_date)),
                "product_details"=> $product_arr,
                "price"=> floatval($hintar),
                "price_without_tax"=> floatval($veroton),
                "tax_price"=> floatval($vero)
            ];
            $response = json_decode(curlFunction($url, json_encode($request)));
            if($response->status){
                $_SESSION["booking_date"] = date('Y-m-d', strtotime($booking_date));
            }
            echo json_encode(["status"=> $response->status, "message"=>$response->message, "payload"=> $response->payload]);
            break;
    
        default:
            header("Location:" . $_SERVER['HTTP_REFERER']);
            break;
    }
    #code ends...
    #mtech