
<?php
/**
 * Template Name: Contractor Welcome Page
 * Template Post Type: page
 */

get_header();

// Get the latest contractor created by the current user
$current_user_id = get_current_user_id();
$args = [
    'post_type'      => 'contractor',
    'author'         => $current_user_id,
    'post_status'    => ['publish', 'pending'],
    'posts_per_page' => 1,
    'orderby'        => 'date',
    'order'          => 'DESC',
];
$contractor = get_posts($args);
?>

<div class="welcome-container">
    <?php if ($contractor): 
        $c        = $contractor[0];
        $title    = get_the_title($c->ID);
        $city     = get_post_meta($c->ID, 'rccc_city', true);
        $zips     = get_post_meta($c->ID, 'rccc_zips_csv', true);
        $email    = get_post_meta($c->ID, 'rccc_contact_email', true);
        $logo     = get_the_post_thumbnail_url($c->ID, 'medium');
        $profile  = get_permalink($c->ID);
    ?>
        <h1>🎉 Welcome, <?php echo esc_html($title); ?>!</h1>
        <p>Your contractor profile has been successfully created.</p>

        <?php if ($logo): ?>
            <img src="<?php echo esc_url($logo); ?>" alt="<?php echo esc_attr($title); ?>" class="contractor-logo">
        <?php endif; ?>

        <div class="contractor-details">
            <p><strong>City:</strong> <?php echo esc_html($city ?: 'Not provided'); ?></p>
            <p><strong>Service ZIPs:</strong> <?php echo esc_html($zips ?: 'Not provided'); ?></p>
            <p><strong>Email:</strong> <?php echo esc_html($email ?: 'Not provided'); ?></p>
        </div>

        <p><a href="<?php echo esc_url($profile); ?>" class="btn">View Your Profile</a></p>
    <?php else: ?>
        <h2>No contractor profile found.</h2>
        <p>Please submit your details on the 
            <a href="<?php echo esc_url(home_url('/contractor-form/')); ?>">Contractor Form</a>.
        </p>
    <?php endif; ?>
</div>

<?php get_footer(); ?>
