<?php
	require "../commonincludefiles.php";
	error_reporting(1);
   	$objContact = new ContactMgmt();
	
	if($_REQUEST['action'] == "group-combo"){
		$i = 0;
		$RES = $objContact->getDistinctGroup();
		if(dbRows($RES) > 0){
			$GroupData = array("count" => $total, "data" => array());
			while($row = dbArray($RES)){
				$temparray["group_name"]= $row["group_name"];
				$GroupData["data"][$i]	= $temparray;   
				$i++;
			}
		}
		echo json_encode($GroupData);	
	}
	else if($_REQUEST['action'] == "contact-grid"){
		$i = 0;
		$condition = "";
		if($_REQUEST['group_name'] != "" && $_REQUEST['group_name'] != "All"){
			$condition .= " AND group_name = '".Escape($_REQUEST['group_name'])."' ";
		}
		if($_REQUEST['occasion_name'] != ""){
			if($_REQUEST['occasion_name'] == "Birth Date"){
				$condition .= " AND birth_date = '".date("Y-m-d",strtotime(Escape($_REQUEST['occasion_date'])))."' ";
			}
			else if($_REQUEST['occasion_name'] == "Anniversary"){
				$condition .= " AND anniversary_date = '".date("Y-m-d",strtotime(Escape($_REQUEST['occasion_date'])))."' ";
			}	
		}
		
		$RES = $objContact->getAllRecords(0,0,$condition,"","");
		$total = dbRows($RES);
		if($total > 0){
			$ContactData = array("count" => $total, "data" => array());
			while($row = dbArray($RES)){
				$temparray["contact_id"] 		= $row["contact_id"];
				$temparray["group_name"] 		= $row["group_name"];
				$temparray["designation"] 		= $row["designation"];
				$temparray["name"]				= $row["name"];
				$temparray["address"]			= $row["address"];
				$temparray["landline_no"]		= $row["landline_no"];
				$temparray["mobile_no1"]		= $row["mobile_no1"];
				$temparray["mobile_no2"]		= $row["mobile_no2"];
				$temparray["email"]				= $row["email"];
				$temparray["birth_date"]		= $row["birth_date"];
				$temparray["joining_date"]		= $row["joining_date"];
				$temparray["bankacno"]			= $row["bankacno"];
				$temparray["pancardno"]			= $row["pancardno"];
				$temparray["biometriccode"]		= $row["biometriccode"];
				$temparray["busfacility"]		= $row["busfacility"];
				$temparray["groupdesig"]		= $row["group_name"].','.$row["designation"];
				$ContactData["data"][$i]		= $temparray;   
				$i++;
			}
		}
		echo json_encode($ContactData);
	}
	else if($_REQUEST['action'] == "save"){	
		$objContact->contact_id 		= $_REQUEST['contact_id'];
		$objContact->group_name 		= $_REQUEST['group_name'];
		$objContact->designation 		= $_REQUEST['designation'];
		$objContact->name 				= $_REQUEST['name'];
		$objContact->address 			= $_REQUEST['address'];
		$objContact->landline_no 		= $_REQUEST['landline_no'];
		$objContact->mobile_no1 		= $_REQUEST['mobile_no1'];
		$objContact->mobile_no2 		= $_REQUEST['mobile_no2'];
		$objContact->email 				= $_REQUEST['email'];
		$objContact->birth_date 		= $_REQUEST['birth_date'];
		$objContact->joining_date 		= $_REQUEST['joining_date'];
		$objContact->bankacno 			= $_REQUEST['bankacno'];
		$objContact->pancardno 			= $_REQUEST['pancardno'];
		$objContact->biometriccode 		= $_REQUEST['biometriccode'];
		$objContact->busfacility 		= $_REQUEST['busfacility'];
		
		if($_REQUEST['contact_id'] == 0){
			$objContact->add();
		} else if($_REQUEST['contact_id'] > 0){
			$objContact->edit();
		}
	}
	else if($_REQUEST['action'] == "del"){	
		$objContact->contact_id 		= $_REQUEST['contact_id'];
		$objContact->delete();
	}
?>