<?php

function add_popUp_imges($popUp_imgesImage, $product_id, $parent_cat_id,$type) {
    global $con;
    $con->query("INSERT INTO `popUp_imges` (`type`,`parent_category_id`,`product_id`,`image`,`date_added`) VALUES ('$type','$parent_cat_id','$product_id','$popUp_imgesImage','" . date("Y-m-d H:i:s") . "')");
    return mysqli_insert_id($con);
}

function popUp_imges_count() {

    global $con;

    $query = $con->query("SELECT * FROM `popUp_imges` ORDER BY `id` ASC");

    $popUp_imges_count = mysqli_num_rows($query);

    return $popUp_imges_count;
}

function view_popUp_imgess($get) {

    global $con;
    $sql = "SELECT * FROM `popUp_imges` WHERE 1 ";
    if (isset($get['parent_category_id']) && $get['parent_category_id'] != '') {
        $sql.= " and `parent_category_id`='" . $get['parent_category_id'] . "' ";
    }

    $sql.="ORDER BY `id` ASC";
    $query = $con->query($sql);

    $x = 1;

    while ($row = mysqli_fetch_assoc($query)) {
        $id = $row['id'];
        $product_id = $row['product_id'];
        $image = $row['image'];
        $parent_category_id = $row['parent_category_id'];
        $date = $row['date_added'];
        $type = $row['type'];
        if ($type == 1) {
            $type = "Item";
        } elseif ($type == 2) {
            $type = "Image";
        }
        ?>
        <tr class="gradeX">
            <td><?php echo $x; ?></td>
            <td>
                <a href="<?php echo $image; ?>" class="image-popup" title="image">
                    <img src="<?php echo $image; ?>" class="thumb-img" alt="image" height="100" style="width:100px;">
                </a>			
            </td>
            <td><?= $type ?></td>
            
            <td><?php echo sub_category_data($product_id)["sub_category_name"]; ?></td>
            <td><a href="popUp_imges_view.php?parent_category_id=<?php echo $parent_category_id; ?>"><?php echo get_parent_cat_by_id($parent_category_id)['parent_category_name']; ?></a></td>

            <td><?php echo $date; ?></td>
            <td class="actions">
                <a href="popUp_imges_edit.php?popUp_imgesID=<?php echo $id; ?>" class="on-default"><i class="fa fa-pencil"></i></a>
                <a href="<?php echo $id; ?>" class="on-default remove-row"><i class="fa fa-trash-o"></i></a>
            </td>
        </tr>		
        <?php
        $x++;
    }

    return mysqli_insert_id($con);
}

if (isset($_POST['popUp_imges_id'])) {

    include("../connection.php");

    $popUp_imges_id = $_POST['popUp_imges_id'];

    $querya = $con->query("SELECT * FROM `popUp_imges` WHERE `id`='$popUp_imges_id' limit 1");

    $row_select = mysqli_fetch_array($querya);

    $popUp_imges_image = $row_select['image'];

    $mostafa = explode('/', $popUp_imges_image);

    $image_name = $mostafa[7];

    $full_img_path = "../../api/uploads/popUp_imges/{$popUp_imges_id}/{$image_name}";

    $folder_full_img_path = "../../api/uploads/popUp_imges/{$popUp_imges_id}";

    if (file_exists($full_img_path)) {
        @unlink($full_img_path);
    }

    rmdir($folder_full_img_path);

    $query = $con->query("DELETE FROM `popUp_imges` WHERE `id`='$popUp_imges_id'");

    if ($query) {
        echo get_success("تم الحذف بنجاح");
    }
}



if (isset($_POST['parent_cat_id_products'])) {

    include("../connection.php");
    global $con;
    $parent_cat_id = $_POST['parent_cat_id_products'];

    $query = $con->query("SELECT * FROM `sub_categories`  WHERE `parent_category_id`='$parent_cat_id' ");
    $options = "";
    while ($row = mysqli_fetch_assoc($query)) {

        $product_id = $row['sub_category_id'];
        $product_name_ar = $row['sub_category_name'];
        $options.= "<option value='{$product_id}'>{$product_name_ar}</option>";
    }

    echo $options;
}
?>
