<?php
    ini_set('max_execution_time', 0);
    set_time_limit(0);
    ini_set('memory_limit', -1);
    include("../utility.php");
    extract($_REQUEST);
    switch ($requestType) {
        case 'bookingList':
            $url = PRODUCTION_API . "booking/list.php";
            $response = json_decode(curlFunction($url, ""));
            $data = [];
            $i = 1;
            $resultsCount = 0;
            if ($response->status == 1 && count($response->payload) > 0) {
                foreach ($response->payload as $row) {
                    extract((array)$row);
                    $data[] = [
                        'SNO' => $i,
                        'NAME' => $name,
                        'EMAIL' => $email,
                        'PHONE' => $phone,
                        'ADDRESS' => $address,
                        'REMARKS' => $remarks,
                        'BOOKINGDATE' => $booking_date,
                        'PRICE' => $price,
                        'PRICEWITHOUTTAX' => $price_without_tax,
                        'TAXPRICE' => $tax,
                        'PRODUCTDETAIL' => '<a href="javascript: void(0);" onClick="productList('.$id.')"><i class="fa-solid fa-eye" style="color: black !important;"></i></a>',
                    ];
                    $i++;
                    $resultsCount++;
                }
            }
            $return = [
                'iTotalRecords' => $resultsCount,
                'aaData' => $data,
            ];
            echo json_encode($return);
            break;
            case 'getproducts' :
             $htm = '';
            $url = PRODUCTION_API . "booking/product_list.php";
            $data = ['id' => (int) $id];
            $response = json_decode(curlFunction($url, json_encode($data)));
            foreach($response->payload as $res){
                $p_name = $res->product_name;
                $p_quantity = $res->product_quantity;
                $htm .= '
                <li> <b>Product Name : </b> '.$p_name.', <b>Product Quantity:</b> '.$p_quantity.' </li>
                ';
            }
              
          echo json_encode($htm);
            break;    
            case 'login':
                ($email == "" || $password == "") ? exit(json_encode(['status' => 0, 'message' => 'Please fill all the fields', 'payload' => []])) : '';
                $url = PRODUCTION_API . "auth/login.php";
                $request = json_encode(["email" => $email, "password" => $password]);
                $response = json_decode(curlFunction($url, $request));
                if ($response->status) {
                    $payload = $response->payload;
                    extract((array)$payload);
                    $_SESSION['isValid']            = true;
                    $_SESSION['id']                 = $id;
                    $_SESSION['name']               = $name;
                    $_SESSION['email']              = $email;
                    $_SESSION['role_type']          = $role_type;
                }
                $return = ['status' => $response->status, 'message' => $response->message, 'payload' => $response->payload];
                echo json_encode($return);
            break;
            case 'bookingCount':
                $url = PRODUCTION_API . "booking/count.php";
                $response = json_decode(curlFunction($url, ""));
                $return = ['status' => $response->status, 'message' => $response->message, 'payload' => $response->payload];
                echo json_encode($return);
            break;
        default:
            header("Location:" . $_SERVER['HTTP_REFERER']);
            break;
    }
    #code ends...
    #mtech