<?php 

// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$countryObj =  new  country();

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'countryadd.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['country_id'])){
    $countryDetail = $countryObj->fromId($_REQUEST['country_id']);
}


if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['country_name']) && $_REQUEST['country_name'] != "")
{
    $data=array(
        'country_name'=>trim($_POST['country_name']),
        'country_shortcode'=>trim($_POST['country_short_code']),
    );
    if(isset($_POST['country_status']) && $_POST['country_status'] == 'active'){
        $data['country_status'] = 'active';
    }else{
        $data['country_status'] = 'inactive';
    } 
     
     $countryDetail = $countryObj->fromId($_REQUEST['country_id']);
     if(!empty($countryDetail))
     {
            $countrydata = "<b>Edit Record</b> ";
            if($countryDetail->country_name != trim($_POST['country_name']))
            {   

                $countrydata .= " <b>Name</b>: "." ".$countryDetail->country_name." <b>To</b> ".$_POST['country_name']." and ";
            
                $chages_data = true;
            }

            if($countryDetail->country_shortcode != trim($_POST['country_short_code']))
            {   

                $countrydata .= " <b>Short Code</b>: "." ".$countryDetail->country_shortcode." <b>To</b> ".$_POST['country_short_code']." and ";
            
                $chages_data = true;
            }

            

            if($countryDetail->country_status != $data['country_status'])
            {
                
                if($data['country_status'] == 'active')
                {
                    $new_status = 'Active';
                }else{
                    $new_status = 'Inactive';
                }

                if($countryDetail->country_status == 'active')
                {
                    $old_status = 'Active';
                }else{
                    $old_status = 'Inactive';
                }

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

    if (isset($_POST['country_id'])){
        
        $id=$_POST['country_id'];
        $data['country_updated_at'] = date('Y-m-d H:i:s');
        $rs=$countryObj->updateRecords($data,$id);
        if ($rs)
        {   
            if($chages_data == true)
            { 
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($countrydata," and"));
            }	
            header('Location:countrylist.php');
            exit;
        }
    }else{
        $data['country_created_at'] = date('Y-m-d H:i:s');

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

        $rs = $countryObj->addRecords($data);
        if($rs)
        {
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$countrydata);
            header('Location:countrylist.php');
            exit;
        }
    }
}

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

?>