
<?php include('header.php'); ?>

<!-- Page Wrapper -->
<div class="page-wrapper">
    <div class="page-content">
        <!-- Breadcrumb with Action Buttons -->
        <div class="page-breadcrumb d-flex align-items-center justify-content-between mb-4 p-3 bg-white rounded shadow-sm">
            <div class="d-flex align-items-center">
                <div class="breadcrumb-title pe-3">Services</div>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb mb-0 ps-2">
                        <li class="breadcrumb-item active text-muted" aria-current="page">Voter Pdf Download ORG</li>
                    </ol>
                </nav>
            </div>
            <div class="btn-group">
             
                <a href="javascript:history.back()" class="btn btn-outline-primary">
                    <i class="bi bi-arrow-left me-1"></i> Back
                </a>
            </div>
        </div>
            <div class="ms-auto">
                <div class="btn-group">
                  
                </div>
            </div>
        </div>
        <!-- End Breadcrumb -->

        <head>
            <script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
            <style>
                .hidden {
                    display: none;
                }
            </style>
        </head>

        <body>
            <?php
            $api_key = urlencode($flexsecure_key);

            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                if (isset($_POST['find']) && !empty($_POST['epic_no'])) {
                    $epic_no = $_POST['epic_no'];
                    $url = "https://" . htmlspecialchars($flexsecureapiurl) . "/panel/serviceapi/voter/Voterpdf_otp.php?apikey=$api_key&epic=$epic_no&type=otp";

                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $response = curl_exec($ch);

                    if ($response === false) {
                        echo "<script>Swal.fire({ title: 'Error', text: 'Failed to fetch data from the API: " . curl_error($ch) . "', icon: 'error', confirmButtonText: 'OK' });</script>";
                        curl_close($ch);
                        return;
                    }

                    curl_close($ch);
                    $result = json_decode($response, true);

                    if (json_last_error() !== JSON_ERROR_NONE) {
                        echo "<script>Swal.fire({ title: 'Error', text: 'Invalid JSON response from the API.', icon: 'error', confirmButtonText: 'OK' });</script>";
                        return;
                    }

                    $statuss = $result['status'] ?? 'unknown';
                    $messages = $result['message'] ?? 'No message provided';

                    if ($statuss === "success") {
                        $epicNumber = $epic_no;
                        echo "<script>Swal.fire({ title: '$messages', text: '$statuss', icon: 'success', confirmButtonText: 'OK' });</script>";
                    } else {
                        echo "<script>Swal.fire({ title: '$messages', text: '$statuss', icon: 'error', confirmButtonText: 'OK' });</script>";
                    }
                }
            }

            $price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='voter_pdf_instant' "));
            $fee = $price['price'];
            $balance = $udata['balance'];
            $mobile = $udata['phone'];
            $debit_fee = $balance - $fee;

            if ($balance > $fee) {
                if (isset($_POST['verify_otp']) && !empty($_POST['otp'])) {
                    $epic = $_POST['epic'];
                    $otp = $_POST['otp'];
                    $apiUrl = "https://" . htmlspecialchars($flexsecureapiurl) . "/panel/serviceapi/voter/voterpdf_otp_verify.php?apikey=$api_key&epic=$epic&type=otpv&otp=$otp";

                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $apiUrl);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $apiResponse = curl_exec($ch);

                    if ($apiResponse === false) {
                        echo '<div class="alert alert-danger" role="alert">cURL request failed: ' . curl_error($ch) . '</div>';
                    } else {
                        $data = json_decode($apiResponse, true);
                        $status = $data['status'] ?? 'unknown';
                        $message = $data['message'] ?? 'No message provided';
                        $refId = $data['refId'] ?? null;
                        $pdf = $data['file'] ?? null;

                        if (!empty($pdf)) {
                            mysqli_query($ahk_conn, "UPDATE users SET balance = balance - '$fee' WHERE phone='$mobile'");
                            $new_balance = $balance - $fee;
                            date_default_timezone_set("Asia/Kolkata");
                            $timestamp = date('d/m/Y g:i:s');
                            mysqli_query($ahk_conn, "INSERT INTO wallethistory(userid, amount, balance, purpose, status, type) VALUES ('$mobile','$fee','$new_balance','$epic Voter ORG PDF','1','Debit')");

                            echo "<script>Swal.fire({ title: '$message', text: '$status', icon: 'success', confirmButtonText: 'OK' });</script>";
                            $epicNumber = 0;
                        } else {
                            echo "<script>Swal.fire({ title: '$message', text: '$status', icon: 'error', confirmButtonText: 'OK' });</script>";
                        }
                    }
                    curl_close($ch);
                }
            } else {
                $msg = "<script>Swal.fire({ title:'Balance Low! Recharge Now', text:'Warning!', icon:'warning', confirmButtonText:'OK' }).then((result) => { if (result.isConfirmed) { window.location = 'wallet.php'; } });</script>";
            }
            ?>

            <div class="container-fluid pt-4 px-4">
                <div class="row">
                    <!-- Left Side: Form Section -->
                    <div class="col-lg-5 mb-4">
                        <div class="card">
                            <div class="card-body">
                               <h3 class="card-title text-dark text-center"><strong>Voter Oringnal PDF Download</strong></h3>
                            </div>
                            <div class="card-body">
                                <a href="vote_mob_link" class="btn btn-dark w-100 mb-3">Mobile Number Change Click Here</a>

                                <h6><?php echo $msg ?? ''; ?></h6>

                                <form id="epic_form" class="user <?php echo ($epicNumber) ? 'hidden' : ''; ?>" method="POST">
                                    <div class="form-group mb-3">
                                        <label for="epic_no">EPIC NO <span class="text-danger">*</span></label>
                                        <input id="epic_no" name="epic_no" class="form-control border-success" type="text" value="<?php echo $epicNumber; ?>" required <?php echo ($epicNumber) ? 'disabled' : ''; ?>>
                                    </div>
                                    <button type="submit" name="find" class="btn btn-primary w-100 mb-3" <?php echo ($epicNumber) ? 'disabled' : ''; ?>>
                                        <i class="fa fa-check-circle"></i> Send OTP
                                    </button>
                                    <div class="col-12 ml-2 d-flex justify-content-center">
    <div class="fee-container text-center">
        <h5 class="text-warning">Application Fee: 
           <?php echo $fee; ?>
        </h5>
    </div>
</div>

<style>
    .fee-container {
        background-color: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        padding: 10px 15px;
        max-width: 300px;
        width: 100%;
        margin-top: 10px;
        margin-bottom: 10px;
    }

    .fee-container h5 {
        color: #ff9800; /* Bright yellow/orange color for emphasis */
        font-size: 18px;
        font-weight: 600;
        margin: 0;
    }
</style>

                                </form>

                                <form id="additional_fields_form" class="user <?php echo ($epicNumber) ? '' : 'hidden'; ?>" method="POST">
                                    <input type="hidden" name="epic" value="<?php echo $epicNumber; ?>">
                                    <div class="form-group mb-3">
                                        <label for="otp">Enter OTP <span class="text-danger">*</span></label>
                                        <input id="otp" name="otp" class="form-control border-primary" type="number" placeholder="ENTER OTP" value="<?php echo $otp; ?>" required>
                                    </div>
                                    <button type="submit" name="verify_otp" class="btn btn-primary w-100">
                                        <i class="fa fa-check-circle"></i> Verify
                                    </button>
                                </form>

                                <?php if (!empty($pdf)) { ?>
                                    <a href="data:application/pdf;base64,<?php echo $pdf; ?>" download="<?php echo $refId; ?>.pdf" class="btn btn-danger w-100 mt-4">
                                        <img src="v_org.png" alt="PDF" style="height:40px; vertical-align: middle; margin-right: 5px;">
                                        <strong>Download PDF</strong>
                                    </a>
                                <?php } ?>
                            </div>
                        </div>
                    </div>

                    <!-- Right Side: PDF Preview -->
                    <?php if (!empty($pdf)) { ?>
                    <div class="col-lg-7">
                        <div class="card h-100">
                            <div class="card-header bg-primary text-white">
                                <h5 class="mb-0">Preview Voter PDF</h5>
                            </div>
                            <div class="card-body p-0" style="height: 750px;">
                                <iframe src="data:application/pdf;base64,<?php echo $pdf; ?>" width="100%" height="100%" frameborder="0"></iframe>
                            </div>
                        </div>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </body>
    </div>
</div>

   

    <!-- Add your footer content here -->

    <script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
    <script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
    <script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
    <script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
    <script src="../template/ahkweb/assets/js/app.js"></script>
<script>
    // Hide the success and error alerts after 3 seconds
    setTimeout(function () {
        document.getElementById('success-alert').style.display = 'none';
        // document.getElementById('error-alert').style.display = 'none';
    }, 3000);
</script>

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