<?php 

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

$clinicObj = new clinic();
$allClinic = $clinicObj->getAllRecords();

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['coupon_id'])){
    $couponDetail = $couponObj->fromId($_REQUEST['coupon_id']);
    
}
/*if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
    if($_REQUEST['email']==''){
        echo ENTER_EMAIL;
        exit;
    }else if(validateEmail($_REQUEST['email'])){

        $select=array('user_email_address','user_id');
        $where =array('user_email_address='=>$_REQUEST['email']);

        if(isset($_REQUEST['coupon_id']) && $_REQUEST['coupon_id']!=""){
            $where['user_id!='] =$_REQUEST['coupon_id'];
        }
        $data =array('fields'=>$select,'where'=>$where);        

        if(isset($_REQUEST['coupon_id']) && $couponObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }else if(!isset($_REQUEST['coupon_id']) && $couponObj->fromFields($data)){
            echo EMAIL_EXIST;
            exit;
        }
    }else{
        echo VALID_EMAIL;
        exit;
    }
    return true;
}*/

if(isset($_POST['submit'])){
    //exit();
    $data=array(
        'coupon_name'        => trim($_POST['coupon_name']),
        'coupon_duration'    => trim($_POST['duration_id']),
        'coupon_amount_off'  => trim($_POST['amount_off_value']),
        'coupon_percent_off' => trim($_POST['percent_off_value']),
        'coupon_status'      => 'active',
    );
    if($_POST['max_redemptions'] != '' && $_POST['max_redemptions'] != '0')
    {
        $data['max_redemptions'] = trim($_POST['coupon_name']);
    }
    if($_POST['duration_id'] != '' && $_POST['duration_id'] == 'repeating')
    {
        $data['coupon_duration_in_months'] = trim($_POST['duration_month']);
    }
    if($_POST['total_coupon_redeemed']!= '' && $_POST['total_coupon_redeemed'] != '')
    {
        $data['coupon_max_redemptions'] = trim($_POST['total_coupon_redeemed']);
    }

    if($_POST['datewise_coupon_redeemed']!= '' && $_POST['timewise_coupon_redeemed'] != '')
    {

        $data['coupon_date_times_redeemed'] = $_POST['datewise_coupon_redeemed']." ".$_POST['timewise_coupon_redeemed'];
    }else if($_POST['datewise_coupon_redeemed']!= '')
    {
        $data['coupon_date_times_redeemed'] = $_POST['datewise_coupon_redeemed'];
    }
    //print_r($data);exit;
    if (isset($_POST['coupon_id'])){
        /*$id=$_POST['coupon_id'];
        $data['coupon_updated_at'] = date('Y-m-d H:i:s');
        $rs=$couponObj->updateRecords($data,$id);
        if ($rs){
            $successMsg = base64_encode(RECORD_UPDATE);			
            header('Location:couponlist.php);
            exit;
        }*/
    }else{
        //$data['clinic_id'] = implode(',', $_POST['clinics']);
        $stripeObj   = new stripePay();
        $allCoupons = $stripeObj->createCoupon($data);
        if($allCoupons->id){
            $data['coupon_stripe_id']              = $allCoupons->id;
            $data['coupon_created_date_timestamp'] = $allCoupons->created;
            $rs                                    = $couponObj->addRecords($data);
        }
        if($rs){

            header('Location:couponlist.php');
            exit;
        }
    }
}

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

?>