<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$subscription_productObj =  new  subscription_product();
if(isset($_REQUEST['act']) && $_REQUEST['act']=='change_status' && isset($_REQUEST['subscription_product_id'])){
    
    
    if(isset($_REQUEST['status']) && $_REQUEST['status'] == '1')
    {
        $data['subscription_product_status'] = '0';

        $updaters=$subscription_productObj->updateAllRecords($data);

        if($updaters)
        {
            $update_status = $subscription_productObj->changeStatus($_REQUEST['subscription_product_id']);
        }
        echo json_encode(array("msg" => RECORD_STATUS));
    }
    /*else if(isset($_REQUEST['status']) && $_REQUEST['status'] == '0')
    {
        $update_status = $subscription_productObj->changeStatus($_REQUEST['subscription_product_id']);
        echo json_encode(array("msg" => 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_stripe_id']) && $_REQUEST['product_stripe_id'] != '')
    {
        $where .= " AND subscription_product_stripe_id = '".$_REQUEST['product_stripe_id']."'";
    }
    if(isset($_REQUEST['product_name']) && $_REQUEST['product_name'] != '')
    {
        $where .= " AND subscription_product_name = '".$_REQUEST['product_name']."'";
    }
    if(isset($_REQUEST['clinics']) && $_REQUEST['clinics'] != '')
    {
        $prod_clinic = explode(",", $_REQUEST['clinics']);
        
        if(count($prod_clinic) > 1)
        {
            foreach ($prod_clinic as $prodkey => $prodvalue)
            {
                $prod_data .= " FIND_IN_SET('".$prodvalue."', subscription_product_clinic_id) OR ";   
            }
        }else{
            $prod_data = " FIND_IN_SET('".$_REQUEST['clinics']."', subscription_product_clinic_id)";
        }
        $prod_or_rem = rtrim($prod_data," OR ");

        $prod_final = ' AND ('.$prod_or_rem.')';
         
        $where .= $prod_final;
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " subscription_product_id LIKE '%".$_REQUEST['search']['value']."%' OR subscription_product_stripe_id LIKE '%".$_REQUEST['search']['value']."%' OR subscription_product_name LIKE '%".$_REQUEST['search']['value']."%' ";
    } 

    //print_r($orderby);exit;
    $data    = array('where' => $where,'limit' => $limit);

    $productDetails = $subscription_productObj->getAllRecords($data);

    $recordsTotal   =   $subscription_productObj->getAllCount($data);

        
    if ($productDetails && count($productDetails)>0){
        foreach ($productDetails as $key => $product){
            
             if(isset($product->subscription_product_clinic_id) && $product->subscription_product_clinic_id != '')
             {

                 $clinicObj =  new  clinic();
                 $fields = array('clinic_id','clinic_name');
                 $where = "clinic_id IN (".$product->subscription_product_clinic_id.")";
                 $clinicArg    = array('fields'=>$fields,'where'=>$where);
                 $clinicObj->resultAs = 'array';
                 $clinicDetails = $clinicObj->getAllRecords($clinicArg);
                 $clinicdata = '';
                 foreach($clinicDetails as $ckey => $clinicdetail)
                 {
                    $clinicdata .= $clinicdetail['clinic_name'].",";
                 }
                 if(isset($clinicdata) && $clinicdata != '')
                 {
                    $all_clinicname = rtrim($clinicdata,",");
                 }

             }else{
                $all_clinicname = '';
             }   
                
             
             $subscription_plansObj =  new  subscription_plans();
             $fields = array('plan_id','plan_status');
             $where = "plan_master_product_id = '".$product->subscription_product_id."' AND plan_status = 'active'";
             $subscription_plansArg    = array('fields'=>$fields,'where'=>$where);
             $subscription_plansObj->resultAs = 'array';
             $subscription_plansDetails = $subscription_plansObj->fromFields($subscription_plansArg);
             if($subscription_plansDetails['plan_id'] == '')
             {
                $status_add_plan = '<a href="planadd.php?product_id='.$product->subscription_product_id.'">Add Plan</a>';
             }else{
                $status_add_plan = '';
             }

            if($product->subscription_product_status == '1' && $product->subscription_product_clinic_id == '' && $subscription_plansDetails['plan_id'] != ''){
              //$status = '<a href="javascript:void(0)" onclick="change_status('.$product->subscription_product_id.',1)" ><span class="badge badge-success">Default</span></a>';
              $status = '<span class="badge badge-success">Default</span></a>';
            }else if(isset($product->subscription_product_clinic_id) && $product->subscription_product_clinic_id != '' ){
              $status = '<span>Assignable</span></a>';
            }else if($subscription_plansDetails['plan_id'] != '' && $product->subscription_product_mode != 'allinone'){
                
              $status = '<a href="javascript:void(0)" onclick="change_status('.$product->subscription_product_id.',1)" ><span class="badge badge-danger">Not default</span></a>';
              //$status = '<span class="badge badge-danger">Not default</span></a>';
            }else if($subscription_plansDetails['plan_id'] != '' && $product->subscription_product_mode == 'allinone'){
              
              $status = '<span>All In One</span></a>';
            }else{
                $status = '';
            }
            //<a id="delete-confirm" onclick="delete_entry('.$product->product_id.');" class="btn btn-danger white-btn"><i class="nav-icon i-Close-Window "></i></a>
            $action = '
                 <a href="productadd.php?act=edit&subscription_product_id='.$product->subscription_product_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$product->product_id .'" class="check_class" />',
                    $product->subscription_product_id,
                    $product->subscription_product_stripe_id,    
                    $product->subscription_product_name, 
                    //$product->subscription_product_clinic_id,
                    $all_clinicname,
                    $status."<br/><br/>".$status_add_plan,  
                    $action,
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}




$clinicObj = new clinic();
$clinicwhere = " clinic_status = '1'";
$clinicArg    = array('where' => $clinicwhere);
$allClinic = $clinicObj->getAllRecords($clinicArg);

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