<?php
/**
 * Edit/Add announcement page template
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

$is_edit = !empty($announcement) && isset($announcement->id) && $announcement->id > 0;
$page_title = $is_edit ? __('Edit Announcement', 'pc-announcements-274') : __('Add New Announcement', 'pc-announcements-274');
?>

<div class="wrap pc-announcements-274-wrap">
    <h1 class="wp-heading-inline">
        <?php echo esc_html($page_title); ?>
    </h1>
    <a href="<?php echo admin_url('admin.php?page=pc-announcements-274'); ?>" class="page-title-action">
        <?php _e('Back to List', 'pc-announcements-274'); ?>
    </a>
    <hr class="wp-header-end">
    
    <form id="pc-announcements-274-form" class="pc-announcements-274-form">
        <div class="pc-announcements-274-main-content">
            <div class="pc-announcements-274-card">
                <div class="pc-announcements-274-card-header">
                    <h2><?php _e('Announcement Details', 'pc-announcements-274'); ?></h2>
                </div>
                <div class="pc-announcements-274-card-body">
                    <div class="pc-announcements-274-form-row">
                        <div class="pc-announcements-274-form-group">
                            <label for="title" class="pc-announcements-274-label">
                                <?php _e('Title', 'pc-announcements-274'); ?> <span class="required">*</span>
                            </label>
                            <input type="text" id="title" name="title" class="regular-text" required
                                   value="<?php echo $is_edit ? esc_attr($announcement->title) : ''; ?>"
                                   placeholder="<?php _e('Enter announcement title', 'pc-announcements-274'); ?>">
                        </div>
                    </div>
                    
                    <div class="pc-announcements-274-form-row">
                        <div class="pc-announcements-274-form-group">
                            <label for="banner_color" class="pc-announcements-274-label">
                                <?php _e('Banner Color', 'pc-announcements-274'); ?>
                            </label>
                            <div class="pc-announcements-274-color-picker">
                                <input type="color" id="banner_color" name="banner_color" 
                                       value="<?php echo $is_edit ? esc_attr($announcement->banner_color) : '#0d47a1'; ?>"
                                       style="width: 60px; height: 40px; padding: 2px; cursor: pointer;">
                                <input type="text" id="banner_color_text" class="regular-text" 
                                       value="<?php echo $is_edit ? esc_attr($announcement->banner_color) : '#0d47a1'; ?>"
                                       placeholder="#0d47a1">
                            </div>
                            <p class="description">
                                <?php _e('Choose the background color for the announcement banner', 'pc-announcements-274'); ?>
                            </p>
                        </div>
                    </div>
                    
                    <div class="pc-announcements-274-form-row">
                        <div class="pc-announcements-274-form-group">
                            <label for="link_url" class="pc-announcements-274-label">
                                <?php _e('Link URL', 'pc-announcements-274'); ?>
                            </label>
                            <input type="url" id="link_url" name="link_url" class="regular-text"
                                   value="<?php echo $is_edit ? esc_url($announcement->link_url) : ''; ?>"
                                   placeholder="<?php _e('https://example.com', 'pc-announcements-274'); ?>">
                            <p class="description">
                                <?php _e('Enter a URL to make the announcement banner clickable', 'pc-announcements-274'); ?>
                            </p>
                        </div>
                    </div>
                    
                    <div class="pc-announcements-274-form-row">
                        <div class="pc-announcements-274-form-group">
                            <label for="image_url" class="pc-announcements-274-label">
                                <?php _e('Image URL', 'pc-announcements-274'); ?>
                            </label>
                            <div class="pc-announcements-274-media-upload">
                                <input type="url" id="image_url" name="image_url" class="regular-text"
                                       value="<?php echo $is_edit ? esc_url($announcement->image_url) : ''; ?>"
                                       placeholder="<?php _e('https://example.com/image.jpg', 'pc-announcements-274'); ?>">
                                <button type="button" class="button pc-announcements-274-upload-image-btn">
                                    <?php _e('Upload Image', 'pc-announcements-274'); ?>
                                </button>
                            </div>
                            <?php if ($is_edit && $announcement->image_url): ?>
                                <div class="pc-announcements-274-image-preview">
                                    <img src="<?php echo esc_url($announcement->image_url); ?>" alt="<?php _e('Preview', 'pc-announcements-274'); ?>" style="max-width: 200px; height: auto;">
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="pc-announcements-274-card">
                <div class="pc-announcements-274-card-header">
                    <h2><?php _e('Scheduling', 'pc-announcements-274'); ?></h2>
                </div>
                <div class="pc-announcements-274-card-body">
                    <div class="pc-announcements-274-form-row">
                        <div class="pc-announcements-274-form-group">
                            <label for="status" class="pc-announcements-274-label">
                                <?php _e('Status', 'pc-announcements-274'); ?>
                            </label>
                            <select id="status" name="status" class="regular-text">
                                <option value="active" <?php echo ($is_edit && $announcement->status === 'active') ? 'selected' : ''; ?>>
                                    <?php _e('Active', 'pc-announcements-274'); ?>
                                </option>
                                <option value="inactive" <?php echo ($is_edit && $announcement->status === 'inactive') ? 'selected' : ''; ?>>
                                    <?php _e('Inactive', 'pc-announcements-274'); ?>
                                </option>
                                <option value="scheduled" <?php echo ($is_edit && $announcement->status === 'scheduled') ? 'selected' : ''; ?>>
                                    <?php _e('Scheduled', 'pc-announcements-274'); ?>
                                </option>
                            </select>
                        </div>
                    </div>
                    
                    <div class="pc-announcements-274-form-row">
                        <div class="pc-announcements-274-form-group">
                            <label for="start_date" class="pc-announcements-274-label">
                                <?php _e('Start Date', 'pc-announcements-274'); ?>
                            </label>
                            <input type="datetime-local" id="start_date" name="start_date"
                                   value="<?php echo $is_edit && $announcement->start_date ? date('Y-m-d\TH:i', strtotime($announcement->start_date)) : ''; ?>">
                            <p class="description">
                                <?php _e('Leave empty to start immediately', 'pc-announcements-274'); ?>
                            </p>
                        </div>
                        
                        <div class="pc-announcements-274-form-group">
                            <label for="end_date" class="pc-announcements-274-label">
                                <?php _e('End Date', 'pc-announcements-274'); ?>
                            </label>
                            <input type="datetime-local" id="end_date" name="end_date"
                                   value="<?php echo $is_edit && $announcement->end_date ? date('Y-m-d\TH:i', strtotime($announcement->end_date)) : ''; ?>">
                            <p class="description">
                                <?php _e('Leave empty to show indefinitely', 'pc-announcements-274'); ?>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="pc-announcements-274-sidebar">
            <div class="pc-announcements-274-card">
                <div class="pc-announcements-274-card-header">
                    <h2><?php _e('Publish', 'pc-announcements-274'); ?></h2>
                </div>
                <div class="pc-announcements-274-card-body">
                    <input type="hidden" name="id" value="<?php echo $is_edit ? $announcement->id : 0; ?>">
                    <input type="hidden" name="action" value="pc_announcements_274_action">
                    <input type="hidden" name="sub_action" value="save_announcement">
                    <?php wp_nonce_field('pc_announcements_274_nonce', 'nonce'); ?>
                    
                    <div class="pc-announcements-274-publish-actions">
                        <button type="submit" class="button button-primary button-large">
                            <?php echo $is_edit ? __('Update Announcement', 'pc-announcements-274') : __('Create Announcement', 'pc-announcements-274'); ?>
                        </button>
                        <a href="<?php echo admin_url('admin.php?page=pc-announcements-274'); ?>" class="button">
                            <?php _e('Cancel', 'pc-announcements-274'); ?>
                        </a>
                    </div>
                    
                    <?php if ($is_edit): ?>
                        <div class="pc-announcements-274-form-info">
                            <p><strong><?php _e('Created:', 'pc-announcements-274'); ?></strong> <?php echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($announcement->created_at)); ?></p>
                            <?php if ($announcement->updated_at !== $announcement->created_at): ?>
                                <p><strong><?php _e('Last Updated:', 'pc-announcements-274'); ?></strong> <?php echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($announcement->updated_at)); ?></p>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </form>
</div>

<div id="pc-announcements-274-delete-modal" class="pc-announcements-274-modal" style="display: none;">
    <div class="pc-announcements-274-modal-backdrop"></div>
    <div class="pc-announcements-274-modal-content">
        <div class="pc-announcements-274-modal-header">
            <h3><?php _e('Delete Announcement', 'pc-announcements-274'); ?></h3>
        </div>
        <div class="pc-announcements-274-modal-body">
            <p><?php _e('Are you sure you want to delete this announcement? This action cannot be undone.', 'pc-announcements-274'); ?></p>
        </div>
        <div class="pc-announcements-274-modal-footer">
            <button type="button" class="button pc-announcements-274-cancel-delete"><?php _e('Cancel', 'pc-announcements-274'); ?></button>
            <button type="button" class="button button-danger pc-announcements-274-confirm-delete"><?php _e('Delete', 'pc-announcements-274'); ?></button>
        </div>
    </div>
</div>