<?php 
include("../config.php");
if(!empty($_POST)){
	$code          		 = mysqli_real_escape_string($conn, $_POST['code']);
	$name          		 = mysqli_real_escape_string($conn, $_POST['name']);
	$mobile       		 = mysqli_real_escape_string($conn, $_POST['mobile']);
	$email       		 = mysqli_real_escape_string($conn, $_POST['email']);
	$b_group         	 = mysqli_real_escape_string($conn, $_POST['b_group']);
	$e_contact           = mysqli_real_escape_string($conn, $_POST['e_contact']);
	$address             = mysqli_real_escape_string($conn, $_POST['address']);
	$status          	 = mysqli_real_escape_string($conn, $_POST['status']);
	$file_img       	 = mysqli_real_escape_string($conn, strtoupper($_POST['file_img']));
	$company_id          = $_SESSION['company_id'];
	$branch_id           = $_SESSION['branch_id'];
	$created_datetime    = date('Y-m-d H:i:s');
	$created_id          = $_SESSION['user_detail']['id'];
	$updated_datetime    = date('Y-m-d H:i:s');
	$updated_id          = $_SESSION['user_detail']['id'];
	$attachment = NULL;
	if(NULL != $_FILES) {
		$attachment = $_FILES;
	}

	if(isset($attachment) && !empty($attachment)) {
		$file_extension = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);

		if (!in_array($file_extension, $config['attachment']['valid_types'])) {
			echo json_encode( array('response' => 2, 'msg' => 'The file type is not allowed to upload. Please try again !!', 'alert' => 1));
                    die();
		}else if ($config['attachment']['file_size'] < $_FILES["image"]["size"]) {
      		echo json_encode( array('response' => 2, 'msg' => 'The file is to big to upload. !!', 'alert' => 1));
            die();
        }
	}
	
	$image_name         = rand(1,99999).'.'.basename($_FILES['image']['name']);
	$upload_file_path   = $config['attachment']['emp_folder_path'] . $image_name;
	
    @move_uploaded_file($attachment["image"]["tmp_name"], $upload_file_path);

	if($_POST["id"] != ''){
  	 	$type               = 'employee_add_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);

  	 	$insert = "UPDATE `emp_mst` SET `code`='".$code."',`name`='".$name."',`mobile`='".$mobile."',`email`='".$email."',`b_group`='".$b_group."',`e_contact`='".$e_contact."',`address`='".$address."',`file_img`='".$image_name."',`status`='".$status."',`branch_id`='".$branch_id."',`company_id`='".$company_id."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE `id`='".$_POST['id']."'";
  	 	$result = mysqli_query($conn,$insert) or die( mysqli_error($conn) );

  	 	$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
    	$acttivity_res = mysqli_query($conn,$activiti);

  }else{
	$type               = 'employee_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	
	$insert = "INSERT INTO `emp_mst`(`code`, `name`, `mobile`, `email`, `b_group`, `e_contact`, `address`, `file_img`, `status`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$name."','".$mobile."','".$email."','".$b_group."','".$e_contact."','".$address."','".$image_name."','".$status."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";

	$result = mysqli_query($conn,$insert);	

	$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
	$acttivity_res = mysqli_query($conn,$activiti);

	
  }
  echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=emp_mst/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=emp_mst/add.php') );die();
} 