<?php
session_start();
require_once '../../Server/connection.php';
$valid['success'] = array('success' => false, 'messages' => array());
include('../includes/session.php');
$sql1 = "SELECT des_id FROM staff where staff_id='$staff_id'";
$result1 = $conn->query($sql1);
while($row1 = $result1->fetch_array()){
$des_id = $row1['des_id'];
		         }

if($_POST){
  $status = $_POST['status'];
  
  if($status=="Accept"){
  $assign_id = $_POST['AssignStatusId'];
  $fowardedto_id = $_POST['fowardedto_id'];
  $entryId  = $_POST['entryId'];
  $entryNameId = $_POST['entryNameId'];
  $b_lNo  = $_POST['b_lNo'];
  $tra_RefNo = $_POST['tra_RefNo'];
  $consignee  = $_POST['consignee'];
  $goods  = $_POST['goods'];
  $vesselFlight  = $_POST['vesselFlight'];
  $sline  = $_POST['sline'];
  $eTa  = $_POST['eta'];
  $eta = date('Y-m-d', strtotime($eTa));
  $destination  = $_POST['dest_id'];
  $remarks  = addslashes($_POST['remarks']);
  $status = 1;
  $assignedBy_role = 'DCO';
  $assignedTo_role = 'ACCOUNTANT';
  //$upl_status = $_POST['upl_status'];
  
  
  
  $sql2 = "SELECT staff.des_id FROM staff,users where users.user_id='$fowardedto_id' and staff.staff_id=users.staff_id";
  $result2 = $conn->query($sql2);
  while($row2 = $result2->fetch_array()){
  $desId = $row2['des_id'];
		         }
	
  
  $sql4 = "SELECT client_id FROM assignstatus where id='$assign_id'";
  $result4 = $conn->query($sql4);
  while($row4 = $result4->fetch_array()){
  $client_id = $row4['client_id'];
		         }
  
   $perm_id = 85;
  
   $action = 'fowarded';
  
  if($entryId==1){
  $filetype_id = 2; 
  }else if($entryId==2){
  $filetype_id = 1;   
  }else{
  $filetype_id = 3;    
  }
  
  $fileOpenDate = date("Y-m-d");
  $filegenYear = date("Y");
  
  $sql22 = "Select * from client_files where filegen_year='$filegenYear'";
  $result22 = $conn->query($sql22);
  $num22 = mysqli_num_rows($result22);
  
  if($num22>=0 && $num22<9){
  $file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BVL"."/".$y."/".'000'.$file_No;  
    }else if($num22>=9 && $num22<99){
   $file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BVL"."/".$y."/".'00'.$file_No;  	 
	 }else if($num22>=99 && $num22<999){
   $file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BVL"."/".$y."/".'0'.$file_No;  	 
	 }else{
	$file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BVL"."/".$y."/".$file_No;  	 
	 }
	 
	 $fileSt = 0;
	 $fileCloseDate = '0000-00-00';
	 
    $sql = "INSERT INTO client_files (filetype_id,client_id,file_no,filegen_year,entryId,entryName_id,b_lNo,tra_RefNo,consignee,vessel_flight,sline,eta,dest_id,goods,file_openDate,file_closeDate,status,fileStatus,clientStatus) VALUES ('$filetype_id','$client_id','$fileNo','$filegenYear','$entryId','$entryNameId','$b_lNo','$tra_RefNo','$consignee','$vesselFlight','$sline','$eta','$destination','$goods','$fileOpenDate','$fileCloseDate','$status','$fileSt','0')";			
	$result = $conn->query($sql);
	
	if($result){
	$file_id = $conn->insert_id;
	
	$sql20 = "UPDATE assignstatus SET status = '1' WHERE id = {$assign_id}";		

	$result20 = $conn->query($sql20);
	
	if($result20){
		
	$sql10 = "INSERT INTO assignstatus (client_id,file_id,assigned_to,assignedToDes_id,assignedTo_role,assigned_by,assignedByDes_id,assignedBy_role,permission_id,status,action,remarks) VALUES ('$client_id','$file_id','$fowardedto_id','$desId','$assignedTo_role','$user_id','$des_id','$assignedBy_role','$perm_id','0','$action','$remarks')";			
    $result10 = $conn->query($sql10);
		
	}
	
	if($result10) {
		
	 	$valid['success'] = true;
		$valid['messages'] = "File Opened Successfully";	
	} else {
	 	$valid['success'] = false;
	 	$valid['messages'] = "Error while Opening File";
	}
	
	$conn->close();

	echo json_encode($valid);
	
	}
  }else{	  
   $assign_id = $_POST['AssignStatusId'];
   $fowardedto_id = $_POST['fowardedto_id1'];
   $remarks =  addslashes($_POST['remarks1']);
   $perm_id = 84;
   $status = 0;
   $action = 'returned';
   
   $assignedBy_role1 = 'DCO';
   $assignedTo_role1 = 'CSO';
  
   $sql2 = "SELECT staff.des_id FROM staff,users where users.user_id='$fowardedto_id' and staff.staff_id=users.staff_id";
  $result2 = $conn->query($sql2);
  while($row2 = $result2->fetch_array()){
  $desId = $row2['des_id'];
		         }
  
 
    $sql4 = "SELECT client_id FROM assignstatus where id='$assign_id'";
  $result4 = $conn->query($sql4);
  while($row4 = $result4->fetch_array()){
  $client_id = $row4['client_id'];
		         }
   
   
   
   $sql2 = "UPDATE assignstatus SET status = '2' WHERE id = {$assign_id}";		

   $result2 = $conn->query($sql2);
   
   if($result2){
	   
	$sql1 = "INSERT INTO assignstatus (client_id,assigned_to,assignedToDes_id,assignedTo_role,assigned_by,assignedByDes_id,assignedBy_role,permission_id,status,action,remarks) VALUES ('$client_id','$fowardedto_id','$desId','$assignedTo_role1','$user_id','$des_id','$assignedBy_role1','$perm_id','0','$action','$remarks')";			
    $result1 = $conn->query($sql1);
	   
   }
   
   if($result1) {
	 	$valid['success'] = true;
		$valid['messages'] = "Successfully Rejected";	
	} else {
	 	$valid['success'] = false;
	 	$valid['messages'] = "Error while Rejecting";
	}
	
	$conn->close();

	echo json_encode($valid);
   
  
	  
  }
 	
}