/**
 * Player stats table.
 *
 * @access public
 * @param array
 * @param string $club
 * @param string $type
 * @param bool $keyarray = false
 * @return void
 */
function wpcm_match_player_stats_table($selected_players = array(), $type = 'lineup', $keyarray = false)
{
    global $post, $player;
    $teams = get_the_terms($post->ID, 'wpcm_team');
    $seasons = get_the_terms($post->ID, 'wpcm_season');
    if (is_array($teams)) {
        $match_teams = array();
        foreach ($teams as $team) {
            $match_teams[] = $team->term_id;
        }
    } else {
        $match_teams = array();
    }
    if (is_array($seasons)) {
        $match_seasons = array();
        foreach ($seasons as $season) {
            $match_seasons[] = $season->term_id;
        }
    } else {
        $match_seasons = array();
    }
    $args = array('post_type' => 'wpcm_player', 'meta_key' => 'wpcm_number', 'orderby' => 'menu_order meta_value_num', 'order' => 'ASC', 'showposts' => -1);
    if ($teams) {
        $args['tax_query'] = array('relation' => 'AND', array('taxonomy' => 'wpcm_team', 'field' => 'term_id', 'terms' => $match_teams), array('taxonomy' => 'wpcm_season', 'field' => 'term_id', 'terms' => $match_seasons));
    }
    $players = get_posts($args);
    if (empty($players)) {
        ?>

		<div class="wpcm-notice-block">
			<p>
				<?php 
        _e('No players found!', 'wp-club-manager');
        ?>
			</p>
		</div>
	<?php 
    } else {
        if (!is_array($selected_players)) {
            $selected_players = array();
        }
        $selected_players = array_merge(array('lineup' => array(), 'subs' => array()), $selected_players);
        $wpcm_player_stats_labels = wpcm_get_sports_stats_labels();
        ?>

		<p class="">
			<?php 
        _e('Click and drag players to reorder', 'wp-club-manager');
        ?>
			<img src="<?php 
        bloginfo('url');
        ?>
/wp-admin/images/loading.gif" id="loading-animation" />
		</p>
		<table class="wpcm-match-players-table">
			<thead>
				<tr class="player-stats-list-labels">
					<th>&nbsp;</th>

					<?php 
        foreach ($wpcm_player_stats_labels as $key => $val) {
            if (get_option('wpcm_show_stats_' . $key) == 'yes') {
                ?>
							<th<?php 
                if ($key == 'greencards' || $key == 'yellowcards' || $key == 'blackcards' || $key == 'redcards') {
                    echo ' class="th-checkbox"';
                }
                if ($key == 'mvp') {
                    echo ' class="th-radio"';
                }
                ?>
><?php 
                echo $val;
                ?>
</th>
						<?php 
            }
        }
        if ($type == 'subs') {
            ?>
						<th><?php 
            _e('Player Off', 'wp-club-manager');
            ?>
</th>
					<?php 
        }
        ?>

				</tr>
			</thead>
			<tbody class="wpcm-sortable">
				<?php 
        foreach ($players as $player) {
            ?>
					<?php 
            $played = is_array($selected_players) && array_key_exists($type, $selected_players) && is_array($selected_players[$type]) && array_key_exists($player->ID, $selected_players[$type]) && is_array($selected_players[$type][$player->ID]);
            $teams = get_the_terms($player->ID, 'wpcm_team');
            $seasons = get_the_terms($player->ID, 'wpcm_season');
            if ($teams) {
                $teamclass = array();
                foreach ($teams as $team) {
                    $teamclass[] = 'team_' . $team->term_id . ' ';
                }
            } else {
                $teamclass = array();
            }
            $player_teams = implode('', $teamclass);
            if ($seasons > 0) {
                foreach ($seasons as $season) {
                    $seasonclass = 'season_' . $season->term_id . ' ';
                }
            } else {
                $seasonclass = 'season_0 ';
            }
            $number = get_post_meta($player->ID, 'wpcm_number', true);
            if ($number) {
                $squad_number = $number . '. ';
            } else {
                $squad_number = '';
            }
            ?>

					<tr id="<?php 
            echo $player->ID;
            ?>
" data-player="<?php 
            echo $player->ID;
            ?>
" class="player-stats-list <?php 
            echo $player_teams;
            ?>
 <?php 
            echo $seasonclass;
            ?>
 sortable sorted">
						<td class="names">
							<label class="selectit">
								<input type="checkbox" data-player="<?php 
            echo $player->ID;
            ?>
" name="wpcm_players[<?php 
            echo $type;
            ?>
][<?php 
            echo $player->ID;
            ?>
][checked]" class="player-select" value="1" <?php 
            checked(true, $played);
            ?>
 /><span class="name">
								<?php 
            echo $squad_number;
            ?>
 <?php 
            echo $player->post_title;
            ?>
</span>
							</label>
						</td>
						<?php 
            foreach ($wpcm_player_stats_labels as $key => $val) {
                $keyarray = is_array($selected_players) && array_key_exists($type, $selected_players) && is_array($selected_players[$type]) && array_key_exists($player->ID, $selected_players[$type]) && is_array($selected_players[$type][$player->ID]) && array_key_exists($key, $selected_players[$type][$player->ID]);
                if (get_option('wpcm_show_stats_' . $key) == 'yes') {
                    if ($key == 'greencards') {
                        ?>

									<td class="<?php 
                        echo $key;
                        ?>
">
										<input type="checkbox" data-card="green" data-player="<?php 
                        echo $player->ID;
                        ?>
" name="wpcm_players[<?php 
                        echo $type;
                        ?>
][<?php 
                        echo $player->ID;
                        ?>
][<?php 
                        echo $key;
                        ?>
]" value="1" <?php 
                        checked(true, $keyarray);
                        if (!$played) {
                            echo ' disabled';
                        }
                        ?>
/>
									</td>

								<?php 
                    } elseif ($key == 'yellowcards') {
                        ?>

									<td class="<?php 
                        echo $key;
                        ?>
">
										<input type="checkbox" data-card="yellow" data-player="<?php 
                        echo $player->ID;
                        ?>
" name="wpcm_players[<?php 
                        echo $type;
                        ?>
][<?php 
                        echo $player->ID;
                        ?>
][<?php 
                        echo $key;
                        ?>
]" value="1" <?php 
                        checked(true, $keyarray);
                        if (!$played) {
                            echo ' disabled';
                        }
                        ?>
/>
									</td>

								<?php 
                    } elseif ($key == 'blackcards') {
                        ?>

									<td class="<?php 
                        echo $key;
                        ?>
">
										<input type="checkbox" data-card="black" data-player="<?php 
                        echo $player->ID;
                        ?>
" name="wpcm_players[<?php 
                        echo $type;
                        ?>
][<?php 
                        echo $player->ID;
                        ?>
][<?php 
                        echo $key;
                        ?>
]" value="1" <?php 
                        checked(true, $keyarray);
                        if (!$played) {
                            echo ' disabled';
                        }
                        ?>
/>
									</td>

								<?php 
                    } elseif ($key == 'redcards') {
                        ?>

									<td class="<?php 
                        echo $key;
                        ?>
">
										<input type="checkbox" data-card="red" data-player="<?php 
                        echo $player->ID;
                        ?>
" name="wpcm_players[<?php 
                        echo $type;
                        ?>
][<?php 
                        echo $player->ID;
                        ?>
][<?php 
                        echo $key;
                        ?>
]" value="1" <?php 
                        checked(true, $keyarray);
                        if (!$played) {
                            echo ' disabled';
                        }
                        ?>
/>
									</td>

								<?php 
                    } elseif ($key == 'rating') {
                        ?>

									<td class="<?php 
                        echo $key;
                        ?>
">
										<input type="number" data-player="<?php 
                        echo $player->ID;
                        ?>
" name="wpcm_players[<?php 
                        echo $type;
                        ?>
][<?php 
                        echo $player->ID;
                        ?>
][<?php 
                        echo $key;
                        ?>
]" value="<?php 
                        wpcm_stats_value($selected_players[$type], $player->ID, $key);
                        ?>
" min="0" max="10"<?php 
                        if (!$played) {
                            echo ' disabled';
                        }
                        ?>
/>
									</td>

								<?php 
                    } elseif ($key == 'mvp') {
                        ?>

									<td class="mvp">
										<input type="radio" data-player="<?php 
                        echo $player->ID;
                        ?>
" name="wpcm_players[<?php 
                        echo $type;
                        ?>
][<?php 
                        echo $player->ID;
                        ?>
][<?php 
                        echo $key;
                        ?>
]" value="1" <?php 
                        checked(true, $keyarray);
                        if (!$played) {
                            echo ' disabled';
                        }
                        ?>
 />
									</td>

								<?php 
                    } else {
                        ?>

									<td class="<?php 
                        echo $key;
                        ?>
">
										<input type="number" data-player="<?php 
                        echo $player->ID;
                        ?>
" name="wpcm_players[<?php 
                        echo $type;
                        ?>
][<?php 
                        echo $player->ID;
                        ?>
][<?php 
                        echo $key;
                        ?>
]" value="<?php 
                        wpcm_stats_value($selected_players[$type], $player->ID, $key);
                        ?>
"<?php 
                        if (!$played) {
                            echo ' disabled';
                        }
                        ?>
/>
									</td>

								<?php 
                    }
                }
            }
            if ($type == 'subs') {
                wpcm_player_subs_dropdown($selected_players, $player->ID, !$played);
            }
            ?>
					</tr>
				<?php 
        }
        ?>
			</tbody>
		</table>
	<?php 
    }
}
/**
 * Player profile stats table.
 *
 * @access public
 * @param array
 * @param string $team
 * @param string $season
 * @return void
 */
function wpcm_profile_stats_table($stats = array(), $team = 0, $season = 0)
{
    $id = get_the_ID();
    if (array_key_exists($team, $stats)) {
        if (array_key_exists($season, $stats[$team])) {
            $stats = $stats[$team][$season];
        }
    }
    $wpcm_player_stats_labels = wpcm_get_sports_stats_labels();
    $stats_labels = array('appearances' => '<a title="' . __('Games Played', 'wp-club-manager') . '">' . __('GP', 'wp-club-manager') . '</a>');
    $stats_labels = array_merge($stats_labels, $wpcm_player_stats_labels);
    ?>

	<table>
		<thead>
			<tr>
				<?php 
    foreach ($stats_labels as $key => $val) {
        if (get_option('wpcm_show_stats_' . $key) == 'yes') {
            ?>

						<th><?php 
            echo $val;
            ?>
</th>
					<?php 
        }
    }
    ?>
			</tr>
		</thead>
		<tbody>
			<tr>
				<?php 
    foreach ($stats_labels as $key => $val) {
        if ($key == 'appearances') {
            if (get_option('wpcm_show_stats_appearances') == 'yes') {
                if (get_option('wpcm_show_stats_subs') == 'yes') {
                    $subs = get_player_subs_total($id, $season, $team);
                    if ($subs > 0) {
                        $sub = ' <span class="sub-appearances">(' . $subs . ')</span>';
                    } else {
                        $sub = '';
                    }
                }
                ?>
					
							<td><span data-index="appearances"><?php 
                wpcm_stats_value($stats, 'total', 'appearances');
                echo get_option('wpcm_show_stats_subs') == 'yes' ? $sub : '';
                ?>
</span></td>

						<?php 
            }
        } elseif ($key == 'rating') {
            $rating = get_wpcm_stats_value($stats, 'total', 'rating');
            $apps = get_wpcm_stats_value($stats, 'total', 'appearances');
            $avrating = wpcm_divide($rating, $apps);
            if (get_option('wpcm_show_stats_rating') == 'yes') {
                ?>
					
							<td><span data-index="rating"><?php 
                echo sprintf("%01.2f", round($avrating, 2));
                ?>
</span></td>

						<?php 
            }
        } else {
            if (get_option('wpcm_show_stats_' . $key) == 'yes') {
                ?>

							<td><span data-index="<?php 
                echo $key;
                ?>
"><?php 
                wpcm_stats_value($stats, 'total', $key);
                ?>
</span></td>
						<?php 
            }
        }
    }
    ?>
				
			</tr>
		</tbody>
	</table>
<?php 
}
/**
 * Club stats table.
 *
 * @access public
 * @param array
 * @param string $comp
 * @param string $season
 * @return mixed $output
 */
function wpcm_club_stats_table($stats = array(), $comp = 0, $season = 0)
{
    $wpcm_standings_stats_labels = array('p' => get_option('wpcm_standings_p_label'), 'w' => get_option('wpcm_standings_w_label'), 'd' => get_option('wpcm_standings_d_label'), 'l' => get_option('wpcm_standings_l_label'), 'otw' => get_option('wpcm_standings_otw_label'), 'otl' => get_option('wpcm_standings_otl_label'), 'pct' => get_option('wpcm_standings_pct_label'), 'f' => get_option('wpcm_standings_f_label'), 'a' => get_option('wpcm_standings_a_label'), 'gd' => get_option('wpcm_standings_gd_label'), 'b' => get_option('wpcm_standings_bonus_label'), 'pts' => get_option('wpcm_standings_pts_label'));
    if (array_key_exists($comp, $stats)) {
        if (array_key_exists($season, $stats[$comp])) {
            $stats = $stats[$comp][$season];
        }
    }
    ?>

	<table>
		<thead>
			<tr>
				<td>&nbsp;</td>
				<?php 
    foreach ($wpcm_standings_stats_labels as $key => $val) {
        ?>
					<th><?php 
        echo $val;
        ?>
</th>
				<?php 
    }
    ?>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<th align="right"><?php 
    _e('Total', 'wp-club-manager');
    ?>
</th>
				<?php 
    foreach ($wpcm_standings_stats_labels as $key => $val) {
        ?>
					<td><input type="text" data-index="<?php 
        echo $key;
        ?>
" value="<?php 
        wpcm_stats_value($stats, 'total', $key);
        ?>
" size="2" tabindex="-1" readonly /></td>
				<?php 
    }
    ?>
			</tr>
		</tfoot>
		<tbody>
			<tr>
				<td align="right"><?php 
    _e('Auto', 'wp-club-manager');
    ?>
</td>
				<?php 
    foreach ($wpcm_standings_stats_labels as $key => $val) {
        ?>
						<td><input type="text" data-index="<?php 
        echo $key;
        ?>
" value="<?php 
        wpcm_stats_value($stats, 'auto', $key);
        ?>
" size="2" tabindex="-1" readonly /></td>
				<?php 
    }
    ?>
			</tr>
			<tr>
				<td align="right"><?php 
    _e('Manual', 'wp-club-manager');
    ?>
</td>
				<?php 
    foreach ($wpcm_standings_stats_labels as $key => $val) {
        ?>
					<td><input type="text" data-index="<?php 
        echo $key;
        ?>
" name="wpcm_stats[<?php 
        echo $comp;
        ?>
][<?php 
        echo $season;
        ?>
][<?php 
        echo $key;
        ?>
]" value="<?php 
        wpcm_stats_value($stats, 'manual', $key);
        ?>
" size="2" /></td>
				<?php 
    }
    ?>
			</tr>
		</tbody>
	</table>
<?php 
}