<?php include("includes/db_config.php"); 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
extract($_POST);
date_default_timezone_set('Asia/Kolkata');
$date = date('Y-m-d H:i:s');
$file = $_FILES["itemfile"]["tmp_name"];
$file_open = fopen($file,"r");
while (($csv = fgetcsv($file_open, 10000, ",", '"')) !== FALSE) {
 $item_name = !empty($csv[0]) ? $csv[0] : '';
 $item_hsn = !empty($csv[1]) ? $csv[1] : '';        
 $item_code = !empty($csv[2]) ? $csv[2] : '';        
 $sale_price = !empty($csv[3]) ? $csv[3] : '';        
 $purchase_price = !empty($csv[4]) ? $csv[4] : '';        
 $tax_rate = !empty($csv[5]) ? $csv[5] : '';        
 $opening_stock = !empty($csv[6]) ? $csv[6] : '';        
 $at_price_unit = !empty($csv[7]) ? $csv[7] : '';        
 $item_date = !empty($csv[8]) ? $csv[8] : '';        
 $min_stock_quatity = !empty($csv[9]) ? $csv[9] : '';        
 $item_location = !empty($csv[10]) ? $csv[10] : '';        
$sql="INSERT into ah_item_details(item_name,item_hsn,item_code,sale_price,tax_type,purchase_price,tax_rate) values ('$item_name','$item_hsn','$item_code','$sale_price','$tax_type','$purchase_price','$tax_rate')";
 if(mysqli_query($conn, $sql)){
 $last_id= mysqli_insert_id($conn);   
    $sql_prds="INSERT into ah_item_tracking_details(item_id,opening_stock,at_price_unit,item_date,min_stock_quatity,item_location) values ('$last_id','$opening_stock','$at_price_unit','$item_date','$min_stock_quatity','$item_location')";
    $prd_ress=mysqli_query($conn, $sql_prds);           
     echo "<script>location.replace('all-item.php');</script>";  
  }
 }
}
?><!DOCTYPE html>
<html lang="en" dir="ltr" data-theme="light">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <?php include("includes/css.php");?> 
</head>

<body class="preload-active aside-active aside-mobile-minimized aside-desktop-maximized">
    <div class="holder">
        <?php include("includes/sidebar.php");?>
        <div class="wrapper">
            <?php include("includes/header.php");?>
            <div class="content">
                <div class="container-fluid g-5">
                    <div class="row">
                        <div class="col-12">
                            <div class="portlet">
                                <div class="portlet-header portlet-header-bordered">
                                    <h3 class="portlet-title">Import & Export Item</h3>
                                </div>
                                <div class="portlet-body">
                                    <div class="row">
                                        <div class="mb-3 col-md-4 text-center">
                                        <div class="dwldbox">
                                            <h3>Download .xls file(excel file)</h3>
                                            <br/>
                                            <button type="button" class="btn btn-primary btn-lg">Download</button>
                                        </div>
                                        </div>
                                        
                                        <div class="mb-3 col-md-8">
                                        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data"> 
                                            <div class="file-upload">
                                                <button class="file-upload-btn" type="submit" >Add Image</button>

                                                <div class="image-upload-wrap">
                                                    <input class="file-upload-input" name="itemfile" type='file' />
                                                    <div class="drag-text">
                                                        <h3>Drag and drop a File(excel file)</h3>
                                                    </div>
                                                </div>
                                                <div class="file-upload-content">
                                                    <img class="file-upload-image" src="#" alt="your image" />
                                                    <div class="image-title-wrap">
                                                        <button type="button" onclick="removeUpload()" class="remove-image">Remove <span class="image-title">Uploaded Image</span></button>
                                                    </div>
                                                </div>
                                            </div>
                                          </form>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include("includes/footer.php");?>
        </div>
    </div>
    <?php include("includes/js.php");?>
    <script>
        function readURL(input) {
            if (input.files && input.files[0]) {

                var reader = new FileReader();

                reader.onload = function(e) {
                    $('.image-upload-wrap').hide();

                    $('.file-upload-image').attr('src', e.target.result);
                    $('.file-upload-content').show();

                    $('.image-title').html(input.files[0].name);
                };

                reader.readAsDataURL(input.files[0]);

            } else {
                removeUpload();
            }
        }

        function removeUpload() {
            $('.file-upload-input').replaceWith($('.file-upload-input').clone());
            $('.file-upload-content').hide();
            $('.image-upload-wrap').show();
        }
        $('.image-upload-wrap').bind('dragover', function() {
            $('.image-upload-wrap').addClass('image-dropping');
        });
        $('.image-upload-wrap').bind('dragleave', function() {
            $('.image-upload-wrap').removeClass('image-dropping');
        });
    </script>
</body>

</html>