<?php 
session_start();
include '../../connection.php';

$id = $_SESSION['resident_id'];
?>   <div class="row">
                <div class="col-md-3">
                    <div class="stats-card">
                        <div class="card-icon">
                            <i class="fas fa-exclamation-circle"></i>
                        </div>
                         <?php
                                $query = "SELECT COUNT(*) as total_complaint from tbl_transaction WHERE status = 'Pending' AND resident_id = '$id'";
                                $query_result = $conn->query($query);
                                $row = $query_result->fetch_assoc();
                                 ?>
                        <div class="stats-number"><?= $row['total_complaint'] ?></div>
                        <div class="stats-label">Pending Complaints</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card in-progress">
                        <div class="card-icon">
                            <i class="fas fa-tasks"></i>
                        </div>
                            <?php
                                $query = "SELECT COUNT(*) as total_accept from tbl_transaction WHERE status = 'Accept' AND resident_id = '$id'";
                                $query_result = $conn->query($query);
                                $row = $query_result->fetch_assoc();
                                 ?>
                        <div class="stats-number"><?= $row['total_accept'] ?></div>
                        <div class="stats-label">In Progress</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card resolved">
                        <div class="card-icon">
                            <i class="fas fa-check-circle"></i>
                        </div>
                          <?php
                                $query = "SELECT COUNT(*) as total_pending from tbl_transaction WHERE status = 'Pending' AND resident_id = '$id'";
                                $query_result = $conn->query($query);
                                $row = $query_result->fetch_assoc();
                                 ?>
                        <div class="stats-number"><?= $row['total_pending'] ?></div>
                        <div class="stats-label">Resolved</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stats-card rejected">
                        <div class="card-icon">
                            <i class="fas fa-times-circle"></i>
                        </div>
                         <?php
                                $query = "SELECT COUNT(*) as total_pending from tbl_transaction WHERE status = 'Settled' AND resident_id = '$id'";
                                $query_result = $conn->query($query);
                                $row = $query_result->fetch_assoc();
                                 ?>
                        <div class="stats-number"><?= $row['total_pending'] ?></div>
                        <div class="stats-label">Rejected</div>
                    </div>
                </div>
            </div>

          
                
                <button class="btn btn-danger m-2 font-weight-bold" data-toggle="modal" data-target="#add-emergency">Emergency <i class="fa fa-bullhorn"></i></button>


<!-- Modal -->
<div class="modal fade" id="add-emergency" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Create Emergency <i class="fa fa-bullhorn"></i></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <textarea class="form-control" id="description" placeholder="Describe the emergency..." style="height: 300px;"></textarea>
        <input type="hidden" id="latitude">
         <input type="hidden" id="longitude">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-dark" onclick="submitEmergency()">Submit</button>
      </div>
    </div>
  </div>
</div>
                
                <!-- Recent Complaints Table -->
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <span>My Recent Complaints</span>
                      
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <div id="mycomplaints"></div>
                        </div>
                    </div>
                </div>

       <script>

        // ✅ Auto-fill geolocation when modal opens
$('#add-emergency').on('shown.bs.modal', function () {
  if ("geolocation" in navigator) {
    navigator.geolocation.getCurrentPosition(
      function (position) {
        $('#latitude').val(position.coords.latitude);
        $('#longitude').val(position.coords.longitude);
      },
      function (error) {
        alert(" Unable to get your location: " + error.message);
      }
    );
  } else {
    alert("Geolocation is not supported by this browser.");
  }
});

function submitEmergency() {
  const description = $("#description").val().trim();
  const latitude = $("#latitude").val();
  const longitude = $("#longitude").val();

  if (description === "" || latitude === "" || longitude === "") {
    swal("Incomplete Info", "Please write a description and ensure location is detected.", "warning");
    return;
  }

  $.ajax({
    method: 'POST',
    url: 'query/emergency_query.php',
    data: {
      adding_emergency: 1,
      description,
      latitude,
      longitude
    },
    success: function (res) {
      swal("Emergency Sent!", "Barangay responders will act immediately.", "success");
      $('#add-emergency').modal('hide');
      $("#description").val('');
      load_mycomplaints(); // refresh list
    },
    error: function () {
      swal(" Failed", "Something went wrong while sending emergency.", "error");
    }
  });
}
        load_mycomplaints();
           function load_mycomplaints(){
            $.ajax({
                method: 'post',
                url: 'query/dashboard_query.php',
                data:{
                    "loading_complaints": 1
                },
                success: function(res){
                    $('#mycomplaints').html(res);
                }
            });
           }
       </script>