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

$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'superuserlist.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['superuser_id'])){
    $superuserDetailLog = $superuserObj->fromId($_REQUEST['superuser_id']);
    if($superuserDetailLog->superuser_status == '1')
    {
        $superuser_status = 'Active';
    }else{
        $superuser_status = 'Inactive';
    }
    $superuser_detail = " <b>Id</b>:".$_REQUEST['superuser_id']."<br/><b>Name</b>:".$superuserDetailLog->superuser_fname." ".$superuserDetailLog->superuser_lname." <br/><b>Change Status Old data</b> ".$superuser_status;
    if(isset($_REQUEST['status'])){

        if($_REQUEST['status'] == '1')
        {
            $superuserstatusnew = 'Active';
        }else{
            $superuserstatusnew = 'Inactive';
        }
        $superuser_detail .= " <b>To</b> ".$superuserstatusnew;

        $superuserObj->changeStatus($_REQUEST['superuser_id']);
        echo RECORD_STATUS;

        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$superuser_detail);
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['superuser_id'])){

    $superuserData = $superuserObj->fromId($_REQUEST['superuser_id']);

    $superuser_detail = " <b>Delete record id</b>:  ".$_REQUEST['superuser_id']." and Name:- ".$superuserData->superuser_fname." ".$userData->user_lname." and <b>Email</b>: ".$superuserData->superuser_email_address;

    //$superuserObj->deleteFromId($_REQUEST['superuser_id']);
    $data['superuser_isdeleted'] = '1';
    $rs=$superuserObj->updateRecords($data,$_REQUEST['superuser_id']);
    $successMsg = base64_encode(RECORD_DELETE);

    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$superuser_detail);
    header('Location:superuserlist.php');
    exit;
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("superuser_status" => '1');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $superuserObj->fromId($val_actchk);
            if($old_data->superuser_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->superuser_status != '1')
            {
                $new_status = 'Active';
                $superuserdata = " <b>Id</b>:".$val_actchk." <br/><b> Change Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$superuserdata);
            }
        }
        $rs = $superuserObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("superuser_status" => '0');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $superuserObj->fromId($val_actchk);
            if($old_data->superuser_status == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->superuser_status != '0')
            {
                $new_status = 'Inactive';
                $superuserdata = " <b>Id</b>:".$val_actchk." <br/><b> Change Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$superuserdata);
            }
        }
        $rs = $superuserObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        $alldata = $superuserObj->fromId($chk_del);
        $superuserdata = " <b>Delete Id</b>: "." ".$chk_del."<br/><b>First Name: </b>".$alldata->superuser_fname." and <b>Last Name: </b>".$alldata->superuser_lname." and <b>Email: </b>".$alldata->superuser_email_address;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$superuserdata);

        $rs = $superuserObj->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 and superuser_isdeleted='0' ";

    /*if(isset($_REQUEST['full_name']) && $_REQUEST['full_name'] != '')
    {
       $fullname = str_replace(' ', '', $_REQUEST['full_name']);

       $where .=" AND CONCAT(superuser_fname,'',superuser_lname) LIKE '%".$fullname."%' ";
    }*/

    if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] != '')
    {
        $firstname = trim($_REQUEST['firstname']);
        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((superuser_fname='".($firstname)."' and   superuser_lname='".($lastname)."') OR (superuser_fname='".(ucfirst($firstname))."' and   superuser_lname='".(ucfirst($lastname))."'))";
    }else if(isset($_REQUEST['firstname']) && $_REQUEST['firstname'] != '' && $_REQUEST['lastname'] == ''){

        $firstname = trim($_REQUEST['firstname']);

        $where .= " AND ((superuser_fname LIKE '%".($firstname)."%') OR (superuser_fname LIKE '%".(ucfirst($firstname))."%' ))";
    }else if(isset($_REQUEST['lastname']) && $_REQUEST['lastname'] != '' && $_REQUEST['firstname'] == ''){

        $lastname = trim($_REQUEST['lastname']);

        $where .= " AND ((superuser_lname LIKE '%".($lastname)."%') OR ( superuser_lname LIKE '%".(ucfirst($lastname))."%'))";
    }
    
    if(isset($_REQUEST['superuser_unique_id']) && $_REQUEST['superuser_unique_id'] != '')
    {
        $where .= " AND superuser_unique_id = '".$_REQUEST['superuser_unique_id']."'";
    }
    
    if(isset($_REQUEST['superuser_email']) && $_REQUEST['superuser_email'] != '')
    {
        $where .= " AND  superuser_email_address = '".$_REQUEST['superuser_email']."' ";
    }
    
    if(isset($_REQUEST['superuser_status']) && $_REQUEST['superuser_status'] != '')
    {
        $where .= " AND  superuser_status = '".$_REQUEST['superuser_status']."' ";
    }

    if(isset($_REQUEST['superuser_id']) && $_REQUEST['superuser_id'] != '')
    {
        $where .= " AND superuser_id = '".$_REQUEST['superuser_id']."'";
    }
    if(isset($_REQUEST['superuser_date']) && $_REQUEST['superuser_date'] != '')
    {
        $where .= " AND date(superuser_create_at) = '".$_REQUEST['superuser_date']."'";
    }

    
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " superuser_unique_id LIKE '%".$_REQUEST['search']['value']."%' OR superuser_id LIKE '%".$_REQUEST['search']['value']."%' OR superuser_fname LIKE '%".$_REQUEST['search']['value']."%' OR superuser_lname LIKE '%".$_REQUEST['search']['value']."%' OR superuser_email_address LIKE '%".$_REQUEST['search']['value']."%' OR date(superuser_create_at) LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    

    $orderFields = array('','superuser_id','superuser_unique_id','superuser_fname,superuser_lname','superuser_email_address','superuser_created_at','superuser_status');    
    
    // 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(" superuser_id desc ");
    }
    //print_r($orderby);exit;

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

    $superuserDetails = $superuserObj->getAllRecords($data);

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

        
    if ($superuserDetails && count($superuserDetails)>0){
        foreach ($superuserDetails as $key => $superuser){

            if(isset($superuser->superuser_clinic) && $superuser->superuser_clinic != '')
            {

                $superclinicId = explode(",", $superuser->superuser_clinic);
                $superclinic_data = '';
                if(count($superclinicId) > 1)
                {
                    foreach ($superclinicId as $ackey => $superclinicvalue)
                    {
                        $superclinic_data .= " FIND_IN_SET('".$superclinicvalue."', clinic_id) OR ";   
                    }
                }else{
                    $superclinic_data = " FIND_IN_SET('".$superuser->superuser_clinic."', clinic_id)";
                }
                $superclinic_or_rem = rtrim($superclinic_data," OR ");
                //echo $superclinic_or_rem;
                $superclinic_final = ' ('.$superclinic_or_rem.')';
                 
                $where = $superclinic_final;

                $clinicObj = new clinic();
                $fields = array('clinic_id','clinic_name');
                //$where =  " clinic_id IN (".$superuser->superuser_clinic.")";
                $clinicArg = array('fields'=>$fields,'where'=>$where);
                $clinicDetail = $clinicObj->getAllRecords($clinicArg);
                foreach($clinicDetail as $clinicvalue){
                    $allclinic .= $clinicvalue->clinic_name.",";    
                }
                $assignclinic = rtrim($allclinic,",");
                
            }else{
                $assignclinic = '';
            }
            

        
            if($superuser->superuser_status =='1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$superuser->superuser_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$superuser->superuser_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
            
            $action = '<a href="changepassword.php?user_id='.$superuser->superuser_id.'&user_type=superuser" class="btn btn-success white-btn"><i class="nav-icon i-Lock " title="Change Password"></i></a>
                 <a href="superuseradd.php?act=edit&superuser_id='.$superuser->superuser_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>
                 <a id="delete-confirm" onclick="delete_entry('.$superuser->superuser_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="'.$superuser->superuser_id.'" class="check_class" />',

                    "<b>Xcare Superuser Id: </b>".$superuser->superuser_id."<br/><b>External Id :</b>".$superuser->superuser_unique_id, 
                    "<b>First Name: </b>".$superuser->superuser_fname."<br/><b>Last Name: </b>".$superuser->superuser_lname."<br/><b>Email: </b>".$superuser->superuser_email_address,
                    $assignclinic,
                    date('Y-m-d H:i:s',strtotime($superuser->superuser_create_at))."<br/><b>Login ip</b> :".$superuser->superuser_last_login_ip,
                    "<div id='td_status_".$superuser->superuser_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');
?>