<?php include_once("include/head.php"); ?>

<div class="main-panel">
  <div class="content-wrapper">
    <div class="card">
      <div class="card-body">
        <div class="row">
          <div class="col-md-3">
            <h4 class="card-title">ADD BRANCH</h4>
          </div>

        </div>

        <div class="row">
          <div class="col-md-12">
            <form class="cmxform" id="commentForm" method="post" action="process_branch.php?action=add">
              <fieldset>
                <div class="row">
                  <div class="form-group col-md-4">
                    <label for="branch_name">Branch Name</label>
                    <input id="branch_name" class="form-control" name="branch_name" minlength="2" type="text" required>
                  </div>
                  <div class="form-group col-md-8">
                    <label for="branch_address">Branch Address</label>
                    <input id="branch_address" class="form-control" name="branch_address" minlength="2" type="text" required>
                  </div>
                </div>

                <input class="btn btn-primary" type="submit" value="Submit">
              </fieldset>
            </form>
          </div>

          <div class="col-md-12 mt-5">
            <div class="table-responsive">
              <table id="order-listing" class="table">
                <thead>
                  <tr>
                    <th>Id</th>
                    <th>Branch Name</th>
                    <th>Branch Address</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  // Fetch branches from database
                  $sql = "SELECT * FROM branches";
                  $result = mysqli_query($conn, $sql);
                  if (mysqli_num_rows($result) > 0) {
                    while ($row = mysqli_fetch_assoc($result)) {

                  ?>

                      <tr>
                        <td><?php echo $row['branch_id'] ?></td>
                        <td><?php echo $row['branch_name'] ?></td>
                        <td><?php echo $row['branch_address'] ?></td>
                        <td>
                          <button type="button" class="btn btn-primary btn-sm mb-2 ml-2" data-toggle="modal" data-target="#exampleModal-<?php echo $row['branch_id'] ?>">Edit</button>
                          <button type="button" class="btn btn-danger btn-sm  mb-2 ml-2" data-toggle="modal" data-target="#deleteModal-<?php echo $row['branch_id'] ?>">Delete</button>
                        </td>
                      </tr>
                  <?php
                    }
                  }
                  ?>

                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</div>
</div>

<?php include_once("include/footer.php"); ?>



<?php
// Generate modals for each branch
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
?>
  <!-- Edit Modal -->
  <div class="modal fade" id="exampleModal-<?php echo $row['branch_id'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel-<?php echo $row['branch_id'] ?>" aria-hidden="true">
    <div class="modal-dialog modal-md" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel-<?php echo $row['branch_id'] ?>">Edit Branch</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <form class="needs-validation" enctype="multipart/form-data" method="POST"  novalidate   action="process_branch.php?action=edit">
            <div class="form-row">
              <div class="col-md-12 mb-3">
                <label for="validationCustom01">Branch Name</label>
                <input type="text" name="branch_name" class="form-control" id="validationCustom01" placeholder="Branch Name" value="<?php echo $row['branch_name'] ?>" required>
                <div class="invalid-feedback">
                  Please enter the Branch Name.
                </div>
              </div>
              <div class="col-md-12 mb-3">
                <label for="validationCustom02">Branch Address</label>
                <input type="text" name="branch_address" class="form-control" id="validationCustom02" placeholder="Branch Address" value="<?php echo $row['branch_address'] ?>" required>
                <div class="invalid-feedback">
                  Please enter the Branch Address.
                </div>
              </div>
            </div>
            <div class="modal-footer">
                <input type="hidden" name="branch_id" value="<?php echo $row['branch_id'] ?>">
              <button type="submit" name="save" class="btn btn-primary">Save changes</button>
              <button type="button" class="btn btn-light" data-dismiss="modal">Close</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

  <!-- Delete Modal -->
  <div class="modal fade" id="deleteModal-<?php echo $row['branch_id']?>" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel-<?php echo $row['branch_id']?>" aria-hidden="true">
            <div class="modal-dialog modal-md" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="deleteModalLabel-' . $row['branch_id'] . '">Delete Branch</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        Are you sure you want to delete this branch?
                    </div>
                    <div class="modal-footer">
                        <form id="deleteDepartmentForm-' . $row['branch_id'] . '" method="POST" action="process_branch.php?action=delete">
                            <input type="hidden" name="delete_id" value="<?php echo $row['branch_id'] ?>">
                            <button type="submit" name="delete" class="btn btn-danger">Delete</button>
                            <button type="button" class="btn btn-light" data-dismiss="modal">Close</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
<?php
}
?>

</body>

</html>