<?php
include('include/auth.php');
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);

?>

<!doctype html>
<html class="fixed dark">
	<?php 
	$pagerole="lists";
	include('include/head.php'); 
	// Fetch API Partners
$api_partners = [];
$partner_query = "SELECT api_partner_id, api_partner_name, api_partner_key FROM api_partner ORDER BY api_partner_name ASC";
$partner_result = $db2->query($partner_query);
while ($row = $partner_result->fetch_assoc()) {
    $api_partners[] = $row;
}

// Fetch Car Aliases
$car_aliases = [];
$alias_query = "SELECT alias FROM jostj_vikrentcar_cars ORDER BY alias ASC";
$alias_result = $db2->query($alias_query);
while ($row = $alias_result->fetch_assoc()) {
    $car_aliases[] = $row['alias'];
}
	?><style>.noshow{display:none;} .done{text-decoration:line-through;} .canc{color:red!important;}</style>
	<body>
		<section class="body">

			<!-- start: header -->
			<header class="header">
				<div class="logo-container">
					<a href="index.php" class="logo">
						<img src="img/logo-light.png" width="75" height="35" alt="Web Voyage" />
					</a>
					<div class="d-md-none toggle-sidebar-left" data-toggle-class="sidebar-left-opened" data-target="html" data-fire-event="sidebar-left-opened">
						<i class="fas fa-bars" aria-label="Toggle sidebar"></i>
					</div>
				</div>
			
				<!-- start: search & user box -->
				<div class="header-right">
			<?php include('include/search.php');?>
					
			
					<span class="separator"></span>
			<?php include('include/notifications.php');?>
				
			
					<span class="separator"></span>
					<?php include('include/userbox.php'); 
					
					?>
				
				</div>
				<!-- end: search & user box -->
			</header>
			<!-- end: header -->

			<div class="inner-wrapper">
				<!-- start: sidebar -->
				<?php include('include/nav.php');
			?>
				<!-- end: sidebar -->

				<section role="main" class="content-body">
					<header class="page-header">
						<h2>Dashboard</h2>
					
						<div class="right-wrapper text-right">
							<ol class="breadcrumbs">
								<li>
									<a href="index.php">
										<i class="fas fa-home"></i>
									</a>
								</li>
								<li><span>WV Admin</span></li>
								<li><span>Dashboard</span></li>
							</ol>
					
							<a class="sidebar-right-toggle" data-open="sidebar-right"><i class="fas fa-chevron-left"></i></a>
						</div>
					</header>
					  
			<!-- start: page -->
		<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                <h2 class="panel-title">API Search</h2>
            </header>
            <div class="panel-body">
                <form id="apiSearchForm">
                    <div class="row">
                           <div class="form-group col-4 mb-3 pt-0" style="border-top:none;">
                       <!-- API Partner Dropdown -->
<label for="api_partner">API Partner</label>
<select id="api_partner" class="form-control populate" data-plugin-selectTwo>
    <option value="">Select API Partner</option>
    <?php foreach ($api_partners as $partner): ?>
        <option value="<?= $partner['api_partner_id']; ?>" data-key="<?= $partner['api_partner_key']; ?>">
            <?= htmlspecialchars($partner['api_partner_name']); ?>
        </option>
    <?php endforeach; ?>
</select>
</div>
 <div class="form-group col-4 mb-3 pt-0" style="border-top:none;">
<!-- Vehicle Type Dropdown -->
<label for="vehicle_type">Vehicle Type</label>
<select id="vehicle_type" class="form-control populate" data-plugin-selectTwo>
    <option value="">Select Vehicle Type</option>
    <option value="1">Cars</option>
    <option value="2">Bikes</option>
</select>
</div>
 <div class="form-group col-4 mb-3 pt-0" style="border-top:none;">
<!-- Car Alias Dropdown -->
<label for="car_alias">Car Alias</label>
<select id="car_alias" class="form-control populate" data-plugin-selectTwo>
    <option value="">Select Car Alias</option>
    <?php foreach ($car_aliases as $alias): ?>
        <option value="<?= htmlspecialchars($alias); ?>"><?= htmlspecialchars($alias); ?></option>
    <?php endforeach; ?>
</select>
<p>if no alias is selected all are shown</p>
</div>
                    </div>
                    <br>
                    <button type="button" class="btn btn-primary" id="searchApi">Search</button>
                </form>
            </div>
        </section>
    </div>
</div>

<!-- JSON Results -->
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                <h2 class="panel-title">API Results</h2>
            </header>
             <div class="panel-body">
                            <pre style="color:#b3b3b3; max-height:500px;" id="apiResults" class="pre-scrollable">Waiting for search...</pre>
                        </div>
        </section>
    </div>
</div>

						<!-- end: page -->
				</section>
			</div>

			<?php include('include/aside.php');?>

		</section>

    <!-- Vendor -->
    <script src="vendor/jquery/jquery.js"></script>
    <script src="vendor/jquery-browser-mobile/jquery.browser.mobile.js"></script>
    <script src="vendor/popper/umd/popper.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.js"></script>
    <script src="vendor/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
    <script src="vendor/common/common.js"></script>
    <script src="vendor/nanoscroller/nanoscroller.js"></script>
    <script src="vendor/magnific-popup/jquery.magnific-popup.js"></script>
    <script src="vendor/jquery-placeholder/jquery.placeholder.js"></script>

    <!-- Specific Page Vendor -->
    <script src="vendor/select2/js/select2.js"></script>
    <script src="vendor/datatables/media/js/jquery.dataTables.min.js"></script>
    <script src="vendor/datatables/media/js/dataTables.bootstrap4.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/dataTables.buttons.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/buttons.bootstrap4.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/buttons.html5.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/Buttons-1.4.2/js/buttons.print.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/JSZip-2.5.0/jszip.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/pdfmake-0.1.32/pdfmake.min.js"></script>
    <script src="vendor/datatables/extras/TableTools/pdfmake-0.1.32/vfs_fonts.js"></script>


    <!--(remove-empty-lines-end)-->

    <!-- Theme Base, Components and Settings -->
    <script src="js/theme.js"></script>

    <!-- Theme Custom -->
    <script src="js/custom.js"></script>

    <!-- Theme Initialization Files -->
    <script src="js/theme.init.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />
    <!-- Examples -->
    <script src="js/examples/examples.datatables.default.js"></script>

<script>
      $(document).ready(function() {
            $('#searchApi').click(function() {
                let partnerId = $('#api_partner').val();
                let partnerKey = $('#api_partner option:selected').data('key');
                let vehicleType = $('#vehicle_type').val();
                let carAlias = $('#car_alias').val(); // Optional

                if (!partnerId || !partnerKey || !vehicleType) {
                    alert('Please select API Partner and Vehicle Type.');
                    return;
                }

                let apiUrl = `api-debug.php`;

                let postData = {
                    pid: partnerId,
                    key: partnerKey,
                    vehicle_type: vehicleType
                };

                if (carAlias) {
                    postData.car_alias = carAlias;
                }

                console.log("Sending Request:", postData);

                $.ajax({
                    url: apiUrl,
                    type: "POST",
                    data: JSON.stringify(postData),
                    contentType: "application/json",
                    dataType: "json",
                    success: function(response) {
                        console.log("API Response:", response);
                        if (!response || $.isEmptyObject(response)) {
                            $('#apiResults').text("No data received from API.");
                        } else {
                            $('#apiResults').text(JSON.stringify(response, null, 4));
                        }
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        console.error("AJAX Error:", textStatus, errorThrown);
                        console.error("Response Text:", jqXHR.responseText);
                        $('#apiResults').text("Error fetching data. Check console.");
                    }
                });
            });

            $(".populate").select2();
        });

</script>

	</body>
</html>
<?php include('include/footer.php');?>


<?php
// Fetch API Partners
$api_partners = [];
$partner_query = "SELECT api_partner_id, api_partner_name, api_partner_key FROM api_partner ORDER BY api_partner_name ASC";
$partner_result = $db2->query($partner_query);
while ($row = $partner_result->fetch_assoc()) {
    $api_partners[] = $row;
}

// Fetch Car Aliases
$car_aliases = [];
$alias_query = "SELECT alias FROM jostj_vikrentcar_cars ORDER BY alias ASC";
$alias_result = $db2->query($alias_query);
while ($row = $alias_result->fetch_assoc()) {
    $car_aliases[] = $row['alias'];
}
?>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                <h2 class="panel-title">API Search</h2>
            </header>
            <div class="panel-body">
                <form id="apiSearchForm">
                    <div class="row">
                           <div class="form-group col-4 mb-3 pt-0" style="border-top:none;">
                       <!-- API Partner Dropdown -->
<label for="api_partner">API Partner</label>
<select id="api_partner" class="form-control populate" data-plugin-selectTwo>
    <option value="">Select API Partner</option>
    <?php foreach ($api_partners as $partner): ?>
        <option value="<?= $partner['api_partner_id']; ?>" data-key="<?= $partner['api_partner_key']; ?>">
            <?= htmlspecialchars($partner['api_partner_name']); ?>
        </option>
    <?php endforeach; ?>
</select>
</div>
 <div class="form-group col-4 mb-3 pt-0" style="border-top:none;">
<!-- Vehicle Type Dropdown -->
<label for="vehicle_type">Vehicle Type</label>
<select id="vehicle_type" class="form-control populate" data-plugin-selectTwo>
    <option value="">Select Vehicle Type</option>
    <option value="1">Cars</option>
    <option value="2">Bikes</option>
</select>
</div>
 <div class="form-group col-4 mb-3 pt-0" style="border-top:none;">
<!-- Car Alias Dropdown -->
<label for="car_alias">Car Alias</label>
<select id="car_alias" class="form-control populate" data-plugin-selectTwo>
    <option value="">Select Car Alias</option>
    <?php foreach ($car_aliases as $alias): ?>
        <option value="<?= htmlspecialchars($alias); ?>"><?= htmlspecialchars($alias); ?></option>
    <?php endforeach; ?>
</select>
</div>
                    </div>
                    <br>
                    <button type="button" class="btn btn-primary" id="searchApi">Search</button>
                </form>
            </div>
        </section>
    </div>
</div>

<!-- JSON Results -->
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                <h2 class="panel-title">API Results</h2>
            </header>
            <div class="panel-body">
                <pre id="apiResults" class="pre-scrollable">Waiting for search...</pre>
            </div>
        </section>
    </div>
</div>
