<?php
if(isset($_SESSION['tmp_doc_pop'])) unset($_SESSION['tmp_doc_pop']);

if($_GET['action'] == "edit" && $_GET['m_id'] != ""){
	$m_id = mysql_real_escape_string(stripslashes($_GET['m_id']));
	$doc_id = (int)mysql_real_escape_string(stripslashes($_GET['doc_id']));
	$user_id = $_SESSION['sys_id']; 
	$orgunitid = mysql_real_escape_string(stripslashes($_GET['orgunitid']));
	$status = mysql_real_escape_string(stripslashes($_GET['status']));
	$start = (int)$_GET['start'];
	$type_io = (int)$_GET['type_io'] !=0 ? $_GET['type_io']: "";
	mysql_query("UPDATE  tbl_in_out_document SET owner = '35' WHERE doc_id = '$doc_id'");
	$bool = mysql_query("UPDATE tbl_manage_doc SET receive_status = 2, sysuserid = '$user_id' WHERE m_id = '$m_id' AND orgunitid = '$orgunitid' ");
	if($bool){
		header("Location: ?d=document/list_docin/addfile_doc&orgunitid=$orgunitid&status=$status&type_io=$type_io&start=$start");
	}else{
		$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class = 'error'>ຢືນຢັນການຮັບ ລົ້ມເຫຼວ, ກະລຸນາລອງໃໝ່....!</span>\"</script>";
	} 
	
}

if (isset($_POST['type'])) {
	for ($i = 0; $i < count($_POST['type']); $i++) {	
				
		$doc_id = mysql_real_escape_string(stripslashes($_POST['id'][$i]));
		//$orgunitid = mysql_real_escape_string(stripslashes($_POST['orgunitid'][$i]));
		$send = mysql_real_escape_string(stripslashes($_POST['send_'][0]));
	
		if ($_POST['id'][$i] != "") {
			//echo $_POST['type'][$i];
			
			switch ($_POST['type'][$i]) {				
				case "added" :	
					
					
				break;
				
				case "edited":
					if($send=="checked"){
						$bool = mysql_query("UPDATE tbl_in_out_document SET m_step='counter_return', owner='31' WHERE doc_id='$doc_id' ");
						$rs = mysql_query("SELECT * FROM orgunit WHERE unittypeid=5");
						while($rss = mysql_fetch_array($rs, MYSQL_ASSOC)){
							sql_execute("DELETE FROM tbl_manage_doc WHERE doc_id = '$h_doc_id' AND orgunitid = '$rss[orgunitid]' ");
						}
					}
					if($bool){
						mysql_query("UPDATE tbl_manage_doc SET orgunitid='31', receive_status=1 WHERE doc_id='$doc_id' AND orgunitid='35' ");
						$err = "<script>document.getElementById('msgwar').innerHTML = \"<span>ແກ້ໄຂຂໍ້ມູນສໍາເລັດ....! </span>\"</script>";
						/*-------------- code check to send mail enter here --------------*/
						
						
						/*--------------- end send mail*/
					}else{
						$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ແກ້ໄຂຂໍ້ມູນລົ້ມເຫຼວ,​ ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
					}
				break;
				
			}
			//if($bool) header("Location: ?d=document/docin/docin&start=".$_GET['start']);
		}
	}
}

/*------------------ */
if($_GET['action'] == "del" && $_GET['doc_id'] != "" && $_SESSION['sys_type']=="counter" ){
	$doc_id = mysql_real_escape_string(stripslashes($_GET['doc_id']));
	$start = $_GET['start'];
	
	$rs=mysql_query("DELETE FROM tbl_manage_doc WHERE doc_id = $doc_id ");
	$rs=mysql_query("DELETE FROM tbl_in_out_document WHERE doc_id = $doc_id AND doc_status=1");

	if($rs){
		header("Location: ?d=document/list_docin/addfile_doc&orgunitid=35&status=".$_GET['status']."&start=$start");
	}else{
		echo "<h2>ERROR Delete</h2>";
	}
}