<?php 
include("../config.php");
if(!empty($_POST)){
	$branch              = mysqli_real_escape_string($conn, $_POST['branch']);
    $company_id          = mysqli_real_escape_string($conn, $_POST['company_id']);
    $state               = mysqli_real_escape_string($conn, $_POST['state']);
    $city                = mysqli_real_escape_string($conn, $_POST['city']);
    $con_name            = mysqli_real_escape_string($conn, $_POST['con_name']);
    $mobile              = mysqli_real_escape_string($conn, $_POST['mobile']);
    $email               = mysqli_real_escape_string($conn, $_POST['email']);
    $pancard             = mysqli_real_escape_string($conn, $_POST['pancard']);
    $gst_in              = mysqli_real_escape_string($conn, $_POST['gst_in']);
    $address             = mysqli_real_escape_string($conn, $_POST['address']);
    $status              = mysqli_real_escape_string($conn, $_POST['status']);
    $prifix              = mysqli_real_escape_string($conn, $_POST['prifix']);
	$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               = 'branch_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert = "UPDATE branch_mst SET company_id='$company_id',branch='$branch',state='$state',city='$city',con_name='$con_name',mobile='$mobile',email='$email',pancard='$pancard',gst_in='$gst_in',address='$address',status='$status',prifix='$prifix',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               = 'branch_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	$insert = "INSERT INTO `branch_mst`(`company_id`, `branch`, `state`, `city`, `con_name`, `mobile`, `email`, `pancard`, `gst_in`, `address`, `status`, `prifix`, `created_datetime`, `created_id`, `updated_datetime`, `updated_id`) VALUES ('".$company_id."','".$branch."','".$state."','".$city."','".$con_name."','".$mobile."','".$email."','".$pancard."','".$gst_in."','".$address."','".$status."','".$prifix."','".$created_datetime."','".$created_id."','".$updated_datetime."','".$updated_id."')";
	$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=branch_mst/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=branch_mst/add.php') );die();
} 