<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/ahkweb/Pan_manual.php');

$price = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pricing WHERE service_name='pan_manual_fee' "));
$fee = $price['price'];

if ($udata['balance'] < $fee) {
    ?>
    <script>
        alert("Dear user your Wallet Balance is Low Please Recharge Now");
        window.location.href = "wallet.php";
    </script>
    <?php 
} elseif (isset($_POST['savedata'])) {	

    $q = "SELECT * FROM users WHERE phone='" . $_SESSION['phone'] . "'";
    $r = mysqli_query($ahk_conn, $q);
    $rw = mysqli_fetch_assoc($r);

    $aadharno   = trim($_POST['pannumber']);
    $name       = trim($_POST['name']);
    $fathername = trim($_POST['fathername']);
    $dobadhar   = trim($_POST['dobadhar']);

    // File types
    $sign_image_type  = $_FILES["signfile"]["type"];
    $image_type       = $_FILES["imagefile"]["type"];
    $word = "image";

    if ($aadharno == "") {
        $msgno = 'Please Enter Pan Card No .... ';
    } elseif ($rw['aadharpoint'] > $rw['balance']) {
        $msgno = "Sorry, Your Balance is Low .... Please Recharge Soon";
        ?>
        <script>
            setTimeout(function () {
                window.location.href= 'panmanual.php';
            }, 2000);
        </script>
        <?php	
    } elseif ($name == "") {
        $msgno = 'Please Enter Name .... ';
    } elseif ($fathername == "") {
        $msgno = 'Please Enter Father Name .... ';
    } elseif ($dobadhar == "") {
        $msgno = 'Please Enter Date of Birth .... ';
    } else {
        $a = mysqli_query($ahk_conn, "SELECT panno FROM panauto WHERE panno='" . $aadharno . "'");
        $b = mysqli_fetch_array($a);

        if ($b['panno'] == $aadharno) {
            $msgno = 'This Pan Card No Already Exist .... ';
        } else {
            if ((strpos($sign_image_type, $word) !== false) && (strpos($image_type, $word) !== false)) {

                // Base64 conversion with prefix
                $image_base64 = "data:" . $image_type . ";base64," . base64_encode(file_get_contents($_FILES['imagefile']['tmp_name']));
                $sign_base64  = "data:" . $sign_image_type . ";base64," . base64_encode(file_get_contents($_FILES['signfile']['tmp_name']));

                date_default_timezone_set('Asia/Kolkata');
                $timestamp = date("Y-m-d H:i:s");

                $query = "INSERT INTO panauto(`userid`,`panno`,`name`,`fathername`,`dob`,`image`,`signimage`,`create_time`) 
                          VALUES('" . $_SESSION['phone'] . "','" . $aadharno . "','" . $name . "','" . $fathername . "','" . $dobadhar . "','" . $image_base64 . "','" . $sign_base64 . "','" . $timestamp . "')";

                $result = mysqli_query($ahk_conn, $query);

                if ($result) {
                    $appliedby = $udata['phone'];
                    $parentPhone = $udata['parent'];
                    $debit_fee = $udata['balance'] - $fee;
                    addCommission($ahk_conn, $appliedby, $fee, $parentPhone, "Pan Manual Verification");

                    mysqli_query($ahk_conn,"UPDATE users SET balance=balance-$fee WHERE phone='$appliedby'");
                    mysqli_query($ahk_conn, "INSERT INTO wallethistory(userid, amount, balance, purpose, status, type) VALUES ('$appliedby', '$fee', '$debit_fee', 'Pan Manual Print', '1', 'Debit')");

                    $msg = "Please Wait Pan Preview just a second...";
                    $_SESSION["Panno"]=trim($aadharno);

                    echo '<script>
                        $(function(){
                            Swal.fire(
                                "' . $msg . '",
                                "",
                                "success"
                            );
                        });
                        setTimeout(function () {
                            window.location = "Pan_manual_list.php";
                        }, 1200);
                    </script>';
                }
            } else {
                $msgno = "Please select PNG and JPEG image only.";
            }
        }
    }
}

if (!empty($msgno)) {
    echo '<script>
        document.addEventListener("DOMContentLoaded", function() {
            alert("' . $msgno . '");
        });
    </script>';
}
?>
