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

$stripeObj   = new stripePay();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'planadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}

$productObj = new subscription_product();
$where = "subscription_product_mode!='allinone'";
//$where = "subscription_product_mode!=''";
$prodarg = array("where"=>$where);
$allProduct = $productObj->getAllRecords($prodarg);


$productObj = new subscription_product();
$where = "subscription_product_mode='allinone'";
$prodargallinone = array("where"=>$where);
$allProductallinone = $productObj->getAllRecords($prodargallinone);




if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['plan_id'])){
    $planDetail = $subscription_planObj->fromId($_REQUEST['plan_id']);
    //print_r($planDetail);exit;
    //print_r($planDetail->plan_amount);exit;
}

if(isset($_POST['submit']))
{
   //print_r($_POST);exit;
    
    $master_planId = $_POST['plan_name'];
    $plan_product = $_POST['plan_product'];
    $priceing_type = $_POST['priceing_type'];

    $masterplanObj = new subscription_master_plans();
    $masterplanDetail = $masterplanObj->fromId($master_planId);
    //print_r($masterplanDetail);exit;

    $productdataObj = new subscription_product();
    $prodwhere = "subscription_product_stripe_id = '".$plan_product."'";
    $productArg = array('where' => $prodwhere); 
    $productplanDetail = $productdataObj->fromFields($productArg);
    

    
    if(isset($_REQUEST['plan_price']) && $_REQUEST['plan_price'] != '')
    {
        $plan_amount = ($_REQUEST['plan_price'] * 100);
    }

    if(isset($_REQUEST['billing_period']) && $_REQUEST['billing_period'] != '')
    {
        if($_REQUEST['billing_period'] == 'monthly')
        {
            $plan_interval = 'month';
            $nickname = $masterplanDetail->master_plan_name." ".'Monthly';
            if($masterplanDetail->master_plan_id == '1')
            {
                $plan_system_type = 'standard';
            }else
            {
                $plan_system_type = 'custom';
            }
        }else if($_REQUEST['billing_period'] == 'yearly')
        {
            $plan_interval = 'year';
            $nickname = $masterplanDetail->master_plan_name." ".'Yearly';
            if($masterplanDetail->master_plan_id == '1')
            {
                $plan_system_type = 'standard';
            }else
            {
                $plan_system_type = 'custom';
            }
        } 
    }


    if(isset($_FILES['plan_image']) && $_FILES['plan_image']['name']!='')
    {
        $image=$_FILES['plan_image']['name'];
        //$image=time().'_'.str_replace(" ", "-", $image);
        move_uploaded_file($_FILES['plan_image']['tmp_name'], DIR_UPLOAD_PLAN.$image);
        $plandata['plan_img_name'] = $image;
    }

    if($masterplanDetail->master_plan_id == '1')
    {
        $plan_priority = '5';
    }else if($masterplanDetail->master_plan_id == '2')
    {
        $plan_priority = '2';
    }else if($masterplanDetail->master_plan_id == '3')
    {
        $plan_priority = '3';
    }else if($masterplanDetail->master_plan_id == '4')
    {
        $plan_priority = '4';
    }
    else if($masterplanDetail->master_plan_id == '5')
    {
        $plan_priority = '1';
    }
    else if($masterplanDetail->master_plan_id == '6')
    {
        $plan_priority = '6';
    }


    $data=array(
        'nickname' => $nickname,
        'amount' => $plan_amount,
        'currency' => 'cad',
        'interval' => $plan_interval,
        'product' => $plan_product,
    );

    //print_r($data);exit;
    
    if (isset($_REQUEST['plan_id']) && $_REQUEST['plan_id'] != '')
    {   
        //$subscription_purchaseornot = $stripeObj->subscription_purchaseornot($_REQUEST['plan_stripe_id']);
       // print_r($subscription_purchaseornot->data);exit;

        /*if(empty($subscription_purchaseornot->data))
        {
            echo 'continue';
        }else{
            echo 'subcription puraches this plan price not change';
        }
        exit;*/


       
        header('Location:planlist.php');
        exit;
    }else
    {
        
        $allPlan= $stripeObj->createPlans($data);
        if($allPlan)
        {
            $plandata['plan_stripe_id'] = $allPlan->id;
            $plandata['plan_product_id'] = $plan_product;

            $plandata['plan_master_plan_id'] = $masterplanDetail->master_plan_id;
            $plandata['plan_amount'] = $_REQUEST['plan_price'];

            $plandata['plan_name'] = $nickname;
            $plandata['plan_type'] = $priceing_type;

            $plandata['plan_system_type'] = $plan_system_type;
            $plandata['plan_interval'] = $plan_interval;
            $plandata['plan_priority'] = $plan_priority;
            $plandata['plan_master_product_id'] = $productplanDetail->subscription_product_id;

            $plandata['plan_title'] = $_REQUEST['plan_title'];
            $plandata['plan_short_desc'] = $_REQUEST['plan_short_desc'];
            $plandata['plan_breif'] = $_REQUEST['plan_breif_desc'];

            $plandata['plan_stripe_response'] = json_encode($allPlan);
            //print_r($plandata);exit;
            $sub_plan = "<b>Add Record</b> ";
            $sub_plan.= " <b>Plan Data</b>: ".json_encode($plandata);
        
            $rs  = $subscription_planObj->addRecords($plandata);
            
            if($rs)
            {
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$sub_plan);
                
                header('Location:planlist.php');
                exit;
            }
        }
    }
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='display_price')
{
    $plan_id = $_POST['plan_id'];
    $displayplan_price = $_POST['displayplan_price'];

    $data['plan_display_amount'] = $displayplan_price;
    $rs=$subscription_planObj->updateRecords($data,$plan_id);
    header('Location:planlist.php');
    exit;

}
$masterplanObj = new subscription_master_plans();
$where = "master_plan_status = 'active'";
$masterplanArg = array('where' => $where);
$allmasterplan = $masterplanObj->getAllRecords($masterplanArg);


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

?>