<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');

$fee = $udata['voter_to_org_pdf_without_otp_a'];
checkServiceStatus($udata, 'voter_to_org_pdf_without_otp_a_status');

$resdata = null;

if (!empty($_POST['epic_number'])) {

    $epic_number = $_POST['epic_number'];
    $appliedby   = $udata['phone'];
    $debit_fee   = $udata['balance'] - $fee;

    if ($udata['balance'] >= $fee) {

       
        $request = [
            'epic_number' => $epic_number
        ];

        $resdata = voter_pdf_org_verification_runy($request);

      
        if (isset($resdata['Message']) && $resdata['Message'] === 'Failed') {

            echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
            <script>
            Swal.fire({
                icon: 'error',
                title: 'Error',
                text: '" . addslashes($resdata['error']) . "',
                confirmButtonText: 'OK'
            }).then(function() {
                window.location.href = 'voter_org_pdf.php';
            });
            </script>";
            exit;
        }

      
        if (isset($resdata['sampleCode']) && $resdata['sampleCode'] === "200") {

            
            $parentPhone = $udata['parent'] ?? null;
            addCommission($tng_conn, $appliedby, $fee, $parentPhone, "Voter 2 Org PDF Without OTP");
            date_default_timezone_set('Asia/Kolkata');
                $timestamp = date("Y-m-d H:i:s");

            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', 'Voter 2 Org PDF Without OTP', '1', 'Debit')");

            
            $name     = mysqli_real_escape_string($tng_conn, $resdata['fullname']);
            $username = mysqli_real_escape_string($tng_conn, $udata['phone']);
            $epic     = mysqli_real_escape_string($tng_conn, $resdata['epic_number']);
            $app_no   = mysqli_real_escape_string($tng_conn, $resdata['application_no']);
            $remark   = mysqli_real_escape_string($tng_conn, $resdata['message']);

            mysqli_query($tng_conn, "
                INSERT INTO voter_pdf 
                (name, username, aadhar, date, status, application_no, remark)
                VALUES 
                ('$name', '$username', '$epic', '$timestamp', 'Pending', '$app_no', '$remark')
            ");

           
            echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
            <script>
            Swal.fire({
                icon: 'success',
                title: 'Success!',
                text: 'Your request has been submitted successfully.',
                confirmButtonText: 'OK'
            }).then(function() {
                window.location.href = 'voter_otp_pdf_list.php';
            });
            </script>";
            exit;

        } else {
            echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>
            <script>
            Swal.fire({
                icon: 'error',
                title: 'Error',
                text: '" . addslashes($resdata['error']) . "',
                confirmButtonText: 'OK'
            }).then(function() {
                window.location.href = 'voter_org_pdf.php';
            });
            </script>";
            exit;
        }

    } else {
        echo "<script>
        Swal.fire({
            icon: 'error',
            title: 'Wallet Balance is Low!',
            text: 'Recharge Now!',
            timer: 2000
        });
        setTimeout(() => { window.location='wallet.php'; }, 2000);
        </script>";
        exit;
    }
}
?>

<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="col-lg-4 col-md-6 col-sm-6">
                    <div class="card" style="margin-left:10px; padding-left:30px; padding-top:12px; box-shadow:1px 5px 5px 5px;">
                        <div class="card-body">

                            <a href="javascript:history.back()" class="back-btn mb-3">
                                <i class="bi bi-arrow-left-circle"></i> Go Back
                            </a>

                            <div class="alert alert-info">
                                Voter 2 Org Pdf Without OTP Verification
                            </div>

                            <form action="" method="POST" class="row g-3">

                                <div class="card-body">
                                    <div class="form-group">
                                        <label>Enter Voter Number</label>
                                        <input name="epic_number" placeholder="Enter Voter Number" type="text" class="form-control" required>
                                    </div>

                                    <hr>

                                    <div class="row mt-3">
                                        <div class="col-md-6">
                                            <input class="form-control" value="Fee ₹ <?= $fee ?>" readonly>
                                        </div>

                                        <div class="col-md-6 text-end">
                                            <button class="btn btn-primary" name="submit" id="submit">
                                                <i class="fa fa-check-circle"></i> Submit
                                            </button>
                                        </div>
                                    </div>

                                </div>

                            </form>

                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<?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>

</div></div></div></div></div>
</html>
