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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'planlist.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['plan_id'])){
    
    
    if(isset($_REQUEST['status']))
    {
        $subplanDetail = $subscription_plansObj->fromId($_REQUEST['plan_id']);
        if($subplanDetail->plan_status == 'active')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

        
        if($_REQUEST['status'] == '0')
        {
            $data['plan_status'] = 'inactive';
            $new_status = 'Inactive';
        }else
        {
            $data['plan_status'] = 'active';
            $new_status = 'Active';
        }
        $planID = $_REQUEST['plan_id'];
        $updaters=$subscription_plansObj->updateRecords($data,$planID);
        $plan_detail = " <b>Id</b>:".$subplanDetail->plan_id."<br/><b>Name</b>:".$subplanDetail->plan_name."<br/><b>plan status</b>: "." ".$old_status." To ".$new_status." ";
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$plan_detail);
        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'){
    $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['product_name']) && $_REQUEST['product_name'] != '')
    {
       $where .= " AND subscription_product_name = '".$_REQUEST['product_name']."'";
    }
    
    if(isset($_REQUEST['plan_name']) && $_REQUEST['plan_name'] != '')
    {
        $where .= " AND plan_name = '".$_REQUEST['plan_name']."'";
    }
    
    if(isset($_REQUEST['plan_type']) && $_REQUEST['plan_type'] != '')
    {
        $where .= " AND  plan_type = '".$_REQUEST['plan_type']."' ";
    }

    if(isset($_REQUEST['plan_interval']) && $_REQUEST['plan_interval'] != '')
    {
        $where .= " AND plan_interval = '".$_REQUEST['plan_interval']."'";
    }

    if(isset($_REQUEST['plan_price']) && $_REQUEST['plan_price'] != '')
    {
        $where .= " AND  plan_amount = '".$_REQUEST['plan_price']."' ";
    }

    if(isset($_REQUEST['plan_status']) && $_REQUEST['plan_status'] != '')
    {
        $where .= " AND  plan_status = '".$_REQUEST['plan_status']."' ";
    }
    

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " subscription_product_name LIKE '%".$_REQUEST['search']['value']."%' OR plan_name LIKE '%".$_REQUEST['search']['value']."%' OR plan_type LIKE '%".$_REQUEST['search']['value']."%' OR plan_interval LIKE '%".$_REQUEST['search']['value']."%' OR plan_amount LIKE '%".$_REQUEST['search']['value']."%' OR plan_status LIKE '%".$_REQUEST['search']['value']."%'";
        
    } 


    
    $tables  = array('tbl_subscription_product sp');
    $jfields = array('s.plan_master_product_id = sp.subscription_product_id');
    $jtype   = array("INNER"); 
    /*$planwhere = array(
        'plan_status=' => 'active'
    );*/
    //'where' => $planwhere,

    $orderFields = array('','plan_id','','plan_name','plan_system_type','plan_interval','plan_amount','');
    
    // 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(" plan_id desc ");
    }


    $planArg    = array('where'=>$where,'joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'limit' => $limit,'orderBy'=>$orderby);

    $planDetails = $subscription_plansObj->getAllRecords($planArg);

    $recordsTotal   =   $subscription_plansObj->getAllCount($planArg);

        
    if ($planDetails && count($planDetails)>0){
        foreach ($planDetails as $key => $plan){
            
            if($plan->plan_status == 'active'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$plan->plan_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$plan->plan_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
            //<a id="delete-confirm" onclick="delete_entry('.$plan->plan_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>
            if($plan->plan_master_plan_id != '1' && $plan->plan_master_plan_id != '' && $plan->subscription_product_mode == 'allinone')
            {   
                $action = '<a href="planadd.php?act=edit&plan_id='.$plan->plan_id.'&product_mode='.$plan->subscription_product_mode.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';
            }else{
                $action = '';
            }

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$plan->plan_id .'" class="check_class" />',
                    $plan->plan_id,
                    $plan->subscription_product_name,
                    $plan->plan_name,    
                    $plan->plan_type, 
                    $plan->plan_interval, 
                    $plan->plan_amount,
                    $plan->plan_display_amount,
                    $status,  
                    $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');
?>