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




if(isset($_POST['type']) && $_POST['type']=='create_products'){
     try{
         
        $pdo->beginTransaction(); 
        
            $product_name = $_POST['product_name'];
            $category = $_POST['category'];
            $sub_category = isset($_POST['sub_category']) ? $_POST['sub_category']: 0;
            $product_type = $_POST['product_type'];
            $unit = $_POST['unit'];
            $quantity = $_POST['quantity'];
            $product_ids = $_POST['product_ids'];
            
            // Weight - Here
            
            $wastage = $_POST['wastage'];
            $purity = $_POST['purity'];
            $tags_label_weight = 0;
            
            // Amount - Here
            $selling_price = 0;
            $purchase_price = 0;
            
            
            // Product HSN Starts Here 
            $pro_hsn="10000";
    	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_products'");
            $statement->execute();
            $products_results = $statement->fetchAll(PDO::FETCH_ASSOC); 
            foreach ($products_results as $pro) {
    	        $pro_hsn = $pro_hsn + $pro['Auto_increment'];
            } 
            // Product HSN Endsss Here 
            $product_hsn = $pro_hsn;
            
        if($product_type==1){
            $cross_weight = $_POST['cross_weight'];
            $stone_cost = 0;
            $stone_weight = 0;
            $net_weight = $cross_weight;
        }else{
            $cross_weight = $_POST['cross_weight'];
            $stone_cost = $_POST['stone_cost'];
            $stone_weight = $_POST['stone_weight'];
            $net_weight = $_POST['net_weight'];
        }
        
        
        
        $statement = $pdo->prepare("INSERT INTO `tbl_products`(`product_name`, `category`, `sub_category`, `type`, `product_hsn`, `created_by`) VALUES (?,?,?,?,?,?)");
        $result = $statement->execute(array($product_name, $category, $sub_category, $product_type, $product_hsn, $session_id));
    	
    
        
       
    
 
        
 
    
        if($result){
            
            $product_id = $pdo->lastInsertId();
            
            // Product HSN Starts Here 
            $pro_hsn = "10000";
    	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_product_variants'");
            $statement->execute();
            $products_results = $statement->fetchAll(PDO::FETCH_ASSOC); 
            foreach ($products_results as $pro) {
    	        $pro_hsn = $pro_hsn + $pro['Auto_increment'];
            } 
            // Product HSN Endsss Here 
            $hsn_code = $pro_hsn;
        

            
             
        $statement = $pdo->prepare("INSERT INTO `tbl_product_variants` (`product_name`, `category`, `sub_category`,  `purchase_price`, `selling_price`, 
        `quantity`,  `purity`, `unit`, 
        `cross_weight`, `stone_weight`, `net_weight`, `stone_cost`, `type`,
        `wastage`, `others_weight`, 
        `created_by`, `product_id`, `hsn_code`, `pro_ids`) VALUES (?,?,?,?,?,
        ?,?,?,
        ?,?,?,?,?,
        ?,?,
        ?,?,?,?)");
      
    	$result = $statement->execute(array($product_name, $category, $sub_category, $purchase_price, $selling_price, 
    	$quantity,  $purity, $unit, 
    	$cross_weight, $stone_weight, $net_weight, $stone_cost, $product_type, 
    	$wastage, $tags_label_weight,
    	$session_id, $product_id, $hsn_code, $product_ids));
            

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

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


if(isset($_POST['type']) && $_POST['type']=='create_products_name'){
     try{
         
        $pdo->beginTransaction(); 
        
            $name = $_POST['name'];
            $quantity = $_POST['quantity'];
            $status = $_POST['status'];
            $product_hsn_code = $_POST['product_hsn_code'];
            $product_type = $_POST['product_type'];
            $product_type = $_POST['product_type'];
            $product_details = $_POST['product_details'];
            
            
            
 
        $statement = $pdo->prepare("INSERT INTO  `tbl_products_name`(`name`, `quantity`,  `status`, `created_by`, `product_hsn_code`, `type`, `product_ids`) VALUES(?,?,?,?,?,?,?)");
        $result = $statement->execute(array($name, $quantity, $status, $session_id, $product_hsn_code, $product_type, $product_details));
        
        
        if($result){
            $product_id = $pdo->lastInsertId(); 
            
            
            for($i=0; $i<count($_POST['size']); $i++){
                $size = $_POST['size'][$i];
                if($size!=""){
                    $statement = $pdo->prepare("INSERT INTO  `tbl_size`(`size`, `product_name_id`, `created_by`) VALUES(?,?,?)");
                    $result = $statement->execute(array($size, $product_id, $session_id));
                }
                
                
            }
            
            
        }
    	
    
        
       
    
 
        
 
    
        if($result){
            
           
        
    	    $pdo->commit();
            $return_arr['message']='Product Name Added Successfully...';
            $return_arr['status']=200;

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

if(isset($_POST['type']) && $_POST['type']=='update_products_name'){
     try{
         
        $pdo->beginTransaction(); 
        
            $ids = $_POST['ids'];
            $name = $_POST['name'];
            $quantity = $_POST['quantity'];
            $status = $_POST['status'];
            $product_hsn_code = $_POST['product_hsn_code'];
            $product_type = $_POST['product_type'];
            $product_details = $_POST['product_details'];
            
 
        $statement = $pdo->prepare("UPDATE `tbl_products_name` SET `name`=?, `quantity`=?, `status`=?, `updated_by`=?, `product_hsn_code`=?, `type`=?, `product_ids`=? WHERE id=?");
        $result = $statement->execute(array($name, $quantity,  $status, $session_id, $product_hsn_code, $product_type, $product_details, $ids));
    	
    
        if($result){
            $product_id = $ids; 
            
            
            for($i=0; $i<count($_POST['size']); $i++){
                $size = $_POST['size'][$i];
                $id = $_POST['size_id'][$i];
                if($size!=""){
                    
                    if($id==0){
                        $statement = $pdo->prepare("INSERT INTO  `tbl_size`(`size`, `product_name_id`, `created_by`) VALUES(?,?,?)");
                        $result = $statement->execute(array($size, $product_id, $session_id));
                    }else{
                        $statement = $pdo->prepare("UPDATE  `tbl_size` SET `size`=?, `product_name_id`=?, `updated_by`=? WHERE id=?");
                        $result = $statement->execute(array($size, $product_id, $session_id, $id));
                    }
                    
                }
                
                
            }
            
            
        }
       
    
 
        
 
    
        if($result){
            
           
        
    	    $pdo->commit();
            $return_arr['message']='Product Name Updated Successfully...';
            $return_arr['status']=200;

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