<?php 
require 'inc/Header.php';
?>
<!-- Loader ends-->
<!-- page-wrapper Start-->
<div class="page-wrapper compact-wrapper" id="pageWrapper">
  <!-- Page Header Start-->
  <?php require 'inc/Navbar.php';?>
  <!-- Page Header Ends-->
  <!-- Page Body Start-->
  <div class="page-body-wrapper">
    <!-- Page Sidebar Start-->
    <?php require 'inc/Sidebar.php';?>
    <!-- Page Sidebar Ends-->
    <div class="page-body">
      <div class="container-fluid">
        <div class="page-title">
          <div class="row">
            <div class="col-sm-6">
              <h3><?php echo $lang['ParcelRateManagement'];?></h3>
            </div>
          </div>
        </div>
      </div>
      <!-- Container-fluid starts-->
      <div class="container-fluid dashboard-default">
        <div class="row">
          <div class="col-sm-12">
            <div class="card">
              <?php 
                if (isset($_GET['id'])) {
    $id = intval($_GET['id']);
    $query = "SELECT * FROM tbl_parcel_rate WHERE id = $id AND operator_id = " . intval($sdata["id"]);
    $result = $h->queryfire($query);
    $data = $result->fetch_assoc();

    if ($data) {
        // Record found — show form
        ?>
        <form method="post">
          <div class="card-body">
            <div class="form-group mb-3">
              <label><?php echo $lang['MinWeight'];?></label>
              <input type="number" step="0.01" name="min_weight" class="form-control" required value="<?php echo htmlspecialchars($data['min_weight']); ?>" />
            </div>

            <div class="form-group mb-3">
              <label><?php echo $lang['MaxWeight'];?></label>
              <input type="number" step="0.01" name="max_weight" class="form-control" value="<?php echo htmlspecialchars($data['max_weight']); ?>" />
            </div>

            <div class="form-group mb-3">
              <label><?php echo $lang['PricePerKm'];?></label>
              <input type="number" step="0.01" name="price_per_km" class="form-control" required value="<?php echo htmlspecialchars($data['price_per_km']); ?>" />
            </div>

            <input type="hidden" name="type" value="edit_parcel_rate" />
            <input type="hidden" name="id" value="<?php echo $id; ?>" />
          </div>
          <div class="card-footer text-left">
            <button type="submit" class="btn btn-primary"><?php echo $lang['UpdateParcelRate'];?></button>
          </div>
        </form>
        <?php
    } else {
        // Record not found — show error message
        echo '<div class="alert alert-danger">Record not found or you do not have permission to edit this record.</div>';
    }
} 
  else 
                {
              ?>
                <form method="post">
                  <div class="card-body">
                   

                    <div class="form-group mb-3">
                      <label><?php echo $lang['MinWeight'];?></label>
                      <input type="number" step="0.01" name="min_weight" class="form-control" required />
                    </div>

                    <div class="form-group mb-3">
                      <label><?php echo $lang['MaxWeight'];?></label>
                      <input type="number" step="0.01" name="max_weight" class="form-control" />
                    </div>

                    <div class="form-group mb-3">
                      <label><?php echo $lang['PricePerKm'];?></label>
                      <input type="number" step="0.01" name="price_per_km" class="form-control" required />
                    </div>

                    <input type="hidden" name="type" value="add_parcel_rate" />
                  </div>
                  <div class="card-footer text-left">
                    <button type="submit" class="btn btn-primary"><?php echo $lang['AddParcelRate'];?></button>
                  </div>
                </form>
              <?php } ?>
            </div>
          </div>
        </div>
      </div>
      <!-- Container-fluid Ends-->
    </div>
    <!-- footer start-->
  </div>
</div>
<!-- latest jquery-->
<?php require 'inc/Footer.php'; ?>
<!-- login js-->
</body>
</html>
