<?php
error_reporting(0);
ob_start();
session_start();
include_once("include/dbconnect.php");
if(isset($_POST['btn_submit']))
{
    $cat_name = $_POST['cat_name'];
$cat_image = $_FILES['cat_image']['name'];
    mysqli_query($conn, "INSERT INTO mec_subsub_category (mec_catid,mec_subid,mec_subsub_name,mec_subsub_image,mec_status,orderset) VALUES ('".$_POST['mec_catid']."','".$_POST['mec_subid']."','".addslashes($cat_name)."','".$cat_image."',1,0)");
    $lastid = mysqli_insert_id($conn);
    if($_FILES['cat_image']['name']!="") {
       $file = $_FILES['cat_image']['tmp_name']; 
       $sourceProperties = getimagesize($file);
       $fileNewName = time();
       $folderPath = "../products/subsubcategory/";
       $ext = pathinfo($_FILES['cat_image']['name'], PATHINFO_EXTENSION);
       $imageType = $sourceProperties[2];

       switch ($imageType) {


           case IMAGETYPE_PNG:
               $imageResourceId = imagecreatefrompng($file); 
               $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
               imagepng($targetLayer,$folderPath. $lastid. ".jpg");
               
               break;


           case IMAGETYPE_GIF:
               $imageResourceId = imagecreatefromgif($file); 
               $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
               imagegif($targetLayer,$folderPath. $lastid. ".jpg");
                 
               break;


           case IMAGETYPE_JPEG:
               $imageResourceId = imagecreatefromjpeg($file); 
               $targetLayer = imageResize($imageResourceId,$sourceProperties[0],$sourceProperties[1]);
               imagejpeg($targetLayer,$folderPath. $lastid. ".jpg");
                
               break;
               


           default:
              header("location: sub-sub-category.php?mec_catid=".$_POST['mec_catid']."&mec_subid=".$_POST['mec_subid']."&action=addsuc");
              ?>
   <script > 
   window.location.replace("sub-sub-category.php?mec_catid=<?php echo $_POST['mec_catid'];?>&mec_subid=".$_POST['mec_subid']."&action=addsuc");
 </script>
    <?php
               exit;
               break;
       }


       move_uploaded_file($file, $folderPath. $lastid. "in.jpg");
       header("location: sub-sub-category.php?mec_catid=".$_POST['mec_catid']."&mec_subid=".$_POST['mec_subid']."&action=addsuc");
       ?>
   <script > 
   window.location.replace("sub-sub-category.php?mec_catid=<?php echo $_POST['mec_catid'];?>&mec_subid=".$_POST['mec_subid']."&action=addsuc");
 </script>
    <?php
   }

   
   header("location: sub-sub-category.php?mec_catid=".$_POST['mec_catid']."&mec_subid=".$_POST['mec_subid']."&action=addsuc");
   ?>
   <script > 
   window.location.replace("sub-sub-category.php?mec_catid=<?php echo $_POST['mec_catid'];?>&mec_subid=".$_POST['mec_subid']."&action=addsuc");
 </script>
    <?php
 
}
function imageResize($imageResourceId,$width,$height) {

    $targetWidth =721;
    $targetHeight =665;

    $targetLayer=imagecreatetruecolor($targetWidth,$targetHeight);
    imagecopyresampled($targetLayer,$imageResourceId,0,0,0,0,$targetWidth,$targetHeight, $width,$height);

    return $targetLayer;
}
if(isset($_POST['btn_submit']))
{
   ?>
   <script > 
  window.location.replace("sub-sub-category.php?mec_catid=<?php echo $_POST['mec_catid'];?>&mec_subid=".$_POST['mec_subid']."&action=addsuc");
</script>
   <?php
}
?><!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
    <!-- meta data -->
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="pixelstrap">
    <link rel="icon" href="assets/images/favicon.png" type="image/x-icon">
    <link rel="shortcut icon" href="assets/images/favicon.png" type="image/x-icon">
    <?php include_once("include/title.php"); ?>

    <!-- Google font -->
    <link href="../../css2?family=Public+Sans:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">

    <!-- Linear Icon css -->
    <link rel="stylesheet" href="assets/css/linearicon.css">

    <!-- Fontawesome css -->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/font-awesome.css">

    <!-- Themify icon css -->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/themify.css">

    <!--Dropzon css -->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/dropzone.css">

    <!-- Feather icon css-->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/feather-icon.css">

    <!-- remixicon css -->
    <link rel="stylesheet" type="text/css" href="assets/css/remixicon.css">

    <!-- Select2 css -->
    <link rel="stylesheet" type="text/css" href="assets/css/select2.min.css">

    <!-- Plugins css -->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/scrollbar.css">
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/animate.css">
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/chartist.css">
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/date-picker.css">

    <!-- Bootstrap css -->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/bootstrap.css">

    <!-- Bootstrap-tag input css -->
    <link rel="stylesheet" type="text/css" href="assets/css/vendors/bootstrap-tagsinput.css">

    <!-- App css -->
    <link rel="stylesheet" type="text/css" href="assets/css/style.css">
</head>

<body>
    <!-- tap on top start -->
    <div class="tap-top">
        <span class="lnr lnr-chevron-up"></span>
    </div>
    <!-- tap on tap end -->

    <!-- page-wrapper start -->
    <div class="page-wrapper compact-wrapper" id="pageWrapper">
        <!-- Page Header Start-->
        <?php include_once("include/header.php"); ?>
        <!-- Page Header Ends-->

        <!-- Page Body start -->
        <div class="page-body-wrapper">
            <!-- Page Sidebar Start-->
            <div class="sidebar-wrapper">
                <div id="sidebarEffect"></div>
                <div>
                <?php include_once("include/nav.php"); ?>
                </div>
            </div>
            <!-- Page Sidebar Ends-->

            <div class="page-body">

                <!-- New Product Add Start -->
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="row">
                                <div class="col-sm-8 m-auto">
                                    <div class="card">
                                        <div class="card-body">
                                            <div class="card-header-2">
                                            <?php
					                $cms_upt=mysqli_query($conn, "select * from mec_main_category  where mec_catid=".$_GET['mec_catid']."");
					                $cms_upt1=mysqli_fetch_array($cms_upt);
                                            ?>
                                             <?php
					                $cms_upt2=mysqli_query($conn, "select * from mec_sub_category  where mec_subid=".$_GET['mec_subid']."");
					                $cms_upt21=mysqli_fetch_array($cms_upt2);
                                            ?>
                                        <h5><?php echo $cms_upt1['mec_category_name']; ?> => <?php echo $cms_upt21['mec_sub_name']; ?></h5>
                                            </div>

                                            <form action="add-sub-sub-category.php?mec_catid=<?php echo $_GET['mec_catid'];?>&mec_subid=<?php echo $_GET['mec_subid'];?>" method="post" enctype="multipart/form-data" class="theme-form theme-form-2 mega-form">
                                                <div class="mb-4 row align-items-center">
                                                    <label class="form-label-title col-sm-3 mb-0">Category Name</label>
                                                    <div class="col-sm-9">
                                                        <input class="form-control" type="text" name="cat_name" placeholder="Category Name">
                                                    </div>
                                                </div>

                                                <div class="mb-4 row align-items-center">
                                                    <label class="col-sm-3 col-form-label form-label-title">Category
                                                        Image</label>
                                                    <div class="form-group col-sm-9">
                                                        <div class="dropzone-wrapper">
                                                            <div class="dropzone-desc">
                                                                <i class="ri-upload-2-line"></i>
                                                                <p>Choose an image file or drag it here.</p>
                                                            </div>
                                                            <input type="file" name="cat_image">
                                                        </div>
                                                    </div>
                                                </div>
                                                <input type="hidden" name="mec_catid" value="<?php echo $_GET['mec_catid'];?>">
                                                <input type="hidden" name="mec_subid" value="<?php echo $_GET['mec_subid'];?>">
                                                <div class="mb-4 row align-items-center"> 
                                                    <div class="col-12">
                                                        <div class="card-footer text-end border-0 pb-0 d-flex justify-content-end">
                                                            <button class="btn btn-primary me-3" type="submit" name="btn_submit">Save</button>
                                                            <a href="sub-sub-category.php?mec_catid=<?php echo $_GET['mec_catid'];?>&mec_subid=<?php echo $_GET['mec_subid'];?>">  <button class="btn btn-outline" type="button">Cancel</button></a>
                                                        </div>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- New Product Add End -->

                <!-- footer Start -->
                <div class="container-fluid">
                <?php include_once("include/footer.php"); ?>
                </div>
                <!-- footer En -->
            </div>
            <!-- Container-fluid End -->
        </div>
        <!-- Page Body End -->
    </div>
    <!-- page-wrapper End -->

    <!-- Modal Start -->
    <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
        <div class="modal-dialog  modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-body">
                    <h5 class="modal-title" id="staticBackdropLabel">Logging Out</h5>
                    <p>Are you sure you want to log out?</p>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>

                    <div class="button-box">
                        <button type="button" class="btn btn--no" data-bs-dismiss="modal">No</button>
                        <button type="button" class="btn  btn--yes btn-primary">Yes</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal End -->

    <!-- latest js -->
    <script src="assets/js/jquery-3.6.0.min.js"></script>

    <!-- Bootstrap js -->
    <script src="assets/js/bootstrap/bootstrap.bundle.min.js"></script>

    <!-- feather icon js -->
    <script src="assets/js/icons/feather-icon/feather.min.js"></script>
    <script src="assets/js/icons/feather-icon/feather-icon.js"></script>

    <!-- scrollbar simplebar js -->
    <script src="assets/js/scrollbar/simplebar.js"></script>
    <script src="assets/js/scrollbar/custom.js"></script>

    <!-- Sidebar js -->
    <script src="assets/js/config.js"></script>

    <!-- bootstrap tag-input js -->
    <script src="assets/js/bootstrap-tagsinput.min.js"></script>
    <script src="assets/js/sidebar-menu.js"></script>

    <!-- customizer js -->
    <script src="assets/js/customizer.js"></script>

    <!--Dropzon js -->
    <script src="assets/js/dropzone/dropzone.js"></script>
    <script src="assets/js/dropzone/dropzone-script.js"></script>

    <!-- Plugins js -->
    <script src="assets/js/notify/bootstrap-notify.min.js"></script>
    <script src="assets/js/notify/index.js"></script>

    <!-- select2 js -->
    <script src="assets/js/select2.min.js"></script>
    <script src="assets/js/select2-custom.js"></script>

    <!-- sidebar effect -->
    <script src="assets/js/sidebareffect.js"></script>

    <!-- Theme js -->
    <script src="assets/js/script.js"></script>

</body>

</html>