<?php

/**
 * SocialPromoter - Upcoming Queue admin page.
 *
 * Shows pending and recently sent queue items with stats for sent posts.
 *
 * @package PublisherKits_SocialPromoter
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
$queue = new PKITS_SP_Queue();
$pending = $queue->get_upcoming( 100 );
$recent = $queue->get_history( 20 );
?>
<div class="wrap pkits-sp-wrap">

	<h1 class="pkits-sp-page-title">
		<span class="pkits-sp-logo-mark">SP</span>
		<?php 
esc_html_e( 'SocialPromoter &mdash; Activity', 'publisherkits-socialpromoter' );
?>
	</h1>

	<?php 
// Upgrade information shown in the free plugin. The separately supplied Pro
// package provides the additional functionality.
if ( PKITS_SP_Admin::show_upgrade_notice() ) {
    ?>
		<div class="pkits-sp-empty-state" style="background:#fff8e5;border:1px solid #f0d98a;margin-bottom:16px;padding:12px 16px;">
			<p style="margin:0;">
				<strong><?php 
    esc_html_e( 'You are on the free plan.', 'publisherkits-socialpromoter' );
    ?></strong>
				<?php 
    esc_html_e( 'Upgrade to Pro for AI copy, branded images, automation, campaigns and engagement analytics.', 'publisherkits-socialpromoter' );
    ?>
				<?php 
    if ( function_exists( 'pkits_sp_fs' ) ) {
        ?>
					<a href="<?php 
        echo esc_url( pkits_sp_fs()->get_upgrade_url() );
        ?>"><?php 
        esc_html_e( 'See Pro &rarr;', 'publisherkits-socialpromoter' );
        ?></a>
				<?php 
    }
    ?>
			</p>
		</div>
	<?php 
}
?>

	<?php 
?>

	<!-- Upcoming / pending -->
	<h2 style="font-family:'Roboto Slab',Georgia,serif;font-size:16px;color:#1a2d4a;margin:0 0 10px;">
		<?php 
esc_html_e( 'Scheduled', 'publisherkits-socialpromoter' );
?>
	</h2>

	<?php 
if ( empty( $pending ) ) {
    ?>
		<div class="pkits-sp-empty-state" style="margin-bottom:24px;">
			<p><?php 
    esc_html_e( 'No scheduled posts. Open any Post in the editor and use the SocialPromoter panel to schedule one.', 'publisherkits-socialpromoter' );
    ?></p>
		</div>
	<?php 
} else {
    ?>
		<table class="widefat pkits-sp-queue-table" style="margin-bottom:30px;">
			<thead>
				<tr>
					<th><?php 
    esc_html_e( 'Platform', 'publisherkits-socialpromoter' );
    ?></th>
					<th><?php 
    esc_html_e( 'Post', 'publisherkits-socialpromoter' );
    ?></th>
					<th><?php 
    esc_html_e( 'Copy preview', 'publisherkits-socialpromoter' );
    ?></th>
					<th><?php 
    esc_html_e( 'Scheduled (UTC)', 'publisherkits-socialpromoter' );
    ?></th>
					<th><?php 
    esc_html_e( 'Status', 'publisherkits-socialpromoter' );
    ?></th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<?php 
    foreach ( $pending as $item ) {
        ?>
				<tr>
					<td>
						<span class="pkits-sp-platform-badge pkits-sp-badge-<?php 
        echo esc_attr( $item->platform );
        ?>">
							<?php 
        echo esc_html( strtoupper( $item->platform ) );
        ?>
						</span>
					</td>
					<td>
						<?php 
        if ( $item->post_id ) {
            ?>
							<a href="<?php 
            echo esc_url( get_edit_post_link( $item->post_id ) );
            ?>" style="font-size:12px;">
								<?php 
            echo esc_html( ( get_the_title( $item->post_id ) ?: '#' . $item->post_id ) );
            ?>
							</a>
						<?php 
        } else {
            ?>
							<span style="font-size:11px;color:#8a9ab0;"><?php 
            esc_html_e( 'Standalone', 'publisherkits-socialpromoter' );
            ?></span>
						<?php 
        }
        ?>
					</td>
					<td style="font-size:12px;color:#5a6a7a;">
						<?php 
        echo esc_html( mb_substr( $item->copy_text, 0, 60 ) . (( mb_strlen( $item->copy_text ) > 60 ? chr( 226 ) . chr( 128 ) . chr( 166 ) : '' )) );
        ?>
					</td>
					<td style="font-size:11px;white-space:nowrap;"><?php 
        echo esc_html( $item->scheduled_at );
        ?></td>
					<td>
						<span class="pkits-sp-status-pill pkits-sp-status-<?php 
        echo esc_attr( $item->status );
        ?>">
							<?php 
        echo esc_html( ucfirst( $item->status ) );
        ?>
						</span>
					</td>
					<td>
						<?php 
        if ( 'pending' === $item->status ) {
            ?>
							<form method="post" action="<?php 
            echo esc_url( admin_url( 'admin-post.php' ) );
            ?>">
								<?php 
            wp_nonce_field( 'pkits_sp_cancel_queue' );
            ?>
								<input type="hidden" name="action" value="pkits_sp_cancel_queue_item" />
								<input type="hidden" name="queue_id" value="<?php 
            echo esc_attr( $item->id );
            ?>" />
								<button type="submit" class="button button-small"><?php 
            esc_html_e( 'Cancel', 'publisherkits-socialpromoter' );
            ?></button>
							</form>
						<?php 
        }
        ?>
					</td>
				</tr>
				<?php 
    }
    ?>
			</tbody>
		</table>
	<?php 
}
?>

	<!-- Recent sent -->
	<?php 
if ( !empty( $recent ) ) {
    ?>
	<h2 style="font-family:'Roboto Slab',Georgia,serif;font-size:16px;color:#1a2d4a;margin:0 0 10px;">
		<?php 
    esc_html_e( 'Recently sent', 'publisherkits-socialpromoter' );
    ?>
	</h2>
	<table class="widefat pkits-sp-queue-table">
		<thead>
			<tr>
				<th><?php 
    esc_html_e( 'Platform', 'publisherkits-socialpromoter' );
    ?></th>
				<th><?php 
    esc_html_e( 'Post', 'publisherkits-socialpromoter' );
    ?></th>
				<th><?php 
    esc_html_e( 'Sent (UTC)', 'publisherkits-socialpromoter' );
    ?></th>
				<th><?php 
    esc_html_e( 'Stats', 'publisherkits-socialpromoter' );
    ?></th>
				<th></th>
			</tr>
		</thead>
		<tbody>
			<?php 
    foreach ( $recent as $item ) {
        $is_sent = 'sent' === $item->status;
        ?>
			<tr>
				<td>
					<span class="pkits-sp-platform-badge pkits-sp-badge-<?php 
        echo esc_attr( $item->platform );
        ?>">
						<?php 
        echo esc_html( strtoupper( $item->platform ) );
        ?>
					</span>
				</td>
				<td>
					<?php 
        if ( $item->post_id ) {
            ?>
						<a href="<?php 
            echo esc_url( get_edit_post_link( $item->post_id ) );
            ?>" style="font-size:12px;">
							<?php 
            echo esc_html( ( get_the_title( $item->post_id ) ?: '#' . $item->post_id ) );
            ?>
						</a>
					<?php 
        } else {
            ?>
						<span style="font-size:11px;color:#8a9ab0;"><?php 
            esc_html_e( 'Standalone', 'publisherkits-socialpromoter' );
            ?></span>
					<?php 
        }
        ?>
				</td>
				<td style="font-size:11px;color:#8a9ab0;white-space:nowrap;">
					<?php 
        echo esc_html( $item->sent_at ?? $item->scheduled_at );
        ?>
				</td>
				<td>
					<?php 
        $sp_cell_done = false;
        if ( !$sp_cell_done ) {
            if ( 'failed' === $item->status && !empty( $item->error_message ) ) {
                ?>
							<span style="font-size:11px;color:#d63638;"><?php 
                echo esc_html( mb_substr( $item->error_message, 0, 50 ) );
                ?></span>
						<?php 
            } elseif ( $is_sent ) {
                ?>
							<span style="font-size:11px;color:#8a9ab0;font-style:italic;"><?php 
                esc_html_e( 'Engagement stats are available in SocialPromoter Pro.', 'publisherkits-socialpromoter' );
                ?></span>
						<?php 
            } else {
                ?>
							-
						<?php 
            }
            ?>
					<?php 
        }
        ?>
				</td>
				<td>
					<?php 
        ?>
				</td>
			</tr>
			<?php 
    }
    ?>
		</tbody>
	</table>
	<p style="text-align:right;font-size:12px;margin-top:6px;">
		<a href="<?php 
    echo esc_url( admin_url( 'admin.php?page=publisherkits-socialpromoter-history' ) );
    ?>"><?php 
    esc_html_e( 'View full history', 'publisherkits-socialpromoter' );
    ?> &rarr;</a>
	</p>
	<?php 
}
?>

</div>

