<?php
class Stylish_Cost_Calculator_Coupon {
  public static function init()
  {}

  public function __construct()
  {
  //  add_action( 'admin_init', array($this,'admin_init') );
    add_action( 'admin_menu', array($this,'admin_menu'), 90 );
 //   $this->license_return =get_option('scc_license_return');
 //   wp_register_script('scc-jquery-ui-js', SCC_URL . 'assets/js/jquery-ui.js');
 //   wp_enqueue_script('scc-jquery-ui-js');
 //   wp_register_style('scc-jquery-ui-css', SCC_URL . 'styles/jquery-ui.css');
}

function coupon_load(){
  //wp_enqueue_style('scc-admin-style', SCC_URL. 'styles/scc-front-end.css', array(), '3.1.1');
  wp_enqueue_style('gf-admin-style', SCC_URL. 'styles/scc-back-end.css', array(), '3.1.1');
  wp_enqueue_style('scc-jquery-ui-css');
//  wp_enqueue_script('scc-jquery-ui-js');
}

function coupon_page()
{
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    include_once SCC_DIR . '/admin/logo-header.php';
    ?>
    <style>
        #border1 {
          border: 2px black;
          -moz-box-shadow: inset 1px 1px 10px #F0F0F0;
          border-radius: 10px;
          background-color: #f8f9ff;
          padding: 15px;
          margin-left: 25px;
          margin-bottom: 15px;
          box-shadow: 0px 0px;
          max-width: 300px;
          box-shadow: 1px 1px 5px gray;
      }

      .green_dot {
          height: 16px;
          width: 16px;
          background-color: green;
          border-radius: 50%;
          display: inline-block;
          margin-left:10px;
          margin-right:10px;
      }

      .red_dot {
          height: 16px;
          width: 16px;
          background-color: red;
          border-radius: 50%;
          display: inline-block;
          margin-left:10px;
          margin-right:10px;
      }

      .yellow_dot{
          height: 16px;
          width: 16px;
          background-color: orange;
          border-radius: 50%;
          display: inline-block;
          margin-left:10px;
          margin-right:10px;
      }
      .scc_title_bar {
        background: #314af3;
        padding: 15px;
        padding-left: 45px;
        margin-left: -23px;
        font-size: 22px;
        color: #fff;
        letter-spacing: 1px;
        text-transform: uppercase;
        width: 105%;
        margin-bottom: 40px;
        margin-top: 10px;
    }
</style>
<?php

global $wp_version;
$test_char = "".DB_CHARSET;
$php_version = phpversion();
ob_start();
phpinfo(INFO_MODULES);
$contents = ob_get_clean();
$moduleAvailable = strpos($contents, 'mod_security') !== false;
$plugin_conflicted = 0;
$theme_conflicted = 0;
$theme = wp_get_theme();
$spl_license_return = get_option('act_ser_conn_refused');
$license_key_activated = get_option('spl_license_return');

$site_url = site_url();

function url(){
    if(isset($_SERVER['HTTPS'])){
        $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
    }
    else{
        $protocol = 'http';
    }
    return $protocol . "://" . $_SERVER['HTTP_HOST'];
}
global $wpdb;
$coupons = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}scc_coupons");

echo '<div class="scc_title_bar" style="background: #314af3; ">Coupon Generator & Editor</div>';

if (!isset($_GET['id'])) {

    echo '<div class="row" style="max-width:550px;background-color:#f9f9fe;padding-left:25px;padding-bottom:25px;padding-top:25px;margin-left:20px;margin-bottom:30px;">';
    echo '<div style="font-size:25px;margin-top:10px;" class="sccsubtitle scc_email_quote_label">ADD A NEW <span style="font-weight:800;color:#314bf8">COUPON</span></div>';
    echo       '<div style="color:green; margin-left:50px">'.(isset($_GET['saved']) && $_GET['saved'] == true ? "Coupon have been successfully saved" : "").'</div>';
    echo       '<div style="color:orange; margin-left:50px">'.(isset($_GET['deleted']) && $_GET['deleted'] == true ? "Coupon have been successfully deleted" : "").'</div>';

} else {
    $coupon_array = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}scc_coupons WHERE id = " .intval($_GET['id']));
    if(is_array($coupon_array)){
      if(sizeof($coupon_array) > 0){
        $coupon = $coupon_array[0];
    }
}

echo '<div class="row" style="max-width:550px;background-color:#f9f9fe;padding-left:25px;padding-bottom:25px;padding-top:25px;margin-left:20px;margin-bottom:30px;">';
echo '<div style="font-size:25px;margin-top:10px;" class="sccsubtitle scc_email_quote_label">EDIT YOUR <span style="font-weight:800;color:#314bf8">COUPON</span></div>';

echo       '<div style="color:green; margin-left:50px">'.(isset($_GET['saved']) && $_GET['saved'] == true ? "Coupon have been successfully saved" : "").'</div>';
echo       '<div style="color:orange; margin-left:50px">'.(isset($_GET['deleted']) && $_GET['deleted'] == true ? "Coupon have been successfully deleted" : "").'</div>';

}
echo '<br><div class="row" style="margin-top: 5px">';
echo   '<div class="col-md-3 col-sm-3">';
echo       '<div style="font-size:15px">Coupon Name</div>';
echo   '</div>';
echo   '<div class="col-md-4 col-sm-10">';
echo   '<div class="form-group">';
echo      '<input type="text" class="form-control" style="height:40px;" id="coupon_name" value="'.(isset($coupon) && isset($coupon->name) ? htmlspecialchars(sanitize_text_field($coupon->name)) : '').'">';
echo   '</div>';
echo   '<p id="coupon_name_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';

echo '<div class="row" style="margin-top: 5px">';
echo   '<div class="col-md-3 col-sm-3">';
echo       '<div style="font-size:15px">Coupon Code</div>';
echo   '</div>';
echo   '<div class="col-md-4 col-sm-10">';
echo   '<div class="form-group">';
echo      '<input class="form-control" id="coupon_code" style="height:40px;" value="' . (isset($coupon) && isset($coupon->code) ? htmlspecialchars(sanitize_text_field($coupon->code)) : "" ). '">';
echo   '</div>';
echo   '<p id="coupon_code_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';
echo '<div class="row" style="margin-top: 5px">';
echo   '<div class="col-md-3 col-sm-10">';
echo       '<div style="font-size:15px">Start Date</div>';
echo   '</div>';
echo   '<div class="col-md-4 col-sm-10" data-provide="datepicker">';
echo   '<div class="input-group date">';
echo      '<input type="text"  class="form-control"  style="height:40px;" id="coupon_start_date"value="' . (isset($coupon) && isset($coupon->startdate) ?  date( "m/d/Y", strtotime($coupon->startdate))  : "") . '" date-format="mm/dd/yy">';
echo      '<div class="input-group-addon">';
echo         '<span class="fas fa-calendar-alt"></span>';
echo      '</div>';
echo   '</div>';
echo   '<p id="coupon_start_date_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';

echo '<div class="row" style="margin-top: 5px">';
echo   '<div class="col-md-3 col-sm-3">';
echo       '<div style="font-size:15px">End Date</div>';
echo   '</div>';
echo   '<div class="col-md-4 col-sm-10">';
echo   '<div class="input-group">';
echo      '<input class="form-control"  style="height:40px;" id="coupon_end_date" value="' . (isset($coupon) && isset($coupon->enddate) ? date( "m/d/Y", strtotime($coupon->enddate))  : "") . '" date-format="mm/dd/yy">';
echo      '<div class="input-group-addon">';
echo         '<span class="fas fa-calendar-alt"></span>';
echo      '</div>';
echo   '</div>';
echo   '<p id="coupon_end_date_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';


echo '<div class="row" style="margin-top: 5px">';
echo   '<div class="col-md-3 col-sm-3">';
echo       '<div style="font-size:15px">Discount %</div>';
echo   '</div>';
echo   '<div class="col-md-3 col-sm-10">';
echo   '<div class="form-group">';
echo      '<input min="0" max="100" style="height:40px;" type="number" step="0.01" class="form-control" id="coupon_discountpercentage" value="' . (isset($coupon) && isset($coupon->discountpercentage) && $coupon->discountpercentage != -1.0 ? floatval($coupon->discountpercentage) : "") . '">';
echo   '</div>';
echo   '<p id="coupon_discountpercentage_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';


echo '<div class="row" style="margin-top: 5px; margin-bottom: 10px">';
echo   '<div class="col-md-3 col-sm-3">';
echo       '<div style="font-size:15px">Discount $</div>';
echo   '</div>';
echo   '<div class="col-md-3 col-sm-10">';
echo   '<div class="form-group">';
echo      '<input type="number" style="height:40px;" min=0 class="form-control" id="coupon_discountvalue" value="' . (isset($coupon) && isset($coupon->discountvalue)  && $coupon->discountvalue != -1.0 ? floatval($coupon->discountvalue) : "") . '">';
echo   '</div>';
echo   '<p id="coupon_discountvalue_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';

echo '<div style="padding:10px; cursor: pointer">';
echo '<div style="font-size:15px;color:#314bf8; margin-bottom: 20px" data-toggle="collapse" data-target=".advanced_settings"> <strong>Advanced Settings</strong></div>';
echo '<div class="row advanced_settings collapse" style="margin-top: 10px" >';
echo   '<div class="col-md-3 col-sm-3">';
echo       '<div style="font-size:15px">Minimum Spend</div>';
echo   '</div>';
echo   '<div class="col-md-3 col-sm-10">';
echo   '<div class="form-group">';
echo      '<input type="number" style="height:40px;" min=0 class="form-control" id="coupon_minspend" value="' . (isset($coupon) && isset($coupon->minspend)  && $coupon->minspend != -1.0 ? floatval($coupon->minspend) : "") . '">';
echo   '</div>';
echo   '<p id="coupon_minspend_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';

echo '<div class="row advanced_settings collapse" style="margin-top: 5px">';
echo   '<div class="col-md-3 col-sm-10">';
echo       '<div style="font-size:15px">Maximum Spend</div>';
echo   '</div>';
echo   '<div class="col-md-3 col-sm-5">';
echo   '<div class="form-group">';
echo      '<input type="number" style="height:40px;" min=0 class="form-control" id="coupon_maxspend" value="' . (isset($coupon) && isset($coupon->maxspend) && $coupon->maxspend != -1.0  ? floatval($coupon->maxspend) : "") . '">';
echo   '</div>';
echo   '<p id="coupon_maxspend_error" style="color:red"></p>';  
echo   '</div>';
echo '</div>';

echo '<input type="hidden" id="coupon_id" value="'.(isset($coupon) && isset($coupon->id) ? $coupon->id : "0").'">';
echo'</div>';


echo '<div class="row" style="margin-top: 5px; margin-bottom: 30px">';
echo   '<div class="col-md-6 col-sm-6">';
echo   '<div class="input-group">';
echo       '<button  onclick="saveCoupon()" id="coupon_saving_button" class="btn btn-lg btn-primary" style="margin:auto;">Save</button>';
echo       '<div style="color:green">'.(isset($_GET['success']) && $_GET['success'] == true ? "Coupon have been successfully saved" : "").'</div>';
echo       '<div style="color:orange; margin-left:10px">'.(isset($_GET['deleted']) && $_GET['deleted'] == true ? "Coupon have been successfully deleted" : "").'</div>';

echo   '</div>';
echo   '</div>';
echo  '</div></div>';

echo '<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">';


echo '<div class="row" style="max-width:1000px;background-color:#f9f9fe;padding-left:25px;padding-bottom:25px;padding-top:25px;margin-left:20px;">';
echo '<div style="margin-top:20px;font-size:25px;" class="sccsubtitle scc_email_quote_label"><span style="font-weight:800;color:#314bf8">EXISTING</span> COUPONS</div>';

echo '<table class="table">';
echo   '<thead>';
echo     '<tr>';
echo          '<th style="max-width:10px;">Coupon Name</th>';
echo         '<th style="max-width:11px;">Coupon Code</th>';
echo         '<th style="max-width:11px;">Start Date</th>';
echo         '<th style="max-width:11px;">End Date</th>';
echo         '<th style="max-width:11px;">Disc %</th>';
echo         '<th style="max-width:11px;">Disc $</th>';
echo         '<th style="max-width:15px;">Actions</th>';
echo     '</tr>';
echo    '</thead>';
echo '<tbody>';
foreach($coupons as $c){
    echo '<tr>';
    echo     '<td>';
    echo          $c->name;
    echo     '</td>';

    echo     '<td>';
    echo          $c->code;
    echo     '</td>';

    echo     '<td>';
    echo          date( "m/d/Y", strtotime($c->startdate)); 
    echo     '</td>';

    echo     '<td>';
    echo          date( "m/d/Y", strtotime($c->enddate));
    echo     '</td>';

    echo     '<td>';
    echo          $c->discountpercentage == -1.0 ? "" : $c->discountpercentage;
    echo     '</td>';

    echo     '<td>';
    echo          $c->discountvalue == -1.0 ? "" : $c->discountvalue;
    echo     '</td>';

    echo     '<td>';
    echo          '<button onclick="editCoupon('.$c->id.')" class="btn btn-xs btn-primary" style="margin-rigth:15px;">Edit</button>';
    echo          '<button onclick="deleteCouponModal('.$c->id.', \''.$c->name.'\')" class="btn btn-xs btn-danger" style="margin-rigth:10px">Delete</button>';        
    echo     '</td>';
    echo '</tr>';

}
if(sizeof($coupons) == 0){
  echo '<tr><td colspan="7" style="text-align: center;">No existing Coupons</td></tr>';
}
echo '</tbody>'; 
echo '</table></div>';
echo '<div id="delete_coupon_modal_container"></div>';


if(isset($_GET['saved']) && $_GET['saved'] == true){
    echo '<div id="save_coupon_modal" class="scc-modal-2020 fade"  role="dialog">';
    echo '	<div class="scc-modal-dialog-2020">';
    echo '		<div class="scc-modal-content-2020" style="margin-top:100px">';
    echo '		<div class="scc-modal-body-2020" >';
    echo '        <h3 class="scc-modal-title-2020" style="color:green;margin:20px">Coupon have been successfully saved</h3>';
    echo '       <center><button type="button" class="btn btn-default" data-dismiss="modal">Ok</button></center>';
    echo '		</div>';
    echo '		</div>';
    echo '	</div>';
    echo '</div>';
}
if(isset($_GET['deleted']) && $_GET['deleted'] == true){
    echo '<div id="deleted_info_coupon_modal" class="scc-modal-2020 fade"  role="dialog">';
    echo '	<div class="scc-modal-dialog-2020">';
    echo '		<div class="scc-modal-content-2020" style="margin-top:100px">';
    echo '		<div class="scc-modal-body-2020" >';
    echo '        <h3 class="scc-modal-title-2020" style="color:red;margin:20px">Coupon have been successfully deleted</h3>';
    echo '       <center><button type="button" class="btn btn-default" data-dismiss="modal">Ok</button></center>';
    echo '		</div>';
    echo '		</div>';
    echo '	</div>';
    echo '</div><span style="margin-top:200px;></span>';
}
echo '<script>function editCoupon(id){ var url = "'.get_admin_url().'admin.php?page=Stylish_Cost_Calculator_Coupon&id=" + id; window.open(url, "_blank");}</script>';



$plugin_data = get_plugin_data(SCC_DIR. '/stylish-cost-calculator.php');
$scc_version = $plugin_data['Version'];

include_once SCC_DIR . '/admin/logo-footer.php';
}

function admin_menu()
{
    if(get_option( 'scc_licensed') != 0){
        add_submenu_page( 'scc-tabs', __( 'Coupon', 'stylishpl' ), __( 'Coupon', 'stylishpl' ), 'manage_options', 'Stylish_Cost_Calculator_Coupon', array( $this, 'coupon_page' ) );
    }
}
}
$stylish_cost_calculator_Coupon = new Stylish_Cost_Calculator_Coupon();
?>