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





if(isset($_POST['type']) && $_POST['type']=='update_price'){
     try{
         
        $pdo->beginTransaction(); 
        
        
        for($i=0; $i<count($_POST['product_ids']); $i++){
            
            $ids = $_POST['ids'][$i];
            $product_ids = $_POST['product_ids'][$i];
            $purity_ids = $_POST['purity_ids'][$i] ?? 0;
            $amount = $_POST['amount'][$i];
            
            
            $statement = $pdo->prepare("UPDATE `tbl_prices` SET `product_id`=?, `purity_id`=?, `amount`=?,  `updated_by`=?, `updated_at`=? WHERE id=?");
            $result = $statement->execute(array($product_ids, $purity_ids, $amount, $session_id, $updated_at, $ids));
            
            
            $statements_price = $pdo->prepare("SELECT * FROM `tbl_prices_reports` WHERE date=? and price_id=?");
            $statements_price->execute(array($today_date, $ids));
            $price_results = $statements_price->fetchAll(PDO::FETCH_ASSOC);
            $price_totalDatas = $statements_price->rowCount();
            
            
            if($price_totalDatas>0){
                // Update Here
                $statement = $pdo->prepare("UPDATE `tbl_prices_reports` SET `date`=?, `price_id`=?, `product_id`=?, `purity_id`=?, `amount`=?,  `updated_by`=?, `updated_at`=? WHERE id=?");
                $result = $statement->execute(array($today_date, $ids, $product_ids, $purity_ids, $amount, $session_id, $updated_at, $price_results[0]['id']));
            }else{
                // Insert Here
                $statement = $pdo->prepare("INSERT INTO `tbl_prices_reports`(`date`, `price_id`, `product_id`, `purity_id`, `amount`, `created_by`) VALUES (?,?,?,?,?,?)");
                $result = $statement->execute(array($today_date, $ids, $product_ids, $purity_ids, $amount, $session_id));
            }
                        
           
    
            
        }
 
    
        
        
     
 
        
 
    
        if($result){
            
            $customer_id = $pdo->lastInsertId();
            

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

?>