<?php
session_start();
require_once '../../Server/connection.php';
include('../includes/session.php');
if($_POST){
	
	
  $status = $_POST['status'];
  
  if($status=="Accept"){
	  
  $client_id = $_POST['editCategoriesId'];
  $assign_id = $_POST['editAssignStatusId'];
  $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;
  
   $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");
  
   $sql22 = "Select * from client_files";
  $result22 = $conn->query($sql22);
  $num22 = mysqli_num_rows($result22);
  
  if($num22>=0 && $num22<9){
  $file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BRVSN"."/".$y."/".'000'.$file_No;  
    }else if($num22>=9 && $num22<99){
   $file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BRVSN"."/".$y."/".'00'.$file_No;  	 
	 }else if($num22>=99 && $num22<999){
   $file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BRVSN"."/".$y."/".'0'.$file_No;  	 
	 }else{
	$file_No = $num22 + 1;
  $y = date("Y");
  $fileNo = "BRVSN"."/".$y."/".$file_No;  	 
	 }
	 
	 $fileSt = 0;
	 $fileCloseDate = '0000-00-00';
	 
    $sql = "INSERT INTO client_files (filetype_id,client_id,file_no,entryId,entryName_id,b_lNo,tra_RefNo,consignee,vessel_flight,sline,eta,dest_id,goods,file_openDate,file_closeDate,status,clientStatus) VALUES ('$filetype_id','$client_id','$fileNo','$entryId','$entryNameId','$b_lNo','$tra_RefNo','$consignee','$vesselFlight','$sline','$eta','$destination','$goods','$fileOpenDate','$fileCloseDate','$status','0')";			
	$result = $conn->query($sql);
	
	if($result){
	$file_id = $conn->insert_id;
	
	$sql1 = "INSERT INTO assignstatus (client_id,file_id,assigned_to,assigned_by,permission_id,status,action,remarks) VALUES ('$client_id','$file_id','$fowardedto_id','$user_id','$perm_id','0','$action','$remarks')";			
    $result1 = $conn->query($sql1);
	
	$sql2 = "UPDATE assignstatus SET status = '1' WHERE id = {$assign_id}";		

	$result2 = $conn->query($sql2);
	
	
	if($result1&&$result2){
	
	$_SESSION['success_message'] = "Customer File has been Opened and fowarded successfully";
                       header("Location: ../createFile.php?i=$client_id&assign_id=$assign_id");
                             exit();
		
	}
	}
  }else{
	  
   $client_id = $_POST['client_id'];
   $assign_id = $_POST['assign_id'];
   $returnedto_id = $_POST['returnedto_id'];
   $remarks =  addslashes($_POST['remarks']);
   $perm_id = 84;
   $status = 0;
   $action = 'returned';
  
   
   $sql = "INSERT INTO assignstatus (client_id,assigned_to,assigned_by,permission_id,status,action,remarks) VALUES ('$client_id','$returnedto_id','$user_id','$perm_id','$status','$action','$remarks')";			
   $result = $conn->query($sql);
   
   $sql2 = "UPDATE assignstatus SET status = '1' WHERE id = {$assign_id}";		

   $result2 = $conn->query($sql2);
   
   if($result&&$result2){
   $_SESSION['success_message'] = "Customer details have been returned successfully";
                       header("Location: ../createFile.php?i=$client_id&assign_id=$assign_id");
                             exit();
   }
   
   
	  
  }
  
 

	
}