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

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'citylist.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']=='change_status' && isset($_REQUEST['city_id'])){
    if(isset($_REQUEST['status'])){

        $cityDetail = $cityObj->fromId($_REQUEST['city_id']);
        if($cityDetail->city_status == 'active')
        {
            $old_status = 'Active';
        }else{
            $old_status = 'Inactive';
        }

        if($_REQUEST['status'] == '1')
        {
            $city_status = 'active';
            $new_status = 'Active';
        }else{
            $city_status = 'inactive';
            $new_status = 'Inactive';
        }
        //$cityObj->changeStatus($_REQUEST['city_id']);

        $id=$_REQUEST['city_id'];
        $data['city_status'] = $city_status;
        $rs=$cityObj->updateRecords($data,$id);

        $cityDetail = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
        
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$cityDetail);
        echo RECORD_STATUS;
    }
    return true;
}
if(isset($_REQUEST['act']) && $_REQUEST['act']=='delete' && isset($_REQUEST['city_id'])){
    $cityObj->deleteFromId($_REQUEST['city_id']);
    $successMsg = base64_encode(RECORD_DELETE);
}
if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("city_status" => '1');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $cityObj->fromId($val_actchk);
            if($old_data->state_status == 'active')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->state_status != 'active')
            {
                $new_status = 'Active';
                $citydata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$citydata);
            }
        }
        $rs = $cityObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("city_status" => '0');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $cityObj->fromId($val_actchk);
            if($old_data->city_status == 'active')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->city_status != 'active')
            {
                $new_status = 'Active';
                $citydata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$citydata);
            }
        }
        $rs = $cityObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }else if($_REQUEST['action']=="delete_all"){

        $chk_del = implode(",",$_POST['check']);
        $citydata = " <b>Delete Id</b>: "." ".$chk_del;
        $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$citydata);
        $rs = $cityObj->deleteManyRecords($_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_DELETE);
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";

    
    
    if(isset($_REQUEST['country_id']) && $_REQUEST['country_id'] != '')
    {
        $where .= " AND c.country_id = '".$_REQUEST['country_id']."'";
    }
    if(isset($_REQUEST['state_id']) && $_REQUEST['state_id'] != '')
    {
        $where .= " AND c.state_id = '".$_REQUEST['state_id']."'";
    }
    
    if(isset($_REQUEST['city_name']) && $_REQUEST['city_name'] != '')
    {
        $where .= " AND  city_name = '".$_REQUEST['city_name']."' ";
    }
    
    if(isset($_REQUEST['city_status']) && $_REQUEST['city_status'] != '')
    {
        $where .= " AND  city_status = '".$_REQUEST['city_status']."' ";
    }
    
    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " city_id LIKE '%".$_REQUEST['search']['value']."%' OR city_name LIKE '%".$_REQUEST['search']['value']."%' ";
    }    
    

    $orderFields = array('city_id');    
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" city_id desc ");
    }
    //print_r($orderby);exit;
    $tables = array('tbl_country cu','tbl_state s');
    $jfields = array('c.country_id = cu.country_id','s.state_id = c.state_id');
    $jtype   = array("INNER","INNER"); 
    $data    = array('joinTables' =>$tables, 'joinFields' =>$jfields, 'joinTypes' => $jtype,'where' => $where,'limit' => $limit, 'orderBy'=>$orderby);
    //$data    = array('where' => $where,'limit' => $limit, 'orderBy'=>$orderby);

    $cityDetails = $cityObj->getAllRecords($data);

    $recordsTotal   =   $cityObj->getAllCount($data);

        
    if ($cityDetails && count($cityDetails)>0){
        foreach ($cityDetails as $key => $citydata){

            

            if($citydata->city_status == 'active'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$citydata->city_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$citydata->city_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
            
            
            $action = '<a href="cityadd.php?act=edit&city_id='.$citydata->city_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$citydata->city_id.'" class="check_class" />',
                    $citydata->city_id, 
                    $citydata->country_name,
                    $citydata->state_name,
                    $citydata->city_name,
                    "<div id='td_status_".$citydata->city_id."'>".$status."</div>",
                    $action,
                    
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');
?>