<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');
$fee    = $udata['mobile_number_to_number_details_fee'];
checkServiceStatus($udata, 'mobile_number_to_number_details_status');

$resdata = [];

if ($_POST['mobileNumber']) {
    $mobileNumber = strtoupper(trim($_POST['mobileNumber']));
    $appliedby = $udata['phone'];
    $debit_fee = $udata['balance'] - $fee;

    if ($udata['balance'] >= $fee) {
        
        $request = [
        'mobileNumber' => $mobileNumber,
        ];

        $resdata = mobile_to_details_verification_api_run($request);
            if ($resdata['sampleCode'] === "200") {
                
                $parentPhone = $udata['parent'] ?? null;
        addCommission($tng_conn, $appliedby, $fee, $parentPhone, "Mobile Number To  Details Verification");
                mysqli_query($tng_conn, "UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
                mysqli_query($tng_conn, "INSERT INTO `wallethistory`(`userid`,`amount`,`balance`,`purpose`,`status`,`type`) VALUES ('$appliedby','$fee','$debit_fee','Mobile Number To  Details Verification','1','Debit')");
            } else {
                echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
                <script>
                Swal.fire({icon:'error', title:'Error', text:'".$resdata['Message']."', timer:4000, timerProgressBar:true});
                </script>";
            }
    } else {
        echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
        <script>
        Swal.fire({icon:'error', title:'Wallet Low', text:'Please Recharge!', timer:3000, timerProgressBar:true});
        </script>";
    }
}
?>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<div class="content-wrap">
 <div class="main">
<div class="page-wrapper">
    <div class="page-content">
        <div class="mobile-menu-overlay"></div>
        <div class="main-container">
            <div class="col-lg-12">
                <div class="card" style="margin-left: 10px; padding-left: 30px; padding-top: 12px; box-shadow: 1px 5px 5px 5px;">
                    <div class="stat-widget-two">
                        <div class="stat-content">
                            <div class="stat-text">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-lg-4 col-md-6 col-sm-6">
                                            <div class="card">
                                                <div class="card-body">
                                                     <div class="mb-4">
  <a href="javascript:history.back()" class="back-btn">
    <i class="bi bi-arrow-left-circle"></i> Go Back
  </a>
</div>
                                                    <div class="alert alert-danger" role="alert">
                                                       
                                                       <h5>Mobile Number To Details</h5>
                                                    </div>
                                                    <form action="" method="POST" class="row g-3">
                                                        <div class="card-body">
                                                            <div class="col-md-12">
                                                                <div class="form-group">
                                                                   <label for="mobileNumber" class="form-label">Enter Mobile Number</label>
                        <input type="text" name="mobileNumber" id="mobileNumber" class="form-control" placeholder="e.g. 89XXXXXX54" required> </div>
                                                                <hr>
                                                                <div class="row mt-3">
                                                                  <div class="col-md-6">
                                                                       <input class="form-control" value="Fee ₹ <?php echo $fee; ?>" readonly>
                                                                   </div>
                                                                   <div class="col-md-6 text-end">
                                                                       <button class="btn btn-success" name="submit" id="submit"><i class="fa fa-check-circle"></i> Submit</button>
                                                                   </div>
                                                               </div>
                                                            </div>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                       <?php if (!empty($resdata) && $resdata['sampleCode'] === "200" && !empty($resdata['data'])): ?>
<div class="col-lg-12">
    <div class="card p-3 shadow-sm bg-light" id="detailsTable" style="width:100%;">
        <h5>Mobile Number To Details Results</h5>
        <div class="table-responsive" style="max-height:500px; overflow:auto;">
            <table id="example2" class="table table-bordered table-striped table-hover">
                <thead class="table-light">
                    <tr>
                        <th>Sr. No</th>
                        <th>Mobile Number</th>
                        <th>Full Name</th>
                        <th>Father Name</th>
                        <th>Address</th>
                        <th>Alt Mobile</th>
                        <th>Circle</th>
                        <th>UID</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($resdata['data'] as $row): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($row['sr_no']); ?></td>
                        <td><?php echo htmlspecialchars($row['mobileNumber']); ?></td>
                        <td><?php echo htmlspecialchars($row['fullname']); ?></td>
                        <td><?php echo htmlspecialchars($row['fathername']); ?></td>
                        <td><?php echo htmlspecialchars($row['address']); ?></td>
                        <td><?php echo htmlspecialchars($row['alt']); ?></td>
                        <td><?php echo htmlspecialchars($row['circle']); ?></td>
                        <td><?php echo htmlspecialchars($row['uid']); ?></td>
                        <td><?php echo htmlspecialchars($row['email']); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <button id="downloadBtn" class="btn btn-primary mt-2">Download Table as Image</button>
    </div>
</div>

<!-- Include jQuery and DataTables -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>

<script>
$(document).ready(function () {
    $('#example2').DataTable({
        "scrollY": "400px",
        "scrollCollapse": true,
        "paging": true
    });
});
</script>

<!-- Include html2canvas for image download -->
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
<script>
document.getElementById("downloadBtn").addEventListener("click", function() {
    const element = document.getElementById("detailsTable");

    // Temporarily expand height to fit all table rows
    element.style.height = 'auto';
    html2canvas(element, { scrollY: -window.scrollY }).then(function(canvas) {
        let link = document.createElement("a");
        link.download = "mobile_details.png";
        link.href = canvas.toDataURL("image/png");
        link.click();
        element.style.height = '';
    });
});
</script>
<?php endif; ?>



                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <br><br><br><br>
            </div>
        </div>
    </div>
</div>
<!-- Include html2canvas for image download -->
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
<script>
document.getElementById("downloadBtn").addEventListener("click", function() {
    const tableContainer = document.getElementById("detailsTable");
    const tableWrapper = tableContainer.cloneNode(true); // clone the element

    // Remove DataTables scroll to show full table
    const scrollDiv = tableWrapper.querySelector('.dataTables_scrollBody');
    if (scrollDiv) {
        const table = scrollDiv.querySelector('table');
        scrollDiv.parentNode.replaceChild(table, scrollDiv);
    }

    // Create a hidden container to render the full table for capture
    const hiddenDiv = document.createElement("div");
    hiddenDiv.style.position = "fixed";
    hiddenDiv.style.left = "-9999px";
    hiddenDiv.style.top = "0";
    hiddenDiv.style.background = "#fff";
    hiddenDiv.style.padding = "10px";
    hiddenDiv.appendChild(tableWrapper);
    document.body.appendChild(hiddenDiv);

    html2canvas(hiddenDiv, { scale: 2 }).then(function(canvas) {
        let link = document.createElement("a");
        link.download = "mobile_details_full.png";
        link.href = canvas.toDataURL("image/png");
        link.click();
        document.body.removeChild(hiddenDiv); // clean up
    });
});
</script>

<?php include('../template/tngweb/footer.php'); ?>
<script src="../template/tngweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/tngweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/tngweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/tngweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/tngweb/assets/js/app.js"></script>
</html>
