<?php
//user_action.php
include('db_con.php');
include('function.php');
if (isset($_SESSION['user_id'])) {
		$user_type = $_SESSION['user_type'];
		$user_id = $_SESSION['user_id'];
		$user_name = $_SESSION['user_name'];
		$branch_id = $_SESSION['branch_id'];
		$branch_name = $_SESSION['branch_name'];
		$store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
	header('Location: login');
	die();
}
if(isset($_POST['btn_action']))
{
	if($_POST['btn_action'] == 'Add')
	{
		$dated = date('Y-m-d');
		 $sql = "SELECT IFNULL(max(acc_no),0) FROM accounts where acc_type = 'DEBTOR'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $acc_no = $statement->fetchColumn();
            if($acc_no < 1048001)
            {
            	$acc_no = 1048001;
            }
            $acc_no++;
         
		$sql = "select acc_name from accounts where acc_name = '".$_POST["acc_name"]."'";
		$statement = $connect->query($sql);
		$acc_name = $statement->fetchColumn();
		if($acc_name =='')
		{


		$query = "
		INSERT INTO accounts (acc_id,acc_no,dated,acc_name,acc_type,acc_parent,
		 address, phone, email, area, city, stn, ntn, notes, op_balance,user_id,status)
        VALUES (:acc_id, :acc_no, :dated, :acc_name, :acc_type, :acc_parent,
        :address, :phone, :email, :area, :city, :stn, :ntn, :notes, :op_balance, :user_id, :status)
    ";	
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':acc_id'       =>  $acc_no,
				':acc_no'       =>  $acc_no,
				':dated'       => $dated,
				':acc_name'       => trim($_POST["acc_name"]),
				':acc_type'       =>  'DEBTOR',
				':acc_parent'       =>  104,
				':address'       =>  trim($_POST["address"]),
				':phone'       =>  trim($_POST["phone"]),
				':email'       =>  trim($_POST["email"]),
				':area'       =>  trim($_POST["area"]),
				':city'       =>  trim($_POST["city"]),
				':stn'       =>  trim($_POST["stn"]),
				':ntn'       =>  trim($_POST["ntn"]),
				':notes'       =>  trim($_POST["notes"]),
				':op_balance'       =>  0,
				':user_id'       =>  $user_id,
				':status'       =>  'Active'
				
        
          )
		);	
		$result = $statement->fetchAll();
		if($result){
			echo 'New Customer Added';
		}
		
		}else{
			echo 'Account Name Already Exist!';
		}

	}
	if($_POST['btn_action'] == 'fetch_single')
	{
		$query = "
		SELECT * FROM accounts WHERE acc_no = :acc_no
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':acc_no'	=>	$_POST["acc_no"]
			)
		);
		$result = $statement->fetchAll();
		foreach($result as $row)
		{
			
			
		//	$output['dated']             = date('Y-m-d');
           $output['acc_name']          =  $row['acc_name'];
          $output['address']          =  $row['address'];
          $output['city']       =  $row["city"];
         // $output['country']       =  $row["country"];
          $output['area']       =  $row["area"];
          $output['phone']       =  $row["phone"];
          $output['email']       =  $row["email"];
      
          $output['stn']       =  $row["stn"];
          $output['ntn']       =  $row["ntn"];
     		 $output['notes']       =  $row["notes"];
         // $output['sm_id']       =  $row["sm_id"];
          $output['status']       =  $row["status"];
          
		}
		echo json_encode($output);
	}
	if($_POST['btn_action'] == 'Edit')
	{
		
		
			$query = "
			UPDATE accounts SET
			
				acc_name = '".$_POST["acc_name"]."',
				address = '".$_POST["address"]."',
				area = '".$_POST["area"]."', 
				city = '".$_POST["city"]."',
			
				phone = '".$_POST["phone"]."',
				email = '".$_POST["email"]."',
				
				
				stn = '".$_POST["stn"]."',
				ntn = '".$_POST["ntn"]."',
				notes = '".$_POST["notes"]."',
				user_id = '".$user_id."',				
				status = '".$_POST["status"]."'
				WHERE acc_no = '".$_POST["acc_no"]."'
			";
		
		$statement = $connect->prepare($query);
		$statement->execute();
		//$result = $statement->fetchAll();
		$acc_no = $_POST['acc_no'];
		
		$result = $statement->fetchAll();
		
		
		if(isset($result))
		{
			echo 'Customer Details Updated';
		}
	}
	if($_POST['btn_action'] == 'delete')
	{
		$status = 'Inactive';
		if($_POST['status'] == 'Active')
		{
			$status = 'Inactive';
		}
	
		$acc_no = $_POST["acc_no"];
		$query = "
		UPDATE accounts
		SET status = :status 
		WHERE acc_no = :acc_no
		";
		$statement = $connect->prepare($query);
		$statement->execute(
			array(
				':status'	=>	$status,
				':acc_no'		=>	$acc_no
			)
		);	
		$result = $statement->fetchAll();	
		if(isset($result))
		{
			echo 'Customer Status change to ' . $status;
		}
	}
}
?>