<?php
//require_once ("m_staff.php");
/*
$action = isset($_GET['action']) ? $_GET['action'] : '';
switch ($action) {
  case 'add' :
    maxId();
    addStaff();
    break;
      
  case 'edit' :
   	editStaff();
    break;

  case 'view' :
   	viewStaffInfo();
    break;
        
  default :
    selectStaff();    	
    // if action is not defined or unknown
    // move to main category page
    header('Location: index.php');       
}
*/
if(isset($_GET['parent_id']) && $_GET['parent_id'] != "" ){
	//maxId();
    //addStaff();
} 
 /*---------- check to insert into emp_personal --------------*/
if(isset($_POST['btAddSt'])){
	$emp_code = mysql_real_escape_string(stripslashes($_POST['emp_code']));
	$name = mysql_real_escape_string(stripslashes($_POST['name']));
	$surname = mysql_real_escape_string(stripslashes($_POST['surname']));
	$name_eng = mysql_real_escape_string(stripslashes($_POST['name_eng']));
	$surname_eng = mysql_real_escape_string(stripslashes($_POST['surname_eng']));
	$status = mysql_real_escape_string(stripslashes($_POST['status']));
	$gender = mysql_real_escape_string(stripslashes($_POST['gender']));
	$tribe = mysql_real_escape_string(stripslashes($_POST['tribe']));
	$religion = mysql_real_escape_string(stripslashes($_POST['religion']));
	$child_n = mysql_real_escape_string(stripslashes($_POST['child_n']));
	$cal_code = mysql_real_escape_string(stripslashes($_POST['cal_code']));
	$hiredate = mysql_real_escape_string(stripslashes($_POST['hiredate'])); // assign on table employment
	$birth_date = mysql_real_escape_string(stripslashes($_POST['birth_date'])); 
	$born_add = mysql_real_escape_string(stripslashes($_POST['born_add']));
	$address = mysql_real_escape_string(stripslashes($_POST['address']));
	$description = mysql_real_escape_string(stripslashes($_POST['description']));
	$position = mysql_real_escape_string(stripslashes($_POST['position'])); // assign on table employment
	//$orgunitid = mysql_real_escape_string(stripslashes($_POST['orgunitid'])); // assign on table employment
	$respon = mysql_real_escape_string(stripslashes($_POST['respon']));
	$edu = mysql_real_escape_string(stripslashes($_POST['edu']));
	$edu_place = mysql_real_escape_string(stripslashes($_POST['edu_place']));
	$old_specialist = mysql_real_escape_string(stripslashes($_POST['old_specialist']));
	$pe_id = mysql_real_escape_string(stripslashes($_POST['pe_id']));
	$pass_id = mysql_real_escape_string(stripslashes($_POST['pass_id']));
	$email_address = mysql_real_escape_string(stripslashes($_POST['email_address']));
	$tel = mysql_real_escape_string(stripslashes($_POST['tel']));
	$hobby = mysql_real_escape_string(stripslashes($_POST['hobby']));
	$free_t = mysql_real_escape_string(stripslashes($_POST['free_t']));
	$remark = mysql_real_escape_string(stripslashes($_POST['remark']));
	$party_date = mysql_real_escape_string(stripslashes($_POST['party_date']));
	$young_date = mysql_real_escape_string(stripslashes($_POST['young_date']));
	$deputy_party_date = mysql_real_escape_string(stripslashes($_POST['deputy_party_date']));
	$labor_date = mysql_real_escape_string(stripslashes($_POST['labor_date']));
	$women_date = mysql_real_escape_string(stripslashes($_POST['women_date']));
	$record_date = mysql_real_escape_string(stripslashes($_POST['record_date']));
	/*---- wife or husband  ---*/
	$status_per = mysql_real_escape_string(stripslashes($_POST['status_per']));
	$status_w = mysql_real_escape_string(stripslashes($_POST['status_w']));
	$status_birthdate = mysql_real_escape_string(stripslashes($_POST['status_birthdate']));
	$status_w_place = mysql_real_escape_string(stripslashes($_POST['status_w_place']));
	
	if (mysql_num_rows(mysql_query("SELECT * FROM emp_personal WHERE  emp_code = '$emp_code' ")) == 0) {
		/*-------------------------- upload file ----------------------------------*/
		$target = basename( $_FILES['stPic']['name']);
		if($target !=""){ 
			if (!defined('UPLOADDIR')) define('UPLOADDIR', (dirname(__FILE__) ."/../../staff/images//staff") );
			if (is_uploaded_file($_FILES['stPic']['tmp_name'])) {
			  $File_tmpname = $_FILES["stPic"]["tmp_name"];
			  $pinfo = pathinfo($_FILES['stPic']['name']);
			  $File_extension = $pinfo['extension'];
			  $File_name = $_POST['name_eng']."_".$_POST['emp_code'].".".$File_extension;
			  //$File_type = $_FILES["stPic"]["type"];
			  //$File_size = $_FILES["stPic"]["size"];
			  move_uploaded_file($File_tmpname, (UPLOADDIR . "//" . $File_name));
			  // move_uploaded_file($File_tmpname, (UPLOADDIR . "//" . $File_name)) chmod("staff/images/staff/".$File_name, 0755);  // on linux
			}
		}
		$sql = "INSERT INTO emp_personal(emp_code, name, surname, name_eng, surname_eng, status, gender, tribe, religion, child_n,
			cal_code, birth_date, born_add,address, description, respon, edu,edu_place, old_specialist,pe_id,pass_id,
			email_address,tel,hobby,free_t, remark,party_date, young_date,deputy_party_date, labor_date,women_date,record_date,
			status_per,status_w,status_birthdate,status_w_place, picture) 
			VALUES('$emp_code', '$name', '$surname', '$name_eng', '$surname_eng','$status','$gender','$tribe', '$religion', '$child_n',
			'$cal_code', '$birth_date', '$born_add','$address', '$description', '$respon', '$edu','$edu_place', '$old_specialist','$pe_id','$pass_id',
			'$email_address','$tel','$hobby','$free_t', '$remark','$party_date', '$young_date','$deputy_party_date', '$labor_date','$women_date','$record_date',
			'$status_per','$status_w','$status_birthdate','$status_w_place', '$File_name')";
		$bool = mysql_query($sql);	
		//echo $sql;
		if($bool){
			/*----- manage the employment and position of stafff -----*/
			$rs_emp = mysql_fetch_array(mysql_query("SELECT MAX(emp_id) as mid FROM  emp_personal"));
			$emp_id = $rs_emp['mid'];
			$orgunitid = (int)$_POST['parent_id'];
			if (mysql_num_rows(mysql_query("SELECT * FROM emp_employment WHERE  emp_id = $emp_id ")) == 0) {
				$bool = mysql_query("INSERT INTO emp_employment(emp_id, orgunitid,position_id, hiredate, workingstatus) 
					VALUES($emp_id, $orgunitid, $position, '$hiredate', 1)");
			}
			header("Location: ?d=manage_emp");
		}else{
			$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ, ກະລຸນາລອງໃໝ່.....! </span>\"</script>";
		}
	}else{
		$err = "<script>document.getElementById('msgwar').innerHTML = \"<span class='error'>ລົ້ມເຫຼວ, ລະຫັດພະນັກງານນີ້ມີໃນຖານຂໍ້ມູນແລ້ວ.....!</span>\"</script>";
	}
	
}

















	

?>