<?php
include "Connection.php";
require_once(dirname(__FILE__)."/_api/global.php");
$conn = new Connection();


if(isset($_REQUEST["funName"])) {
	$db = $conn->getConnection();
	$fun = $_REQUEST["funName"];
	switch($fun) {
		case "dealerlist":
        case "adddealer":
        case "editdealer":    
        case "deletedealer":        
			$fun();     
		break;
        
	}
}
        
// DELAER LIST
function dealerlist() {
	global $conn;
	try {
        $lang = (isset($_REQUEST["lang"]))?$_REQUEST["lang"]:'en_us';
        
		$pn = $_REQUEST["pn"];
		$nr = $_REQUEST["nr"];
		$key = escape_str($_REQUEST["key"]);
		if(isset($_SESSION["userid"])){
        
			$userstr = "SELECT SQL_CALC_FOUND_ROWS * FROM " . DMS_DEALER . " WHERE 1=1";
            
			if (!empty($key)) {
				$userstr .= " and ((dealer_code like '%".$key."%') or (dealer_name like '%".$key."%') or (zone like '%".$key."%') or (state like '%".$key."%') or (city like '%".$key."%')  ) ";
			}
			if (!empty($pn) and !empty($nr)) {
				// Navigation
    			$userstr .= " limit " .  (($pn - 1) *  $nr) . ", " . $nr;
			}
        
			$query = $conn->query($userstr);

			// Total records
			$rows_query = $conn->query("select FOUND_ROWS() as total_records");
			$rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
			$total_records = $rows_result[0]->total_records;

			$arr['total_records'] = $total_records;
            $userlist=$query->fetchAll(PDO::FETCH_OBJ);
            if(count($userlist)>0)
            {
                /*
                foreach($userlist as $mkey=>$res)
                {
                   
                }
                */
            }
            
			$arr['users'] = $userlist;
			if(count($arr) != 0) {
				$conn->message(true,$conn->getMessage($lang,'dealer','success1'),$arr );
			}else {
				$conn->message(false,$conn->getMessage($lang,'dealer','error2'));
			}
		}else {
			$conn->message(false,$conn->getMessage($lang,'dealer','error1'));
		}
	}
	catch(PDOException $e) {
		$conn->message(false,$conn->getMessage($lang,'dealer','error4'));
	}	
}

     
// ADD dealer
function adddealer() {
	global $conn;
	try {
        $lang = (isset($_REQUEST["lang"]))?$_REQUEST["lang"]:'en_us';
        
		if(isset($_SESSION["userid"]))
        { 
        
            //GET EMPLOYEE 
            $dealer_code = escape_str($_REQUEST["dealer_code"]); 
            $dealer_name = escape_str($_REQUEST["dealer_name"]); 
            $city = escape_str($_REQUEST["city"]); 
            $state = escape_str($_REQUEST["state"]); 
            $zone = escape_str($_REQUEST["zone"]); 
            $country ='India';
            $phone= escape_str($_REQUEST["phone"]);
            $zm_name=escape_str($_REQUEST["zm_name"]);
            $rm_name=escape_str($_REQUEST["rm_name"]);
            $am_name=escape_str($_REQUEST["am_name"]);
            $zm_email=escape_str($_REQUEST["zm_email"]);
            $rm_email=escape_str($_REQUEST["rm_email"]);
            $am_email=escape_str($_REQUEST["am_email"]);
            $zm_mobile=escape_str($_REQUEST["zm_mobile"]);
            $rm_mobile=escape_str($_REQUEST["rm_mobile"]);
            $am_mobile=escape_str($_REQUEST["am_mobile"]);

            if(!($dealer_code!='' && $dealer_name!='' && $city!=''   && $state!='' && $zone!='' && $phone!='' && $zm_name!='' && $rm_name!='' && $am_name!='' && $zm_email!=''  && $rm_email!=''  && $am_email!='' && $zm_mobile!='' && $rm_mobile!='' && $am_mobile!='' ))
            {
                $conn->message(false,$conn->getMessage($lang,'dealer','error6')); 
            }
        
            //GET SMPIN
            $query_str = "SELECT `id`  FROM ".DMS_DEALER." WHERE dealer_code='".$dealer_code."' LIMIT 1";
            $check_result_arr=$conn->getQueryValue($query_str); 

            if(count($check_result_arr)){ $conn->message(false,$conn->getMessage($lang,'dealer','error5')); }    

            $insertstr = "INSERT INTO ".DMS_DEALER." ( `dealer_code`, `dealer_name`, `city`, `state`, `zone`, `country`, `phone`, `zm_name`, `rm_name`, `am_name`, `zm_email`, `rm_email`, `am_email`, `zm_mobile`, `rm_mobile`, `am_mobile`, `status`, `is_deleted`, `created_on`, `modified_on`) VALUES( '".$dealer_code."','".$dealer_name."','".$city."','".$state."','".$zone."','".$country."','".$phone."','".$zm_name."','".$rm_name."','".$am_name."','".$zm_email."','".$rm_email."','".$am_email."','".$zm_mobile."','".$rm_mobile."','".$am_mobile."','".$status."','".$is_deleted."','".NOW."', '".NOW."')";
            $insertquery = $conn->query($insertstr);
            $insertid = $conn->lastinsetid();              

            $conn->message(true,$conn->getMessage($lang,'dealer','success1')); 
		}else {
			$conn->message(false,$conn->getMessage($lang,'dealer','error1'));
		}
	}
	catch(PDOException $e) {
		$conn->message(false,$conn->getMessage($lang,'dealer','error4'));
	}	
}

// EDIT dealer
function editdealer() {
	global $conn;
	try {
        $lang = (isset($_REQUEST["lang"]))?$_REQUEST["lang"]:'en_us';
        
		if(isset($_SESSION["userid"]))
        { 
            $id = escape_str($_REQUEST["id"]);
            
            //GET DEALER
            $query_str = "SELECT *  FROM ".DMS_DEALER." WHERE  id='".$id."' LIMIT 1";
            $check_data_arr=$conn->getQueryValue($query_str); 

            if(!(count($check_data_arr)>0))  $conn->message(false,$conn->getMessage($lang,'dealer','error2'));
            
            
            
            //GET EMPLOYEE 
            $dealer_code = escape_str($_REQUEST["dealer_code"]); 
            $dealer_name = escape_str($_REQUEST["dealer_name"]); 
            $city = escape_str($_REQUEST["city"]); 
            $state = escape_str($_REQUEST["state"]); 
            $zone = escape_str($_REQUEST["zone"]); 
            $country ='India';
            $phone= escape_str($_REQUEST["phone"]);
            $zm_name=escape_str($_REQUEST["zm_name"]);
            $rm_name=escape_str($_REQUEST["rm_name"]);
            $am_name=escape_str($_REQUEST["am_name"]);
            $zm_email=escape_str($_REQUEST["zm_email"]);
            $rm_email=escape_str($_REQUEST["rm_email"]);
            $am_email=escape_str($_REQUEST["am_email"]);
            $zm_mobile=escape_str($_REQUEST["zm_mobile"]);
            $rm_mobile=escape_str($_REQUEST["rm_mobile"]);
            $am_mobile=escape_str($_REQUEST["am_mobile"]);

            if(!($dealer_code!='' && $dealer_name!='' && $city!=''   && $state!='' && $zone!='' && $phone!='' && $zm_name!='' && $rm_name!='' && $am_name!='' && $zm_email!=''  && $rm_email!=''  && $am_email!='' && $zm_mobile!='' && $rm_mobile!='' && $am_mobile!='' ))
            {
                $conn->message(false,$conn->getMessage($lang,'dealer','error6')); 
            }
        
            //GET SMPIN
            $query_str = "SELECT `id`  FROM ".DMS_DEALER." WHERE dealer_code='".$dealer_code."' AND id!='".$id."' LIMIT 1";
            $check_result_arr=$conn->getQueryValue($query_str); 

            if(count($check_result_arr)){ $conn->message(false,$conn->getMessage($lang,'dealer','error5')); }    

            $udpatestr = "UPDATE  ".DMS_DEALER." SET `dealer_code`='".$dealer_code."', `dealer_name`='".$dealer_name."', `city`='".$city."', `state`='".$state."', `zone`='".$zone."', `country`='".$country."', `phone`='".$phone."', `zm_name`='".$zm_name."', `rm_name`='".$rm_name."', `am_name`='".$am_name."', `zm_email`='".$zm_email."', `rm_email`='".$rm_email."', `am_email`='".$am_email."', `zm_mobile`='".$zm_mobile."', `rm_mobile`='".$rm_mobile."', `am_mobile`='".$am_mobile."',  `modified_on`= '".NOW."' WHERE  id='".$id."'";
            $udpatequery = $conn->query($udpatestr);
        
            $conn->message(true,$conn->getMessage($lang,'dealer','success1')); 
		}else {
			$conn->message(false,$conn->getMessage($lang,'dealer','error1'));
		}
	}
	catch(PDOException $e) {
		$conn->message(false,$conn->getMessage($lang,'dealer','error4'));
	}	
}

// DELETE DEALER
function deletedealer() {
	global $conn;
	try {
        $lang = (isset($_REQUEST["lang"]))?$_REQUEST["lang"]:'en_us';
      
		if(isset($_SESSION["userid"]))
        {  
            $id = escape_str($_REQUEST["id"]); 
        
            //GET EMPLOYEE
            $query_str = "SELECT *  FROM ".DMS_DEALER." WHERE  id='".$id."' LIMIT 1";
            $check_data_arr=$conn->getQueryValue($query_str); 

            if(!(count($check_data_arr)>0))  $conn->message(false,$conn->getMessage($lang,'dealer','error2')); 

            $query_str = "DELETE FROM ".DMS_DEALER." WHERE  id='".$id."' LIMIT 1";
            $conn->executeQuery($query_str);

        
            $conn->message(true,$conn->getMessage($lang,'dealer','success1')); 
        
        }else {
			$conn->message(false,$conn->getMessage($lang,'dealer','error1'));
		}
    }
	catch(PDOException $e) {
		$conn->message(false,$conn->getMessage($lang,'dealer','error4'));
	}	
}

?>