<?php 
include "config.php";
error_reporting(0);
?>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="datatable/data.css">
<script src="datatable/data.js" type="text/javascript"></script>
<script src="datatable/table.js" type="text/javascript"></script>
<script src="datatable/datatable.js"></script>
<link rel="stylesheet" href="datatable/data-boot.css">
<script src="restriction/product_restriction.js"></script>

<style>
    
    #navbar
    {
        flex:15%;
       
        flex-direction:row;
        overflow:auto;
        
    }
    #content
    {
        flex:90%;
        overflow:auto;
        padding-left:10px;
        padding-right:10px;
    }
    #fullbody
    {
        display:flex;
        
         height:100%;         
    }
    label
    {
      font-size:20px;
    }
    .edit
    {
        font-size:18px;
        color:green;
        cursor:pointer;
    }
    .dele
    {
        font-size:18px;
        color:red;
        cursor:pointer;
    }
    .edit:hover
    {
        font-size:20px;
        
    }
    .dele:hover
    {
        font-size:20px;
        color:red;
        cursor:pointer;
        
    }
    td
    {
      text-align:center;
    }
    th
    {
      text-align:center;
    }
    @media (max-width: 800px) {
        #fullbody
    {
        display:flex;
        flex-wrap: wrap;
                 
    }
        #navbar,#content {
    flex: 100%;
  }
}
</style>
<script>
$(document).ready( function () {
    $('#protable').DataTable();
} );
</script>
</head>
<body>
    <div ><?php include "navbar.php"?></div>

    <div id="fullbody"> 

<div id="content">
<br>
<h3>Product-Master</h3>
<br>
<?php 
        $userall=$_SESSION['log'];
        $usper = mysqli_query($connect,"select * from userpermission where username = '$userall'  && filename='productmaster'");
while($per=mysqli_fetch_array($usper))
{
          ?>
<?php if($per['dataadd'] =='1'){ ?>
<input type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal"  value="+ Add" style="float:right;margin-right:10px;">
<br><?php }?><br>
<!-- table -->
<div>
<table class="table table-bordered table-hover " style="border:1px solid lightgrey" id="protable">
    <thead >
<tr>
  <th>S.no</th>
    <th>Code</th>
    <th>Name</th>
    <th>Price</th>
    <th>Hsn</th>
    <th>Unit</th>
    <th>Tax</th>
    <th <?php if($per['dataedit'] !='1'){ echo 'style="display:none"';} ?>>Edit</th>
    <th <?php if($per['datadelete'] !='1'){ echo 'style="display:none"';} ?>>Delete</th>
</tr>

    </thead>
    <tbody>
<?php 
$pro=mysqli_query($connect,"select * from productdetail where status !='deleted' order by id desc");
while($proshow = mysqli_fetch_array($pro))
{
?>
        <tr>
          
            <td><?php echo $proshow['id'] ?></td>
            <td><?php echo $proshow['productcode'] ?></td>
            <td><?php echo $proshow['productname'] ?></td>
            <td><?php echo $proshow['price'] ?></td>
            <td><?php echo $proshow['hsn'] ?></td>
            <td><?php echo $proshow['unit'] ?></td>
            <td><?php echo $proshow['tax'] ?></td>
            <td <?php if($per['dataedit'] !='1'){ echo 'style="display:none"';} ?>><a data-toggle="modal" data-target="#edit<?php echo $proshow['productcode']?>"><i class="fas fa-edit edit"></i></a></td>
            <td <?php if($per['datadelete'] !='1'){ echo 'style="display:none"';} ?>><a data-toggle="modal" data-target="#delete<?php echo $proshow['productcode']?>"><i  class="fas fa-trash-alt dele"></a></i></td>
        </tr>
        <?php
      } 
    }
      ?>
    </tbody>



</table>
</div>

<!-- modal -->
<div class="modal fade" id="myModal">
    <div class="modal-dialog modal-dialog-centered modal-lg">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Add Product</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
            <!--  -->
            <form action="insertproduct.php" method="post" name="productmaster">
              <?php 
              $code = mysqli_query($connect,"select max(id)as count from productdetail ");
              while($row=mysqli_fetch_array($code))
              {
                $code = $row['count'];
              $codeco = sprintf("%04d",$code + 1);

              }
              ?>
              <input type="hidden" class="form-control" autocomplete="off" value="PRD-<?php echo $codeco ?>" name="productcode" placeholder="Name">
			  
			  
			  <div class="row">
              <div class="col-sm-3"><label>SAP Code</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" autocomplete="off" name="sapcode" placeholder="sapcode" id="sapcode"></div>
              
            </div><br>
			  
          <div class="row">
              <div class="col-sm-3"><label>Product Name</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" autocomplete="off" name="productname" placeholder="Name" id="productnameadd"></div>
              
            </div><br>
            <?php 
$proname_val=mysqli_query($connect,"select * from productdetail order by id desc");
while($proname_val_show = mysqli_fetch_array($proname_val))
{
?>
<input type="hidden" class="productNameValue" value="<?php echo $proname_val_show['productname'] ?>">
<?php
}
?>
<script>
  var proName = document.querySelectorAll(".productNameValue");
  var proAdd = document.getElementById("productnameadd");
  proAdd.addEventListener("keyup",function(e){
    //console.log("a");
   var product = e.target.value.toLowerCase();
   proName.forEach((list)=>{
    if(product.replace(/ /g,"") == list.value.toLowerCase().replace(/ /g,"")){
      document.getElementById("alertUser").click();
      proAdd.value="";
   }
   });
     
  });
  console.log(userName);
  </script>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Hsn</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" autocomplete="off" name="hsn" placeholder="HSN" id="hsnadd"></div>
            </div><br>
            <!--  -->
            <script>
              $(document).ready(function(){
              $(".tonn").click(function(){
              var unitval= "1000";
              $(".unitconvert").val(unitval);
              $(".unitconvert").attr("disabled","false");
              });
              $(".kgs").click(function(){
              var unitval= "5400";
              $(".unitconvert").val(unitval);
              $(".unitconvert").prop("disabled",false);
              });
			  $(".kg").click(function(){
              var unitval= "1";
              $(".unitconvert").val(unitval);
              $(".unitconvert").prop("disabled",false);
              });
              });
            </script>
            <div class="row">
              <div class="col-sm-3"><label>Units</label></div>:
              <div class="col-sm-2"><input type="radio" class="tonn uni"autocomplete="off" name="unit" placeholder="Name" value="TONN"><label>Tonn</label></div>
              <div class="col-sm-2"><input type="radio" class="kgs uni" autocomplete="off" name="unit" placeholder="Name" value="UNIT"><label>Unit</label></div>
			   <!-- <div class="col-sm-2"><input type="radio" class="kg uni" autocomplete="off" name="unit" placeholder="Name" value="KG"><label>Kg</label></div> -->
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Price</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" autocomplete="off" name="price" placeholder="Price" id="priceadd"></div>
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Unit to Kg</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control unitconvert" autocomplete="off" name="unittokg" id="unitconversionadd" placeholder="unit to kg"></div>
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Tax</label></div>:
              <div class="col-sm-6"><select class="form-control selectpro" autocomplete="off" name="tax" placeholder="Tax" id="taxadd">
<option value="">--select tax --</option>
              <option>5</option>
              <option>12</option>
              <option>18</option>
              <option>28</option>
</select></div>
            </div><br>
			<div class="row">
              <div class="col-sm-3"><label>Product&nbsp;Category</label></div>:
              <div class="col-sm-6"><select class="form-control " autocomplete="off" name="product_category" placeholder="product_category" id="product_category" required>
<option value="">--select category --</option>
              <option>Sand</option>
              <option>Jally</option>
			  <option>Boulders</option>
              <option>Others</option>
              
</select></div>
            </div><br>
            <!--  -->
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <input type="button" class="btn btn-success" data-toggle="modal" data-target="#addpro" value="ADD" onMouseOver="sub()" id="sumbitbut" onKeyUp="sub()">
        </div>
        <!-- modal for add -->
<div class="modal fade" id="addpro">
    <div class="modal-dialog modal-sm">
      <div class="modal-content" style="background-color:rgba(77, 219, 106,0.9)">
      
        <!-- Modal Header -->
        
        
        <!-- Modal body -->
        <div class="modal-body">
          <span style="color:white"><i class="fas fa-check" style="font-size:24px"></i> Product Have Been Created Successfully</span>
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="submit" type="button" class="btn btn-warning" >Ok</button>
        </div>
</form>
      </div>
    </div>
  </div>
  
<!-- modal for add -->
      </div>
    </div>
  </div>
<!-- modal -->
<!-- modal for edit -->
<?php 
$proedit=mysqli_query($connect,"select * from productdetail order by id desc");
while($proshowedi = mysqli_fetch_array($proedit))
{
?>
<form action="updateproduct.php" method="post">
<div class="modal fade" id="edit<?php echo $proshowedi['productcode']?>">
    <div class="modal-dialog modal-dialog-centered modal-lg">
      <div class="modal-content">
      
        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Edit Product</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>
        
        <!-- Modal body -->
        <div class="modal-body">
            <!--  -->
			
			<div class="row">
              <div class="col-sm-3"><label>SAP Code</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" autocomplete="off" name="sapcodeedit" placeholder="sapcode" id="sapcodeedit" value="<?php echo $proshowedi['sap_code'] ?>"></div>
              
            </div><br>
			
          <div class="row">
              <div class="col-sm-3"><label>Product Name</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" autocomplete="off" name="productnameedit" value="<?php echo $proshowedi['productname'] ?>" placeholder="Name">
              <input type="hidden" class="form-control" autocomplete="off" name="productcodeedit" value="<?php echo $proshowedi['productcode'] ?>" placeholder="Code">
            </div>
              
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Hsn</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" autocomplete="off" name="hsnedit" value="<?php echo $proshowedi['hsn'] ?>"placeholder="HSN"></div>
            </div><br>
            <!--  -->
            <script>
              $(document).ready(function(){
              $(".tonnedit").click(function(){
              var unitval= "1000";
              $(".unitconvertedit").val(unitval);
              $(".unitconvertedit").attr("disabled",false);
              });
              $(".kgsedit").click(function(){
              var unitval= "5400";
              $(".unitconvertedit").val(unitval);
              $(".unitconvertedit").prop("disabled",false);
              });
			   $(".kgedit").click(function(){
              var unitval= "1";
              $(".unitconvertedit").val(unitval);
              $(".unitconvertedit").prop("disabled",false);
              });
              });
            </script>
            <div class="row">
              <div class="col-sm-3"><label>Units</label></div>:
              <div class="col-sm-2"><input type="radio" class="tonnedit uniii" autocomplete="off" name="unitedit<?php echo$proshowedi['productcode'] ?>" placeholder="Name" value="TONN"<?php echo ($proshowedi['unit']== 'TONN') ?  "checked" : "" ;  ?>><label>Tonn</label></div>
              <div class="col-sm-2"><input type="radio" class="kgsedit uniii" autocomplete="off" name="unitedit<?php echo$proshowedi['productcode'] ?>" placeholder="Name" value="UNIT"<?php echo ($proshowedi['unit']== 'UNIT') ?  "checked" : "" ;  ?>><label>Unit</label></div>
			  <!-- <div class="col-sm-2"><input type="radio" class="kgedit uniii" autocomplete="off" name="unitedit<?php echo$proshowedi['productcode'] ?>" placeholder="Name" value="KG"<?php echo ($proshowedi['unit']== 'KG') ?  "checked" : "" ;  ?>><label>Kg</label></div> -->
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Price</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control" value="<?php echo $proshowedi['price'] ?>" autocomplete="off" name="priceedit" placeholder="Price"></div>
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Unit to Kg</label></div>:
              <div class="col-sm-6"><input type="text" class="form-control unitconvertedit" value="<?php echo $proshowedi['unittokg'] ?>" autocomplete="off" name="unittokgedit" placeholder="unit to kg"></div>
            </div><br>
            <!--  -->
            <div class="row">
              <div class="col-sm-3"><label>Tax</label></div>:
              <div class="col-sm-6"><select class="form-control selectpro" autocomplete="off" name="taxedit" placeholder="Tax">
<option><?php echo $proshowedi['tax'] ?></option>
              <option>5</option>
              <option>12</option>
              <option>18</option>
              <option>28</option>
</select></div>
            </div><br>
				<div class="row">
              <div class="col-sm-3"><label>Product&nbsp;Category</label></div>:
              <div class="col-sm-6"><select class="form-control " autocomplete="off" name="product_category" placeholder="product_category" id="product_category" required>
<option ><?php echo $proshowedi['product_category'] ?></option>
              <option>Sand</option>
              <option>Jally</option>
			  <option>Boulders</option>
              <option>Others</option>
              
</select></div>
            </div><br>
            <!--  -->
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <input type="button"  class="btn btn-success" data-toggle="modal" data-target="#editpro<?php echo $proshowedi['productcode'] ?>" value="Update" >
        </div>
        <!-- modal for add -->
<div class="modal fade" id="editpro<?php echo $proshowedi['productcode'] ?>">
    <div class="modal-dialog modal-sm">
      <div class="modal-content" style="background-color:rgba(77, 219, 106,0.9)">
      
        <!-- Modal Header -->
        
        
        <!-- Modal body -->
        <div class="modal-body">
          <span style="color:white"><i class="fas fa-check" style="font-size:24px"></i> Product Have Been Updated Successfully</span>
        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="submit" type="button" class="btn btn-warning" >Ok</button>
        </div>
</form>
      </div>
    </div>
  </div>
  
<!-- modal for add -->
      </div>
    </div>
    
  </div>
  <?php
}
  ?>
  <!--  -->
<!-- modal for edit -->
<!-- modal for delete -->
<!-- The Modal -->
<?php 
$prodele=mysqli_query($connect,"select * from productdetail order by id desc");
while($proshowdele = mysqli_fetch_array($prodele))
{
?>
<div class="modal fade" id="delete<?php echo $proshowdele['productcode']?>">

    <div class="modal-dialog modal-sm">
      <div class="modal-content" style="background-color:rgba(247, 2, 80,0.9)">
      
        <!-- Modal Header -->
        <form action="deleteproduct.php" method="post">
        <input type="hidden" value="<?php echo $proshowdele['productcode']?>" name="delete">
        <!-- Modal body -->
        <div class="modal-body" >
         Are You Sure Want To Delete !.

        </div>
        
        <!-- Modal footer -->
        <div class="modal-footer">
        <button type="submit" class="btn btn-success">Yes</button>
          <button type="button" class="btn btn-warning" data-dismiss="modal">No</button>
</form>
        </div>
        
      </div>
    </div>
  </div>
  <?php 
}
  ?> 
<!-- ,odal for delete -->
<!-- table -->


<!-- main-content-div -->
</div>
<!-- alert producttype-->
<button type="button" class="btn btn-primary alertunit" data-toggle="modal" data-target="#myunit" style="display:none">
      </button>

  <!-- The Modal -->
  <div class="modal fade" id="myunit">
    <div class="modal-dialog modal-lg">
     <div class="alert alert-danger alert-dismissible fade show">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <strong>Please Select Product Type</strong>
  </div>
  </div>
        </div>
<!-- alert -->

<!-- main-content-div -->
</div>
<!-- modal to blockdate -->
<button type="button" class="btn btn-primary" id="alertUser" data-toggle="modal" data-target="#advanceLimit" style="display:none">
      </button>

  <!-- The Modal -->
  <div class="modal fade" id="advanceLimit">
    <div class="modal-dialog modal-lg">
     <div class="alert alert-danger alert-dismissible fade show">
    <button type="button" class="close"  data-dismiss="alert">&times;</button>
    <strong>Productname Already Exist</strong>
  </div>
  </div>
  </div>
        
<!-- alert -->
</body>