<?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 == 'superuseradd.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']=='edit' && isset($_REQUEST['superuser_id'])){
    $superuserDetail = $superuserObj->fromId($_REQUEST['superuser_id']);
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='validate_email'){
    if($_REQUEST['email']==''){
        echo ENTER_EMAIL;
        exit;
    }else if(validateEmail($_REQUEST['email'])){

        $select=array('superuser_email_address','superuser_id');
        $where =array('superuser_email_address='=>$_REQUEST['email']);

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

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

//if(isset($_POST['submit'])){
if(isset($_REQUEST['act']) && $_REQUEST['act']=='load')
{
    $data=array(
        'superuser_fname'=>trim($_POST['superuser_fname']),
        'superuser_lname'=>trim($_POST['superuser_lname']),
        'superuser_email_address'=>trim($_POST['superuser_email_address']),
    );

    if(isset($_POST['clinics']))
    {
        $data['superuser_clinic'] = implode(',', $_POST['clinics']);
    }else{
        $data['superuser_clinic'] = '';
    }
    
    if(isset($_POST['superuser_status']) && $_POST['superuser_status'] == '1'){
        $data['superuser_status'] = '1';
    }else{
        $data['superuser_status'] = '0';
    } 
    
    if (isset($_REQUEST['superuser_id'])){
        
        $id=$_REQUEST['superuser_id'];

        $superuser_detail = '';
        $superuser_detail = 'Edit SuperUser Old';
        $superuserDetailLog = $superuserObj->fromId($_POST['superuser_id']);
        
        if($superuserDetailLog->superuser_fname != $_POST['superuser_fname'])
        {
            $superuser_detail .= " <b>First name</b>: "." ".$superuserDetailLog->superuser_fname." <b>To</b> ".$_POST['superuser_fname']." and ";
            
            $chages_data = true;
        }
        if($superuserDetailLog->superuser_lname != $_POST['superuser_lname'])
        {
            $superuser_detail .= " <b>Last name</b>: "." ".$superuserDetailLog->superuser_lname." <b>To</b> ".$_POST['superuser_lname']." and ";
            
            $chages_data = true;
        }
        if($superuserDetailLog->superuser_email_address != $_POST['superuser_email_address'])
        {
            $superuser_detail .= " <b>Email</b>: "." ".$superuserDetailLog->superuser_email_address." <b>To</b> ".$_POST['superuser_email_address']." and ";
            
            $chages_data = true;
        }
        
        if($superuserDetailLog->superuser_status != $data['superuser_status'])
        {
            
            if($superuserDetailLog->superuser_status == '1')
            {
                $old_superuser_status = 'Active';
            }else{
                $old_superuser_status = 'Inactive';
            }

            if($data['superuser_status'] == '1')
            {
                $superuser_status = 'Active';
            }else{
                $superuser_status = 'Inactive';
            }

            $superuser_detail .= " <b>Status</b>: "." ".$old_superuser_status." <b>To</b> ".$superuser_status." and ";
            
            $chages_data = true;
        }


        $data['superuser_updated_at'] = date('Y-m-d H:i:s');
        
        $rs=$superuserObj->updateRecords($data,$id);
        if ($rs)
        {

            if($chages_data == true)
            {
                $ruser_detail = substr($superuser_detail, 0, -4);
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$ruser_detail);
            }
            $successMsg = base64_encode(RECORD_UPDATE);			
            //?successMsg='.$successMsg
            header('Location:superuserlist.php');
            exit;
        }
    }else{
        
        $data['superuser_unique_id'] = $superuserObj->getNextUniqueId('superuser_unique_id','superuser');
        $data['superuser_password'] = w::password($_POST['password']);
        $data['superuser_email_verified'] = date('Y-m-d H:i:s');
        
        $rs = $superuserObj->addRecords($data);
        if($rs){
            
            /* Start reset password link sent to email */
            $urldata = array(
                "id"=>$rs,
                "uniqueid"=>$data['superuser_unique_id'],
                "email"=>$data['superuser_email_address'],
                "modelType" =>"superuser",
                "time" =>time()+emailTemplates::ACCESS_TIME,
            );
            $mailArray =array(
                "new"=>array("email"=>$data['superuser_email_address'],'password'=>$_POST['password']),
                "urldata" => $urldata,
            );
            $subject = emailTemplates::subject('verify');
            $mailText = emailTemplates::verifyOrSignup($mailArray);
            //sendMail(emailTemplates::ADMIN_FROM_EMAIL,emailTemplates::ADMIN_FROM_NAME,$data['user_email_address'],"",$subject,$mailText);
            /* End reset password link sent to email */

            if($data['superuser_status'] == '1')
            {
                $superuser_status = 'Active';
            }else{
                $superuser_status = 'Inactive';
            }


            $superuser_detail = "Add User data "."<b>First name</b>: ".trim($_POST['superuser_fname'])." <b>and Last Name</b>: ".trim($_POST['superuser_lname'])." <b>and Email address</b>: ".trim($_POST['superuser_email_address'])." <b>and Status</b>: ".$superuser_status;

            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$superuser_detail);

            $successMsg = base64_encode(RECORD_ADD);
            //?successMsg='.$successMsg
            header('Location:superuserlist.php');
            exit;
        }
    }
}

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


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

?>