<?php
date_default_timezone_set('Asia/Kolkata'); 
include ('../config.php');
@session_start();
@$user_id1=$_SESSION['user_id1'];
@$idvfied1=$_SESSION['idverified1'];
@$namevfied1=$_SESSION['namevfied1'];
if(!isset($_SESSION['user_id1'])){
    header("Location: index.php");
}

$timeout = 60 * 60 * 24 * 365;
$s_name = session_name();
if(isset($_COOKIE[$s_name])) {
    setcookie($s_name, $_COOKIE[$s_name], time() + $timeout, '/');
}

$sel1 = mysqli_query($conn, "select * from hr_dump where empid='" . @$user_id1 . "'");
$fetch1 = mysqli_fetch_array($sel1);

$queryFR = mysqli_query($conn, "select * from tracker where Functional_Reporting='$fetch1[Employee_Number]'");
$dataFR = mysqli_fetch_array($queryFR);

$queryFR = mysqli_query($conn, "select * from emp_leave where Functional_Reporting='$fetch1[Employee_Number]'");
$dataFR = mysqli_fetch_array($queryFR);
?>

<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum=1, viewport-fit=cover" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <title>VC Project</title>
    <?php include('header.php'); ?>
    <div id="appCapsule">
        <div class="section wallet-card-section pt-1">
            <div class="wallet-card">
                <?php include('include/wallet-balance.php'); ?>
                <div class="mb-4 py-2">
                    <h6 class="text-center">Enter VC Details</h6>
                    <?php
                    if (isset($_POST['vcSub'])) {
                        if ($_FILES['vcscreenshot']['name'] != '') {
                            $fileExtension = pathinfo($_FILES['vcscreenshot']['name'], PATHINFO_EXTENSION);
                            $sanitizedFileName = preg_replace('/[^a-zA-Z0-9\-_\.]/', '', basename($_FILES['vcscreenshot']['name'], '.' . $fileExtension));
                            $vcscreenshot = uniqid() . '-' . $sanitizedFileName . '.' . $fileExtension;

                            // Move the uploaded file
                            $source = $_FILES['vcscreenshot']['tmp_name'];
                            $destination = "../vcscreenshot/" . $vcscreenshot;

                            if (move_uploaded_file($source, $destination)) {
                                $stmt = $connpe->prepare("INSERT INTO vc_client_details (empid, Employee_Full_Name, Team_Name, Role, Location_of_Work, Functional_Reporting, Functional_Reporting_Name, client_name, client_mobile, client_email, client_location, vcscreenshot, project_name, status, ProjCreatedBy) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

                                if (!$stmt) {
                                    die("Error: " . $connpe->error);
                                }

                                $stmt->bind_param("sssssssssssssss", $_POST['empid'], $_POST['Employee_Full_Name'], $_POST['Team_Name'], $_POST['Role'], $_POST['Location_of_Work'], $_POST['Functional_Reporting'], $_POST['Functional_Reporting_Name'], $_POST['client_name'], $_POST['client_mobile'], $_POST['client_email'], $_POST['client_location'], $vcscreenshot, $_POST['project_name'], $_POST['status'], $_POST['ProjCreatedBy']);

                                if ($stmt->execute()) {
                                    echo "<div class='col-md-12 text-center btn btn-success'>Details submitted successfully!</div>";
                                } else {
                                    die("Error executing statement: " . $stmt->error);
                                    echo "<div class='col-md-12 text-center btn btn-danger'><strong>Error!</strong> Sorry something went wrong!</div>";
                                }
                            } else {
                                die("Error moving uploaded file.");
                            }
                        }
                    }
                    ?>
                    <form class="form-horizontal" name="vcprojects" action="" method="post" enctype="multipart/form-data">
                        <div class="row bg-light rounded py-2 mb-4">
                            <input class="form-control" type="hidden" name="empid" value="<?php echo $fetch1['empid']; ?>" >
                            <input class="form-control" type="hidden" name="Employee_Full_Name" value="<?php echo $fetch1['Employee_Full_Name']; ?>" >
                            <input class="form-control" type="hidden" name="Team_Name" value="<?php echo $fetch1['Team_Name']; ?>" >
                            <input class="form-control" type="hidden" name="Role" value="<?php echo $fetch1['Role']; ?>" >
                            <input class="form-control" type="hidden" name="Location_of_Work" value="<?php echo $fetch1['Location_of_Work']; ?>" >
                            <input class="form-control" type="hidden" name="Functional_Reporting" value="<?php echo $fetch1['Functional_Reporting']; ?>" >
                            <input class="form-control" type="hidden" name="Functional_Reporting_Name" value="<?php echo $fetch1['Functional_Reporting_Name']; ?>" >
                            <input class="form-control" type="hidden" name="ProjCreatedBy" id="ProjCreatedBy" value="">
                            <div class="col-md-3 col-sm-3"><label><b>Client Name*</b></label>
                                <input class="form-control" type="text" name="client_name" placeholder="Enter Client Name" required oninput="removeSpecialChars(this)">
                            </div>
                            <div class="col-md-3 col-sm-3"><label><b>Client Mobile*</b></label>
                                <input class="form-control" type="text" name="client_mobile" minlength="10" maxlength="10" required oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" placeholder="Only 10 Digits Mobile No">
                            </div>
                            <div class="col-md-3 col-sm-3"><label><b>Client Email*</b></label>
                                <input class="form-control" type="email" name="client_email" placeholder="Client Email" required>
                            </div>
                            <div class="col-md-3 col-sm-3"><label><b>Client Location*</b></label>
                                <input class="form-control" type="text" name="client_location" placeholder="Client Location" required oninput="removeSpecialChars(this)">
                            </div>
                            <div class="col-md-6 col-sm-6"><label><b>Upload Screenshot*</b></label>
                                <input class="form-control" type="file"  name="vcscreenshot" required >
                            </div>
                            <?php
                                $service_query = $connpe->query("SELECT * FROM vc_projects where status='Active' ORDER BY project_name ASC");
                                $serviceRowCount = $service_query->num_rows;
                            ?>
                            <div class="col-md-6 col-sm-6"><label><b>Project Name*</b></label>
                                <select name="project_name" id="Project" required class="form-control margin-bottom" onchange="setProjCreatedBy(this)">
                                    <option value="">-Select-</option>
                                    <?php if ($serviceRowCount > 0) {
                                        while ($row = $service_query->fetch_assoc()) { 
                                            echo "<option value='{$row['project_name']} - {$row['project_location']}' data-createdby='{$row['ProjCreatedBy']}'>{$row['project_name']} - {$row['project_location']}</option>";
                                        }
                                    } else {
                                        echo '<option value="">Category not available</option>';
                                    } ?>
                                </select>
                            </div>
                            <div class="col-md-12 col-sm-12 text-center">
                                <input class="form-control" type="hidden" name="status" value="Pending">
                                <button name="vcSub" class="btn btn-success">Submit</button>
                            </div>
                        </div>
                    </form>
                    <div class="row stat-box py-2">
                        <h6 class="text-center">View VC Details</h6>
                        <div class="table">
                            <table id="" class="table table-striped table-bordered">
                                <thead>
                                    <tr class="small">
                                        <th>ID</th>
                                        <th>Client Name</th>
                                        <th>Project Name</th>
                                        <th>Client Mobile</th>
                                        <th>Client Email</th>
                                        <th>Client Location</th>
                                        <th>Status</th>
                                        <th>Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php
                                function mask_mobile($mobile) {
                                    return '******' . substr($mobile, -4);
                                }
                                function mask_email($email) {
                                    $parts = explode('@', $email);
                                    return substr($parts[0], 0, 2) . '*****@' . $parts[1];
                                }

                                $query = "select * from vc_client_details where empid = '$fetch1[empid]' ORDER BY cid DESC";
                                $result = mysqli_query($connpe, $query);
                                while ($data = mysqli_fetch_assoc($result)) {
                                    @$sn++;
                                ?>
                                <tr class="small">
                                    <td><?php echo $data['cid'] ?></td>
                                    <td><?php echo $data['client_name'] ?></td>
                                    <td><?php echo $data['project_name'] ?></td>
                                    <td><?php echo mask_mobile($data['client_mobile']) ?></td>
                                    <td><?php echo mask_email($data['client_email']) ?></td>
                                    <td><?php echo $data['client_location'] ?></td>
                                    <td><?php echo $data['status'] ?></td>
                                    <td><?php echo $data['created_date'] ?></td>
                                </tr>
                                <?php } ?> 
                                </tbody>
                            </table>        
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script type="text/javascript">
        function removeSpecialChars(element) {
            element.value = element.value.replace(/[^a-zA-Z0-9\s]/g, '');
        }

        function setProjCreatedBy(select) {
            var selectedOption = select.options[select.selectedIndex];
            var ProjCreatedBy = selectedOption.getAttribute('data-createdby');
            document.getElementById('ProjCreatedBy').value = ProjCreatedBy;
        }
        </script>
        <?php include('include/copyright.php'); ?>
    </div>
    <?php include('footer.php'); ?>
</body>
</html>
