@extends('layout.master')

@section('title', 'Tambah Kriteria')

@section('content')
<div class="card border-top-primary shadow mb-4">
    <form action="{{ route('store_kriteria') }}" method="POST" id="formTambahKriteria">
        @csrf
        <div class="card-body pt-3">
            <div class="row">
                <div class="col-lg-12">
                    <div class="form-group">
                        <label for="">Kode Kriteria</label>
                        <input type="text" class="form-control" name="kode_kriteria">
                    </div>
                </div>
                <div class="col-lg-12">
                    <div class="form-group">
                        <label for="inputSifat">Sifat</label><br>
                        <label class="radio-inline">
                            <input type="radio" name="sifat" value="B"> Benefit
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="sifat" value="C"> Cost
                        </label>
                    </div>
                </div>
                <div class="col-lg-12">
                    <div class="form-group">
                        <label for="">Nama Kriteria</label>
                        <input type="text" class="form-control" name="nama_kriteria">
                    </div>
                </div>
                <div class="col-lg-12">
                    <div class="form-group">
                        <label for="">Bobot Kriteria</label>
                        <input type="number" class="form-control" name="bobot_kriteria">
                    </div>
                </div>
                <div class="col-lg-12">
                    <div class="form-group">
                        <label for="">Item Kriteria</label>
                        <div class="mb-3">
                            <input type="itemKriteria1" class="form-control" name="itemKriteria1" placeholder="Value 1">
                        </div>
                        <div class="mb-3">
                            <input type="itemKriteria2" class="form-control" name="itemKriteria2" placeholder="Value 2">
                        </div>
                        <div class="mb-3">
                            <input type="itemKriteria3" class="form-control" name="itemKriteria3" placeholder="Value 3">
                        </div>
                        <div class="mb-3">
                            <input type="itemKriteria4" class="form-control" name="itemKriteria4" placeholder="Value 4">
                        </div>
                    </div>
                </div>

            </div>
        </div>
        <div class="card-footer text-left">
            <button type="button" class="btn btn-primary" onclick="konfirmasiTambah()">Tambah</button>
            <a href="{{ route('data_kriteria') }}" class="btn btn-secondary">Kembali</a>
        </div>
    </form>
</div>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10"></script>
<script>
    function konfirmasiTambah() {
        // Validasi input
        var kode_kriteria = $('#kode_kriteria').val();
        var sifat = $("input[name='sifat']:checked").val();
        var nama_kriteria = $('#nama_kriteria').val();
        var bobot_kriteria = $('#bobot_kriteria').val();
        var itemKriteria1 = $('#itemKriteria1').val();
        var itemKriteria2 = $('#itemKriteria2').val();
        var itemKriteria3 = $('#itemKriteria3').val();
        var itemKriteria4 = $('#itemKriteria4').val();

        if (
            kode_kriteria === '' ||
            sifat === undefined ||
            nama_kriteria === '' ||
            bobot_kriteria === '' ||
            itemKriteria1 === '' ||
            itemKriteria2 === '' ||
            itemKriteria3 === '' ||
            itemKriteria4 === ''
        ) {
            Swal.fire({
                title: 'Peringatan',
                text: 'Semua kolom harus diisi!',
                icon: 'warning',
                confirmButtonColor: '#3085d6',
                confirmButtonText: 'OK'
            });
        } else {
            // Jika semua input terisi, tampilkan popup konfirmasi
            Swal.fire({
                title: 'Konfirmasi',
                text: 'Apakah Anda yakin ingin menambahkan kriteria?',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Ya, Tambahkan!',
                cancelButtonText: 'Batal'
            }).then((result) => {
                if (result.isConfirmed) {
                    // Jika user mengonfirmasi, submit form
                    document.getElementById('formTambahKriteria').submit();
                }
            });
        }
    }
</script>
@endsection