<?php 	
require_once '../../Server/connection.php';
$sql = "SELECT staff_contracts.contract_id,staff_contracts.contract_start,staff_contracts.contract_end,staff_contracts.cont_dur_id,staff.firstname,staff.lastname,staff.staffId,designation.des_short_name FROM staff,staff_contracts,designation where staff.staff_id=staff_contracts.staff_id and designation.des_id=staff.des_id order by staff_contracts.contract_end asc";

$result = $conn->query($sql);

$output = array('data' => array());

if($result->num_rows > 0) { 

 // $row = $result->fetch_array();
 $active = ""; 
 $x = 1;

 while($row = $result->fetch_array()){
 	$categoriesId = $row['contract_id'];
	$contract_start = $row['contract_start'];
	$contract_end = $row['contract_end'];
	$cont_dur_id = $row['cont_dur_id'];
	$firstname = $row['firstname'];
	$lastname = $row['lastname'];
	$dshortName = $row['des_short_name'];
	$staffId = $row['staffId'];
	
	$currentDate = date("Y-m-d");
	$contStart = date('d-m-Y', strtotime($contract_start));
    $contEnd = date('d-m-Y', strtotime($contract_end));
	
	
    $staffName = $firstname." ".$lastname;
	
	   $d1 = strtotime("$contStart");
                             $d2 = strtotime("$contEnd");
                             $min_date = min($d1, $d2);
                             $max_date = max($d1, $d2);
                                    $i = 0;
				while (($min_date = strtotime("+1 YEAR", $min_date)) <= $max_date) {
                                   $i++;
                                      }
									  $Year = $i;
									  $age=$Year;
									  
							if($age>0) {
									$age=$Year; 
										 
									 }else{
										 
						$d11 = strtotime("$contStart");
                             $d22 = strtotime("$contEnd");
                             $min_date = min($d11, $d22);
                             $max_date = max($d11, $d22);
                                    $i = 0;
				while (($min_date = strtotime("+1 MONTH", $min_date)) <= $max_date) {
                                   $i++;
                                      }					  
										  
								$Month = $i;		  
								$age1 = $Month.' '.'Months';
                                  
                                  if($Month>0){
									 $age =  $age1;
								      }else{
									$d111 = strtotime("$contStart");
                             $d222 = strtotime("$contEnd");
                             $min_date = min($d111, $d222);
                             $max_date = max($d111, $d222);
                                    $i = 0;
				while (($min_date = strtotime("+1 DAY", $min_date)) <= $max_date) {
                                   $i++;
                                      }	

                                $day = $i;		  
								$age1 = $day.' '.'Days';									  
								$age =  $age1;	  
									  }								  
										 
									 }
									 
				if($contract_end>$currentDate){
					
					
		$d1m = strtotime("$currentDate");
                             $d2m = strtotime("$contEnd");
                             $mindate = min($d1m, $d2m);
                             $maxdate = max($d1m, $d2m);
                                    $i = 0;
				while (($mindate = strtotime("+1 YEAR", $mindate)) <= $maxdate) {
                                   $i++;
                                      }
									  $Ye_ar = $i;
									  $ag_e=$Ye_ar;
									  
							if($ag_e>0) {
									$ag_e=$Ye_ar; 
										 
									 }else{
										 
						$d11m = strtotime("$currentDate");
                             $d22m = strtotime("$contEnd");
                             $mindate = min($d11m, $d22m);
                             $maxdate = max($d11m, $d22m);
                                    $i = 0;
				while (($mindate = strtotime("+1 MONTH", $mindate)) <= $maxdate) {
                                   $i++;
                                      }					  
										  
								$Mon_th = $i;		  
								$ag_e1 = $Mon_th.' '.'Months';
                                  
                                  if($Mon_th>0){
									 $ag_e =  $ag_e1;
								      }else{
									$d111m = strtotime("$currentDate");
                             $d222m = strtotime("$contEnd");
                             $mindate = min($d111m, $d222m);
                             $maxdate = max($d111m, $d222m);
                                    $i = 0;
				while (($mindate = strtotime("+1 DAY", $mindate)) <= $maxdate) {
                                   $i++;
                                      }	

                                $da_y = $i;		  
								$ag_e1 = $da_y.' '.'Days';									  
								$ag_e =  $ag_e1;	  
									  }								  
										 
									 }			
					
					
					
					
		$st = $ag_e." "."Left";
	}else if($contract_end==$currentDate){
		$st = "Contract End";
	}else{
		$st = "Contract End";
	}
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="viewDocs.php?i='.$clientId.'&assign_id='.$assignId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal" id="editCategoriesModalBtn" data-target="#editCategoriesModal" onclick="editCategories('.$categoriesId.')"> Manage </a>
	</div>';
	
	$button3 = '<!-- Single button -->
	<div class="btn-group">
	<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#removeCategoriesModal" id="removeCategoriesModalBtn" onclick="removeCategories('.$categoriesId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	</div>';
	
	$button4 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editContract.php?i='.$categoriesId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-edit"></i></center></a>
	</div>';
	
	$button5 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="pdf_Contfile.php?i='.$categoriesId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	

 	$output['data'][] = array( 

	    $x,
 		$staffName,
        $staffId, 
        $dshortName, 		
 		$contStart,
        $contEnd,
		$cont_dur_id,
		$st,
        $button2,
        $button3,
        $button4,
  		$button5
 		); 
        
		$x++;
		
 } // /while 

}// if num_rows

$conn->close();

echo json_encode($output);