<?php
/**
 * YP Countdown
 *
 * Example:
 * [yp_countdown style="default" custom="" date="11/28/2017"]
 */
add_shortcode("yp_countdown", "yp_countdown");
function yp_countdown($atts, $content = null) {
    static $countdown_id = 0;
    $countdown_id++;

    extract(shortcode_atts(array(
        "style"    => "default",
        "custom"   => "%D days %H:%M:%S",
        "date"     => "11/28/2017",
        "class"    => ""
    ), $atts));

    if(!$date) {
      return "";
    }

    $timer_function = '
      jQuery(this).text(
        event.strftime("%D days %H:%M:%S")
      );
    ';

    if($style == "styled") {
      $class .= " style-1";
      $timer_function = "
        jQuery(this).html(
          event.strftime([
            '<div class=\"row\">',
              '<div class=\"col-xs-6 col-sm-3\">',
                '<span>" . __('Days', 'youplay') . "</span>',
                '<span><span>%D</span></span>',
              '</div>',
              '<div class=\"col-xs-6 col-sm-3\">',
                '<span>" . __('Hours', 'youplay') . "</span>',
                '<span><span>%H</span></span>',
              '</div>',
              '<div class=\"col-xs-6 col-sm-3\">',
                '<span>" . __('Minutes', 'youplay') . "</span>',
                '<span><span>%M</span></span>',
              '</div>',
              '<div class=\"col-xs-6 col-sm-3\">',
                '<span>" . __('Seconds', 'youplay') . "</span>',
                '<span><span>%S</span></span>',
              '</div>',
            '</div>'
          ].join(''))
        );";
    } else if($style == 'custom') {
      $timer_function = '
        jQuery(this).html(
          event.strftime("' . $custom . '")
        );
      ';
    }

    $result = "
      <div class=\"countdown " . yp_sanitize_class($class) . "\" id=\"youplay_countdown_id_" . intval($countdown_id) . "\" data-end=\"" . esc_attr($date) . "\"></div>

      <script type=\"text/javascript\">
        jQuery(function() {
          jQuery(\"#youplay_countdown_id_" . intval($countdown_id) . "\").each(function() {
              console.log(this);
              jQuery(this).countdown(jQuery(this).attr('data-end'), function(event) {
                " . ($timer_function ? $timer_function : "") . "
              });
          })
        })
      </script>";

    return $result;
}



/* Add VC Shortcode */
add_action( "after_setup_theme", "vc_youplay_countdown" );
function vc_youplay_countdown() {
    if(function_exists("vc_map")) {
        /* Register shortcode with Visual Composer */
        vc_map( array(
           "name"     => __("YP Countdown", "youplay"),
           "base"     => "yp_countdown",
           "controls" => "full",
           "category" => "Youplay",
           "icon"     => "icon-youplay",
           "params"   => array(
              array(
                 "type"       => "dropdown",
                 "heading"    => __("Style", "youplay"),
                 "param_name" => "style",
                 "value"      => array(
                    __("Default", "youplay")  => "default",
                    __("Styled", "youplay")   => "styled",
                    __("Custom", "youplay")   => "custom"
                  ),
                 "description" => ""
              ),
              array(
                 "type"        => "textfield",
                 "heading"     => __("Custom Markup", "youplay"),
                 "param_name"  => "custom",
                 "value"       => __("%D days %H:%M:%S", "youplay"),
                 "description" => __("Type here custom coundown markup. More info here <a href='http://hilios.github.io/jQuery.countdown/' target='_blank'>http://hilios.github.io/jQuery.countdown/</a>", "youplay"),
                "dependency"  => array(
                  "element"   => "style",
                  "value"     => array( "custom" ),
                ),
              ),
              array(
                 "type"       => "textfield",
                 "heading"    => __("Date", "youplay"),
                 "param_name" => "date",
                 "value"      => __("11/28/2017", "youplay"),
                 "description" => __("Date Format: MM/DD/YYYY", "youplay")
              ),
              array(
                 "type"        => "textfield",
                 "heading"     => __("Custom Classes", "youplay"),
                 "param_name"  => "class",
                 "value"       => __("", "youplay"),
                 "description" => "",
              ),
           )
        ) );
    }
}