<?php
include("db_connection.php");
$Mainclass=new DB_Controller();
  /*ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); */
$Offer_Name=mysqli_real_escape_string($Mainclass->dbh,$_REQUEST['Offer_Name']);
$Dealers=ltrim(implode(',',$_REQUEST['dealers']),',');
$Offer_On=$_REQUEST['Offer_On'];
$Collection=$_REQUEST['Offer_On']=='Collection' ? ltrim(implode(',',$_REQUEST['Collection']),','):'';
$Category=$_REQUEST['Offer_On']=='Category' ? ltrim(implode(',',$_REQUEST['Category']),','):'';
$From=date('Y-m-d',strtotime($_REQUEST['From']));
$To=date('Y-m-d',strtotime($_REQUEST['To']));
$Mc_Disc=$_REQUEST['MC_Disc']!='' ? $_REQUEST['MC_Disc']:'0.00';
$Dia_Disc=$_REQUEST['Dia_Disc']!='' ? $_REQUEST['Dia_Disc']:'0.00';
$Soli_Disc=$_REQUEST['Soli_Disc']!='' ? $_REQUEST['Soli_Disc']:'0.00';
$Stone_Disc=$_REQUEST['Stn_Disc']!='' ? $_REQUEST['Stn_Disc']:'0.00';
$Created_At=date('Y-m-d H:i:s');
$Active="Yes";


$Ins_Offer=$Mainclass->dbh->prepare("INSERT INTO gold_discount_master(Dealer_Code,Offer_Name,Offer_On,Collections,Product_Category,Making_Charge_Disc,Diamond_Disc,Solitaire_Disc,Stone_Disc,From_Date,To_Date,Active,Created_At,Updated_At) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$Ins_Offer->bind_param("ssssssssssssss",$Dealers,$Offer_Name,$Offer_On,$Collection,$Category,$Mc_Disc,$Dia_Disc,$Soli_Disc,$Stone_Disc,$From,$To,$Active,$Created_At,$Created_At);
$Ins_Offer->execute();
$last_id = $Mainclass->dbh->insert_id;
$Ins_Offer->close();

$Discount_Id=$last_id;



$Dealer_Codes = explode(",", $Dealers);
$Product_Category = explode(",", $Category);
$Product_Collection= explode(",", $Collection);

 // --- Optimized Batch Insert for Dealers ---
    if (!empty($Dealer_Codes)) {
        // Filter out any empty strings that might result from explode (e.g., "A,,B")
        $Dealer_Codes = array_filter($Dealer_Codes);

        if (!empty($Dealer_Codes)) {
            $dealer_values = [];
            $dealer_types = ''; // String for bind_param types (e.g., 'sisisi')
            $dealer_params = []; // Array for bind_param values

            foreach ($Dealer_Codes as $dealer) {
                $dealer_values[] = '(?,?)';
                $dealer_types .= 'si';
                $dealer_params[] = trim($dealer); // Trim to remove potential whitespace
                $dealer_params[] = $Discount_Id;
            }

            $Insert_Dealer_sql_query = "INSERT INTO gold_category_discount_dealers (Dealer_Code, Category_Discount_Id) VALUES " . implode(',', $dealer_values);
            $Insert_Dealer_sql = $Mainclass->dbh->prepare($Insert_Dealer_sql_query);

            // Dynamically bind parameters
            $Insert_Dealer_sql->bind_param($dealer_types, ...$dealer_params);
            $Insert_Dealer_sql->execute();
            $Insert_Dealer_sql->close();
        }
    }

// --- Optimized Batch Insert for Categories ---
    if (!empty($Product_Category)) {
        // Filter out any empty strings
        $Product_Category = array_filter($Product_Category);

        if (!empty($Product_Category)) {
            $category_values = [];
            $category_types = '';
            $category_params = [];

            foreach ($Product_Category as $category) {
                $category_values[] = '(?,?)';
                $category_types .= 'si';
                $category_params[] = trim($category); // Trim whitespace
                $category_params[] = $Discount_Id;
            }

            $Insert_category_sql_query = "INSERT INTO gold_category_discount_product_cat (Product_Category, Category_Discount_Id) VALUES " . implode(',', $category_values);
            $Insert_category_sql = $Mainclass->dbh->prepare($Insert_category_sql_query);

            // Dynamically bind parameters
            $Insert_category_sql->bind_param($category_types, ...$category_params);
            $Insert_category_sql->execute();
            $Insert_category_sql->close();
        }
    }

// --- Optimized Batch Insert for Collection ---
    if (!empty($Product_Collection)) {
        // Filter out any empty strings
        $Product_Collection = array_filter($Product_Collection);

        if (!empty($Product_Collection)) {
            $collection_values = [];
            $collection_types = '';
            $collection_params = [];

            foreach ($Product_Collection as $collection) {
                $collection_values[] = '(?,?)';
                $collection_types .= 'si';
                $collection_params[] = trim($collection); // Trim whitespace
                $collection_params[] = $Discount_Id;
            }

            $Insert_collection_sql_query = "INSERT INTO gold_category_discount_collection (Collection_Id, Category_Discount_Id) VALUES " . implode(',', $collection_values);
            $Insert_collection_sql = $Mainclass->dbh->prepare($Insert_collection_sql_query);

            // Dynamically bind parameters
            $Insert_collection_sql->bind_param($collection_types, ...$collection_params);
            $Insert_collection_sql->execute();
            $Insert_collection_sql->close();
        }
    }


?>