<?php

include 'connection.php';


function get_category_id($name, $parent_category_name_ar) {
    global $con;
    $query = $con->query("SELECT `parent_category_id` FROM `parent_categories` where `parent_category_name`='$name' ");
    if ($query) {
        $row = mysqli_num_rows($query);
        if ($row) {
            $row = mysqli_fetch_array($query);
            $id = $row['parent_category_id'];
            if ($id > 0) {
                return $id;
            }
        } else {
            $mesqlquey = "insert into `parent_categories` (`parent_category_name`,`parent_category_name_ar`,`parent_category_desc`,`parent_category_desc_ar`) "
                    . " values ('$name','$parent_category_name_ar','$name','$parent_category_name_ar') ";
            $query2 = $con->query($mesqlquey);
            $id = mysqli_insert_id($con);
            return $id;
        }
    }
}

function get_sub_category_id($parent_category_id, $sub_category_name, $sub_category_name_ar, $SizeName1, $SizePrice1, $SizeName2, $SizePrice2, $SizeName3, $SizePrice3, $SizeName4, $SizePrice4) {
    global $con;
    $query = $con->query("SELECT `sub_category_id` FROM `sub_categories` where `sub_category_name`='$sub_category_name' " );
    if ($query) {
        $row = mysqli_num_rows($query);
        
        if ($row==0) {
            $mesqlquey = "insert into `sub_categories` (`sub_category_name`,`sub_category_name_ar`,`sub_category_desc`,`sub_category_desc_ar`,`parent_category_id`) "
                    . " values ('$sub_category_name','$sub_category_name_ar','$sub_category_name','$sub_category_name_ar','$parent_category_id') ";
            $query2 = $con->query($mesqlquey);
            $sub_category_id = mysqli_insert_id($con);
            if(isset($SizePrice1) && $SizePrice1!=''){
            add_size($sub_category_id,$SizePrice1,$SizeName1);
            }
            if(isset($SizePrice2) && $SizePrice2!=''){
            add_size($sub_category_id,$SizePrice2,$SizeName2);
            }

            if(isset($SizePrice3) && $SizePrice3!=''){
            add_size($sub_category_id,$SizePrice3,$SizeName3);
            }

            if(isset($SizePrice4) && $SizePrice4!=''){
            add_size($sub_category_id,$SizePrice4,$SizeName4);
            }

        } 
        
    }

    return $sub_category_id;
}

function add_size($sub_category_id,$price,$sub_category_size_name){
    global $con;

        $delete_sizes_sqlquery = "delete from  `sub_categories_size_prices` where sub_category_id=$sub_category_id and "
                . "sub_categories_size_prices='$price' and sub_category_size_name='$sub_category_size_name' ";
        $update_resault2 = $con->query($delete_sizes_sqlquery);

        $insert_new_size = "insert into `sub_categories_size_prices` (`sub_category_id`,`sub_category_size_price`,`sub_category_size_name`,`sub_category_size_name_ar`) "
                . " values ($sub_category_id,'$price','$sub_category_size_name','$sub_category_size_name') ";
        echo $insert_new_size ;
        
        $update_resault3 = $con->query($insert_new_size);
        
        
        

    
}


if (isset($_POST['id'])) {
    $js = json_decode($_POST['id'], true);

    $item_list = "";
    $ItemID = 0;
    $ItemenName_EN = "";
    $ItemarName = "";
    $GroupName = "";
    $SizePrice = "";
    foreach ($js as $key => $row) {

            $ItemenName_EN = $row["product_en"];
            $ItemarName_AR = $row["product_ar"];
            $GroupName_AR = $row["parent_ar"];
            $SizePrice1 = $row["Price"];
            $SizeName1 = "Normal";

            $SizePrice2 = $row["price2"];
            $SizeName2 = $row["size2"];

            $SizePrice3 = $row["price3"];
            $SizeName3 = $row["size3"];

            $SizePrice4 = $row["price4"];
            $SizeName4 = $row["size4"];

            $GroupName_EN  = $row['parent_en'];
            
            if($ItemenName_EN !="product_en"){
           
            $cat_id = get_category_id($GroupName_EN,$GroupName_AR);
            $sub_id = get_sub_category_id($cat_id, $ItemenName_EN, $ItemarName_AR, $SizeName1, $SizePrice1, $SizeName2, $SizePrice2, $SizeName3, $SizePrice3, $SizeName4, $SizePrice4);

            $item_list .= "<br><a>{$sub_id}'--'{$ItemenName_EN}</a>";
}
            //$item_list .= "<br><a>{$ItemID}{$ItemenName}{$ItemarName}{$GroupName}{$SizePrice} </a>";
        
    }

    echo $item_list;
}



?>






