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

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'appointment_typeadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;

}

$specialityObj = new speciality();
if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['appointmenttype_id'])){
    $appointmenttypeDetail = $appointmenttypeObj->fromId($_REQUEST['appointmenttype_id']);
}
if(isset($_POST['submit'])){
    	$data=array(
          'appointmenttype_speciality_id'=>trim($_POST['appointmenttype_speciality_id']),
          'appointmenttype_name'=>trim($_POST['appointmenttype_name']),
          'appointmenttype_duration'=>trim($_POST['appointmenttype_duration']),
	);
        if(isset($_POST['appointmenttype_status'])){
            $data['appointmenttype_status'] 			= '1';
        }else{
            $data['appointmenttype_status'] 			= '0';
        } 

        $appointmenttypeDetail = $appointmenttypeObj->fromId($_REQUEST['appointmenttype_id']);
        if(!empty($appointmenttypeDetail))
        {
            $appointmenttypedata = "<b>Edit Record</b> ";
            if($appointmenttypeDetail->appointmenttype_speciality_id != trim($_POST['appointmenttype_speciality_id']))
            {   

                $oldappointmenttypeDetail = $specialityObj->fromId($appointmenttypeDetail->appointmenttype_speciality_id);
                $newappointmenttypeDetail = $specialityObj->fromId($_POST['appointmenttype_speciality_id']);

                $appointmenttypedata .= " <b>Title</b>: "." ".$oldappointmenttypeDetail->speciality_name." <b>To</b> ".$newappointmenttypeDetail->speciality_name." and ";
            
                $chages_data = true;
            }

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

            if($appointmenttypeDetail->appointmenttype_duration != trim($_POST['appointmenttype_duration']))
            {
                $appointmenttypedata .= " <b>Duration</b>: "." ".$appointmenttypeDetail->appointmenttype_duration." <b>To</b> ".$_POST['appointmenttype_duration']." and ";
            
                $chages_data = true;
            }
            

            if($appointmenttypeDetail->appointmenttype_status != $data['appointmenttype_status'])
            {
                
                if($data['appointmenttype_status'] == '1')
                {
                    $new_status = 'Active';
                }else{
                    $new_status = 'Inactive';
                }

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

                $appointmenttypedata .= " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." and ";
            
                $chages_data = true;
            }  
        }               
	if (isset($_POST['appointmenttype_id'])){
        $id=$_POST['appointmenttype_id'];
        $rs=$appointmenttypeObj->updateRecords($data,$id);
        if ($rs){

                if($chages_data == true)
                { 
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($appointmenttypedata," and"));
                }
                $successMsg = base64_encode(RECORD_UPDATE);			
                header('Location:appointment_typelist.php?appointmenttype_speciality_id='.$_POST['appointmenttype_speciality_id'].'&successMsg='.$successMsg);
                exit;
            }
	}else{

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

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

?>