<?php

/**
 * @package Onepage
 * @author JoomShaper http://www.joomshaper.com
 * @copyright Copyright (c) 2010 - 2018 JoomShaper
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
 */
//no direct accees
defined('_JEXEC') or die('resticted aceess');

// Requred soccer helper
require_once JPATH_BASE . '/components/com_spsoccer/helpers/helper.php';

class SppagebuilderAddonTop_player extends SppagebuilderAddons {

    public function render() {
        $class = (isset($this->addon->settings->class) && $this->addon->settings->class) ? $this->addon->settings->class : '';

        $sp_top_player_item = (isset($this->addon->settings->sp_top_player_item) && $this->addon->settings->sp_top_player_item) ? $this->addon->settings->sp_top_player_item : '';
        $output = '<div class="sppb-addon sppb-top-player-wrapper ' . $class . '">';
        $output .= '<div class="sppb-row">';
        foreach ($sp_top_player_item as $key => $player) {
            $output .= '<div class="sppb-col-sm-6 sppb-col-md-3">';
            $player_info = $this->getPlayerInfo($player->player);

            $player_url = JRoute::_('index.php?option=com_spsoccer&view=player&id=' . $player_info->spsoccer_player_id . ':' . $player_info->slug . SpsoccerHelper::getItemid('players'));
            $player_info->image = SpsoccerHelper::hasImg($player_info->image);

            $output .= '<div class="team-rising-player">';

            if(strpos($player_info->image, 'http://') !== false || strpos($player_info->image, 'https://') !== false){
                $output .= '<img class="img-responsive" src="' . $player_info->image . '" alt="'.$player_info->title . '">';
            } else {
                $output .= '<img class="img-responsive" src="' . JURI::base(true) . '/' . $player_info->image . '" alt="' . $player_info->title . '">';
            }

            $output .= '<div class="rising-player-content-wrap">';
            $output .= '<div class="team-logo-img">';
            if(strpos($player->team_logo, 'http://') !== false || strpos($player->team_logo, 'https://') !== false){
                $output .= '<img src="' . $player->team_logo . '" alt="' . $player->team_title . '"/>';
            } else {
                $output .= '<img src="' . JURI::base(true) . '/' . $player->team_logo . '" alt="' . $player->team_title . '"/>';
            }
            $output .= '</div>';
            $output .= '<span>' . $player->team_title . '</span>';
            $output .= '<h4 class="tp-player-name">';
            $output .= '<a href="' . $player_url . '">' . $player_info->title . '</a>';
            $output .= '</h4>';
            $output .= '</div>'; // .rising-player-content-wrap
            $output .= '</div>'; // .team-rising-player
            $output .= '</div>'; // END /.sppb-col-sm-4
        }
        $output .= '</div>'; // END /.sppb-row
        $output .= '</div>'; // END /.sppb-top-player-wrapper

        return $output;
    }

    function getPlayerInfo($player_id) {
        // Get Player Data
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select($db->quoteName(array('a.spsoccer_player_id', 'a.title', 'a.slug', 'a.image', 'a.spsoccer_possition_id')));
        $query->select($db->quoteName('b.title', 'player_position'));
        $query->from($db->quoteName('#__spsoccer_players', 'a'));
        $query->join('LEFT', $db->quoteName('#__spsoccer_possitions', 'b') . ' ON (' . $db->quoteName('a.spsoccer_possition_id') . ' = ' . $db->quoteName('b.spsoccer_possition_id') . ')');
        $query->where($db->quoteName('a.enabled') . " = 1");
        $query->where($db->quoteName('a.spsoccer_player_id') . " = " . $db->quote($player_id));
        $db->setQuery($query);
        $result = $db->loadObject();
        return $result;
    }

}