<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$userObj   = new  user();
$couponObj = new coupons();


if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['user_id'])){
    if(isset($_REQUEST['status'])){
        $userObj->changeStatus($_REQUEST['user_id']);
        echo RECORD_STATUS;
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['user_id'])){
    $userObj->deleteFromId($_REQUEST['user_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("user_status" => '1');
        $rs = $userObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("user_status" => '0');
        $rs = $userObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){
        $rs = $userObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){

    $stripeObj   = new stripePay();
    $allCoupons = $stripeObj->getAllCoupons();
    if($allCoupons->data){
        
        foreach ($allCoupons->data as $key => $value) {
            $couponData = array(
                "coupon_stripe_id"              => $value->id,
                "coupon_name"                   => $value->name,
                "coupon_duration"               => $value->duration,
                "coupon_amount_off"             => $value->amount_off,
                "coupon_percent_off"            => $value->percent_off,
                "coupon_times_redeemed"         => $value->times_redeemed,
                "coupon_created_date_timestamp" => $value->created,
                "coupon_expiry_date_timestamp"  => $value->redeem_by,
            );
            //$rs = $couponObj->addRecords($couponData);
        }
    }

    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " coupon_stripe_id LIKE '%".$_REQUEST['search']['value']."%' OR coupon_id LIKE '%".$_REQUEST['search']['value']."%' OR coupon_name LIKE '%".$_REQUEST['search']['value']."%' OR coupon_duration LIKE '%".$_REQUEST['search']['value']."%' OR coupon_percent_off LIKE '%".$_REQUEST['search']['value']."%' OR coupon_amount_off LIKE '%".$_REQUEST['search']['value']."%'";
    }    
    $orderFields = array('coupon_id', 'coupon_name', 'coupon_created_at');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" coupon_id desc ");
    }

    $data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);

    $userDetails  = $couponObj->getAllRecords($data);
    $recordsTotal = $couponObj->getAllCount($data);

        
    if ($userDetails && count($userDetails)>0){
        foreach ($userDetails as $key => $user){

            /*$stripecardObj = new stripecard();
            $where = array("stripecard_user_id="=>$user->user_id,'stripecard_status='=>'Active');
            $stripeArg = array('where'=>$where);
            $cardExist = $stripecardObj->FromFields($stripeArg);
            $last4="";
            if($cardExist){
                $last4 = "*****".$cardExist->stripecard_last4;
            }*/
            if($user->coupon_status =='active'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$user->coupon_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$user->coupon_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }

            $action = '<a href="couponadd.php?act=edit&coupon_id='.$user->coupon_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>
                 <a id="delete-confirm" onclick="delete_entry('.$user->coupon_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>';

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$user->coupon_id.'" class="check_class" />',
                    $user->coupon_id, 
                    $user->coupon_name, 
                    $user->coupon_amount_off,
                    $user->coupon_percent_off,
                    $user->coupon_times_redeemed,
                    date('Y-m-d H:i:s',strtotime($user->coupon_created_at)),
                    "<div id='td_status_".$user->coupon_id."'>".$status."</div>",    
                    $action
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php')
?>