<?php
include "include/header.php";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $doc_type = mysqli_real_escape_string($myConn, $_POST['doc_typ']);

    $uploadDir = "uploads/";

    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    /* FRONT FILE */
    $frontName = time() . "_front_" . basename($_FILES['kyc_file_front']['name']);
    $frontPath = $uploadDir . $frontName;

    /* BACK FILE */
    $backName = time() . "_back_" . basename($_FILES['kyc_file_back']['name']);
    $backPath = $uploadDir . $backName;

    /* MOVE FILES */
    if (
        move_uploaded_file($_FILES['kyc_file_front']['tmp_name'], $frontPath) &&
        move_uploaded_file($_FILES['kyc_file_back']['tmp_name'], $backPath)
    ) {

        $sql = "INSERT INTO kyc (username, email, doc_type, idfront, idback)
                VALUES (?, ?, ?, ?, ?)";

        $stmt = mysqli_prepare($myConn, $sql);
        mysqli_stmt_bind_param($stmt, "sssss", $name, $email, $doc_type, $frontPath, $backPath);

        if (mysqli_stmt_execute($stmt)) {
            echo "<script>
                    alert('KYC uploaded successfully!');
                    window.location.href = '';
                  </script>";
        } else {
            echo "<script>alert('Database error. Please try again.');</script>";
        }

        mysqli_stmt_close($stmt);
    } else {
        echo "<script>alert('File upload failed.');</script>";
    }
}
?>

<script src="https://www.cryptohopper.com/widgets/js/script" defer></script>

<div class="page-content footer-clear">

            <!-- Page Title-->
            <div class="pt-3">
                <div class="page-title d-flex">
                    <div class="align-self-center">
                        <a href="#" data-back-button class="me-3 ms-0 icon icon-xxs bg-theme rounded-s shadow-m">
                            <i class="bi bi-chevron-left color-theme font-14"></i>

                        </a>
                    </div>
                    <div class="align-self-center me-auto">
                        <h1 class="color-theme mb-0 font-18">Verify Account</h1>
                    </div>
                    <div class="align-self-center ms-auto">
                        <a href="#" data-bs-toggle="offcanvas" data-bs-target="#menu-sidebar"
                            class="icon icon-xxs gradient-highlight color-white shadow-bg shadow-bg-xs rounded-s">
                            <i class="bi bi-list font-20"></i>
                        </a>
                    </div>
                </div>
            </div>




            <link rel="stylesheet" href="card/style.css">
            <script src="card/costom.js"></script>


            <div class="card card-style">
                <div class="content">

                    <div class="text-center">
                        <img src="images/XXL_height.webp" class="img-fluid rounded-s">
                    </div>
                    <h4>User Verification</h4>
                    <h5>Upload your ID card</h5>
                    <p style="margin-bottom: 20px !important;" class="mb-0">
                        (Driving License or Government ID card)<br>
                        Uploading your ID helps as ensure the safety and security of your funds
                    </p>


                    <?php
$email = $_SESSION['user_email'];

$stmt = mysqli_prepare(
    $myConn,
    "SELECT status FROM kyc WHERE email = ? LIMIT 1"
);

mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);

/* BIND RESULT */
mysqli_stmt_bind_result($stmt, $status);

/* FETCH RESULT */
if (mysqli_stmt_fetch($stmt)) {

    $status = strtolower($status);

    if ($status === 'approved') {
        $alertClass = 'alert-success';
    } elseif ($status === 'rejected') {
        $alertClass = 'alert-danger';
    } else {
        $alertClass = 'alert-warning';
        $status = 'pending';
    }
?>
    <div class="alert <?php echo $alertClass; ?> alert-dismissible fade show" role="alert">
        <strong>KYC Submitted!</strong><br>
        Status: <b><?php echo ucfirst($status); ?></b>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
<?php
}

mysqli_stmt_close($stmt);
?>

    

                    <form method="post" enctype="multipart/form-data" id="">

                        <div class="form-custom form-label form-icon mb-3">
                            <i class="bi bi-file-earmark-binary font-13"></i>

                            <!-- <select name="doc_typ" class="form-select rounded-xs" id="c6" aria-label="Floating label select example">
                                    <option class="opt" selected value="">Select Document</option>
                                    <option class="opt" value="Int Passport">Int Passport</option>
                                    <option class="opt" value="Driving License">Driving License</option>
                                    <option class="opt" value="Identity Card">Govt Approved Identity Card</option>
                                </select> -->

                            <select name="doc_typ" class="form-select rounded-xs" id="c6"
                                aria-label="Floating label select example">
                                <option class="opt" value="">Select Document</option>
                                <option class="opt" value="Int Passport">Int Passport</option>
                                <option class="opt" value="Driving License">Driving License</option>
                                <option class="opt" value="Identity Card">Govt Approved Identity Card</option>
                            </select>





                            <label for="c6" class="color-highlight form-label-always-active">Select Document
                                Type</label>
                        </div>

                        <div class="row">



                            <div class="file-data">
                                <div class="form-custom form-label mb-3 form-icon">
                                    <!-- Dropify File Input -->
                                    <input required type="file" name="kyc_file_front" class="dropify" data-height="50"
                                        data-default-file="images/" />
                                    <label for="c6" class="color-highlight form-label-always-active">Document
                                        Front</label>
                                </div>
                            </div>


                            <div class="file-data">
                                <div class="form-custom form-label mb-3 form-icon">
                                    <!-- Dropify File Input -->
                                    <!-- <label>Phone Number</label> -->
                                    <input required type="file" name="kyc_file_back" class="dropify" data-height="50"
                                        data-default-file="images/" />
                                    <label for="c6" class="color-highlight form-label-always-active">Document
                                        Back</label>
                                </div>
                            </div>




                        </div>
                        <div class="divider mt-2 mb-4"></div>

                        <button type="submit" class="btn btn-full gradient-highlight shadow-bg shadow-bg-s">Upload</button>

                    </form>


























                </div>

            </div>

        </div>
        <!-- End of Page Content-->


<script src="scripts/bootstrap.min.js"></script>
<script src="scripts/custom.js"></script>
</body>