

<?php $__env->startSection('styles'); ?>
<link rel="stylesheet" href="<?php echo e(asset('css/form.css')); ?>">
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>

<div class="page-wrapper">
<div class="form-card">

<div class="form-header">
    <h2>✏ Edit Laporan Serangan Hama</h2>
    <p>Perbarui data laporan</p>
</div>

<form action="<?php echo e(url('/lapor/update/'.$lapor->id)); ?>" method="POST">
<?php echo csrf_field(); ?>

<div class="form-grid">

<div class="form-group">
<label>Nama Petani</label>
<input type="text" name="nama_petani" value="<?php echo e($lapor->nama_petani); ?>" required>
</div>

<div class="form-group">
<label>Jenis Hama</label>
<select name="jenis_hama" required>
<option value="Wereng" <?php echo e($lapor->jenis_hama=='Wereng'?'selected':''); ?>>Wereng</option>
<option value="Tikus" <?php echo e($lapor->jenis_hama=='Tikus'?'selected':''); ?>>Tikus</option>
<option value="Walang Sangit" <?php echo e($lapor->jenis_hama=='Walang Sangit'?'selected':''); ?>>Walang Sangit</option>
<option value="Penggerek Batang" <?php echo e($lapor->jenis_hama=='Penggerek Batang'?'selected':''); ?>>Penggerek Batang</option>
</select>
</div>

<div class="form-group">
<label>Tingkat Serangan</label>
<select name="tingkat_serangan" required>
<option value="Ringan" <?php echo e($lapor->tingkat_serangan=='Ringan'?'selected':''); ?>>Ringan</option>
<option value="Sedang" <?php echo e($lapor->tingkat_serangan=='Sedang'?'selected':''); ?>>Sedang</option>
<option value="Berat" <?php echo e($lapor->tingkat_serangan=='Berat'?'selected':''); ?>>Berat</option>
</select>
</div>

<div class="form-group">
<label>Luas Lahan</label>
<select name="luas_lahan" required>
<option value="luas_kecil" <?php echo e($lapor->luas_lahan=='luas_kecil'?'selected':''); ?>>Kurang dari 0,5 ha</option>
<option value="luas_sedang" <?php echo e($lapor->luas_lahan=='luas_sedang'?'selected':''); ?>>0,5 - 1 ha</option>
<option value="luas_besar" <?php echo e($lapor->luas_lahan=='luas_besar'?'selected':''); ?>>Lebih dari 1 ha</option>
</select>
</div>

<div class="form-group">
<label>Lama Kejadian</label>
<select name="lama_kejadian" required>
<option value="cepat" <?php echo e($lapor->lama_kejadian=='cepat'?'selected':''); ?>>Kurang dari 3 hari</option>
<option value="menengah" <?php echo e($lapor->lama_kejadian=='menengah'?'selected':''); ?>>3 - 7 hari</option>
<option value="lama" <?php echo e($lapor->lama_kejadian=='lama'?'selected':''); ?>>Lebih dari 7 hari</option>
</select>
</div>

</div>


<?php if($lapor->foto): ?>
<div style="margin-top:20px;">
    <label>Foto Saat Ini</label><br>
    <img src="<?php echo e(asset('foto/'.$lapor->foto)); ?>" style="width:300px;border-radius:10px;">
</div>
<?php endif; ?>


<div class="camera-section">
<label>📷 Ambil Foto Ulang (Opsional)</label>
<video id="video" autoplay playsinline></video>
<button type="button" class="btn-primary" onclick="ambilFoto()">Ambil Foto Baru</button>

<canvas id="canvas" style="display:none;"></canvas>
<img id="preview" class="preview-image">
<input type="hidden" name="foto_base64" id="foto_base64">
</div>

<div class="form-group">
<label>Koordinat</label>
<input type="text" id="koordinat" name="koordinat" value="<?php echo e($lapor->koordinat); ?>" required>
</div>

<div class="form-group">
<label>Lokasi</label>
<input type="text" id="lokasi" name="lokasi" value="<?php echo e($lapor->lokasi); ?>" required>
</div>

<div class="form-group">
    <label>Kecamatan</label>
    <input type="text" name="kecamatan" id="kecamatan" required>
</div>

<div class="form-group">
<label>Deskripsi</label>
<textarea name="deskripsi" required><?php echo e($lapor->deskripsi); ?></textarea>
</div>

<button type="submit" class="btn-submit">💾 Update Laporan</button>

</form>
</div>
</div>

<?php $__env->stopSection(); ?>


<?php $__env->startSection('scripts'); ?>
<script>

let streamRef;

// ================= AKTIFKAN KAMERA =================
navigator.mediaDevices.getUserMedia({
    video:{
        facingMode:"environment"
    }
})
.then(stream => {

    streamRef = stream;

    document.getElementById('video').srcObject = stream;

});


// ================= AMBIL FOTO =================
function ambilFoto(){

    const video = document.getElementById('video');
    const canvas = document.getElementById('canvas');
    const preview = document.getElementById('preview');

    const ctx = canvas.getContext('2d');

    canvas.width = video.videoWidth;
    canvas.height = video.videoHeight;

    // ambil frame video
    ctx.drawImage(video,0,0,canvas.width,canvas.height);

    // ================= DATA =================
    const namaPetani = document.querySelector('input[name="nama_petani"]').value;

    const koordinat = document.getElementById('koordinat').value;

    let lat = '-';
    let lng = '-';

    if(koordinat.includes(',')){
        lat = koordinat.split(',')[0];
        lng = koordinat.split(',')[1];
    }

    // ================= WATERMARK =================
    const now = new Date();

    const tanggal = now.toLocaleDateString('id-ID', {
        day:'2-digit',
        month:'long',
        year:'numeric'
    });

    const jam = now.toLocaleTimeString('id-ID', {
        hour:'2-digit',
        minute:'2-digit',
        second:'2-digit'
    });

    const boxHeight = 110;

    // background watermark
    ctx.fillStyle = "rgba(0,0,0,0.45)";
    ctx.fillRect(
        0,
        canvas.height - boxHeight,
        canvas.width,
        boxHeight
    );

    // text watermark
    ctx.fillStyle = "#ffffff";
    ctx.font = "18px Arial";

    ctx.fillText(
        "🌾 SPK Hama - Kabupaten Gowa",
        20,
        canvas.height - 75
    );

    ctx.fillText(
        "Petani : " + namaPetani,
        20,
        canvas.height - 50
    );

    ctx.fillText(
        "Koordinat : " + lat + ", " + lng,
        20,
        canvas.height - 25
    );

    ctx.fillText(
        "Waktu : " + tanggal + " | " + jam,
        20,
        canvas.height - 5
    );

    // hasil foto
    const dataURL = canvas.toDataURL('image/jpeg',0.9);

    document.getElementById('foto_base64').value = dataURL;

    preview.src = dataURL;

    preview.style.display = "block";

}

</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\Maylha\pelaporanpertanian\resources\views/lapor/edit.blade.php ENDPATH**/ ?>