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

$user_id = $_SESSION['user_id'];
$emp_id = $_SESSION['emp_id'];
$user_role = $_SESSION['user_role'];

if (isset($_GET['edit'])) {
    if ($user_role > 0) {
        $count = mysqli_num_rows(mysqli_query($db, "select * from users where id='$_GET[edit]' and leader='$emp_id' "));
        if ($count == 0) {
            echo "<script>location.href='404';</script>";
        }
    }

    $edit = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM `users` WHERE `id`='$_GET[edit]' "));
}


?>


<!-- ============================================================== -->
<!-- Start Page Content here -->
<!-- ============================================================== -->

<div class="content-page">
    <div class="content">
        <div class="container-fluid">
            <div class="mt-5"></div>

            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body p-4">
                            <h4 class="header-title mt-1 mb-4">
                                Add Employees
                            </h4>

                            <form id="employeeFormSubmit" class="parsley-examples">
                                <div class="row">

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="userName">User Name<span class="text-danger">*</span></label>
                                            <input type="text" name="name" required placeholder="Enter user name" class="form-control" id="userName" value="<?php echo $edit['name']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="<?php echo isset($_GET['edit']) ? 'employeeId' : 'addEmployeeId'; ?>">Employee Id<span class="text-danger">*</span></label>
                                            <input type="text" name="<?php echo isset($_GET['edit']) ? 'employeeId' : 'addEmployeeId'; ?>" required placeholder="Enter Employee Id" class="form-control" id="<?php echo isset($_GET['edit']) ? 'employeeId' : 'addEmployeeId'; ?>" value="<?php echo $edit['employee_id']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="emailAddress">Email address<span class="text-danger">*</span></label>
                                            <input type="email" name="email" required placeholder="Enter email" class="form-control" id="emailAddress" value="<?php echo $edit['email']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="phoneNumber">Phone<span class="text-danger">*</span></label>
                                            <input type="text" name="phone" data-parsley-type="number" required placeholder="Enter phone number" class="form-control" id="phoneNumber" value="<?php echo $edit['phone']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="gender">Gender<span class="text-danger">*</span></label>
                                            <select data-plugin="customselect" name="gender" class="form-control custom-select-picker" data-placeholder="Select Gender" id="gender" required>
                                                <option></option>
                                                <option <?php echo ($edit['gender'] == 'Female') ? 'selected' : ''; ?> value="Female">Female</option>
                                                <option <?php echo ($edit['gender'] == 'Male') ? 'selected' : ''; ?> value="Male">Male</option>
                                                <option <?php echo ($edit['gender'] == 'Other') ? 'selected' : ''; ?> value="Other">Other</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="dob">Date of Birth (DOB)<span class="text-danger">*</span></label>
                                            <input type="date" name="dob" required placeholder="Enter Date of Birth" class="form-control" id="dob" value="<?php echo (isset($edit['dob']) ? ((!empty($edit['dob'])) ? date("Y-m-d", strtotime($edit['dob'])) : '') : ''); ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="joiningDate">Joining Date<span class="text-danger">*</span></label>
                                            <input type="date" name="joiningDate" required class="form-control" id="joiningDate" value="<?php echo (isset($edit['joining_date']) ? ((!empty($edit['joining_date'])) ? date("Y-m-d", strtotime($edit['joining_date'])) : '') : ''); ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="full_time_date">Full Time Date<span class="text-danger">*</span></label>
                                            <input type="date" name="full_time_date" required class="form-control" id="full_time_date" value="<?php echo (isset($edit['full_time_date']) ? ((!empty($edit['full_time_date'])) ? date("Y-m-d", strtotime($edit['full_time_date'])) : '') : ''); ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="probationDate">Probation Date</label>
                                            <input type="date" name="probationDate" placeholder="Enter phone number" class="form-control" id="probationDate" value="<?php echo (isset($edit['probation_date']) ? ((!empty($edit['probation_date'])) ? date("Y-m-d", strtotime($edit['probation_date'])) : '') : ''); ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="noticePeriod">Notice Period (In Days)</label>
                                            <input type="text" name="noticePeriod" pattern="[0-9]+" data-parsley-type="number" placeholder="Enter Notice Period" class="form-control" id="noticePeriod" value="<?php echo $edit['notice_period']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="department">Department<span class="text-danger">*</span></label>
                                            <select name="department" class="form-control custom-select-picker-ajax" data-search="departments" data-param="<?php echo htmlspecialchars(json_encode(['default'=>$edit['department'], 'current_page'=>$currentPageName])); ?>" data-plugin="customselect" data-placeholder="Select Department" id="department" required>
                                                <option></option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="designation">Designation<span class="text-danger">*</span></label>
                                            <select name="designation" class="form-control custom-select-picker-ajax" data-search="designations" data-param="<?php echo htmlspecialchars(json_encode(['default'=>$edit['designation'], 'current_page'=>$currentPageName])); ?>" data-plugin="customselect" data-placeholder="Select Designation" id="designation" required>
                                                <option></option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Role<span class="text-danger">*</span></label>
                                            <select data-plugin="customselect" name="user_role" class="form-control custom-select-picker" data-placeholder="Select Role" required>
                                                <option></option>
                                                <?php if ($user_role == 0) { ?>
                                                    <option <?php echo ($edit['role'] == '0') ? 'selected' : ''; ?> value="0">Boss</option>
                                                    <option <?php echo ($edit['role'] == '1') ? 'selected' : ''; ?> value="1">Manager</option>
                                                <?php } ?>
                                                <option <?php echo ($edit['role'] == '2' || $user_role == 1) ? 'selected' : ''; ?> value="2">Employee</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Leader<span class="text-danger">*</span></label>
                                            <select name="leader" id="leader" class="form-control custom-select-picker-ajax" data-search="managers" data-param="<?php echo htmlspecialchars(json_encode(['default'=>$edit['leader'], 'current_page'=>$currentPageName])); ?>" data-plugin="customselect" data-placeholder="Select Leader" required>
                                                <option></option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Location<span class="text-danger">*</span></label>
                                            <input type="text" name="location" id="location" class="form-control" placeholder="Enter Location" value="<?php echo $edit['location']; ?>" required>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="panNumber">PAN No.</label>
                                            <input type="text" name="panNumber" placeholder="Enter PAN No." class="form-control" id="panNumber" value="<?php echo $edit['pan_no']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="accountNumber">Account No.</label>
                                            <input type="text" name="accountNumber" placeholder="Enter Account No." class="form-control" id="accountNumber" value="<?php echo $edit['account_no']; ?>">
                                        </div>
                                    </div>
                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="ifscCode">IFSC Code</label>
                                            <input type="text" name="ifscCode" placeholder="Enter IFSC Code" class="form-control" id="ifscCode" value="<?php echo $edit['ifsc_code']; ?>">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label>Loi Reference ID</label>
                                            <select name="loi_id" id="loi_id" class="form-control custom-select-picker-ajax" data-search="loi_ids" data-param="<?php echo htmlspecialchars(json_encode(['default'=>$edit['loi_id'], 'current_page'=>$currentPageName])); ?>" data-plugin="customselect" data-placeholder="Select Loi Reference ID">
                                                <option></option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-12">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label for="" class="d-block">Having ESIC No.(Previous)</label>
                                                    <div class="form-check d-inline-block">
                                                        <input class="form-check-input" type="radio" name="havingESIStatus" value="Yes" id="havingESIStatusYes" <?php echo ($edit['esi_status'] == 'Yes') ? 'checked' : ''; ?>>
                                                        <label class="form-check-label" for="havingESIStatusYes">Yes</label>
                                                    </div>
                                                    <div class="form-check d-inline-block ml-3">
                                                        <input class="form-check-input" type="radio" name="havingESIStatus" value="No" id="havingESIStatusNo" <?php echo isset($edit['esi_status']) ? (($edit['esi_status'] == 'No') ? 'checked' : '') : 'checked'; ?>>
                                                        <label class="form-check-label" for="havingESIStatusNo">No</label>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-md-6" id="havingESIHide">
                                                <div class="form-group">
                                                    <label for="ESINo">Enter ESIC No.</label>
                                                    <input type="text" name="ESINo" placeholder="Enter ESIC No." class="form-control" id="ESINo" value="<?php echo $edit['esi_no']; ?>">
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-12">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label for="" class="d-block">Having UAN No.(PF No. Previous)</label>
                                                    <div class="form-check d-inline-block">
                                                        <input class="form-check-input" type="radio" name="havingUANStatus" value="Yes" id="havingUANStatusYes" <?php echo ($edit['uan_no_status'] == 'Yes') ? 'checked' : ''; ?>>
                                                        <label class="form-check-label" for="havingUANStatusYes">Yes</label>
                                                    </div>
                                                    <div class="form-check d-inline-block ml-3">
                                                        <input class="form-check-input" type="radio" name="havingUANStatus" value="No" id="havingUANStatusNo" <?php echo isset($edit['uan_no_status']) ? (($edit['uan_no_status'] == 'No') ? 'checked' : '') : 'checked'; ?>>
                                                        <label class="form-check-label" for="havingUANStatusNo">No</label>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="col-md-6" id="havingUANHide">
                                                <div class="form-group">
                                                    <label for="UANNo">Enter UAN No.(PF)</label>
                                                    <input type="text" name="UANNo" placeholder="Enter UAN No." class="form-control" id="UANNo" value="<?php echo $edit['uan_no']; ?>">
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-12" id="optingForPFHide" style="display:<?php echo isset($edit['uan_no_status']) ? (($edit['uan_no_status'] == 'Yes') ? 'none' : 'block') : 'block'; ?>;">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label for="" class="d-block">Opting For PF</label>
                                                    <div class="form-check d-inline-block">
                                                        <input class="form-check-input" type="radio" name="optingPFStatus" value="Yes" id="optingPFStatusYes" <?php echo ($edit['opting_epf_status'] == 'Yes') ? 'checked' : ''; ?>>
                                                        <label class="form-check-label" for="optingPFStatusYes">Yes</label>
                                                    </div>
                                                    <div class="form-check d-inline-block ml-3">
                                                        <input class="form-check-input" type="radio" name="optingPFStatus" value="No" id="optingPFStatusNo" <?php echo isset($edit['opting_epf_status']) ? (($edit['opting_epf_status'] == 'No') ? 'checked' : '') : 'checked'; ?>>
                                                        <label class="form-check-label" for="optingPFStatusNo">No</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="col-12">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <div class="form-group">
                                                    <label for="" class="d-block">PF Applicability <span id="checkForUANpf">
                                                            <?php if (isset($edit['uan_no_status']) && $edit['uan_no_status'] == 'Yes') { ?>
                                                                <i class="uil uil-check-circle text-success"></i>
                                                            <?php } ?>
                                                            <?php if (isset($edit['opting_epf_status']) && $edit['opting_epf_status'] == 'Yes') { ?>
                                                                <i class="uil uil-check-circle text-success"></i>
                                                            <?php } ?>
                                                        </span>
                                                    </label>
                                                    <div class="form-check d-inline-block">
                                                        <input class="form-check-input" type="radio" name="PFApplicabilityStatus" value="Yes" id="PFApplicabilityStatusYes" <?php echo ($edit['epf_applicable_status'] == 'Yes') ? 'checked' : ''; ?> disabled>
                                                        <label class="form-check-label" for="PFApplicabilityStatusYes">Yes</label>
                                                    </div>
                                                    <div class="form-check d-inline-block ml-3">
                                                        <input class="form-check-input" type="radio" name="PFApplicabilityStatus" value="No" id="PFApplicabilityStatusNo" <?php echo isset($edit['epf_applicable_status']) ? (($edit['epf_applicable_status'] == 'No') ? 'checked' : '') : 'checked'; ?> disabled>
                                                        <label class="form-check-label" for="PFApplicabilityStatusNo">No</label>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <!-- ############################## -->


                                    <div class="col-md-12">
                                        <div class="row">
                                            <div class="col-6">
                                                <div class="form-group">
                                                    <label for="consider_as">Consider As<span class="text-danger">*</span></label>
                                                    <select name="consider_as" id="consider_as" class="form-control custom-select-picker" data-plugin="customselect" data-placeholder="Select Consider As" required>
                                                        <option></option>
                                                        <option value="Employee" <?php echo ($edit['consider_as'] == 'Employee') ?'selected' : ''; ?>>Employee</option>
                                                        <option value="Considered As Intern" <?php echo ($edit['consider_as'] == 'Considered As Intern') ?'selected' : ''; ?>>Considered As Intern</option>
                                                        <option value="Intern" <?php echo ($edit['consider_as'] == 'Intern') ?'selected' : ''; ?>>Intern</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>


                                    <?php if (isset($edit['id'])) { ?>
                                        <div class="col-md-12">
                                            <p class="mb-0" style="font-weight:700;">Note:</p>
                                            <p style="font-size:0.75rem; line-height:1rem;">
                                                <span>For maintain old password don't fill the password fields.</span>
                                                <br><span class="text-danger">Or</span><br>
                                                <span>For create new password enter both fields of passwords.</span>
                                            </p>
                                        </div>
                                    <?php } ?>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="pass1">Password<?php echo isset($edit['id']) ? '' : '<span class="text-danger">*</span>'; ?></label>
                                            <input id="pass1" type="password" name="password" placeholder="Password" <?php echo isset($edit['id']) ? '' : 'required'; ?> class="form-control">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="passWord2">Confirm Password<?php echo isset($edit['id']) ? '' : '<span class="text-danger">*</span>'; ?></label>
                                            <input data-parsley-equalto="#pass1" name="cPassword" type="password" <?php echo isset($edit['id']) ? '' : 'required'; ?> placeholder="Password" class="form-control" id="passWord2">
                                        </div>
                                    </div>

                                </div>

                                <div class="form-group text-right mb-0 mt-4">
                                    <?php if (isset($edit['id'])) { ?>
                                        <input type="hidden" name="editUserId" value="<?php echo $edit['id']; ?>">
                                        <button class="btn btn-primary mr-1" name="updateUser" id="employeeFormSubmitButton" type="button">Update User</button>
                                    <?php } else { ?>
                                        <button class="btn btn-primary mr-1" name="addUser" id="employeeFormSubmitButton" type="button">Add User</button>
                                    <?php } ?>
                                </div>

                            </form>
                        </div>
                    </div> <!-- end card -->
                </div>
                <!-- end col -->

            </div>
            <!-- end row -->

        </div>
    </div> <!-- content -->

</div>


<?php
include "pin/footer.php";
?>

<script>
    $("input[name=havingESIStatus]").change(function() {
        if ($("input[name=havingESIStatus]:checked").val() == 'Yes') {
            $("#havingESIHide input").prop('required', true);
        } else {
            $("#havingESIHide input").prop('required', false);
            $("#havingESIHide input").val('');
        }
    });

    $("input[name=havingUANStatus]").change(function() {

        if ($("input[name=havingUANStatus]:checked").val() == 'Yes') {

            $("#havingUANHide input").prop('required', true);
            $("#havingUANHide").slideDown();

            $("#optingForPFHide input[name=optingPFStatus][value=No]").prop('checked', true).trigger("click");
            $("#optingForPFHide").slideUp();


            $("#checkForUANpf").children().last().remove();
            $("input[name=PFApplicabilityStatus][value=Yes]").prop('checked', true).trigger("click");
            $("#checkForUANpf").append('<i class="uil uil-check-circle text-success"></i>');


        } else {

            $("#havingUANHide input").prop('required', false);
            $("#havingUANHide input").val('');

            $("#optingForPFHide").slideDown();

            $("input[name=PFApplicabilityStatus][value=No]").prop('checked', true).trigger("click");
            $("#checkForUANpf").children().last().remove();

        }
    });

    $("input[name=optingPFStatus]").change(function() {
        if ($("input[name=optingPFStatus]:checked").val() == 'Yes') {

            $("#havingUANHide").slideUp();

            $("#checkForUANpf").children().last().remove();
            $("input[name=PFApplicabilityStatus][value=Yes]").prop('checked', true).trigger("click");
            $("#checkForUANpf").append('<i class="uil uil-check-circle text-success"></i>');

        } else {

            $("#havingUANHide").slideDown();

            $("input[name=PFApplicabilityStatus][value=No]").prop('checked', true).change();
            $("#checkForUANpf").children().last().remove();

        }
    });

    function hasEmptyFields(form) {
        // Get all input elements within the form
        const inputs = form.querySelectorAll('input, select, textarea');

        // Check if any input is empty or null
        return Array.from(inputs).some(input => {
                // console.log(input.required, input.value.trim(), input.id);
                return input.required && input.value.trim() === ''
            }
        );
    }

    $("#employeeFormSubmitButton").click(function(e) {
        // $("input[name=employeeIdDetails]").prop("disabled", false);
        
        // e.preventDefault();
        let form = document.getElementById('employeeFormSubmit');
        // let form_data = new FormData(form);
        let form_data = $('#employeeFormSubmit').serialize();
        console.log(form_data);
        
        if(hasEmptyFields(form)){
            failedMessage('Please, Fill all the Fields.','<?php echo $_SESSION['user_name']; ?>');
            return false;
        }
        console.log('enter');

        $("input[name=PFApplicabilityStatus]").prop("disabled", false);
        form_data = $('#employeeFormSubmit').serialize();

        $.ajax({
            url: 'fun/function',
            type: 'POST',
            data: form_data,
            dataType: 'json',
            success: function(res) {
                // console.log(res);
                if (res.status === 200) {
                    successMessage(res.message, '<?php echo $_SESSION['user_name']; ?>');
                    setTimeout(() => {
                        location.href='all_users';
                    }, 2000);
                } else {
                    failedMessage(res.message, '<?php echo $_SESSION['user_name']; ?>');
                }
            },
            error: function(xhr, status, error) {
                console.log(`Error: ${JSON.stringify(error)}`);
            }
        });

    });

</script>