<?php

if (isset($_POST['type'])) {

	$h_doc_id = mysql_real_escape_string(stripslashes($_POST['h_doc_id']));
	$m_id = mysql_real_escape_string(stripslashes($_POST['h_m_id'][$i]));
	$now = date('Y-m-d G:i:s');
	$today = date('Ymd');
	/*-------------------------- document info -------------*/
	$doc_code = mysql_real_escape_string(stripslashes($_POST['doc_code'])); 
	/*--------- emp ---------------*/
	$user_id = $_SESSION['sys_id']; 
	
	/*------------- Loop to check Enter data---------------*/
	for ($i = 0; $i < count($_POST['type']); $i++) {	
				
		$orgunitid = mysql_real_escape_string(stripslashes($_POST['id'][$i]));
		$send = mysql_real_escape_string(stripslashes($_POST['orgunitid'][0]));
		//$emp_id = mysql_real_escape_string(stripslashes($_POST['id'][$i]));
		
		if ($_POST['id'][$i] != "") {
			//echo $_POST['type'][$i]	;
			
			switch ($_POST['type'][$i]) {				
				case "added" :	
										
				break;
				
				case "edited":
					/* edit as insert in manage_doc*/
					if($send=="checked"){
						if (mysql_num_rows(mysql_query("SELECT * FROM tbl_manage_doc WHERE doc_id = $h_doc_id AND orgunitid='$orgunitid'")) == 0) {
							$rs = mysql_query("SELECT * FROM orgunit WHERE unittypeid=5");
							//$rs = mysql_query("DELETE FROM tbl_manage_doc WHERE doc_id = '$h_doc_id' AND orgunitid = '$orgunitid' ");
							
							while($rss = mysql_fetch_array($rs, MYSQL_ASSOC)){
								mysql_query("DELETE FROM tbl_manage_doc WHERE doc_id = '$h_doc_id' AND orgunitid = '$rss[orgunitid]' AND orgunitid <> '$orgunitid' ");
							}
							$sql_m = "INSERT INTO tbl_manage_doc(doc_id, orgunitid, receive_status, sysuserid) VALUES('$h_doc_id', '$orgunitid', 1, $user_id) "; 
							$boolean_in = mysql_query($sql_m);
						}else{
							while($rss = mysql_fetch_array($rs, MYSQL_ASSOC)){
								mysql_query("DELETE FROM tbl_manage_doc WHERE doc_id = '$h_doc_id' AND orgunitid = '$rss[orgunitid]' AND orgunitid <> '$orgunitid' ");
							}
							$boolean_in = mysql_query($sql_m);
							sql_execute("INSERT INTO tbl_manage_doc(doc_id, orgunitid, receive_status, sysuserid) VALUES('$h_doc_id', '$orgunitid', 1, $user_id) "); 
						}
					}else{
						//$sql_m = "DELETE FROM tbl_manage_doc WHERE doc_id = '$h_doc_id' AND orgunitid = '$orgunitid' ";
						//$boolean_in = mysql_query($sql_m);
						//sql_execute("INSERT INTO tbl_manage_doc(doc_id, orgunitid, receive_status, sysuserid) VALUES('$h_doc_id', '$orgunitid', 1, $user_id) "); 
					}
						
				break;
				
			}
		}else{
			// $err = "<script>alert('ບໍ່ສາມາດບັນທຶກ, ກະລຸນາເລືອກຜູ້ຮັບອຸປະພ້ອມ...!')</script>";
		}
	}
	// header after save complete
	if($boolean_in==true){
	/*=========== check to send mail ====================*/
		if($h_doc_id !=""){
			//send_mail($h_doc_id, ((int)$_GET['orgunitid'])); // on local
			//send_mail_server($h_doc_id, $_GET['orgunitid']); // on server 
		}
		// after kk
		$status = mysql_real_escape_string(stripslashes($_GET['status']));
		$orgunitid = (int)mysql_real_escape_string(stripslashes($_GET['orgunitid']));
		//echo $sql_m;
		//header("Location: ?d=document/list_docin/addfile_doc&orgunitid=$orgunitid&status=$status");
	}else{
		$err = "<script>document.getElementById('errEq').innerHTML = \"<span class='errChk' >ບັນທຶກລົ້ມເຫຼວ, ກະລຸນາລອງໃໝ່</span>\"</script>";
	}
}else{
	$err = "<script>document.getElementById('errEq').innerHTML = \"<span class='errChk' >(ກະລຸນາເລືອກຢ່າງໜ້ອຍ 1 ທ່ານ)</span>\"</script>";
}


?>