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

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'featureadd.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['feature_id'])){
    $featureDetail = $featureObj->fromId($_REQUEST['feature_id']);
}
if(isset($_POST['submit'])){
    	$data=array(
          'feature_speciality_id'=>trim($_POST['feature_speciality_id']),
          'feature_name'=>trim($_POST['feature_name']),
          'feature_desc'=>trim($_POST['feature_desc']),
	);
        if(isset($_POST['feature_status'])){
            $data['feature_status'] 			= '1';
        }else{
            $data['feature_status'] 			= '0';
        }

        $featureDetail = $featureObj->fromId($_REQUEST['feature_id']);
        if(!empty($featureDetail))
        {
            $featuredata = "<b>Edit Record</b> ";
            if($featureDetail->feature_speciality_id != trim($_POST['feature_speciality_id']))
            {
                $oldfeatureDetail = $specialityObj->fromId($featureDetail->feature_speciality_id);
                $newfeatureDetail = $specialityObj->fromId($_POST['feature_speciality_id']);
                   
                $featuredata .= " <b>Specality Name</b>: "." ".$oldfeatureDetail->speciality_name." <b>To</b> ".$newfeatureDetail->speciality_name." and ";
            
                $chages_data = true;
            }

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

            if($featureDetail->feature_desc != trim($_POST['feature_desc']))
            {
                $featuredata .= " <b>Description</b>: "." ".$featureDetail->feature_desc." <b>To</b> ".$_POST['feature_desc']." and ";
            
                $chages_data = true;
            }

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

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

                $featuredata .= " <b>Status</b>: "." ".$old_status." To ".$new_status." and ";
            
                $chages_data = true;
            }  
        }

	if (isset($_POST['feature_id'])){
        $id=$_POST['feature_id'];
        $rs=$featureObj->updateRecords($data,$id);
        if ($rs){

                if($chages_data == true)
                { 
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($featuredata," and"));
                }
                $successMsg = base64_encode(RECORD_UPDATE);			
                header('Location:featurelist.php?feature_speciality_id='.$_POST['feature_speciality_id'].'&successMsg='.$successMsg);
                exit;
            }
	}else{
        $data['feature_order'] = $featureObj->getNextorder('feature_order');
            $featuredata = "<b>Add Record</b> ";
            $featuredata.= " <b>Feature Data</b>: ".json_encode($data);
            $rs = $featureObj->addRecords($data);
            if($rs){
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$featuredata);
                $successMsg = base64_encode(RECORD_ADD);			
                header('Location:featurelist.php?feature_speciality_id='.$_POST['feature_speciality_id'].'&successMsg='.$successMsg);
                exit;
            }
	}
}
$allspecialities = $specialityObj->getAllRecords();

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

?>