<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$hygieneObj =  new  hygiene();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'hygieneadd.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['hygiene_id'])){
    $hygieneDetail = $hygieneObj->fromId($_REQUEST['hygiene_id']);
}
if(isset($_POST['submit'])){
    	$data=array(
          'hygiene_title'=>trim($_POST['hygiene_title']),
          'hygiene_desc'=>trim($_POST['hygiene_desc']),
          'hygiene_slug'=>trim(w::slugify($_POST['hygiene_title'])),
       );
        if(isset($_POST['hygiene_status'])){
            $data['hygiene_status'] 			= '1';
        }else{
            $data['hygiene_status'] 			= '0';
        }

        $hygieneDetail = $hygieneObj->fromId($_REQUEST['hygiene_id']);
        if(!empty($hygieneDetail))
        {
            $hygienedata = "<b>Edit Record</b> ";
            if($hygieneDetail->hygiene_title != trim($_POST['hygiene_title']))
            {   
                $hygienedata .= " <b>Title</b>: "." ".$hygieneDetail->hygiene_title." <b>To</b> ".$_POST['hygiene_title']." and ";
            
                $chages_data = true;
            }

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

            /*if($hygieneDetail->hygiene_slug != trim($_POST['hygiene_slug']))
            {
                $hygienedata .= " Slug: "." ".$hygieneDetail->hygiene_slug." To ".$_POST['hygiene_slug']." and ";
            
                $chages_data = true;
            }*/

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

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

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

                if($chages_data == true)
                { 
                    $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($hygienedata," and"));
                }
                $successMsg = base64_encode(RECORD_UPDATE);			
                header('Location:hygienelist.php?successMsg='.$successMsg);
                exit;
            }
	}else{
        $data['hygiene_order'] = $hygieneObj->getNextorder('hygiene_order');
            $hygienedata = "<b>Add Record</b> ";
            $hygienedata.= " <b>Hygiene Data</b>: ".json_encode($data);
            $rs = $hygieneObj->addRecords($data);
            if($rs){
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$hygienedata);
                $successMsg = base64_encode(RECORD_ADD);			
                header('Location:hygienelist.php?successMsg='.$successMsg);
                exit;
            }
	}
}


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

?>