<?php 

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

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'stateadd.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['state_id'])){
    $stateDetail = $stateObj->fromId($_REQUEST['state_id']);
}


if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['state_name']) && $_REQUEST['state_name'] != "")
{
    $data=array(
        'country_id'=>trim($_POST['country_id']),
        'state_name'=>trim($_POST['state_name']),
        'state_shortcode'=>trim($_POST['state_shortcode']),
    );
    if(isset($_POST['state_status']) && $_POST['state_status'] == 'active'){
        $data['state_status'] = 'active';
    }else{
        $data['state_status'] = 'inactive';
    } 
     
     $stateDetail = $stateObj->fromId($_REQUEST['state_id']);
     if(!empty($stateDetail))
     {
            $statedata = "<b>Edit Record</b> ";
            if($stateDetail->country_id != trim($_POST['country_id']))
            {   
                $old_countryname = $countryObj->fromId($stateDetail->country_id);
                $new_countryname = $countryObj->fromId($_REQUEST['country_id']);

                $statedata .= " <b>Name</b>: "." ".$old_countryname->country_name." <b>To</b> ".$new_countryname->country_name." and ";
            
                $chages_data = true;
            }

            if($stateDetail->state_name != trim($_POST['state_name']))
            {   

                $statedata .= " <b>Name</b>: "." ".$stateDetail->state_name." <b>To</b> ".$_POST['state_name']." and ";
            
                $chages_data = true;
            }

            if($stateDetail->state_shortcode != trim($_POST['state_shortcode']))
            {   

                $statedata .= " <b>Short Code</b>: "." ".$stateDetail->state_shortcode." <b>To</b> ".$_POST['state_shortcode']." and ";
            
                $chages_data = true;
            }

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

                $statedata .= " Short Code: "." ".$stateDetail->country_shortcode." To ".$_POST['country_short_code']." and ";
            
                $chages_data = true;
            }*/

            

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

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

                $statedata .= " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." and ";
            
                $chages_data = true;
            } 
            
     } 
    if (isset($_POST['state_id'])){
        
        $id=$_POST['state_id'];
        $data['state_updated_at'] = date('Y-m-d H:i:s');
        $rs=$stateObj->updateRecords($data,$id);
        if ($rs)
        {   
            if($chages_data == true)
            { 
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($statedata," and"));
            }	
            header('Location:statelist.php');
            exit;
        }
    }else{
        
        $data['state_created_at'] = date('Y-m-d H:i:s');

        $statedata = "<b>Add Record</b> ";
        $statedata.= " <b>State Data</b>: ".json_encode($data);
        
        $rs = $stateObj->addRecords($data);
        if($rs)
        {
           $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$statedata);
           header('Location:statelist.php');
            exit;
        }
    }
}

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

?>