<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$serviceObj =  new  service();
$specialityObj = new speciality();

$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'serviceadd.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['service_id'])){
    $serviceDetail = $serviceObj->fromId($_REQUEST['service_id']);
}
if(isset($_POST['submit'])){
    	$data=array(
          'service_speciality_id'=>trim($_POST['service_speciality_id']),
          'service_name'=>trim($_POST['service_name']),
	);
        if(isset($_POST['service_status'])){
            $data['service_status'] 			= '1';
        }else{
            $data['service_status'] 			= '0';
        }

        $serviceDetail = $serviceObj->fromId($_REQUEST['service_id']);
        if(!empty($serviceDetail))
        {
            $servicedata = "<b>Edit Record</b> ";
            if($serviceDetail->service_speciality_id != trim($_POST['service_speciality_id']))
            {
                $oldspecialityDetail = $specialityObj->fromId($serviceDetail->service_speciality_id);
                $newspecialityDetail = $specialityObj->fromId($_POST['service_speciality_id']);
                   
                $servicedata .= " <b>Speciality Name</b>: "." ".$oldspecialityDetail->speciality_name." <b>To</b> ".$newspecialityDetail->speciality_name." and ";
            
                $chages_data = true;
            }

            if($serviceDetail->service_name != trim($_POST['service_name']))
            {
                $servicedata .= " <b>Name</b>: "." ".$serviceDetail->service_name." <b>To</b> ".$_POST['service_name']." and ";
            
                $chages_data = true;
            }

            if($serviceDetail->service_status != $data['service_status'])
            {

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

                if($serviceDetail->service_status == '1')
                {
                    $old_status = 'Active';
                }else{
                    $old_status = 'Inactive';
                }

                $servicedata .= " <b>Status</b>: "." ".$old_status." To ".$new_status." and ";
            
                $chages_data = true;
            }  
        }
        
    	if (isset($_POST['service_id'])){
            $id=$_POST['service_id'];
            $rs=$serviceObj->updateRecords($data,$id);
            if ($rs)
            {
                if($chages_data == true)
                { 
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($servicedata," and"));
                }

                $successMsg = base64_encode(RECORD_UPDATE);			
                header('Location:servicelist.php?service_speciality_id='.$_POST['service_speciality_id'].'&successMsg='.$successMsg);
                exit;
            }
    	}else{
                $servicedata = "<b>Add Record</b> ";
                $servicedata.= " <b>Service Data</b>: ".json_encode($data);
                $rs = $serviceObj->addRecords($data);
                if($rs){
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$servicedata);
                    $successMsg = base64_encode(RECORD_ADD);			
                    header('Location:servicelist.php?service_speciality_id='.$_POST['service_speciality_id'].'&successMsg='.$successMsg);
                    exit;
                }
    	}
}
$allspecialities = $specialityObj->getAllRecords();

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

?>