<?php
error_reporting(0);
ob_start();
session_start();
include_once("../include/dbconnect.php");
if($_GET['action']=="add"){
	 
  if(is_array($_FILES)) {
      $file = $_FILES['csv_upload']['tmp_name']; 
      $folderPath = "../products/csv_files/";
      $newfilename = date('his').'_csv_file.csv';
      $ext = pathinfo($_FILES['csv_upload']['name'], PATHINFO_EXTENSION);
      $fileType = $sourceProperties[2];

      if($ext == 'csv')
      {
        if (move_uploaded_file($file, $folderPath.$newfilename)) {

          $row = 0;
          if (($handle = fopen($folderPath.$newfilename, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
              $num = count($data);
              $row++;
              if($row > 1){
                $num = count($data);
                if(isset($data[0]))
                {  
                     $mec_catid = $_GET['mec_catid'];
                     $mec_subid = $_GET['mec_subid'];
                     $mec_subsubid = 0;             
                 $pname = $data[0];
                 $unit_type = $data[1];
                 $price = $data[2];
                 $quantity = $data[3];
                 $mec_description = $data[5]; 
                 $weight = $data[4];
                // echo "INSERT INTO  `mec_all_products` (`mec_catid`, `mec_subid`, `mec_subsubid`, `mec_brd_id`, `pname`, `unit_type`, `price`, `quantity`, `mec_description`, `prodimg1`, `prodimg2`, `prodimg3`, `prodimg4`, `prodimg5`, `weight`, `mec_status`, `orderset`)
                 ///VALUES ('".$mec_catid."',  '".$mec_subid."', '".$mec_subsubid."', 0, '".addslashes($pname)."',  '".addslashes($unit_type)."',  '".$price."',  '".$quantity."',  '".addslashes($mec_description)."', '', '', '', '', '', '', '0', '0')";
                 mysqli_query($conn, "INSERT INTO  `mec_all_products` (`mec_catid`, `mec_subid`, `mec_subsubid`, `mec_brd_id`, `pname`, `unit_type`, `price`, `quantity`, `mec_description`, `prodimg1`, `prodimg2`, `prodimg3`, `prodimg4`, `prodimg5`, `weight`, `mec_status`, `orderset`)
                  VALUES ('".$mec_catid."',  '".$mec_subid."', '".$mec_subsubid."', 0, '".addslashes($pname)."',  '".addslashes($unit_type)."',  '".$price."',  '".$quantity."',  '".addslashes($mec_description)."', '', '', '', '', '', '".$weight."', '0', '0')");                
                }
            
              }
            }
            fclose($handle);
          }              
        } else {
            echo "Upload failed";
  echo "<script>window.open('csv-upload.php?error=Upload failed&mec_catid=".$_GET['mec_catid']."&mec_subid=".$_GET['mec_subid'].",'_self')</script>";
        }
      }
      else{
       echo "<script>window.open('csv-upload.php?error=Upload CSV file&mec_catid=".$_GET['mec_catid']."&mec_subid=".$_GET['mec_subid'].",'_self')</script>";
      }
  }
  echo "<script>window.open('cat-products.php?action=addsuc&mec_catid=".$_GET['mec_catid']."&mec_subid=".$_GET['mec_subid'].",'_self')</script>";
}

?><!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">
                                             <h5>Upload CSV</h5>
                                            </div> 
                                            <form action="csv-upload.php?action=add&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="col-sm-3 col-form-label form-label-title"> 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="csv_upload">
                                                        </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-category.php?mec_catid=<?php echo $_GET['mec_catid']; ?>">  <button type="button" class="btn btn-outline">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>