<?= $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+ REGISTRY REGISTRATION</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 !-->
   <!-- ADD FORM here !-->
    <form action="<?= base_url('administrator/redd/add-client') ?>" method="post" enctype="multipart/form-data">
        <?= csrf_field(); ?>
        <div class="card mb-3">
            <div class="card-header alert-primary fw-bold text-uppercase">Client Information</div>
            <div class="card-body row g-3">

                <div class="col-md-6">
                    <label class="form-label  fw-bold mb-0">First Name*</label>
                    <input type="text" name="fname" class="form-control" required>
                </div>
                <div class="col-md-6">
                    <label class="form-label  fw-bold mb-0">Last Name*</label>
                    <input type="text" name="lname" class="form-control" required>
                </div>

                <div class="col-md-4">
                    <label class="form-label  fw-bold mb-0">Email*</label>
                    <input type="email" name="email" id="email" class="form-control" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label  fw-bold mb-0">Phone*</label>
                    <input type="text" name="phone" class="form-control" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label  fw-bold mb-0">Website</label>
                    <input type="text" name="website" class="form-control" >
                </div>
                <div class="col-md-6">
                    <label class="form-label  fw-bold mb-0">Password</label>
                    <div class="input-group">
                    <input type="text" id="passwordgen" name="password" class="form-control" readonly required>
                    <button type="button" class="btn btn-outline-secondary" onclick="onPwd()">Generate</button>
                    </div>
                </div>

                <div class="col-md-6">
                    <label class="form-label  fw-bold mb-0">Photo <small class="text-muted fw-normal">(Upload company logo)</small></label>
                    <input type="file" name="photo" class="form-control" accept="image/*">
                </div>

                <div class="col-md-6">
                    <label class="form-label  fw-bold mb-0">Address* <small class="text-muted fw-normal">(Offices)</small></label>
                    <input type="text" name="address" class="form-control" required>
                </div>

                <div class="col-md-6">
                    <label class="form-label  fw-bold mb-0">Entity Type*</label>
                    <select name="entity_type_id" class="form-select" required>
                    <option value="">-- Select Entity Type --</option>
                    <?php foreach($entity_types as $et): ?>
                        <option value="<?= $et->id ?>"><?= esc($et->name) ?></option>
                    <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-4">
                    <label for="country_id" class="form-label  fw-bold mb-0">Country</label>
                    <select name="country_id" id="country_id" class="form-select" required>
                        <option value="">Select Country</option>
                        <?php foreach($countries as $c):?>
                            <option value="<?= $c->id; ?>"><?= $c->name;?></option>
                        <?php endforeach;?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label for="state_id" class="form-label  fw-bold mb-0">State</label>
                    <select name="state_id" id="state_id" class="form-select">
                        <option value="">Select ---</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label for="city_id" class="form-label  fw-bold mb-0">City*</label>
                    <select name="city_id" id="city_id" class="form-select">
                        <option value="">Select ---</option>
                    </select>
                </div>
                <div class="col-md-12">
                    <label class="form-label  fw-bold mb-0">Description</label>
                    <textarea name="description" rows="3" class="form-control"></textarea>
                </div>
            </div>
        </div><!-- Client card -->

        <div class="card mb-3">
            <div class="card-header alert-success ">
                <span class="card-title fw-bold text-uppercase">Project Information</span>
                <p><small>NOTE: If client has more than one <strong>Projects</strong> you will be able to ADD the other projects under client page</small></p>
            </div>
            <div class="card-body row g-3">

            <div class="col-md-12">
                <label class="form-label fw-bold mb-0">Project Name*</label>
                <input type="text" name="project_name" class="form-control" required>
            </div>
            <div class="col-md-4">
                <label class="form-label fw-bold mb-0">Project Status*</label>
                <select name="project_status_id" class="form-select" required>
                <option value="">-- Select Project Status --</option>
                <?php foreach($statuses as $pt): ?>
                    <option value="<?= $pt->id ?>"><?= esc($pt->name) ?></option>
                <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-4">
                <label class="form-label fw-bold mb-0">Project Area / Size* <small class="text-muted fw-normal">(Hectares - Ha)</small></label>
                <input type="number" name="size" step="any" class="form-control" required>
            </div>
            <div class="col-md-4">
                <label class="form-label fw-bold mb-0">Project Validator</label>
                <input type="text" name="project_validator" class="form-control">
            </div>
            <div class="col-md-12">
                <label class="form-label fw-bold">Program / Project Type*</label>
                <div class="row">
                    <?php foreach($types as $pt): ?>
                        <div class="col-md-12">
                            <div class="form-check">
                                <input 
                                    class="form-check-input" 
                                    type="checkbox" 
                                    name="project_type_id[]" 
                                    id="pt_<?= $pt->id ?>" 
                                    value="<?= $pt->id ?>">
                                <label class="form-check-label" for="pt_<?= $pt->id ?>">
                                    <small><?= esc($pt->name) ?></small>
                                </label>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>      

            <div class="alert alert-primary"><h5>Project Location Details</h5></div>
            <div class="col-md-12">
                <label class="form-label fw-bold mb-0">Location / Address*</label>
                <textarea name="location_description" class="form-control"></textarea>
            </div>

            <div class="col-md-4">
                <label for="district_id" class="form-label  fw-bold mb-0">District*</label>
                <select name="district_id" id="district_id" class="form-select" required>
                    <option value="">Select District</option>
                    <?php foreach($districts as $c):?>
                        <option value="<?= $c['district_id']; ?>"><?= $c['district_name'];?></option>
                    <?php endforeach;?>
                </select>
            </div>
            <div class="col-md-4">
                <label for="county_id" class="form-label  fw-bold mb-0">County</label>
                <select name="county_id" id="county_id" class="form-select">
                    <option value="">Select County</option>
                </select>
            </div>
            <div class="col-md-4">
                <label for="subcounty_id" class="form-label  fw-bold mb-0">Subcounty</label>
                <select name="subcounty_id" id="subcounty_id" class="form-select">
                    <option value="">Select Subcounty</option>
                </select>
            </div>
            <div class="col-md-4">
                <label for="parish_id" class="form-label  fw-bold mb-0">Parish</label>
                <select name="parish_id" id="parish_id" class="form-select">
                    <option value="">Select Parish</option>
                </select>
            </div>
            <div class="col-md-4">
                <label for="village_id" class="form-label  fw-bold mb-0">Village</label>
                <select name="village_id" id="village_id" class="form-select">
                    <option value="">Select Village</option>
                </select>
            </div>
            <div class="col-xl-2 col-md-6">
                <label class="form-label fw-bold mb-0">Latitude</label>
                <input type="number" name="geo_lat" step="any" class="form-control">
            </div>
            <div class="col-xl-2 col-md-6">
                <label class="form-label fw-bold mb-0">Longitude</label>
                <input type="number" name="geo_long" step="any" class="form-control">
            </div>
            <div class="col-md-12">
                <label class="form-label  fw-bold mb-0">Draw Forest Boundary</label>
                <div id="addProjectMap" style="height: 400px; max-width:100%; border:1px solid #ccc;"></div>
                <input type="hidden" name="boundary_file" id="boundaryGeoJson">
            </div>
            <hr style="background-color:#fff;border-color:#fff"/>

            <div class="col-md-12">
                <label class="form-label fw-bold">AFOLU Activity</label>
                <div class="row">
                    <?php foreach($activities as $at): ?>
                        <div class="col-md-4">
                            <div class="form-check">
                                <input 
                                    class="form-check-input" 
                                    type="checkbox" 
                                    name="activity_id[]" 
                                    id="pt_<?= $at->id ?>" 
                                    value="<?= $at->id ?>">
                                <label class="form-check-label" for="pt_<?= $at->id ?>">
                                    <small><?= esc($at->name) ?></small>
                                </label>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div><hr style="background-color:#fff;border-color:#fff"/>

            <div class="col-md-12">
                <label class="form-label fw-bold">Methodology</label>
                <div class="row">
                    <?php foreach($methodologies as $metho): ?>
                        <div class="col-md-12">
                            <div class="form-check">
                                <input 
                                    class="form-check-input" 
                                    type="checkbox" 
                                    name="methodology_id[]" 
                                    id="pt_<?= $metho->id ?>" 
                                    value="<?= $metho->id ?>">
                                <label class="form-check-label" for="pt_<?= $metho->id ?>">
                                    <small><?= esc($metho->name) ?></small>
                                </label>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="col-md-12">
                <label class="form-label fw-bold">Carbon Pool</label>
                <div class="row">
                    <?php foreach($carbon_pools as $pool): ?>
                        <div class="col-md-12">
                            <div class="form-check">
                                <input 
                                    class="form-check-input" 
                                    type="checkbox" 
                                    name="carbon_pool_id[]" 
                                    id="pt_<?= $pool->id ?>" 
                                    value="<?= $pool->id ?>">
                                <label class="form-check-label" for="pt_<?= $pool->id ?>">
                                    <small><?= esc($pool->name) ?></small>
                                </label>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
            
            <div class="col-md-12">
                <label class="form-label fw-bold">Gases</label>
                <div class="row">
                    <?php foreach($gases as $gas): ?>
                        <div class="col-md-12">
                            <div class="form-check">
                                <input 
                                    class="form-check-input" 
                                    type="checkbox" 
                                    name="gas_id[]" 
                                    id="pt_<?= $gas->id ?>" 
                                    value="<?= $gas->id ?>">
                                <label class="form-check-label" for="pt_<?= $gas->id ?>">
                                    <small><?= esc($gas->name) ?></small>
                                </label>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <hr style="background-color:#fff;border-color:#fff"/>

            <div class="col-md-12">
                <label class="form-label  fw-bold mb-0">Description</label>
                <textarea name="project_description" rows="3" class="form-control"></textarea>
            </div>
            </div>
        </div><!-- Project card -->

        <div class="text-end mb-3">
            
            <a href="<?= base_url('administrator/clients') ?>" class="btn btn-light">Cancel</a>
            <button type="submit" class="btn btn-danger">Save Registration</button>
        </div>
    </form>
    <!-- END FORM !-->
       
</div><!-- container !-->

<!-- Modals!-->

<!-- JS !-->
<!-- inside your view -->
<script>
    var csrfTokenName = '<?= csrf_token() ?>';   // the CSRF field name (e.g. "csrf_test_name")
    var csrfToken     = '<?= csrf_hash() ?>';    // the CSRF hash value
</script>

<script src="<?= base_url();?>assets/js/modules/members.js"></script>
<script src="<?= base_url();?>assets/js/districtselection.js"></script>
<script src="<?= base_url();?>assets/js/countryselection.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function() {
    // Initialize map
    var map = L.map('addProjectMap').setView([0.3476, 32.5825], 7); // Center on Uganda

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '© OpenStreetMap'
    }).addTo(map);

    var drawnItems = new L.FeatureGroup();
    map.addLayer(drawnItems);

    var drawControl = new L.Control.Draw({
        edit: { featureGroup: drawnItems },
        draw: {
            marker: false,
            circle: false,
            circlemarker: false,
            polyline: false,
            polygon: true,
            rectangle: true
        }
    });
    map.addControl(drawControl);

    // Capture new drawing
    map.on(L.Draw.Event.CREATED, function (e) {
        drawnItems.clearLayers(); // allow only one polygon
        var layer = e.layer;
        drawnItems.addLayer(layer);

        var geojson = layer.toGeoJSON();
        document.getElementById('boundaryGeoJson').value = JSON.stringify(geojson.geometry);
    });
});
</script>
<script>
function onPwd() {
    const length = 15; 
    const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
    let password = "";
    for (let i = 0; i < length; i++) {
      password += charset.charAt(Math.floor(Math.random() * charset.length));
    }
    document.getElementById("passwordgen").value = password;
  }
</script>
<script src="<?= base_url();?>/assets/js/checkemail.js"></script> 
<?= $this->endSection();?>