<?php 
// Include main file
include_once('../libs/main.php');
// Check insurance Login
include_once('checkLogin.php');

// Logical section of page
$insuranceObj =  new  insurance();

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'insuranceadd.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['insurance_id'])){
    $insuranceDetail = $insuranceObj->fromId($_REQUEST['insurance_id']);
}

if(isset($_POST['submit'])){
    	$data=array(
	  'insurance_name'=>trim($_POST['insurance_name']), 
          'insurance_plan'=>json_encode($_POST['insurance_plan']), 
	);
        if(isset($_POST['insurance_status'])){
            $data['insurance_status'] 			= '1';
        }else{
            $data['insurance_status'] 			= '0';
        }

        $insuranceDetail = $insuranceObj->fromId($_REQUEST['insurance_id']);
        if(!empty($insuranceDetail))
        {
            $insurancedata = "<b>Edit Record</b> ";
            if($insuranceDetail->insurance_name != trim($_POST['insurance_name']))
            {   

                $insurancedata .= " <b>Name</b>: "." ".$insuranceDetail->insurance_name." <b>To</b> ".$_POST['insurance_name']." and ";
            
                $chages_data = true;
            }

            if($insuranceDetail->insurance_plan != trim($_POST['insurance_plan']))
            {   

                $insurancedata .= " <b>Plan</b>: "." ".$insuranceDetail->insurance_plan." <b>To</b> ".$_POST['insurance_plan']." and ";
            
                $chages_data = true;
            }

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

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

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

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

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

            $insurancedata = "<b>Add Record</b> ";
            $insurancedata.= " <b>Insurance Data</b>: ".json_encode($data);

            $rs = $insuranceObj->addRecords($data);
            if($rs){
                 $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$insurancedata);
    		$successMsg = base64_encode(RECORD_ADD);			
            //?successMsg='.$successMsg
    		header('Location:insurancelist.php');
    		exit;
            }
	}
}
// Include Template 
include_once('../libs/routs.php')

?>