<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$agencyuserObj   = new agencyuser();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'agencyuseradd.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['agencyuser_id'])){
	$agencyuserDetail = $agencyuserObj->fromId($_REQUEST['agencyuser_id']);
    $agencyuser_clinicsObj = new agencyuser_clinics();
    $where =array('agencyid='=>$agencyuserDetail->agencyuser_agency_id,'agencyuserid='=>$agencyuserDetail->agencyuser_id);
    $arg =array('where'=>$where);
    $agencyUserClinicsDetails  =$agencyuser_clinicsObj->getAllRecords($arg);
    $agencuserClinics=array();
    if(!empty($agencyUserClinicsDetails)){
        $agencuserClinics =array_column($agencyUserClinicsDetails, 'clinicid');
    }
}
if(isset($_POST['submit'])){
       
        $data=array(
          'agencyuser_agency_id'=>trim($_POST['agencyuser_agency_id']),
          'agencyuser_fname'=>trim($_POST['agencyuser_fname']),
          'agencyuser_lname'=>trim($_POST['agencyuser_lname']),
          'agencyuser_role'=>trim($_POST['agencyuser_role']),
          'agencyuser_phone'=>trim($_POST['agencyuser_phone']),
          'agencyuser_email'=>trim($_POST['agencyuser_email'])
        );
        
        if(isset($_POST['agencyuser_status'])){
            $data['agencyuser_status']           = '1';
        }else{
            $data['agencyuser_status']           = '0';
        }  
    $chages_data = false;
    if (isset($_POST['agencyuser_id'])){
        $id=$_POST['agencyuser_id'];
        $rs=$agencyuserObj->updateRecords($data,$id);
        if ($rs){
            $agencyuser_clinicsObj = new agencyuser_clinics();
            $where =array('agencyid='=>$_POST['agencyuser_agency_id'],'agencyuserid='=>$id);
            $arg =array('where'=>$where);
            $allclinics  =$agencyuser_clinicsObj->getAllRecords($arg);
            $clinicsArray= array();
            if(!empty($allclinics)){
                $clinicsArray =array_column($allclinics, 'clinicid');
            }
            if($_POST['agencyuser_clinics'] && count($_POST['agencyuser_clinics'])>0){
                foreach ($_POST['agencyuser_clinics'] as $key => $clinicId) {
                    if(!empty($clinicsArray) && !in_array($clinicId, $clinicsArray)){
                        $data =array(
                            "agencyid"=>$_POST['agencyuser_agency_id'],
                            "agencyuserid"=>$id,
                            "clinicid"=>$clinicId,
                        );
                        $crs = $agencyuser_clinicsObj->addRecords($data);
                    }elseif(empty($clinicsArray)){
                        $data =array(
                            "agencyid"=>$_POST['agencyuser_agency_id'],
                            "agencyuserid"=>$id,
                            "clinicid"=>$clinicId,
                        );
                        $crs = $agencyuser_clinicsObj->addRecords($data);
                    }
            }}
            if($allclinics && count($allclinics)>0){
                foreach ($allclinics as $key => $value) {
                    if(!in_array($value->clinicid, $_POST['agencyuser_clinics'])){
                        $agencyuser_clinicsObj->deleteFromId($value->agencyuser_clinics_id);
                        $where = array(
                            'agencyid='=>$value->agencyid,
                            'clinicid='=>$value->clinicid
                        );            
                        $deleteArg   =   array('where'=>$where);
                        $agencyuser_clinicsObj->deleteFromFields($deleteArg);
                    }
                }
            }

            $successMsg = base64_encode(RECORD_UPDATE);          
            header('Location:agencyuserlist.php');
            exit;
        }
    }else{
        $data['agencyuser_unique_id'] = $agencyuserObj->getNextUniqueId('agencyuser_unique_id');
        $data['agencyuser_created_at']=date('Y-m-d H:i:s');
        $data['agencyuser_created_by']=$_SESSION['admin_id'];
        $data['agencyuser_password']  = w::password($_POST['agencyuser_password']);    
        $rs = $agencyuserObj->addRecords($data);
        if($rs){
            $agencyuser_clinicsObj = new agencyuser_clinics();
            if($_POST['agencyuser_clinics'] && count($_POST['agencyuser_clinics'])>0){
                foreach ($_POST['agencyuser_clinics'] as $key => $clinicId) {
                    $data =array(
                        "agencyid"=>$_POST['agencyuser_agency_id'],
                        "agencyuserid"=>$rs,
                        "clinicid"=>$clinicId,
                    );
                    $crs = $agencyuser_clinicsObj->addRecords($data);
                }
            }
            $successMsg = base64_encode(RECORD_ADD);         
            header('Location:agencyuserlist.php');
            exit;
        }
    }
}
$agencyObj= new agency();
$allagency =$agencyObj->getAllRecords(array());
// Include Template 
include_once('../libs/routs.php');

?>