<?php 

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

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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'cityadd.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['city_id'])){
    $cityDetail = $cityObj->fromId($_REQUEST['city_id']);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='getstatedata' && isset($_REQUEST['country_id']) && $_REQUEST['country_id'] != "")
{
    $stateObj = new state();
    $where = "country_id = '".$_REQUEST['country_id']."' ";
    $orderby = array(" state_id desc ");
    $stateArg    = array('where' => $where,'orderBy'=>$orderby);
    $stateDetails = $stateObj->getAllRecords($stateArg);
    ?>
    <option value="">Select State</option>
    <?php
    foreach ($stateDetails as $key => $statevalue) 
    { ?>
        <?php if(isset($_REQUEST['state_id']) && $_REQUEST['state_id'] != '')
        { ?>
            <option value="<?php echo $statevalue->state_id; ?>" <?php echo (isset($_REQUEST['state_id']) && $_REQUEST['state_id'] == $statevalue->state_id) ? "selected='selected'" : ""; ?> ><?php echo $statevalue->state_name; ?></option>
       <?php }else{ ?>
            <option value="<?php echo $statevalue->state_id; ?>" ><?php echo $statevalue->state_name; ?></option>
        <?php } ?>
       
                             
    <?php }
    exit;
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['city_name']) && $_REQUEST['city_name'] != "")
{
    $data=array(
        'country_id'=>trim($_POST['country_id']),
        'state_id'=>trim($_POST['state_id']),
        'city_name'=>trim($_POST['city_name']),
    );
    if(isset($_POST['city_status']) && $_POST['city_status'] == 'active'){
        $data['city_status'] = 'active';
    }else{
        $data['city_status'] = 'inactive';
    } 
     
    $cityDetail = $cityObj->fromId($_REQUEST['city_id']);
     if(!empty($cityDetail))
     {
            $citydata = "<b>Edit Record</b> ";
            if($cityDetail->country_id != trim($_POST['country_id']))
            {   
                $old_countryname = $countryObj->fromId($cityDetail->country_id);
                $new_countryname = $countryObj->fromId($_REQUEST['country_id']);

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

            if($cityDetail->state_id != trim($_POST['state_id']))
            {   
                $old_statename = $stateObj->fromId($cityDetail->state_id);
                $new_statename = $stateObj->fromId($_REQUEST['state_id']);

                $citydata .= " <b>State Name</b>: "." ".$old_statename->state_name." <b>To</b> ".$new_statename->state_name." and ";
            
                $chages_data = true;
            }

            if($cityDetail->city_name != trim($_POST['city_name']))
            {   

                $citydata .= " <b>City Name</b>: "." ".$cityDetail->city_name." <b>To</b> ".$_POST['city_name']." and ";
            
                $chages_data = true;
            }

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

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

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

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

                $citydata .= " <b>Status</b>: "." ".$old_status." To ".$new_status." and ";
            
                $chages_data = true;
            } 
            
     }  
    if (isset($_POST['city_id'])){
        
        $id=$_POST['city_id'];
        $data['city_updated_at'] = date('Y-m-d H:i:s');
        $rs=$cityObj->updateRecords($data,$id);
        if ($rs)
        {  

            if($chages_data == true)
            { 
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($citydata," and"));
            } 	
           header('Location:citylist.php');
            exit;
        }
    }else{
        
        $data['city_created_at'] = date('Y-m-d H:i:s');

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

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

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

?>