<?php
include("config.php");
if (!loggedin()) {
    header("Location: login.php");
    exit();
}
?>
<!DOCTYPE html>
<html>

    <?php include("include/heads.php"); ?>	
    <body class="fixed-left">

        <div id="wrapper">
            <!-- Top Bar Start -->
            <?php include("include/topbar.php"); ?>
            <!-- Top Bar End -->

            <!-- Left Sidebar Start -->
            <?php include("include/leftsidebar.php"); ?>
            <!-- Left Sidebar End -->

            <!-- Start right Content here -->

            <!-- ============================================================== -->
            <!-- Start right Content here -->
            <!-- ============================================================== -->  		

            <?php
// error_reporting(0);

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

                $popUp_imgesID_update = $_POST['popUp_imgesID_update'];
                $product_id = $_POST['product_id'];

                $type = $_POST['type'];
                $parent_cat_id = $_POST['parent_cat'];

                if (isset($_FILES['image_update']['name']) && !empty($_FILES['image_update']['name'])) {

                    $image_ext_old = $_POST['image_ext_old'];
                    $mostafa = explode('/', $image_ext_old);
                    $image_name = $mostafa[7];
                    $full_img_path = "../api/uploads/popUp_imges/$popUp_imgesID_update" . "/" . $image_name;
                    if (file_exists($full_img_path)) {
                        @unlink($full_img_path);
                    }

                    if (!file_exists("../api/uploads/popUp_imges/" . $popUp_imgesID_update)) {
                        mkdir("../api/uploads/popUp_imges/" . $popUp_imgesID_update, 0777, true);
                    }

                    $image_name_update = $_FILES['image_update']['name'];
                    $image_tmp_update = $_FILES['image_update']['tmp_name'];

                    $image_path = "../api/uploads/popUp_imges/$popUp_imgesID_update" . "/" . round(microtime(true)) . '.' . "jpg";
                    $image_database = "{$sit_url}/api/uploads/popUp_imges/$popUp_imgesID_update" . "/" . round(microtime(true)) . '.' . "jpg";

                    $update = $con->query("UPDATE  `popUp_imges` SET `image`='$image_database'  WHERE `id`='$popUp_imgesID_update'");

                    if (move_uploaded_file($image_tmp_update, $image_path)) {
                        
                    }

                    if ($update) {
                        echo get_success("Updated Successfully ");
                    } else {
                        echo get_error("there's an error ");
                    }
                }
                $update = $con->query("UPDATE  `popUp_imges` SET `type`='$type', `product_id`='$product_id' ,`parent_category_id`='$parent_cat_id' WHERE `id`='$popUp_imgesID_update'");
            }
            ?>


            <div class="content-page">
                <!-- Start content -->
                <div class="content">
                    <div class="container">

                        <!-- Page-Title -->
                        <div class="row">
                            <div class="col-sm-12">
                                <h4 class="page-title">Popup Images </h4>
                                <ol class="breadcrumb">
                                    <li><a href="popUp_imges_view.php">Popup Images  </a></li>
                                    <li class="active"> Update Popup Images  </li>
                                </ol>
                            </div>
                        </div>

                        <div class="updateData"></div>

                        <?php
                        if ($_GET['popUp_imgesID']) {

                            $get_popUp_imges_id = $_GET['popUp_imgesID'];

                            $query_select = $con->query("SELECT * FROM `popUp_imges` WHERE `id` = '{$get_popUp_imges_id}' LIMIT 1");
                            $row_select = mysqli_fetch_array($query_select);

                            $id = $row_select['id'];
                            $image = $row_select['image'];
                            $parent_category_id_data = $row_select['parent_category_id'];
                            $get_product_id = $row_select['product_id'];
                            $type = $row_select['type'];

                            if ($query_select) {
                                ?>
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="card-box"> 									
                                            <form method="POST" enctype="multipart/form-data" data-parsley-validate novalidate>
                                                <input type="hidden" name="popUp_imgesID_update" id="popUp_imgesID_update" parsley-trigger="change" required value="<?php echo $id; ?>" class="form-control">


                                                <div class="form-group col-md-3">
                                                    <label class="control-label">  Type</label>
                                                    <select class="form-control" name="type" id="type"  parsley-trigger="change">
                                                        <option value="">Choose</option>

                                                        <option value="2" <?php if ($type == 2) echo "selected"; ?>>Only Image</option>
                                                        <option value="1" <?php if ($type == 1) echo "selected"; ?>>Product</option>
                                                    </select>
                                                </div>

                                                <div  id="type_div" <?php if ($type != 1) { ?> style="display: none;" <?php } ?>>

                                                    <div class="form-group col-md-3">
                                                        <label class="control-label">  Category Name </label>
                                                        <select class="form-control select2m option_require" name="parent_cat" id="parent_cat" >
                                                            <option value="0"> Choose </option>
                                                            <?php
                                                            $query = $con->query("SELECT * FROM `parent_categories`  ORDER BY `parent_category_id` ASC");
                                                            while ($row = mysqli_fetch_assoc($query)) {

                                                                $parent_category_id = $row['parent_category_id'];
                                                                if ($parent_category_id_data == $parent_category_id) {
                                                                    $selected = "selected";
                                                                } else {
                                                                    $selected = "";
                                                                }
                                                                $name_ar = $row['parent_category_name'];
                                                                echo "<option {$selected} value='{$parent_category_id}'>{$name_ar}</option>";
                                                            }
                                                            ?>
                                                        </select>											
                                                    </div>


                                                    <div class="form-group col-md-3">
                                                        <label for="product_id"> Item Name </label>
                                                        <select class="form-control select2m option_require" name="product_id" id="product_id_update"  parsley-trigger="change">
                                                            <?php
                                                            $query = $con->query("SELECT * FROM `sub_categories` WHERE `parent_category_id`='$parent_category_id_data'  ORDER BY `sub_category_id` ASC");
                                                            while ($row = mysqli_fetch_assoc($query)) {
                                                                $product_id = $row['sub_category_id'];
                                                                $product_name_ar = $row['sub_category_name'];
                                                                if ($get_product_id == $product_id) {
                                                                    echo "<option value='{$product_id}' selected='selected'>{$product_name_ar}</option>";
                                                                } else {
                                                                    echo "<option value='{$product_id}'>{$product_name_ar}</option>";
                                                                }
                                                            }
                                                            ?>
                                                        </select>					
                                                    </div>
                                                </div>
                                                <div class="clearfix"></div>


                                                <input type="hidden" name="image_ext_old" value="<?php echo $image; ?>" />
                                                <div class="form-group m-b-0">
                                                    <label for="image"> Image * <a class="showImg">Update ?</a> </label>								

                                                    <div class="gal-detail thumb getImage">
                                                        <a href="<?php echo $image; ?>" class="image-popup" title="image">
                                                            <img src="<?php echo $image; ?>" class="thumb-img" alt="image">
                                                        </a>
                                                    </div>					

                                                    <div class="form-group m-b-0">
                                                        <label class="control-label"> Image Popup Images </label>
                                                        <input type="file" name="image_update" id="image_update" class="filestyle" data-buttonname="btn-primary">
                                                    </div>

                                                </div>
                                                <br>
                                                <div class="form-group text-right m-b-0">
                                                    <button class="btn btn-primary waves-effect waves-light" type="submit" name="popUp_imges_update" id="updateMenu">Update</button>
                                                </div>
                                            </form>

                                        </div>
                                    </div>
                                </div>
                                <?php
                            }
                        }
                        ?>

                    </div>			
                </div>
                <?php include("include/footer_text.php"); ?>

            </div>			

            <!-- End Right content here -->

            <!-- Right Sidebar -->
            <div class="side-bar right-bar nicescroll">
                <?php include("include/rightbar.php"); ?>
            </div>
            <!-- /Right-bar -->
        </div>
        <!-- END wrapper -->
        <?php include("include/footer.php"); ?>

        <script>

            $("#type").change(function () {
                var type = $(this).val();
                if (type == 1) {
                    $("#type_div").show();
                    $('.option_require').attr('required', true);


                } else {
                    $('#type_div').css('display', 'none');
                    $(".option_require").removeAttr("required");

                }
            });


            $(document).ready(function () {
                $("#csspopUp_imges ul>li").removeClass("active");
                $("#item2222").addClass("active");

                $("#parent_cat").on('change', function () {
                    // alert("sss");
                    var parent_cat = $(this).val();
                    var dataString = 'parent_cat_id_products=' + parent_cat;
                    console.log(dataString);
                    $.ajax({
                        type: "POST",
                        url: "functions/popUp_imges_functions.php",
                        data: dataString,
                        dataType: 'text',
                        cache: false,
                        success: function (html) {
                            console.log(html);
                            $('#product_id_update option').remove();
                            $('#product_id_update').removeAttr('disabled');
                            $('#product_id_update').append(html);

                        }, error: function (html) {
                            alert(html);
                        }
                    });

                });
            });
        </script>	
        <script>
            $('.select2m').select2({
                placeholder: "Select",
                width: 'auto',
                allowClear: true
            });
        </script>

    </body>
</html>