@php
    use App\Models\Kriteria;
@endphp
@extends('layouts.app')
@section('title', 'Edit Kriteria - ' . $kriteria->nama_kriteria)
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card shadow-sm">
                <div class="card-header text-white d-flex justify-content-between align-items-center" style="background-color: rgb(0, 122, 153);">
                    <h5 class="mb-0">
                        <i class="fas fa-edit mr-2"></i>Edit Kriteria
                    </h5>
                    <a href="{{ route('kriteria.index') }}" class="btn btn-sm btn-light">
                        <i class="fas fa-arrow-left"></i>
                    </a>
                </div>
                
                <div class="card-body">
                    @if (Session::has('msg'))
                    <div class="alert alert-info alert-dismissible fade show">
                        <i class="fas fa-info-circle mr-2"></i>
                        {{ Session::get('msg') }}
                        <button type="button" class="close" data-dismiss="alert">
                            <span>&times;</span>
                        </button>
                    </div>
                    @endif

                    @php
                        $totalBobotSaatIni = round(App\Models\Kriteria::sum('bobot'), 2);
                        $selisih = $totalBobotSaatIni - $kriteria->bobot;
                        $bobotBaru = old('bobot', $kriteria->bobot);
                        $totalSetelahEdit = round($selisih + $bobotBaru, 2);
                    @endphp

                    @if ($totalSetelahEdit < 1)
                        <div class="alert alert-warning">
                            ⚠️ Total bobot setelah penyimpanan akan menjadi <strong>{{ number_format($totalSetelahEdit, 2) }}</strong><br>
                            Total masih kurang sebesar <strong>{{ number_format(1 - $totalSetelahEdit, 2) }}</strong>.
                        </div>
                    @elseif ($totalSetelahEdit > 1)
                        <div class="alert alert-danger">
                            ❌ Total bobot setelah penyimpanan akan menjadi <strong>{{ number_format($totalSetelahEdit, 2) }}</strong><br>
                            Total melebihi sebesar <strong>{{ number_format($totalSetelahEdit - 1, 2) }}</strong>.
                        </div>
                    @else
                        <div class="alert alert-success">
                            ✅ Total bobot setelah penyimpanan akan tepat <strong>1</strong>.
                        </div>
                    @endif

                    <form id="editKriteriaForm" method="POST" action="{{ route('kriteria.update', $kriteria->id) }}">                        
                        @csrf
                        @method('PUT')
                        
                        <div class="form-group">
                            <label>Nama Kriteria</label>
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><i class="fas fa-tag"></i></span>
                                </div>
                                <input type="text" class="form-control @error('nama_kriteria') is-invalid @enderror" 
                                       name="nama_kriteria" value="{{ old('nama_kriteria', $kriteria->nama_kriteria) }}">
                                @error('nama_kriteria')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label>Attribut Kriteria</label>
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><i class="fas fa-balance-scale"></i></span>
                                </div>
                                <select name="attribut" class="form-control @error('attribut') is-invalid @enderror" required>
                                    <option value="benefit" {{ old('attribut', $kriteria->attribut) == 'benefit' ? 'selected' : '' }}>Benefit</option>
                                    <option value="cost" {{ old('attribut', $kriteria->attribut) == 'cost' ? 'selected' : '' }}>Cost</option>
                                </select>
                                @error('attribut')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>
                        
                        <div class="form-group">
                            <label>Bobot Kriteria</label>
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <span class="input-group-text"><i class="fas fa-percentage"></i></span>
                                </div>
                                <input type="number" step="0.0001" min="0" max="1" 
                                       class="form-control @error('bobot') is-invalid @enderror"
                                       name="bobot" value="{{ old('bobot', $kriteria->bobot) }}" 
                                       required id="bobotInput">
                                @error('bobot')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            <small class="text-muted">Nilai antara 0.0000 sampai 1.0000</small>
                        </div>
                        
                        <div class="form-group text-right mt-4">
                            <button type="submit" class="btn btn-primary px-4" id="saveButton">
                                <i class="fas fa-save mr-2"></i>Simpan
                            </button>
                            <a href="{{ route('kriteria.index') }}" class="btn btn-secondary px-4">
                                <i class="fas fa-times mr-2"></i>Batal
                            </a>
                        </div>
                    </form>
                </div>
                
                <div class="card-footer text-muted small">
                    <i class="fas fa-history mr-1"></i> 
                    Terakhir diupdate: {{ $kriteria->updated_at->format('d/m/Y H:i') }}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
$(document).ready(function() {
    // Fungsi untuk menghitung total bobot dan mengupdate status tombol
    function updateSaveButton() {
        const currentBobot = parseFloat("{{ $kriteria->bobot }}");
        const newBobot = parseFloat($('#bobotInput').val()) || 0;
        const currentTotal = parseFloat("{{ Kriteria::sum('bobot') }}");
        
        // Hitung total baru setelah perubahan
        const newTotal = currentTotal - currentBobot + newBobot;
        
        // Bulatkan ke 4 desimal untuk perbandingan
        const roundedTotal = Math.round(newTotal * 10000) / 10000;
        
        if (roundedTotal === 1) {
            $('#saveButton').prop('disabled', false);
            $('#saveButton').removeClass('btn-secondary').addClass('btn-primary');
        } else {
            $('#saveButton').prop('disabled', true);
            $('#saveButton').removeClass('btn-primary').addClass('btn-secondary');
        }
    }
    
    // Panggil fungsi saat input bobot berubah
    $('#bobotInput').on('input', updateSaveButton);
    
    // Panggil sekali saat halaman dimuat
    updateSaveButton();
});
</script>
@endsection