<?php 
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );
?>
<?php  
//print_r($_REQUEST); 
//print_r($_FILES);
//print_r($_SESSION);
foreach($_REQUEST as $key=>$value)
{
  $$key=$value;
}

//for($z=0;$z<250;$z++){
$predoc_id ="";
//$workgroup = "";//$_SESSION['workgroup'];
$content1 = htmlspecialchars($content1);
$content2 = htmlspecialchars($content2);
$content3 = htmlspecialchars($content3);

//Get book no.
$sqlBookNo = "Select MAX(book_number)+1 as MaxBookNo From idocument_main Where book_year = (".$_SESSION['bookregister_year'].")";

$query_BookNo=mysqli_query($connect,$sqlBookNo);
$result_BookNo=mysqli_fetch_array($query_BookNo);

$book_number = ($result_BookNo['MaxBookNo'] == NULL)?1:$result_BookNo['MaxBookNo'];
$book_year = $_SESSION['bookregister_year'];
$book_no = $book_number ."/". $book_year;
$book_date = date("Y-m-d");

if(!(isset($workgroup_txt))){
$workgroup_txt="";
}

$sql = "INSERT INTO `idocument_main` (
	`id`, 
	`workgroup`, 
	`workgroup_txt`, 
	`book_year`, 
	`book_number`, 
	`book_no`, 
	`book_date`, 
	`subject`, 
	`pre_doc_id`,
	`book_to`, 
	`content1`, 
	`content2`, 
	`content3`, 
	`officer`, 
	`officer_name`, 
	`officer_position`,
	`book_status`,
	`book_type`)
	 VALUES 
	 (NULL, 
	 	'$workgroup', 
	 	'$workgroup_txt', 
	 	'$book_year', 
	 	'$book_number', 
	 	'$book_no', 
	 	'$book_date', 
	 	'$subject', 
	 	'$predoc_id', 
	 	'$book_to', 
	 	'$content1', 
	 	'$content2', 
	 	'$content3', 
	 	'$officer', 
	 	'$officer_name', 
	 	'$officer_position',
	 	'0',
	 	'1');";
//echo $bookto_id;
//echo "<br />".$sql;

if($bookto_id!=""){
$dbquery = mysqli_query($connect,$sql);
}
else{
echo "<br>";
echo "<div align='center'>กรุณาเลือกบุคคลพิจารณา/เห็นชอบ</div>";
exit();
}

//$dbquery = mysqli_query($connect,$sql);
if ($dbquery){
	$last_id = mysqli_insert_id($connect);
	$sqlSento = "Insert INTO idocument_sendto(document_id, rec_id, person_id,document_from) 
	Values('$last_id', md5('$last_id'), '$bookto_id', '$officer');";
	//echo $sqlSento;
	//mysqli_query($connect, $sqlSento);
	mysqli_query($connect, $sqlSento);
//echo $sqlSento;
//Upload file Ref Doc.
			$target_dir = "./modules/".$_GET['option']."/upload_files/";
			$file_no = 0;
			$x=1;
         	//for($j=0;$j<count($_FILES['UploadedFile']['tmp_name']);$j++) {
         	for($j=0;$j<count($UploadedFileComment);$j++) {
         		
         		$commentFile = $UploadedFileComment[$j];

				if(!empty($_FILES['UploadedFile']['tmp_name'][$j])) {
					++$file_no;
					$target_file = $target_dir . basename($_FILES["UploadedFile"]["name"][$j]);
					$uploadOk = 1;
					$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
					
					$rename_file = $target_dir . $last_id . '-ref-' . round(microtime(true)) . '_'.$file_no.'.'. strtolower($imageFileType);

			    if (move_uploaded_file($_FILES["UploadedFile"]["tmp_name"][$j], $rename_file)) {
			        //echo "The file ". basename( $_FILES["UploadedFile"]["name"][$j]). " has been uploaded.";
			    }

				}else{
					$rename_file = "";
					$imageFileType = "";
				}	
				
				if($_FILES["UploadedFile"]["name"][$j]){
							if($commentFile==""){
							$doc="เอกสารอ้างถึง_$x";
							$commentFile=$doc;
							$x=$x+1;
							}
					}	
				
				if($commentFile<>""){
			        $sql = "INSERT INTO idocument_files( document_id, file_name,file_des,filetype, docType) VALUE( $last_id,'$rename_file', '$commentFile', '$imageFileType', 'ref')";			       
					$result = mysqli_query($connect,$sql);
				}
			        //echo $sql."<br>";
			}//for	
//Upload file
//Upload file Attach Doc.
			$target_dir = "./modules/".$_GET['option']."/upload_files/";
			$file_no = 0;
			$x=1;
         	for($j=0;$j<count($UploadedFileComment2);$j++) {
         		
         		$commentFile = $UploadedFileComment2[$j];

				if(!empty($_FILES['UploadedFile2']['tmp_name'][$j])) {
					++$file_no;
					$target_file = $target_dir . basename($_FILES["UploadedFile2"]["name"][$j]);
					$uploadOk = 1;
					$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
					
					$rename_file = $target_dir . $last_id . '-attach-' . round(microtime(true)) . '_'.$file_no.'.'. strtolower($imageFileType);

			    if (move_uploaded_file($_FILES["UploadedFile2"]["tmp_name"][$j], $rename_file)) {
			        //echo "The file ". basename( $_FILES["UploadedFile"]["name"][$j]). " has been uploaded.";
			    }

				}else{
					$rename_file = "";
					$imageFileType = "";
				}			
				
				if($_FILES["UploadedFile2"]["name"][$j]){
						if($commentFile==""){
						$doc2="สิ่งที่ส่งมาด้วย_$x";
						$commentFile=$doc2;
						$x=$x+1;
						}
				}
								
				if($commentFile<>""){
			        $sql = "INSERT INTO idocument_files(document_id, file_name,file_des,filetype, docType) VALUE($last_id,'$rename_file', '$commentFile', '$imageFileType', 'attach')";			       
					$result = mysqli_query($connect,$sql);
			        //echo $sql."<br>";
				}
			}//for	
//Upload file
}

//}
//echo "<script language='javascript'>window.location.href = '?option=". $option ."&task=view'</script>";
echo "<script language='javascript'>window.location.href = '?option=". $option ."&task=detail&id=".$last_id."'</script>";
?>