<?php 
if(!isset($_SESSION)) { 
  session_start(); 
  include('../includes/dbcon.php');
}
if(empty($_SESSION['rm_id'])):
header('Location:../index.php');
endif;

//$item_id
if(isset($_REQUEST['item_id']))

  {
 
    $rid=$_GET['item_id'];

    $stringParts = explode("*", $rid);

    $item_id  = $stringParts[0];

    $loc_status= $stringParts[1];

   
    if ($loc_status=="Yes"){
      $cval=0;
    }
    else {
      $cval=1;
    }
    mysqli_query($con,"update  item_master set on_visi=$cval where item_id=$item_id ")or die(mysqli_error());





}

?>
<!DOCTYPE html>
<html lang="en">


<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>SalesHub | Item Management </title>
  <!-- plugins:css -->
  <?php include('../includes/commonheader.php');?>

</head>
<body>
 <?php include('../includes/header.php');?>
 <?php include('../includes/leftsidebar.php');?>
           <!-- partial -->
 <div class="main-panel">        
        <div class="content-wrapper">      
             
         <div class="col-12 grid-margin">
              <div class="card">
                <div class="card-body">
                    <form method="post" action="admin_item_view_pdf.php" enctype="multipart/form-data" target="_blank">

 <button type="submit" class="btn btn-primary">PDF</button>
 </form>
<div class="table-responsive">
                    <table id="order-listing" class="table">
                       <thead>
  <tr class="bg-primary text-white">
   
    <th width="7%">#</th>
    <th width="10%">Item Name</th>
    <th width="10%">Description</th>
   
    <th width="10%">Sales Price</th> 
    <th width="10%">Cost Price</th>   
    <th width="4%">QOH</th> 
    <th width="4%">ROL</th> 
    <th width="4%">Image</th> 
    <th width="4%">Online Visible</th> 
    
    <th width="25%">Edit</th>   

   
    </tr>
  </thead>
<tbody>
<?php
$rcount=0;

//echo$tt;

$sql="SELECT * from view_item_master"  ;

$query=mysqli_query($con,$sql)or die(mysqli_error()); 

              while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
                $item_name=$row['item_name']; 
                 $item_id=$row['item_id']; 
                $description=$row['description']; 
                $cat_des=$row['cat_des'];
                $type_des=$row['type_des'];
                $sp=$row['sales_price'];
                $cp=$row['cost_price'];
                $qoh=$row['qoh'];
                $rol=$row['rol'];
                 $cat_id=$row['cat_id'];
                  $type_id=$row['type_id'];
                 $case_id=$row['case_id'];
                 $Weight=$row['Weight'];
                  $unit_des=$row['unit_des'];
                  $on_visi=$row['on_visi'];
                  
                   if ($on_visi==1) {
                    $online_visible="Yes";
                }
             
                if ($on_visi==0) {
                    $online_visible="No";
                }
                 $image = "uploads";
              ?>   

               <tr>       
                  <td> <?php echo $rcount;   ?></td>
            <td> <?php echo $item_name;   ?></td>
             <td> <?php echo $description;   ?></td>
         
           <td> <?php echo $sp;   ?></td>
            <td><?php echo $cp; ?></td>
            <td> <?php echo  $qoh;   ?></td>
            <td><?php echo $rol; ?></td>  
                     <td>
            <?php if (!empty($image)) { ?>
                <img src="uploads/<?php echo $image; ?>" alt="Item Image" width="50" height="50">
            <?php } else { ?>
                <img src="uploads/default.png" alt="No Image" width="50" height="50">
            <?php } ?>
            <br>
            <!-- Button to Open Upload Modal -->
            <button class="btn btn-sm btn-primary" data-toggle="modal" data-target="#uploadModal<?php echo $item_id; ?>">Add Image</button>
        </td>
             
                 <!-- Modal for Image Upload -->
    <div class="modal fade" id="uploadModal<?php echo $item_id; ?>" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Upload Image for <?php echo $item_name; ?></h5>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <form action="upload_image.php" method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="item_id" value="<?php echo $item_id; ?>">
                        <input type="file" name="image" class="form-control" required>
                        <br>
                        <button type="submit" class="btn btn-success">Upload</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
             
             
             
             
             
             
             
             
             
             
             <td><?php if ($on_visi>0) { ?> &#10004;<?php } else { ?> &#10006; <?php } ?><a href="admin_item_view.php?item_id=<?php echo $row['item_id'];echo "*";echo $online_visible;?>"> Change</a></td>



<td>
       <a href="#addvoucher<?php echo $row['item_id'];?>" data-target="#addvoucher<?php echo $row['item_id'];?>"data-toggle="modal"     </div> <i class="fas fa-edit "></i>  </i> </a> </td>
              

</td>
 <!----------------------Model ------------------------------->
     <div id="addvoucher<?php echo $row['item_id'];?>" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
          <div class="modal-dialog" style="width:80%; aria-hidden="true>
          <div class="modal-content" style="height:auto">        
          <div class="modal-body">
  
             <form class="form-horizontal" method="post" action="admin_item_edit.php" enctype='multipart/form-data'>
      <div class="form-group">
                     <label for="exampleInputPassword1">Item Name</label>
                <input type="text" class="form-control" id="itemname_edit" name="itemname_edit" value="<?php echo $item_name; ?>">
              <input type="hidden" class="form-control" id="itemid_edit" name="itemid_edit" value="<?php echo $row['item_id'];?>">

                </div>  
 <div class="form-group">
                     <label for="exampleInputPassword1">Item Des</label>
                <input type="text" class="form-control" id="itemdes_edit" name="itemdes_edit" value="<?php echo $description; ?>">
      

                </div>  



               <div class="form-group">
              <label for="exampleInputEmail1">Category </label>
         

               <select class="form-control select2" style="width: 100%;" name="cat_edit" id="cat_edit" required >           
          <?php            
          $querydoc=mysqli_query($con,"select * from item_category"
)or die(mysqli_error());          
          while($rowr=mysqli_fetch_array($querydoc)){
            if ($row['cat_id']==$rowr['cat_id']){ ?>            
            <option value="<?php echo $rowr['cat_id'];?>" selected><?php echo $rowr['cat_des'];?></option>
            <?php } else {
          ?>
          <option value="<?php echo $rowr['cat_id'];?>"><?php echo $rowr['cat_des'];?></option>        
          <?php } }?>
          </select> 



               </div>


                  <div class="form-group">
             

                   
<div class="form-group">
                  <label for="exampleInputPassword1">Item Type</label>
 <select class="form-control select2" style="width: 100%;" name="item_type_edit" id="item_type_edit" required >           
          <?php            
          $querydoc=mysqli_query($con,"select * from item_type"
)or die(mysqli_error());          
          while($rowd=mysqli_fetch_array($querydoc)){
            if ($row['type_id']==$rowd['type_id']){ ?>            
            <option value="<?php echo $rowd['type_id'];?>" selected><?php echo $rowd['type_des'];?></option>
            <?php } else {
          ?>
          <option value="<?php echo $rowd['type_id'];?>"><?php echo $rowd['type_des'];?></option>        
          <?php } }?>
          </select>                  
</div>
                </div>  


                <div class="form-group">
                     <label for="exampleInputPassword1">Sales Price</label>
                <input type="text" class="form-control" id="sp_edit" name="sp_edit" value="<?php echo $sp; ?>">    
                </div> 

                <div class="form-group">
                     <label for="exampleInputPassword1">Cost Price</label>
                <input type="text" class="form-control" id="cp_edit" name="cp_edit" value="<?php echo $cp; ?>">    
                </div> 

                 <div class="form-group">
                     <label for="exampleInputPassword1">QOH</label>
                <input type="text" class="form-control" id="qoh_edit" name="qoh_edit" value="<?php echo $qoh; ?>">    
                </div>

                     <div class="form-group">
                     <label for="exampleInputPassword1">ROL</label>
                <input type="text" class="form-control" id="rol_edit" name="rol_edit" value="<?php echo $rol; ?>">    
                </div>


                     <div class="form-group">
                     <label for="exampleInputPassword1">Weight</label>
                <input type="text" class="form-control" id="weight_edit" name="weight_edit" value="<?php echo $Weight; ?>">    
                </div>




                   <div class="modal-footer">
                   <button type="submit" class="btn btn-primary">Save changes</button>
                   <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                   </div>                   
          </form>

   


          </div>
        </div>
      </div>
    </div>
<!--------------- End of Model ------------------------------------>






                    </tr>                
                    <?php }  ?>
                    </tbody>
                    </table>
                  </div>
 </div> </div> </div>

        </div>  <!----- <div class="content-wrapper">------>
  </div>

 </div><!-----End of main panel--~~~~~~~~~~~~~~~~~~~~~~~--------------->
 </div><!-----Dont Delete this DIV--~~~~~~~~~~~~~~~~~~~~~~~--------------->
  <!-- container-scroller -->
<?php include('../includes/footer.php');?>
<?php include('../includes/commonjs.php');?>
 
</body>


</html>

<script>
    $( document ).ready(function() {
         // alert("Please Select Order Type");

    });



</script>

 <!-- End custom js for this page-->
  <script>
  $(function () {
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })
  })

</script>



