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

if(isset($_REQUEST['loc_id']))

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

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

    $loc_id  = $stringParts[0];

    $loc_status= $stringParts[1];

   
    if ($loc_status=="Yes"){
      $cval=0;
    }
    else {
      $cval=1;
    }
    mysqli_query($con,"update stock_locations set loc_active=$cval where loc_id=$loc_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 | Location 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="page-header">
            <h3 class="page-title">
                New Stock Locations 
            </h3>
          
          </div>
          <div class="col-8 grid-margin">
              <div class="card">
                <div class="card-body">

              
         
                 
    <form class="needs-validation" novalidate method="post" action="admin_location_add.php" enctype="multipart/form-data" onsubmit="return validateForm()"> 
                  
                    <div class="row">                     
                      <div class="col-md-6">
                        <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Location Name </label>
                          <div class="col-sm-9">
                            <input type="text" class="form-control" id="lname" name="lname" placeholder="">
                          </div>
                        </div>
                      </div>
                      <div class="col-md-6">
                          <div class="form-group row">
                          <label class="col-sm-3 col-form-label">Location Type</label>
                          <div class="col-sm-9">
                          <select class="form-control select2" style="width: 100%;" name="loctype" id="loctype" required >
           <option value="0">~~~Select ~~~~</option>  
            <option value="stores">stores</option> 
             <option value="vehicle">Vehicle</option> 
         
          </select> 
          </div> 
</div>



             
                       




          </div>                            
                    </div>   

  




                 
                     <button type="submit" class="btn btn-primary">Save</button>
                    <button class="btn btn-light">Cancel</button>
                  </form>
                </div>
              </div>
            </div>


  <div class="content-wrapper">
          <div class="col-lg-22 grid-margin stretch-card">
              <div class="card">
                <div class="card-body">
                  <h4 >Stock Location </h4>
                  <p class="card-description">
                    <button type="button" onclick="document.location='home.php'" class="btn btn-warning "><i class="fas fa-arrow-left"></i> Go Back
                    
                  </button>
                  </p>
                  <div class="table-responsive">
                    <table class="table table-hover">
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Location</th>
                          <th>Type</th>
                      
                          <th>Active</th>
                          <th>Edit</th> 
                        </tr>
                      </thead>
                      <tbody>
<?php
$rcount=0;
$query=mysqli_query($con,"SELECT * from stock_locations")or die(mysqli_error()); 
 while($row=mysqli_fetch_array($query)){   $rcount=$rcount+1;
             
                $loc_id=$row['loc_id'];
                $loc_name=$row['loc_name']; 
                $loc_active=$row['loc_active'];
                // $emp_id=$row['emp_id'];
                //  $emp_name=$row['emp_name'];

                if ($loc_active==1) {
                    $locactive="Yes";
                }
             
                if ($loc_active==0) {
                    $locactive="No";
                }

 ?>
   <tr>
                         <td><?php echo $rcount;  ?></td>
                         <td><?php echo $row['loc_name'];?></td>
                           <td><?php echo $row['loc_type'];?></td>
                          
                         <td><?php if ($row['loc_active']>0) { ?> &#10004;<?php } else { ?> &#10006; <?php } ?><a href="admin_location_view.php?loc_id=<?php echo $row['loc_id'];echo "*";echo $locactive;?>"> Change</a></td>


                 
</td>  

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


<!----------------------Model ------------------------------->
     <div id="addvoucher<?php echo $row['loc_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_location_edit.php" enctype='multipart/form-data'>

    <div class="form-group">
                  <label for="exampleInputPassword1">Location</label>
                <input type="text" class="form-control" id="loc_edit" name="loc_edit" value="<?php echo $row['loc_name']; ?>">

                  </div>  


                   <input type="hidden" class="form-control" id="locid_edit" name="locid_edit" value="<?php echo $row['loc_id'];?>">


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

               <select class="form-control select2" style="width: 100%;" name="loctype_edit" id="loctype_edit" required >           

           <?php if ($row['loc_type']== "vehicle") {  ?>
            <option value="vehicle" selected>Vehicle</option> 
            <option value="stores">Stores</option>
             <?php } else { ?>
             <option value="stores" selected>Stores</option> 
               <option value="vehicle" >Vehicle</option> 
          <?php }  ?>
          </select> 


             

           
               </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 ------------------------------------>









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



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

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


</html>

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

    });

function validateForm() { 

    //````````route````````````````````````````````````
    if($('#lname').val().trim().length == 0) {
           alert("Please enter the Location Name !!!");               
            return false;
    } ////``````````````````````````````````````````

     if ($('#loctype').val() === "0") {
        alert("Please select a valid Location Type!");
        return false;
    }



 } ///validateForm() { 

</script>

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

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

</script>



