<?php 
include("../config.php");
if(!empty($_POST)){
	$name          		 = mysqli_real_escape_string($conn, $_POST['name']);
	$address         	 = mysqli_real_escape_string($conn, $_POST['address']);
	$phone       		 = mysqli_real_escape_string($conn, $_POST['phone']);
	$email       		 = mysqli_real_escape_string($conn, $_POST['email']);
	$panno       		 = mysqli_real_escape_string($conn, $_POST['panno']);
	$gst       		 	 = mysqli_real_escape_string($conn, $_POST['gst']);
	$status       		 = mysqli_real_escape_string($conn, $_POST['status']);
	$account_no       	 = mysqli_real_escape_string($conn, $_POST['account_no']);
	$bank_name   		 = mysqli_real_escape_string($conn, $_POST['bank_name']);
	$ifsc_code       	 = mysqli_real_escape_string($conn, $_POST['ifsc_code']);
	$branch       		 = mysqli_real_escape_string($conn, $_POST['branch']);
	$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               = 'company_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);
  	 	$insert = "UPDATE company_mst SET name='$name',phone='$phone',email='$email',panno='$panno',gst='$gst',address='$address',account_no='$account_no',bank_name='$bank_name',branch='$branch',ifsc_code='$ifsc_code',status='$status',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               = 'company_add';
	$user_id            = $_SESSION['user_detail']['id'];
	$data               = serialize($_POST);
	$insert = "INSERT INTO `company_mst`(`name`, `phone`, `email`, `panno`, `gst`, `address`, `account_no`, `bank_name`, `branch`, `ifsc_code`, `status`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$name."','".$phone."','".$email."','".$panno."','".$gst."','".$address."','".$account_no."','".$bank_name."','".$branch."','".$ifsc_code."','".$status."','".$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=company_mst/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=company_mst/add.php') );die();
} 