<?php 

require_once("config/configDB.php"); 
require "financial_year_calculation.php";
require "userPermission.php";
require "inSpareProFetchData.php";

$edit_id  = $_GET['id'];
$sql      = mysqli_query($conn,"SELECT `invoice`, `invoicedate`, `vehicleno`, `givenby`, `receivedby`, `remarks`,kms,hrs FROM internalpro_spare_hdr WHERE id='$edit_id' "); 
$row      = mysqli_fetch_array($sql,MYSQLI_ASSOC);


$invoice          = $row['invoice'];
$tdate_temp       = new DateTime(stripslashes($row['invoicedate']));
$invoicedate 		  = $tdate_temp->format('d-m-Y');
$vehicleno        = $row['vehicleno'];
$kms        = $row['kms'];
$hrs        = $row['hrs'];
$givenby          = $row['givenby'];
$receivedby       = $row['receivedby'];
$remarks          = $row['remarks'];

$sqlVehicleQuery = mysqli_query($conn,"SELECT id, vehicleno  FROM vehicle_master WHERE isdelete=1 ");

$Details_Result  = mysqli_query($conn,"SELECT  ppsd.spareID, spm.productname ,ppsd.spareqty 
FROM internalpro_spare_dtl as ppsd 
LEFT JOIN product_master as spm ON spm.id=ppsd.spareID

WHERE  ppsd.hdr_id='$edit_id'");


?>
<!DOCTYPE html>
<html>
<head>
    <?php 
      require 'meta.php'; 
      require 'header.php'; 
    ?>
</head>
<body>
  <?php require 'menu.php' ?>
  <div class="container-fluid">
    <h3 class="text-center"><strong>Internal Spare Service Details</strong></h3>
    <form class="form-horizontal" action="inSpareProUpdate.php" method="post" id="inSpareProFrom">
      <div class="col-sm-12 row">
        <div class=" col-sm-1"></div>
        <div class=" col-sm-3 form-group">
          <label class="control-label">Invoice No</label>
          <input class="form-control input-lg" type="text" name="invoice" value="<?php echo $invoice; ?>" readonly >
          <input type="hidden" value=<?php echo $edit_id; ?> name="edit_id">
        </div>
        <div class="col-sm-3"></div>
        <div class="col-sm-3 form-group">
          <label class="control-label">Date</label>
          <input class="form-control input-lg datepicker-invoicedate"  type="text" name="invoicedate" value="<?php echo $invoicedate; ?>" autocomplete="off" readonly>
        </div>
        
      </div>
      <div class=" col-sm-12 row"><br></div>
      <div class=" col-sm-12 row">
        <div class=" col-sm-1"></div>
        <div class=" col-sm-2 form-group">
          <label class="control-label">Vehicle/Machinery</label>
          <select class="form-control input-lg selectpicker select-vehicle-details" data-live-search="true" name="vehicleno" id="vehicleno">
            <option disabled selected>Select your Option</option>
            <?php   while($prow = mysqli_fetch_array($sqlVehicleQuery)) { ?>
                <option value="<?php echo $prow['id'] ?>" <?php if( $prow['id']  == $vehicleno ): ?> selected="selected"<?php endif; ?> > <?php echo $prow['vehicleno']; ?> </option>
            <?php  }  ?>
          </select> 
        </div>
        
         <div class="col-sm-2 form-group">
          <label class="control-label">KiloMeter</label>
          <input class="form-control input-lg" type="text" name="kms" placeholder="KMS" required value="<?php echo $kms; ?>">
        </div>
        
         <div class="col-sm-2 form-group">
          <label class="control-label">Running Hrs</label>
          <input class="form-control input-lg" type="text" name="hrs" placeholder="HRS" required value="<?php echo $hrs; ?>">
        </div>
        
        <div class="col-sm-2 form-group">
          <label class="control-label">Given By</label>
          <input class="form-control input-lg" type="text" name="givenby" value="<?php echo $givenby; ?>" placeholder="Given By">
        </div>
        <div class="col-sm-2 form-group">
          <label class="control-label">Received By</label>
          <input class=" form-control input-lg" type="text" name="receivedby" value="<?php echo $receivedby; ?>" placeholder="Received By">
        </div>
      </div>
      <div class=" col-sm-12 row"><br></div>
      <div class="col-sm-12 row">
        <div class="col-sm-1"></div>
        <div class="col-sm-9 form-group">
          <label class="control-label">Remarks</label>
          <textarea name="remarks" class="form-control" rows="2" cols="50" placeholder="Describe yourself here." ><?php echo $remarks; ?></textarea>
        </div>                                                                                 
  
      </div>

      <div class=" col-sm-12 row"><br></div>
      <div class="container-fluid">
        <div class="col-sm-4" style="padding-top: 10px;margin-top: 10px;">          
        <table class="table  table-striped table-bordered dt-responsive" style="overflow-x:auto;" id="saleAdd">
            <tr>
              <th>Spare Name</th>
              <td><select class="form-control spare-select-box  input-lg selectpicker" data-live-search="true" name="sparedetails" id="sparedetails">
              <option disabled selected>Select your Spare Option</option>
              <?php echo  $sqlSpareName; ?>
              </select>
              </td>
            </tr>  
            <tr>
            <th>Available Qty</th>
              <td><input class="form-control aqty-input" type="text" id="aqty" readonly></td>
            </tr>
            <tr>
              <th>Invoice Qty</th>
              <td><input class="form-control eqty-input" type="text" id="eqty" autocomplete="off"></td>
            </tr>
            <tr>
              <th>Action</th>
              <td><input type="button" class="btn btn-default add-inSparePro" value="Add"></td>
            </tr>
          
        </table>
        </div>

        <div class="col-sm-8 " style="padding-top: 10px;margin-top: 10px;">          
          <table class="table table-responsive table-striped table-bordered dt-responsive nowrap" id="inSpareProDetailsAdd" cellspacing="0">
            <thead>
              <tr>
                <th>Spare</th>
                <th>Qty</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>

            <?php

              $trlength = 0;
              while($drow = mysqli_fetch_array($Details_Result))
              {
                $trlength++;
                $DetailsTable = '';
                $DetailsTable ='<tr id="'.$trlength.'">';
                $spareName= $drow['productname'];


                $DetailsTable .='<td class="product_name">'.$spareName.'<input class="form-control" name="spareID[]"  value="'.$drow['spareID'].'" type="hidden"></td>';
                $DetailsTable .='<td>'.$drow['spareqty'].'<input class="form-control" name="spareqty[]" type="hidden" value="'.$drow['spareqty'].'" ></td>';
                $DetailsTable .='<td><input type="button" class="btn btn-default removeDetails" id="removeDetails" value="-"></td>';
                $DetailsTable .='</tr>';
                echo $DetailsTable;

              }

            ?>              
            </tbody>
          
          </table>
        </div>

      </div>     
      

<div class="container-fluid">
                                                                                       
  
</div>

    <div class="form-group">        
      <div class="col-sm-12 text-center">
        <button type="submit" class="btn btn-default btn-lg">Save</button>
      </div>
    </div>
    </form>   
  </div>
  <?php require 'footer.php' ?>
 
  <script type="text/javascript">var base_url = "<?= $base_url ?>";</script>
  
  <script type="text/javascript" src="inSpareProScript.js"></script>
<script>
	$(document).ready(function(){
	  var date_input=$('input[name="invoicedate"]'); //our date input has the name "date"
		var container=$('form >  .datepicker-invoicedate').length>0 ? $('form > .datepicker-invoicedate').parent() : "body";
		date_input.datepicker({
			  format: 'dd-mm-yyyy',
        container: container,
			  todayHighlight: true,
			  autoclose: true,
    });
    

    $('#inSpareProFrom').bootstrapValidator({
        // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
          invoice: {
                message: 'The invoice number is not valid',
                validators: {
                    notEmpty: {
                        message: 'The invoice number is required and cannot be empty'
                    }
                }
            },
          invoicedate: {
                message: 'The invoice date is not valid',
                validators: {
                    notEmpty: {
                        message: 'The invoice date is required and cannot be empty'
                    }
                }
          },  
          givenby: {
                message: 'The given by is not valid',
                validators: {
                    notEmpty: {
                        message: 'The given by is required and cannot be empty'
                    }
                }
            },
            receivedby: {
                message: 'The received by is not valid',
                validators: {
                    notEmpty: {
                        message: 'The received by is required and cannot be empty'
                    }
                }
            },
            remarks: {
              message: 'The remarks is not valid',
                validators: {
                    notEmpty: {
                        message: 'The remarks is required and cannot be empty'
                    }
                }
            }
        }
    });

   

	});
</script>
</body>
</html>