<?= $this->extend('Modules\Admin\Views\index'); ?>
<?= $this->section('content');?>
<div class="container-fluid">
    <div class="row mb-md-3">
        <div class="col">
            <h4>UGA REDD+ PROJECTS</h4>
            <nav aria-label="breadcrumb"><small>
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="<?= base_url('administrator') ?> ">Home</a></li>
                    <li class="breadcrumb-item active" aria-current="page"><?= esc($bcrumb) ?> </li>
                </ol></small>
            </nav>
        </div>
    </div><!-- breadcrumb !-->
    <div class="row mb-2">
        <div class="col">
            <!-- print success message !-->
            <?php if(session()->getTempdata('success')): ?>
                <div class="alert alert-success" role="alert"><?= session()->getTempdata('success');?></div>
            <?php endif;?>
            <!-- print failure message !-->
            <?php if(session()->getTempdata('error')): ?>
                <div class="alert alert-danger" role="alert"><?= session()->getTempdata('error');?></div>
            <?php endif;?>
            <?php if(isset($validation)): ?>
                <div class="alert alert-danger"><?= $validation->listErrors()?></div>
            <?php endif;?>
        </div>
    </div> 
    <!-- END print success message !-->
    <div class="row">
        <div class="col-md-12 mb-3">
            <div class="table-responsive">
            <table class="table  table-bordered table-border w-100" id="datalist">
                <thead style="font-size:12px">
                    <tr>
                        <th>Project_ID</th>
                        <th>Project_Name</th>
                        <th>Geo_Boundary</th>
                        <th>Status</th>
                        <th>Project_Type</th>
                        <th>Organization_Name</th>
                        <th>AFOLU_Activities</th>
                        <th>Methodology</th>
                        <th>Carbon_Pool</th>
                        <th>Gases</th>
                        <th>Estimated_ER</th>
                        <th>Buffer_Credit</th>
                        <th>Area(ha)</th>
                        <th>Credit_Period</th>
                        <th>Reff_Period</th>
                        <th>Uncertainty</th>
                        <th>Registry</th>
                    </tr>
                </thead>
                <tbody style="font-size:12px">
                    <?php foreach($projects as $project):?>
                     <?php $singleCarbonData = $project->carbon_data; ?>
                        <tr>
                            <td><?= $project->project_number; ?></td>
                            <td><?= $project->name; ?></td>
                            <td>
                                <small>Lat: <?= $project->geo_lat; ?></small><br/>
                                <small>Long: <?= $project->geo_long; ?></small><br/>
                                <button 
							class="btn btn-sm btn-success" 
							data-bs-toggle="modal" 
							data-bs-target="#mapModal" 
							data-id="<?= $project->id; ?>" 
							data-lat="<?= $project->geo_lat; ?>" 
							data-long="<?= $project->geo_long; ?>" 
							data-org="<?= $project->name; ?>" 
							data-boundary='<?= $project->boundary_file; ?>'>
							<i class="bi bi-map"></i> Map
						</button>
						<!-- Forest Map Modal -->
						<div class="modal fade" id="mapModal" tabindex="-1" aria-labelledby="mapModalLabel" aria-hidden="true">
						<div class="modal-dialog modal-lg modal-dialog-centered">
							<div class="modal-content">
							<div class="modal-header">
								<h5 class="modal-title" id="mapModalLabel">Project Location</h5>
								<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
							</div>
							<div class="modal-body p-0">
								<div id="forestMap" style="height: 400px; width: 100%;"></div>
							</div>
							</div>
						</div>
						</div>
						<!-- End Modal !-->
                            </td>
                            <td>
                                <?php if (!empty($project->singleStatus)): ?>
                                        <span class="badge" 
                                              style="background-color: <?= esc($project->singleStatus->status_color) ?>;">
                                            <i class="<?= esc($project->singleStatus->status_icon) ?> me-2"></i>
                                            <?= esc($project->singleStatus->status_name) ?>
                                        </span>
                                    <?php else: ?>
                                        <span class="badge bg-secondary">No Status</span>
                                    <?php endif; ?>
                            </td>
                            <td>
                                <ul type="square" style="padding-left:0px;margin-left:0px;">
                                    <?php foreach ($project->types as $type): ?>
                                    <li><?= esc($type->type_name) ?></li> 
                                    <?php endforeach; ?>
                                </ul>
                            </td>
                            <td><a href="<?= base_url('administrator/redd/single-client/id='.$project->client_id);?>"><?= $project->fname.' '.$project->lname; ?></a></td>
                            <td>
                                <ul type="square" style="padding-left:0px;margin-left:0px;">
                                    <?php foreach ($project->activities as $activity): ?>
                                    <li><?= esc($activity->activity_name) ?></li> 
                                    <?php endforeach; ?>
                                </ul>
                            </td>
                            <td>
                                <ul type="square" style="padding-left:0px;margin-left:0px;">
                                    <?php foreach ($project->methodologies as $methodology): ?>
                                    <li><?= esc($methodology->name) ?></li> 
                                    <?php endforeach; ?>
                                </ul>
                            </td>
                            <td>
                                <ul type="square" style="padding-left:0px;margin-left:0px;">
                                    <?php foreach ($project->carbon_pools as $pool): ?>
                                    <li><?= esc($pool->name) ?></li> 
                                    <?php endforeach; ?>
                                </ul>
                            </td>
                            <td>
                                <ul type="square" style="padding-left:0px;margin-left:0px;">
                                    <?php foreach ($project->gases as $gas): ?>
                                    <li><?= esc($gas->name) ?></li> 
                                    <?php endforeach; ?>
                                </ul>
                            </td>
                            <td><?= number_format($singleCarbonData->estimated_er); ?></td>
                            <td><?= number_format($singleCarbonData->buffer_credit); ?></td>
                            <td><?= $project->size; ?></td>
                            <td><?= date('Y', strtotime($singleCarbonData->credit_period_start)); ?> to <?= date('Y', strtotime($singleCarbonData->credit_period_end)); ?></td>
                            <td><?= date('Y', strtotime($singleCarbonData->reff_period_start)); ?> to <?= date('Y', strtotime($singleCarbonData->reff_period_end)); ?></td>
                            <td><?= $singleCarbonData->uncertainty; ?></td>
                            <td><?=  $singleCarbonData->registry; ?></td>
                        </tr>
                    <?php endforeach;?>
                </tbody>
            </table>
        </div>
        </div>
    </div><!-- Row with details !-->
    
</div><!-- container !-->

<!-- Modals!-->
<?= view('Modules\Redd\Views\\modals\\edit-clientModal') ?>

<!-- JS !-->
<script>
	let forestMap;
    let forestLayer;

$('#mapModal').on('shown.bs.modal', function (event) {
  let button = $(event.relatedTarget); // Button that triggered modal
  let lat = button.data('lat');
  let long = button.data('long');
  let org_name = button.data('org');
  let boundary = button.data('boundary'); // This is GeoJSON string

  // Initialize Leaflet map if not already
  if (!forestMap) {
    forestMap = L.map('forestMap').setView([lat, long], 10);
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
      attribution: '© OpenStreetMap'
    }).addTo(forestMap);
  }

  // Clear previous layers
  if (forestLayer) {
    forestMap.removeLayer(forestLayer);
  }

  // Add marker if no boundary
  if (!boundary) {
    forestLayer = L.marker([lat, long]).addTo(forestMap)
      .bindPopup(org_name).openPopup();
    forestMap.setView([lat, long], 12);
  } else {
    try {
      let geojson = JSON.parse(boundary);
      forestLayer = L.geoJSON(geojson).addTo(forestMap);
      forestMap.fitBounds(forestLayer.getBounds());
    } catch (e) {
      console.error("Invalid GeoJSON", e);
    }
  }
});
</script>
<script src="<?= base_url();?>assets/js/modules/members.js"></script>
<script src="<?= base_url();?>assets/js/countryselection.js"></script>
 
<?= $this->endSection();?>