<?php

/**
 * Template for displaying player transfers in a table format.
 * This template is included by the PTSP_Shortcode class.
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
    // Exit if accessed directly
}
if ( isset( $id ) ) {
    $player = $id;
}
// Query arguments based on shortcode attributes
$args = [
    'post_type'      => 'ptfs_player_transfer',
    'posts_per_page' => -1,
    'orderby'        => 'meta_value',
    'meta_key'       => 'ptfs_transfer_date',
    'meta_type'      => 'DATE',
    'meta_query'     => [],
];
if ( !empty( $season ) ) {
    $args['tax_query'][] = [
        'taxonomy' => 'sp_season',
        'field'    => 'slug',
        'terms'    => $season,
    ];
}
if ( !empty( $club_in ) ) {
    $args['meta_query'][] = [
        'key'     => 'ptfs_club_in',
        'value'   => $club_in,
        'compare' => '=',
    ];
}
if ( !empty( $club_out ) ) {
    $args['meta_query'][] = [
        'key'     => 'ptfs_club_out',
        'value'   => $club_out,
        'compare' => '=',
    ];
}
if ( !empty( $player ) ) {
    $args['meta_query'][] = [
        'key'     => 'ptfs_player_name',
        'value'   => $player,
        'compare' => '=',
    ];
}
if ( !empty( $date_from ) ) {
    $args['meta_query'][] = [
        'key'     => 'ptfs_transfer_date',
        'value'   => $date_from,
        'compare' => '>=',
    ];
}
if ( !empty( $date_to ) ) {
    $args['meta_query'][] = [
        'key'     => 'ptfs_transfer_date',
        'value'   => $date_to,
        'compare' => '<=',
    ];
}
$query = new WP_Query($args);
// Get title of the table.
if ( !empty( $title ) ) {
    echo '<h4 class="sp-table-caption">' . esc_html( $title ) . '</h4>';
} else {
    echo '<h4 class="sp-table-caption">' . esc_html__( 'Player Transfers', 'player-transfers-for-sportspress' ) . '</h4>';
}
?>

<table id="player-transfers-table" class="display">
    <thead>
        <tr>
            <th><?php 
esc_html_e( 'Player', 'player-transfers-for-sportspress' );
?></th>
            <th><?php 
esc_html_e( 'Season', 'player-transfers-for-sportspress' );
?></th>
            <th><?php 
esc_html_e( 'Club In', 'player-transfers-for-sportspress' );
?></th>
            <th><?php 
esc_html_e( 'Club Out', 'player-transfers-for-sportspress' );
?></th>
            <th><?php 
esc_html_e( 'Transfer Date', 'player-transfers-for-sportspress' );
?></th>
        </tr>
    </thead>
    <tbody>
        <?php 
if ( $query->have_posts() ) {
    ?>
            <?php 
    while ( $query->have_posts() ) {
        $query->the_post();
        ?>
                <tr>
                    <td>
                        <a href="<?php 
        echo esc_url( get_permalink( rwmb_meta( 'ptfs_player_name' ) ) );
        ?>">
                            <?php 
        echo esc_html( get_the_title( rwmb_meta( 'ptfs_player_name' ) ) );
        ?>
                        </a>
                    </td>
                    <td><?php 
        echo get_the_term_list(
            get_the_ID(),
            'sp_season',
            '',
            ', ',
            ''
        );
        ?></td>
                    <td>
                        <a href="<?php 
        echo esc_url( get_permalink( rwmb_meta( 'ptfs_club_in' ) ) );
        ?>">
                            <?php 
        echo esc_html( get_the_title( rwmb_meta( 'ptfs_club_in' ) ) );
        ?>
                        </a>
                    </td>
                    <td>
                        <a href="<?php 
        echo esc_url( get_permalink( rwmb_meta( 'ptfs_club_out' ) ) );
        ?>">
                            <?php 
        echo esc_html( get_the_title( rwmb_meta( 'ptfs_club_out' ) ) );
        ?>
                        </a>
                    </td>
                    <td><?php 
        echo esc_html( wp_date( get_option( 'date_format' ), strtotime( rwmb_meta( 'ptfs_transfer_date' ) ) ) );
        ?></td>
                </tr>
            <?php 
    }
    ?>
            <?php 
    wp_reset_postdata();
    ?>
        <?php 
} else {
    ?>
            <tr>
                <td colspan="5"><?php 
    esc_html_e( 'No transfers found.', 'player-transfers-for-sportspress' );
    ?></td>
            </tr>
        <?php 
}
?>
    </tbody>
</table>

