<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";



/*include_once('../config/custom-functions.php');
$fn = new custom_functions;
$db = new Database();*/






if(isset($_POST['type']) && $_POST['type']=='create_customers'){
     try{
         
        $pdo->beginTransaction(); 
        
            $customer_name = $_POST['customer_name'];
            $mobile_no = $_POST['mobile_no'];
            $alter_mobile_no = $_POST['alter_mobile_no'];
            $gender = $_POST['gender'];
            $pan_no = $_POST['pan_no'];
            $aadhaar_no = $_POST['aadhaar_no'];
            $address = $_POST['address'];
            $dob = $_POST['dob'];
            $religion = $_POST['religion'];
            $city = $_POST['city_town'];
            $wedding_date = $_POST['wedding_date'];
            $gst_no = $_POST['gst_no'];
            
            $driving_no = $_POST['driving_no'];
            $voter_id_no = $_POST['voter_id_no'];
            $other_proof_no = $_POST['other_proof_no'];
            $nominee_name = $_POST['nominee_name'];
            $nominee_mobile_no = $_POST['nominee_mobile_no'];
            $nominee_address = $_POST['nominee_address'];
            $nominee_type = $_POST['nominee_type'];
            $remarks = $_POST['remarks'];
            $state = $_POST['customer_state'];
            $pincode = $_POST['pincode'] ?? "";
            
            $proof = $_POST['proof'];
            $location_id = isset($_POST['location_id']) ? $_POST['location_id'] : '';
            
            // Bank Details
            $bank_name = isset($_POST['bank_name']) ? $_POST['bank_name'] : '';
            $bank_account_holder_name = isset($_POST['bank_account_holder_name']) ? $_POST['bank_account_holder_name'] : '';
            $account_no = isset($_POST['account_no']) ? $_POST['account_no'] : '';
            $ifsc_code = isset($_POST['ifsc_code']) ? $_POST['ifsc_code'] : '';
            $branch	 = isset($_POST['branch']) ? $_POST['branch'] : '';
            
            
            
        if (isset($mobile_no) && $mobile_no!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_customers` WHERE mobile_no=?");
            $resultsss = $statementsss->execute(array($mobile_no));
            $totalsss = $statementsss->rowCount();    
            if($totalsss>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Mobile Number is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        

        
        $unique_id = "10000";
	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_customers'");
        $statement->execute();
        $memeber_id_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($memeber_id_result as $result) {
	        $unique_id = $unique_id + $result['Auto_increment'];
        }
        
        $unique_id = "SPM_CUS_".$unique_id; 
         
    
        
        $statement = $pdo->prepare("INSERT INTO `tbl_customers`(`customer_name`, `mobile_no`, `alter_mobile_no`, `gender`, `religion`, 
                                    `address`, `city`, `dob`, `wedding_date`, `pan_no`, 
                                    `aadhaar_no`, `created_by`, `unique_id`, `gst_no`,
                                    `driving_no`, `voter_no`, `others_no`, `nominee_name`, `nominee_mobile_no`, `nominee_address`, `remarks`, 
                                    nominee_type, state, pincode, `proof`, `cus_location_id`,
                                    `bank_name`, `bank_account_holder_name`, `account_no`, `ifsc_code`, `branch`) VALUES (?,?,?,?,?,
                                    ?,?,?,?,?,
                                    ?,?,?,?,
                                    ?,?,?,?,?,?,?,
                                    ?,?,?,?,?,
                                    ?,?,?,?,?)");
      
    	$result = $statement->execute(array($customer_name, $mobile_no, $alter_mobile_no, $gender, $religion, 
                                	$address, $city, $dob, $wedding_date, $pan_no, 
                                	$aadhaar_no, $session_id, $unique_id, $gst_no,
                                	$driving_no, $voter_id_no, $other_proof_no, $nominee_name, $nominee_mobile_no, $nominee_address, $remarks, 
                                	$nominee_type, $state, $pincode, $proof, $location_id,
                                	$bank_name, $bank_account_holder_name, $account_no, $ifsc_code, $branch));
    
 
        
 
    
        if($result){
            
            $customer_id = $pdo->lastInsertId();
            

    	    
    	    $pdo->commit();
    	    $return_arr['customer_id']=$customer_id;
    	    $return_arr['customer_name']=$customer_name;
    	    $return_arr['mobile_no']=$mobile_no;
            $return_arr['message']='Customer Added Successfully...';
            $return_arr['status']=200;

        }
        else
        {
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
            
            
             
    }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
    }   
    echo json_encode($return_arr);
}







if(isset($_POST['type']) && $_POST['type']=='update_customers'){
     try{
         
        $pdo->beginTransaction(); 
        
            $ids = $_POST['ids'];
            $customer_name = $_POST['customer_name'];
            $mobile_no = $_POST['mobile_no'];
            $alter_mobile_no = $_POST['alter_mobile_no'];
            $gender = $_POST['gender'];
            $pan_no = $_POST['pan_no'];
            $aadhaar_no = $_POST['aadhaar_no'];
            $address = $_POST['address'];
            $dob = $_POST['dob'];
            $religion = $_POST['religion'];
            $city = $_POST['city_town'];
            $wedding_date = $_POST['wedding_date'];
            $gst_no = $_POST['gst_no'];
            
            
            $driving_no = $_POST['driving_no'];
            $voter_id_no = $_POST['voter_id_no'];
            $other_proof_no = $_POST['other_proof_no'];
            $nominee_name = $_POST['nominee_name'];
            $nominee_mobile_no = $_POST['nominee_mobile_no'];
            $nominee_address = $_POST['nominee_address'];
            $nominee_type = $_POST['nominee_type'];
            $remarks = $_POST['remarks'];
            $state = $_POST['customer_state'];
            $pincode = $_POST['pincode'] ?? "";
            
            $proof = $_POST['proof'];
            $location_id = isset($_POST['location_id']) ? $_POST['location_id'] : '';
            
            
            
            
        if (isset($mobile_no) && $mobile_no!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_customers` WHERE mobile_no=? and id!=?");
            $resultsss = $statementsss->execute(array($mobile_no, $ids));
            $totalsss = $statementsss->rowCount();    
            if($totalsss>0){
                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "This Mobile Number is Already Existed!";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
            }
        }
        
  
        
        
        $statement = $pdo->prepare("UPDATE `tbl_customers` SET `customer_name`=?, `mobile_no`=?, `alter_mobile_no`=?, `gender`=?, `religion`=?, 
                                    `address`=?, `city`=?, `dob`=?, `wedding_date`=?, `pan_no`=?, 
                                    `aadhaar_no`=?, `updated_by`=?, `gst_no`=?,
                                    `driving_no`=?, `voter_no`=?, `others_no`=?, `nominee_name`=?, `nominee_mobile_no`=?, `nominee_address`=?, `remarks`=?,
                                    `nominee_type`=?, `state`=?, `pincode`=?, `proof`=?, `cus_location_id`=?
                                    WHERE `id`=?");
      
    	$result = $statement->execute(array($customer_name, $mobile_no, $alter_mobile_no, $gender, $religion, 
                                	$address, $city, $dob, $wedding_date, $pan_no, 
                                	$aadhaar_no, $session_id, $gst_no, 
                                	$driving_no, $voter_id_no, $other_proof_no, $nominee_name, $nominee_mobile_no, $nominee_address, $remarks,
                                	$nominee_type, $state, $pincode, $proof, $location_id,
                                	$ids));
    
 
        
 
    
        if($result){
            
            $customer_id = $pdo->lastInsertId();
            

    	    
    	    $pdo->commit();
            $return_arr['message']='Customer Updated Successfully...';
            $return_arr['status']=200;

        }else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
            
            
             
     }catch(PDOException $e){
        $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}
?>