<?php include('lib/config.php');?>
<?php       
    //===============================================================  
	 
    $toDay = date("Y-m-d");
    $cDateTime = date("mdYHis");
    
    $orgid = isset($_POST['orgid'])?$_POST['orgid']:'';
	
	$name = isset($_POST['name'])?$_POST['name']:'';
	$address = isset($_POST['address'])?$_POST['address']:'';
	$contact_no = isset($_POST['contact_no'])?$_POST['contact_no']:'';
	$contact_person = isset($_POST['contact_person'])?$_POST['contact_person']:'';
	$email = isset($_POST['email'])?$_POST['email']:'';
	//===============================================================
	//============== Uploading Files ===========================
	//===============================================================
	$oldDocfile = isset($_POST['old_logo'])?$_POST['old_logo']:'';
	
	$uploaddir = 'uploads/organizations/';
	
	if(isset($_FILES['logo']['name']) && $_FILES['logo']['name'] != '')
	{			
		$docarr = explode(".", $_FILES['logo']['name']);						
		$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
		$upload = $uploaddir . $docfile;
		if(move_uploaded_file($_FILES['logo']['tmp_name'], $upload))
		{
			@unlink("uploads/organizations/$oldImgfile");			
			$oldDocfile = $docfile;
		}	
	}
    //===============================================================
    //============== Add record =====================================
    //===============================================================
    if(isset($_POST['action']) && $_POST['action'] =='save')
    { 		
		$insQry = "INSERT INTO ".TBL_ORGANIZATIONS." SET 
        name='".addslashes($name)."',
		logo='".addslashes($oldDocfile)."',		
        address='".addslashes($address)."',
		contact_no='".addslashes($contact_no)."',
        contact_person='".addslashes($contact_person)."',
        email='".addslashes($email)."',
		date_added='".$toDay."' ";        
        $response = $con->insertRecords($insQry, __FILE__,__LINE__);
		$orgid = mysqli_insert_id($dbcon);
        
        if($response===TRUE )
		{
			$return_arr[] = array("message" => 'ok');
		}
        else
            $return_arr[] = array("message" => 'err');
                    
        echo json_encode($return_arr);
    }
	//===============================================================
    //============== Update S1 ======================================
    //===============================================================
    elseif(isset($_POST['action']) && $_POST['action'] =='update')
    {	
		$insQry = "UPDATE ".TBL_ORGANIZATIONS." SET 
        name='".addslashes($name)."',
		logo='".addslashes($oldDocfile)."',	
        address='".addslashes($address)."',
		contact_no='".addslashes($contact_no)."',
        contact_person='".addslashes($contact_person)."',
        email='".addslashes($email)."',
		date_modified='".$toDay."' 		
		WHERE organization_id = '".$orgid."' LIMIT 1 ";        
        $response = $con->insertRecords($insQry, __FILE__,__LINE__);
        
        if($response===TRUE )
		{
			$return_arr[] = array("message" => 'ok');
		}	
        else
            $return_arr[] = array("message" => 'err');
                    
        echo json_encode($return_arr);
    }
?>