@extends('admin.template')

@section('content')
<div class="content-wrapper">
    <div class="row">
        <div class="col-md-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Tambah Data Sampah Tahunan</h4>
                    <p class="card-description">
                        Form input data sampah per tahun (satuan dalam Ton)
                    </p>

                    <form action="{{ route('admin.sampah.store') }}" method="POST">
                        @csrf

                        <div class="form-group">
                            <label>Tahun</label>
                            <input type="number" name="tahun" class="form-control"
                                   placeholder="Contoh: 2024" required>
                        </div>

                        <div class="form-group">
                            <label>Total Sampah</label>
                            <div class="input-group">
                                <input type="number" step="0.01" name="total_sampah"
                                       id="total_sampah"
                                       class="form-control"
                                       placeholder="Total sampah dihasilkan" required>
                                <div class="input-group-append">
                                    <span class="input-group-text">Ton</span>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label>Total Sampah Dikelola</label>
                            <div class="input-group">
                                <input type="number" step="0.01" name="total_kelola"
                                       id="total_kelola"
                                       class="form-control"
                                       placeholder="Total sampah yang dikelola" required>
                                <div class="input-group-append">
                                    <span class="input-group-text">Ton</span>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label>Total Sampah Daur Ulang</label>
                            <div class="input-group">
                                <input type="number" step="0.01" name="total_daur_ulang"
                                       id="total_daur_ulang"
                                       class="form-control"
                                       placeholder="Total sampah yang didaur ulang" required>
                                <div class="input-group-append">
                                    <span class="input-group-text">Ton</span>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label>Sisa Sampah</label>
                            <div class="input-group">
                                <input type="number" step="0.01" name="sisa_sampah"
                                       id="sisa_sampah"
                                       class="form-control" readonly>
                                <div class="input-group-append">
                                    <span class="input-group-text">Ton</span>
                                </div>
                            </div>
                            <small class="text-muted">
                                Sisa sampah dihitung otomatis
                            </small>
                        </div>

                        <button type="submit" class="btn btn-primary mr-2">
                            Simpan
                        </button>
                        <a href="{{ route('admin.sampah.index') }}" class="btn btn-light">
                            Batal
                        </a>
                    </form>

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

<script>
    function hitungSisa() {
        let total = parseFloat(document.getElementById('total_sampah').value) || 0;
        let kelola = parseFloat(document.getElementById('total_kelola').value) || 0;
        let daur = parseFloat(document.getElementById('total_daur_ulang').value) || 0;

        let sisa = total - (kelola + daur);
        document.getElementById('sisa_sampah').value = sisa >= 0 ? sisa : 0;
    }

    document.getElementById('total_sampah').addEventListener('input', hitungSisa);
    document.getElementById('total_kelola').addEventListener('input', hitungSisa);
    document.getElementById('total_daur_ulang').addEventListener('input', hitungSisa);
</script>
@endsection
