<?php
@session_start();
include_once("../class/class.MySQL.php");
require '../class/resizeImage.php';
$db	=new MySQL();

$action = $_GET['action'];

if($action == "insert"){
	
	$data['company_name'] = $_POST['company_name'];
	$data['company_address'] = $_POST['company_address'];
	$data['company_phone'] = $_POST['company_phone'];
	$data['company_mobile'] = $_POST['company_mobile'];
	$data['company_email'] = $_POST['company_email'];
	$data['pf_code_num'] = $_POST['pf_code_no'];


	if(isset($_FILES['company_logo']['name']) && $_FILES['company_logo']['name']!=''){
		$randnum		=rand(1200156,9994564569);
		$image			=$randnum.$_FILES['company_logo']['name'];
		
		$data['company_logo']		=$image;
		
		$paththumb= "../upload/thumb/".$randnum.$_FILES['company_logo']['name'];
		$pathimg= "../upload/image/".$randnum.$_FILES['company_logo']['name'];
		
		try {
	
				$image = new ImageResize($_FILES['company_logo']['tmp_name']);
				
				//// Save thumb
				$image->resizeToWidth(300);
				$image->save($paththumb);
				
				/// Save Image
				$image1 = new ImageResize($_FILES['company_logo']['tmp_name']);
				$image1->resizeToWidth(500);
				$image1->save($pathimg);

		} catch (Exception $e) {

		}
	
	}
		
		$db->Insert($data,'addhrm_company_master');	
		$data['id']=$db->LastInsertID();	
		echo '{"leaves":'.json_encode($data).'}';
		//print_r($data);
		/*$count = $db->CountRows("addhrm_company_master");
		$where['id']	=1;
		if($count > 0){
			$db->Update("addhrm_company_master",$data,$where);
		}else{
			$db->Insert($data,'addhrm_company_master');
		}*/
		
	
}


elseif($action == "list"){
/*$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='manageCompany' and m.menu_id=s.menu_id "));
	
 $userid=$_SESSION['admin_id'];

	$where['user_id']	= $userid;
	$where['s_submenu_id']	= $sub_rights['s_id'];
	


	$companyDetails = $db->Select('addhrm_company_master');
	
			if($userid==1)
	{
	$companyDetails[0]['permissions'][0] = array("user_id"=>$userid);
	}
	else
	{
	$companyDetails[0]['permissions']  =  $db->fetchAll("tbl_user_rights",$where);
	}
	
	//echo json_encode($companyDetails);
	echo '{"companyDetails":'.json_encode($companyDetails).'}';*/
	
	$company = $db->fetchQuery("SELECT * FROM addhrm_company_master ");
	
		$sub_rights=mysql_fetch_array(mysql_query("select * from menu as m,menu_sub as s where  s.s_menu_name='Branch' and m.menu_id=s.menu_id "));
	
	$userid=$_SESSION['admin_id'];

	$where['user_id']	= $userid;
	$where['s_submenu_id']	= $sub_rights['s_id'];
	if(count($company) > 0){
	if($userid==1)
	{
	$company[count($company)-1]['permissions'][0] = array("user_id"=>$userid);
	}
	else
	{
	$company[count($company)-1]['permissions'] =  $db->fetchAll("tbl_user_rights",$where);
	}
	}
	//print_r($leaves);
	echo '{"companyDetails":'.json_encode($company).'}';
	
	
	
}

else if($action == "update")
{
	
	$where['id']	 = $_POST['userid'];
	$data['company_name'] = $_POST['company_name'];
	$data['company_address'] = $_POST['company_address'];
	$data['company_phone'] = $_POST['company_phone'];
	$data['company_mobile'] = $_POST['company_mobile'];
	$data['company_email'] = $_POST['company_email'];
	$data['pf_code_num'] = $_POST['pf_code_no'];
	
	$db->Update('addhrm_company_master',$data,$where);
	echo json_encode($data);
	
	
	
}
elseif($action == "removeded"){

	$where['id']	= $_GET['did'];
	
	$deductions = $db->Delete('addhrm_company_master',$where);
	

}



?>