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


if(isset($_POST['type']) && $_POST['type']=='create_suppliers'){
     try{
         
        $pdo->beginTransaction(); 
        
            $supplier_name = $_POST['supplier_name'];
            $mobile_no = $_POST['mobile_no'];
            $alter_mobile_no = $_POST['alter_mobile_no'];
            $gst_number = $_POST['gst_number'];
            $address = $_POST['address'];
            $company_name = "";
            $product_ids = $_POST['product_ids'];
            
            
        if (isset($mobile_no) && $mobile_no!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_suppliers` 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_suppliers'");
        $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 = "SUP_".$unique_id; 
         
        
        
        
        $statement = $pdo->prepare("INSERT INTO `tbl_suppliers`(`name`, `mobile_no`, `alter_mobile_no`, 
        `gst_no`, `company_name`, `unique_id`, `address`,  `created_by`, `product_ids`) VALUES (?,?,?,
        ?,?,?,?,?,?)");
      
    	$result = $statement->execute(array($supplier_name, $mobile_no, $alter_mobile_no, 
    	$gst_number, $company_name, $unique_id, $address, $session_id, $product_ids));
    
 
        
 
    
        if($result){
            
            $last_inserted_id = $pdo->lastInsertId();
            

    	    
    	    $pdo->commit();
            $return_arr['message']='Suuplier 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_suppliers'){
     try{
         
        $pdo->beginTransaction(); 
        
            $ids = $_POST['ids'];       
            $name = $_POST['name'];
            $phone = $_POST['phone'];
            $alter_mobile_no = $_POST['alter_mobile_no'];
            $gst_no = $_POST['gst_no'];
            $address = $_POST['address'];
            $product_ids = $_POST['product_ids'];
            
            
        if (isset($mobile_no) && $mobile_no!=""){
            $statementsss = $pdo->prepare("SELECT * FROM `tbl_suppliers` 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_suppliers SET name=?, mobile_no=?, alter_mobile_no=?, gst_no=?, address=?, product_ids=? WHERE id=?");
        $result = $statement->execute(array($name, $phone, $alter_mobile_no, $gst_no,  $address, $product_ids, $ids));
    
 
        
 
    
        if($result){
            
            $last_inserted_id = $pdo->lastInsertId();
            

    	    
    	    $pdo->commit();
            $return_arr['message']='Suuplier 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);
}

?>