<?php include("includes/db_config.php");
if (isset($_POST['submit'])) {
// var_dump($_FILES);
extract($_POST);
date_default_timezone_set('Asia/Kolkata');
$datetime = date('Y-m-d H:i:s');
/*if($disc_type=="Percentage"){
$dis_price = $sale_price - ($sale_price * ($disc_sprice / 100));
}elseif($disc_type=="Amount"){
$dis_price = $sale_price - $disc_sprice;    
}else{
$dis_price = $sale_price;    
}
if($tax_rates == "None"){
$total=$dis_price;
}else{
$sqlct=mysqli_query($conn,"select * from tax_rate where id='".$tax_rates."'");
$resct= mysqli_fetch_array($sqlct);
$tax=$resct['tax_gst'];
$tax_rate=filter_var($tax, FILTER_SANITIZE_NUMBER_INT);
$tax=$dis_price*$tax_rate/100;
$total=$dis_price+$tax;
}*/
$sql="INSERT into ah_item_details(item_name,item_hsn,item_code,sale_price,tax_type,punit) values ('$item_name','$item_hsn','$item_code','$sale_price','$quantity','$punit')";
 if  (mysqli_query($conn, $sql)){
     $last_id= mysqli_insert_id($conn);
      if(count($_FILES['item_img']['name']) > 0)  {
            for($i = 0; $i < count($_FILES['item_img']['name']); $i++){
                $tmp_file = $_FILES['item_img']['tmp_name'][$i];
                $ext = pathinfo($_FILES["item_img"]["name"][$i], PATHINFO_EXTENSION);
                $rand = md5(uniqid().rand());
                $upd_image = $rand.".".$ext;
                move_uploaded_file($tmp_file,"images/item/".$upd_image);
    $sql_prd="INSERT into ah_item_images(item_id,image) values ('$last_id','$upd_image')";
    $prd_res=mysqli_query($conn, $sql_prd) or die(mysqli_error());
      }
    }        
     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");?>
    <style>
    .modal-dialog {
  max-width: 800px!important;
  margin-right: auto;
  margin-left: auto;
}</style>
</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">Add New Item</h3>
                                </div>
                                <div class="portlet-body">
                                    <form class="row g-3" action="" method="post" enctype="multipart/form-data">
                                        <div class="mb-3 col-md-6">
                                            <label class="form-label">Item Name</label>
                                            <input type="text" placeholder="Enter Item Name" name="item_name" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-3">
                                            <label class="form-label">Item HSN/SAC</label>
                                            <input type="text" name="item_hsn" placeholder="Enter Item HSN/SAC" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-3">
                                            <label class="form-label">Item Code</label>
                                            <input type="text" value="" name="item_code" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-3">
                                            <label class="form-label">Primary Unit</label>
                                            <select class="form-control puid" name="punit" id="uid">
                                                <option value="">Select</option>
                                                <?php $sql2="SELECT * from primery_unit";
                                                    $exe2=mysqli_query($conn,$sql2);
                                                    while($res2=mysqli_fetch_array($exe2)){ ?>
                                                <option value="<?= $res2['id'];?>" data-wd="<?= $res2['unit']; ?>"><?= $res2['unit'];?>(<?= $res2['short_name'];?>)</option>
                                                <?php } ?>
                                            </select>
                                        </div>
                                        <div class="mb-3 col-md-3">
                                            <label class="form-label">Secondary Unit</label>
                                            <select class="form-control sunit" name="sunit" id="sid">
                                                <option value="">Select</option>
                                            </select>
                                        </div>
                                        <div class="mb-3 col-md-3" id="crate" style="display: none;">
                                            <label class="form-label">Select Conversion Rate</label><br>
                                            <input type="radio" name="ctrate" > 1<span class="punit">  
                                            </span>= <input type="text" value="0.0" name="crate" ><span class="sunits"></span>
                                        </div> -->
                                        <hr/>
                                         <div class="portlet-header portlet-header-bordered">
                                            <h3 class="portlet-title">Pricing</h3>
                                        </div>
                                        <div class="row">
                                        <div class="mb-3 col-md-2">
                                            <label class="form-label">Sale Price(Without Tax)</label>
                                            <input type="text" name="sale_price" id="sale_price" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-1">
                                            <label class="form-label">Quantity</label>
                                            <input type="text" name="quantity" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-2">
                                            <label class="form-label">Unit</label>
                                            <select class="form-control puid" name="punit" id="uid">
                                                <option value="">Select</option>
                                                <?php $sql2="SELECT * from primery_unit";
                                                    $exe2=mysqli_query($conn,$sql2);
                                                    while($res2=mysqli_fetch_array($exe2)){ ?>
                                                <option value="<?= $res2['id'];?>" data-wd="<?= $res2['unit']; ?>"><?= $res2['unit'];?>(<?= $res2['short_name'];?>)</option>
                                                <?php } ?>
                                            </select>
                                        </div> 
                                        
                                      <div class="mb-3 col-md-1">
                                            <label class="form-label">Tax Rate</label>
                                            <select class="form-control" name="tax_rates" id="tax_rates">
                                                <option value="">Select</option>
                                                <option value="None">None</option>
                                                <?php $sql2="SELECT * from tax_rate";
                                                    $exe2=mysqli_query($conn,$sql2);
                                                    while($res2=mysqli_fetch_array($exe2)){ ?>
                                                <option value="<?= $res2['id'];?>"><?= $res2['tax_gst'];?></option>
                                                <?php } ?>
                                            </select>
                                        </div>  -->
                                      <div class="mb-3 col-md-2">
                                            <label class="form-label">Sale Price(With Tax)</label>
                                            <input type="text" name="sale_price" class="form-control" readonly>
                                        </div>
                                          <div class="mb-3 col-md-2">
                                            <label class="form-label">Disc. On Sale Price</label>
                                            <input type="text" name="disc_sprice" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-1">
                                            <label class="form-label">Disc. Type</label>
                                            <select class="form-control" name="disc_type">
                                                <option value="">Select</option>
                                                <option>Percentage</option>
                                                <option>Amount</option>
                                            </select>
                                        </div> -->
                                          <div class="col-md-1"><button type="button" class="btn btn-sm btn-success" style="margin-top:40px">+Add More</button></div>-->
                                        </div> 
                                        <hr/>
                                        <div class="mb-3 col-md-2">
                                            <label class="form-label">Wholesale Price(Without Tax)</label>
                                            <input type="text" name="wholesale_price" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-2">
                                            <label class="form-label">Tax Type</label>
                                            <select class="form-control" name="wtax">
                                                <option value="">Select</option>
                                                <option>With Tax</option>
                                                <option>Without Tax</option>
                                            </select>
                                        </div>
                                         <div class="mb-3 col-md-2">
                                            <label class="form-label">Min Wholesale Qty.</label>
                                            <input type="text" name="min_wqty" class="form-control">
                                        </div> -->
                                        <hr/>
                                       <div class="mb-3 col-md-4">
                                            <label class="form-label">Purchase Price</label>
                                            <input type="text" name="purchase_price" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-6">
                                            <label class="form-label">Select Tax Rate</label>
                                            <select class="form-control" name="tax_rates">
                                                <option value="">Select</option>
                                                <option value="None">None</option>
                                                <?php $sql2="SELECT * from tax_rate";
                                                    $exe2=mysqli_query($conn,$sql2);
                                                    while($res2=mysqli_fetch_array($exe2)){ ?>
                                                <option value="<?= $res2['id'];?>"><?= $res2['tax_gst'];?></option>
                                                <?php } ?>
                                            </select>
                                        </div> 
                                        <hr/>
                                        <div class="clearfix"></div>
                                         <div class="portlet-header portlet-header-bordered">
                                            <h3 class="portlet-title">Stock</h3>
                                        </div>
                                        <div class="mb-3 col-md-4">
                                            <label class="form-label">Opening Stock</label>
                                            <input type="tel" name="opening_stock" placeholder="Opening Stock" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-4">
                                            <label class="form-label">At Price/Unit</label>
                                            <input type="text" name="at_price_unit" placeholder="At Price/Unit" class="form-control">
                                        </div>
                                       
                                        <div class="mb-3 col-md-4">
                                            <label class="form-label">Min Stock Quantity</label>
                                            <input type="text" class="form-control" name="min_stock_quatity" value="">
                                        </div> -->
                                       <div class="mb-3 col-md-4">
                                            <label class="form-label">Date</label>
                                            <input type="date" placeholder="" name="item_date" class="form-control">
                                        </div>
                                        <div class="mb-3 col-md-4">
                                            <label class="form-label">Item Location</label>
                                            <input type="text" class="form-control" name="item_location" value="">
                                        </div>
                                        <div class="mb-3 col-md-4">
                                            <label class="form-label">Attachment Item Images<small>(Multiple Images Max.5)</small>)</label>
                                            <input type="file" name="item_img[]" multiple class="form-control">
                                        </div> -->
                                        <div class="clearfix"></div>
                                        <div class="col-md-4"><button name="submit" type="submit" class="btn btn-primary btn-lg">Save</button></div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include("includes/footer.php");?>
        </div>
    </div>
<?php include("includes/js.php");?>
<script>
$(document).ready(function(){
    $('#uid').on('change', function(){
        var uid = $(this).val();
       // alert(cat_id);
        if(uid){
            $.ajax({
                type:'POST',
                url:'ajax_get_item.php',
                data:'uid='+uid,
                success:function(html){
                    $('#sid').html(html);
                   // console.log(html);
                   // $('#city').html('<option value="">Select Division</option>'); 
                }
            }); 
        }
    });
});
 $(document).ready(function(){
    $("#uid").change(function(){
        $("#crate").toggle();
    });
});
 $(function() {
$('body').on('change', '.puid', function() {
   data_uid = $(this).find(':selected').data('wd');
    $(".punit").text(data_uid);
});
}); 
 $(function() {
$('body').on('change', '.sunit', function() {
   data_suid = $(this).find(':selected').data('us');
    $(".sunits").text(data_suid);
});
}); 
 (function() {
    "use strict";
    $("body").on("change", ".tax_gsts", function() {    
        var row = $(this).closest(".container-fluid");
        var gst_id=$(this).val();
        var price = parseFloat(row.find(".sale_price").val());
        $.ajax({
            type:'post',
            url:'get_gsts_price.php',
            data:{gst_id: gst_id , price: price},
            dataType:'json',
            success:function(data){
                console.log(data);
                 row.find('#ttl_amt').val(data.total); 
                 row.find('#ds_amt').val(data.taxs); 
                // row.find('#amount').val(data.taxs); 
                //$('#container').container('show'); 
                 someFunction();
            },
            error:function(){}
        });
    });
  })(); 
    </script>
</body>

</html>