<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Coupon extends CI_Controller {

public function __construct() {
  parent::__construct();
  $this->load->model('campaign_model');
  $this->load->helper('cookie');                
}

public function index(){}

public function coupon(){
  $data_o['get_coupon_one'] = $this->campaign_model->get_coupons_one($this->uri->segment(3));
  $data_ca['get_campaign_one'] = $this->campaign_model->get_campaign_one($this->uri->segment(3));

  date_default_timezone_set("America/New_York");
  $ip = '';   
  $codcss = '<style>'.$data_ca['get_campaign_one']->c_css.'</style>' . '<script
    src="https://code.jquery.com/jquery-3.2.1.js"
    integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
    crossorigin="anonymous"></script>' . "<script> $(document).ready(function () { $.fn.textWidth = function(text, font) {
      
      if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
      
      $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));
      
      return $.fn.textWidth.fakeEl.width();
  };
  
  $('.width-dynamic').on('input', function() {
      var inputWidth = $(this).textWidth();
      $(this).css({
          width: inputWidth + 25
      })
  }).trigger('input');
  
  
  function inputWidth(elem, minW, maxW) {
      elem = $(this);
  }
  
  var targetElem = $('.width-dynamic');
  
  inputWidth(targetElem); });</script>"    ;
  if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
      $ip = $_SERVER['HTTP_CLIENT_IP'];
  } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  } else {
      $ip = $_SERVER['REMOTE_ADDR'];
  }
  if($data_o['get_coupon_one']){
    $data_c['ip_use'] = $ip;            
    $data_c['time_visit'] = date("y:m:d:h:i:sa");
    $data_c['used'] = 'yes';            
    $slug_coupons =  $data_o['get_coupon_one']->slug_coupons;
    $coupons =  $data_o['get_coupon_one']->c_coupons;
    $css = $data_ca['get_campaign_one']->c_css;
    $c_dailylimit = $data_ca['get_campaign_one']->c_dailylimit;
    $real_whattodo = $data_ca['get_campaign_one']->real_whattodo;
    $c_startdate = $data_ca['get_campaign_one']->c_startdate;
    $c_enddate = $data_ca['get_campaign_one']->c_enddate;
    $c_coupons = '';
    $c_coupons_used = '';
    $count = 0;
    $check_ip_used = '';
    
    $unused = $this->campaign_model->get_coupons_unused($this->uri->segment(3));
    $used   = $this->campaign_model->get_coupons_used($this->uri->segment(3));

    if($used){
      foreach ($used as $key => $value) {             
        if( strtotime(date("Y-m-d 0:0:0"))   < strtotime($value->time_visit) 
          && strtotime(date("Y-m-d 23:59:0")) > strtotime($value->time_visit)){
          if( $value->ip_use == $ip) { $check_ip_used = 'used'; }            
        }
        $count +=1;     
      }   
    }

    if(strtotime($c_startdate) < strtotime(date("Y-m-d h:i:sa")) 
      && strtotime($c_enddate)  > strtotime(date("Y-m-d h:i:sa")) ) {
        if($check_ip_used == 'used') {
            $data['coupon'] = $this->campaign_model->get_coupons_used_ip($this->uri->segment(3), $ip);              
                echo '<center><input class="width-dynamic proba dva" type="text" name="textbox1" disabled="disabled" onClick="this.select();" id="code" value="'. $data['coupon'][0]->c_coupons .'"></center>';
                echo $codcss;              
        } else {
          if($count <= $c_dailylimit){
              $this->campaign_model->update_coupons_one($slug_coupons,$coupons,$data_c);
              if($unused){
                foreach ($unused as $key => $value) {
                  if($key == 0){

                  }else {
                    $c_coupons .= $value->c_coupons ;           
                  }
                }
              }
              if($used){
                foreach ($used as $key => $value) {
                  $c_coupons_used .= $value->c_coupons ;      
                }   
              } else {
                $c_coupons_used .= $coupons ;   
              }                   
              $data['c_coupons'] =  $c_coupons;

              $data['used_coupons'] =  $c_coupons_used;

              $this->campaign_model->update_campaign($this->uri->segment(3),$data);

              echo '<center><input class="width-dynamic proba dva" type="text" name="textbox1" disabled="disabled" onClick="this.select();" id="code" value="'.$coupons .'"></center>';
              echo $codcss;
          } else {
            if($real_whattodo == '2'){
              echo '<center><span id="sorry" class="hr">'. $data_ca['get_campaign_one']->c_displaytext .'</span></center>';
              echo $codcss;
            }else{
              redirect($data_ca['get_campaign_one']->c_redirect);
            }
          }
      }
    } else {
      if($real_whattodo == '2'){
        echo '<center><span id="sorry" class="ef">'. $data_ca['get_campaign_one']->c_displaytext .'</span></center>';
        echo $codcss;
      }else{
        redirect($data_ca['get_campaign_one']->c_redirect);
      }               
    }
  } else {
    $css = $data_ca['get_campaign_one']->c_css;
    $c_dailylimit = $data_ca['get_campaign_one']->c_dailylimit;
    $real_whattodo = $data_ca['get_campaign_one']->real_whattodo;
    $c_startdate = $data_ca['get_campaign_one']->c_startdate;
    $c_enddate = $data_ca['get_campaign_one']->c_enddate;
    $data['coupon'] = $this->campaign_model->get_coupons_used_ip($this->uri->segment(3), $ip); 
    if(strtotime($c_startdate) < strtotime(date("Y-m-d h:i:sa")) && 
      strtotime($c_enddate)  > strtotime(date("Y-m-d h:i:sa")) && $data['coupon'])  { 
        echo '<center><input class="width-dynamic proba dva" type="text" name="textbox1" disabled="disabled" onClick="this.select();" id="code" value="'. $data['coupon'][0]->c_coupons .'"></center>'; 
    }else{
      echo '<center><span id="sorry" class="432"> Sorry, this campaign has ended. </span></center>';
    }           
    echo $codcss;      
  }
}











}