<?php 
include("../config.php");
if(!empty($_POST)){
	$name         		= mysqli_real_escape_string($conn, strtoupper($_POST['name']));
	$category_id  		= mysqli_real_escape_string($conn, strtoupper($_POST['category_id']));
	$phone        		= mysqli_real_escape_string($conn, strtoupper($_POST['phone']));
	$mobile        		= mysqli_real_escape_string($conn, strtoupper($_POST['mobile']));
	$mobile2        	= mysqli_real_escape_string($conn, strtoupper($_POST['mobile2']));
	$fax        		= mysqli_real_escape_string($conn, strtoupper($_POST['fax']));
	$email        		= mysqli_real_escape_string($conn, strtoupper($_POST['email']));
	$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'];

	if($_POST["id"] != ''){
  	 	$type               = 'phone_book_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert = "UPDATE `phone_book` SET `name`='".$name."',`category_id`='".$category_id."',`phone`='".$phone."',`mobile`='".$mobile."',`mobile2`='".$mobile2."',`fax`='".$fax."',`email`='".$email."',`company_id`='".$company_id."',`branch_id`='".$branch_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               = 'phone_book_add';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
		$insert = "INSERT INTO `phone_book`(`name`, `category_id`, `phone`, `mobile`, `mobile2`, `fax`, `email`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$name."','".$category_id."','".$phone."','".$mobile."','".$mobile2."','".$fax."','".$email."','".$company_id."','".$branch_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=phone_book/add.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=phone_book/add.php') );die();
} 