<?php
// 1. Creează rolul "Agency"
function create_agency_user_role() {
    if (!get_role('agency')) {
        add_role('agency', 'Agency', array('read' => true));
    }
}
add_action('init', 'create_agency_user_role');

// 2. Blochează acces wp-admin
function restrict_agency_admin_access() {
    if (current_user_can('agency') && is_admin() && !defined('DOING_AJAX')) {
        wp_redirect(home_url('/agency-dashboard'));
        exit;
    }
}
add_action('admin_init', 'restrict_agency_admin_access');

// 3. Ascunde bara admin
function remove_agency_admin_bar() {
    if (current_user_can('agency')) {
        show_admin_bar(false);
    }
}
add_action('after_setup_theme', 'remove_agency_admin_bar');

// 4. Redirecționează după login
function agency_login_redirect($redirect_to, $request, $user) {
    if (isset($user->roles) && in_array('agency', $user->roles)) {
        return home_url('/agency-dashboard');
    }
    return $redirect_to;
}
add_filter('login_redirect', 'agency_login_redirect', 10, 3);

// 5. Register nouă locație de meniu
function register_agency_menu_location() {
    register_nav_menu('agency_menu', __('Agency Menu'));
}
add_action('after_setup_theme', 'register_agency_menu_location');

// 6. Shortcode: [agency_add_candidate]
function agency_add_candidate_form_shortcode() {
    if (!current_user_can('agency')) {
        return '<p>You do not have permission to access this page.</p>';
    }

    ob_start();

    if (isset($_GET['success']) && $_GET['success'] === 'true') {
        echo '<p style="color:green;">✅ This candidate was successfully added.</p>';
    }

    if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['agency_candidate_submit'])) {
        $username = sanitize_user($_POST['username']);
        $email = sanitize_email($_POST['email']);
        $password = sanitize_text_field($_POST['password']);
        $phone = sanitize_text_field($_POST['phone']);
        $country = sanitize_text_field($_POST['country']);
        $birthdate = sanitize_text_field($_POST['birthdate']);

        if (username_exists($username) || email_exists($email)) {
            echo '<p style="color:red;">⛔ Username or email already exists.</p>';
        } else {
            $user_id = wp_create_user($username, $password, $email);
            if (!is_wp_error($user_id)) {
                wp_update_user(array('ID' => $user_id, 'role' => 'candidate'));
                update_user_meta($user_id, 'phone', $phone);
                update_user_meta($user_id, 'country', $country);
                update_user_meta($user_id, 'birthdate', $birthdate);
                update_user_meta($user_id, 'created_by_agency', get_current_user_id());

                // Upload poză de profil dacă este
                if (!empty($_FILES['profile_picture']['name'])) {
                    require_once ABSPATH . 'wp-admin/includes/file.php';
                    $uploadedfile = $_FILES['profile_picture'];
                    $upload_overrides = array('test_form' => false);
                    $movefile = wp_handle_upload($uploadedfile, $upload_overrides);

                    if ($movefile && !isset($movefile['error'])) {
                        update_user_meta($user_id, 'profile_picture', $movefile['url']);
                    }
                }

                wp_redirect(add_query_arg('success', 'true', get_permalink()));
                exit;
            } else {
                echo '<p style="color:red;">Error: ' . $user_id->get_error_message() . '</p>';
            }
        }
    }
    ?>

    <form method="post" class="agency-modern-register-form" enctype="multipart/form-data">
        <h3>Create a Candidate</h3>

        <div class="form-group">
            <input type="text" name="username" required placeholder="Username">
        </div>

        <div class="form-group">
            <input type="email" name="email" required placeholder="Email">
        </div>

        <div class="form-group">
            <input type="password" name="password" required placeholder="Password">
        </div>

        <div class="form-group">
            <input type="text" name="phone" required placeholder="Phone Number">
        </div>

        <div class="form-group">
            <select name="country" class="styled-select" required>
                <option value="" disabled selected hidden>Select Country</option>
                <option value="India">India</option>
                <option value="Sri Lanka">Sri Lanka</option>
                <option value="Bangladesh">Bangladesh</option>
                <option value="Nepal">Nepal</option>
                <option value="Pakistan">Pakistan</option>
            </select>
        </div>

        <div class="form-group">
            <input type="date" name="birthdate" required placeholder="Birth Date">
        </div>

        <div class="form-group">
            <label>Profile Picture</label>
            <input type="file" name="profile_picture" accept="image/*">
        </div>

        <div class="form-group terms">
            <label><input type="checkbox" required> I accept terms & conditions</label>
        </div>

        <div class="form-group">
            <input type="submit" name="agency_candidate_submit" value="Register Now" class="submit-button">
        </div>
    </form>

    <?php
    return ob_get_clean();
}
add_shortcode('agency_add_candidate', 'agency_add_candidate_form_shortcode');

// CSS Styling în wp_head
add_action('wp_head', function () {
    if (!is_page('agency-add-candidate')) return;
    ?>
    <style>
        .agency-modern-register-form {
            max-width: 480px;
            margin: 40px auto;
            background: #fff;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 0 40px rgba(0,0,0,0.05);
        }

        .agency-modern-register-form h3 {
            margin-bottom: 25px;
            text-align: center;
            font-size: 24px;
        }

        .agency-modern-register-form .form-group {
            margin-bottom: 20px;
        }

        .agency-modern-register-form input[type="text"],
        .agency-modern-register-form input[type="email"],
        .agency-modern-register-form input[type="password"],
        .agency-modern-register-form input[type="date"],
        .agency-modern-register-form input[type="file"] {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 14px;
        }

        .agency-modern-register-form select.styled-select {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 14px;
            background-color: #fff;
            font-family: inherit;
            color: #333;
            appearance: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg fill='%23333' height='12' viewBox='0 0 24 24' width='12' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 12px center;
            background-size: 12px;
            cursor: pointer;
        }

        .agency-modern-register-form select.styled-select:focus {
            border-color: #0073aa;
            outline: none;
        }

        .agency-modern-register-form .terms {
            font-size: 13px;
            color: #444;
        }

        .agency-modern-register-form .submit-button {
            width: 100%;
            background-color: #0047ab;
            border: none;
            color: #fff;
            padding: 12px;
            border-radius: 8px;
            font-size: 15px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .agency-modern-register-form .submit-button:hover {
            background-color: #003585;
        }
    </style>
    <?php
});


.delete-button {
    background-color: #dc3545;
    color: white;
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 13px;
    border: none;
    cursor: pointer;
}
.delete-button:hover {
    background-color: #a71d2a;
}




input[type="checkbox"] {
    display: inline-block !important;
    width: auto !important;
}












/* === JOB LIST LAYOUT - Naukrigulf Style === */

.job-list.v3.map-item {
    background: #fff;
    border: 1px solid #e2e6ea;
    border-radius: 10px;
    padding: 20px;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 20px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.05);
    transition: box-shadow 0.2s ease;
}

.job-list.v3.map-item:hover {
    box-shadow: 0 6px 20px rgba(0,0,0,0.08);
}

.job-list .inner-left {
    flex: 1;
}

.job-list .employer-logo img {
    width: 60px;
    height: 60px;
    border-radius: 6px;
    object-fit: cover;
}

.job-list .job-title a {
    font-size: 20px;
    font-weight: 600;
    color: #0a66c2;
    text-decoration: none;
}

.job-list .job-title a:hover {
    text-decoration: underline;
}

.job-list .job-metas {
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    font-size: 14px;
    color: #555;
}

.job-list .job-type,
.job-list .job-salary,
.job-list .job-deadline,
.job-list .job-category {
    display: flex;
    align-items: center;
    gap: 6px;
}

.job-list .job-type .type-job,
.job-list .featured-text,
.job-list .urgent {
    background-color: #e6f2ff;
    color: #0a66c2;
    font-size: 12px;
    padding: 4px 8px;
    border-radius: 4px;
}

.job-list .urgent {
    background-color: #ffecec;
    color: #cc0000;
}

.job-list .ali-right {
    min-width: 80px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-between;
}

.job-list .btn-apply {
    padding: 8px 14px;
    background: #0a66c2;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 500;
    text-transform: uppercase;
    text-align: center;
    transition: background 0.2s ease;
}

.job-list .btn-apply:hover {
    background: #004f9f;
}





