<?php
/**
 * @package SP Page Builder
 * @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');

//Load Helix
$helix3_path = JPATH_PLUGINS . '/system/helix3/core/helix3.php';

if (file_exists($helix3_path)) {
    require_once($helix3_path);
    helix3::addJS('jquery.countdown.min.js'); // JS Files
}

class SppagebuilderAddonUpcoming_match extends SppagebuilderAddons {

    public function render() {
        $title = (isset($this->addon->settings->title) && $this->addon->settings->title) ? $this->addon->settings->title : '';
        $match = (isset($this->addon->settings->match) && $this->addon->settings->match) ? $this->addon->settings->match : '';
        $cd_finished = (isset($this->addon->settings->cd_finished) && $this->addon->settings->cd_finished) ? $this->addon->settings->cd_finished : 'This Match is over !';
        $class = (isset($this->addon->settings->class) && $this->addon->settings->class) ? $this->addon->settings->class : '';

        // Get Match Data
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select($db->quoteName(array('a.spsoccer_match_id', 'a.title', 'a.slug', 'a.date', 'a.time', 'a.teamone', 'a.teamtwo')));
        $query->select($db->quoteName('b.title', 'teamone_name'));
        $query->select($db->quoteName('c.title', 'teamtwo_name'));
        $query->select($db->quoteName('d.title', 'match_vanue'));
        $query->from($db->quoteName('#__spsoccer_matches', 'a'));
        $query->join('LEFT', $db->quoteName('#__spsoccer_gameteams', 'b') . ' ON (' . $db->quoteName('a.teamone') . ' = ' . $db->quoteName('b.spsoccer_gameteam_id') . ')');
        $query->join('LEFT', $db->quoteName('#__spsoccer_gameteams', 'c') . ' ON (' . $db->quoteName('a.teamtwo') . ' = ' . $db->quoteName('c.spsoccer_gameteam_id') . ')');
        $query->join('LEFT', $db->quoteName('#__spsoccer_vanues', 'd') . ' ON (' . $db->quoteName('a.spsoccer_vanue_id') . ' = ' . $db->quoteName('d.spsoccer_vanue_id') . ')');
        $query->where($db->quoteName('a.enabled') . " = 1");
        $query->where($db->quoteName('a.spsoccer_match_id') . " = " . $db->quote($match));
        $db->setQuery($query);
        $match_info = $db->loadObject();

        // Variabeles
        $match_time = JHtml::date($match_info->date, 'Y/m/d') . ' ' . $match_info->time;

        $output = '<div class="sppb-addon sppb-addon-upcoming-match">';

        $output .= '<div class="sppb-addon-um-content">';
        $output .= '<div class="next-match-counter">';
        $output .= '<p>' . JText::_('UM_NEXT_MATCH') . '</p>';
        $output .= '<h2 class="team-name">';
        $output .= '<span class="first-match-name">' . $match_info->teamone_name . '</span><span class="verses-text">'.JText::_('COM_SPPAGEBUILDER_ADDON_UPCOMING_MATCH_VERSES').'</span><span class="second-match-name">' . $match_info->teamtwo_name . '</span>';
        $output .= '</h2>'; //team name
        $output .= '<ul id="sppb-um-match-countdown" data-umcountdown="' . $match_time . '" data-umcountdownover="' . $cd_finished . '"></ul>';
        $output .= '</div>'; //next-match-counter
        $output .= '</div>'; //sppb-addon-um-content
        $output .= '</div>'; //sppb-addon-upcoming-match

        return $output;
    }

    public function js() {
        return 'jQuery(function($) {
                        var $sppbUmCountDown  = $("#sppb-um-match-countdown"),
                            $umCountDownDate  = $sppbUmCountDown.data("umcountdown"),
                            $umCountDownOver  = $sppbUmCountDown.data("umcountdownover");
                            
                        $sppbUmCountDown.countdown( $umCountDownDate, function(event) {
                        $(this).html(event.strftime(\'<li class="days"><span class="number">%-D</span><span class="string"> Days</span></li><li class="hours"><span class="number">%H</span><span class="string"> Hours</span></li><li class="minutes"><span class="number">%M</span><span class="string"> Months</span></li><li class="seconds"><span class="number">%S</span><span class="string"> Minutes</span></li>\'));
                        });
                        $sppbUmCountDown.on("finish.countdown", function(event) {
                         $(this).html( $umCountDownOver )
                           .parent().addClass("disabled")
                       });
                  });
                ';
    }

}
