@extends('layouts/base_view')
@section('title')
{!! $variables['header_routes'] !!}
@endsection
@section('content')
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card card-outline card-warning">
                    <div class="card-header">
                        <h3 class="card-title">{{ $variables['judul'] }}</h3>
                    </div>
                    <div class="card-body">
                        <form id="formID" method="post" action="{{ route('raw_materials.store', ['prm_id' => $data->id]) }}">
                            @csrf

                            <div class="row">
                                <!-- Basic Information -->
                                <div class="col-md-6">
                                    <div class="card">
                                        <div class="card-header card-outline card-warning">
                                            <h5 class="mb-0">Informasi Dasar</h5>
                                        </div>
                                        <div class="card-body">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="code" class="font-weight-bold">Kode <span class="text-danger">*</span></label>
                                                        <small class="text-muted text-xs">*({{ $data->id > 0 ? 'Kode tidak dapat diubah' : 'Otomatis dibuat jika kosong' }})</small>
                                                        <input value="{{ old('code', $data->code ?? '') }}" type="text" class="form-control form-control-sm" name="code" id="code" {{ $data->id > 0 ? 'readonly' : '' }}>
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="name" class="font-weight-bold">Nama <span class="text-danger">*</span></label>
                                                        <input value="{{ old('name', $data->name ?? '') }}" type="text" class="form-control form-control-sm" name="name" id="name" required>
                                                    </div>
                                                </div>
                                                <div class="col-md-12">
                                                    <div class="form-group">
                                                        <label for="description" class="font-weight-bold">Deskripsi</label>
                                                        <textarea class="form-control form-control-sm" name="description" id="description" rows="3">{{ old('description', $data->description ?? '') }}</textarea>
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="uom" class="font-weight-bold">UOM <span class="text-danger">*</span></label>
                                                        <select class="form-control form-control-sm select2 select2-warning" data-dropdown-css-class="select2-warning" name="uom" id="uom" required>
                                                            <option value="">Pilih UOM</option>
                                                            <option value="KG" {{ old('uom', $data->uom ?? '') == 'KG' ? 'selected' : '' }}>KG</option>
                                                            <option value="LT" {{ old('uom', $data->uom ?? '') == 'LT' ? 'selected' : '' }}>LT</option>
                                                            <option value="PA" {{ old('uom', $data->uom ?? '') == 'PA' ? 'selected' : '' }}>PA</option>
                                                            <option value="BX" {{ old('uom', $data->uom ?? '') == 'BX' ? 'selected' : '' }}>BX</option>
                                                            <option value="PCS" {{ old('uom', $data->uom ?? '') == 'PCS' ? 'selected' : '' }}>PCS</option>
                                                            <option value="DZ" {{ old('uom', $data->uom ?? '') == 'DZ' ? 'selected' : '' }}>DZ</option>
                                                        </select>
                                                    </div>
                                                </div>                                                
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="konversi_satuan_utuh" class="font-weight-bold">Konversi UOM Utuh</label>
                                                        <small class="text-muted text-xs">*(Unit untuk satuan utuh (contoh: DUS, BOX))</small>
                                                        <input value="{{ old('konversi_satuan_utuh', $data->konversi_satuan_utuh ?? '') }}" type="text" class="form-control form-control-sm" name="konversi_satuan_utuh" id="konversi_satuan_utuh" maxlength="10">
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="konversi_satuan_pakai" class="font-weight-bold">Konversi UOM Pakai</label>
                                                        <small class="text-muted text-xs">*(Unit untuk satuan pakai (contoh: LT, KG))</small>
                                                        <input value="{{ old('konversi_satuan_pakai', $data->konversi_satuan_pakai ?? '') }}" type="text" class="form-control form-control-sm" name="konversi_satuan_pakai" id="konversi_satuan_pakai" maxlength="10">
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="konversi_satuan_kirim" class="font-weight-bold">Konversi UOM Kirim</label>
                                                        <small class="text-muted text-xs">*(Unit untuk satuan kirim (contoh: PACK, UNIT))</small>
                                                        <input value="{{ old('konversi_satuan_kirim', $data->konversi_satuan_kirim ?? '') }}" type="text" class="form-control form-control-sm" name="konversi_satuan_kirim" id="konversi_satuan_kirim" maxlength="10">
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="tipe_barang" class="font-weight-bold">Tipe Barang <span class="text-danger">*</span></label>
                                                        <small class="form-text text-muted text-xs">*(Klasifikasi barang berdasarkan karakteristik penyimpanan)</small>
                                                        <select class="form-control form-control-sm select2 select2-warning" data-dropdown-css-class="select2-warning" name="tipe_barang" id="tipe_barang" required>
                                                            <option value="Kering" {{ old('tipe_barang', $data->tipe_barang ?? 'Kering') == 'Kering' ? 'selected' : '' }}>Kering</option>
                                                            <option value="Basah" {{ old('tipe_barang', $data->tipe_barang ?? '') == 'Basah' ? 'selected' : '' }}>Basah</option>
                                                        </select>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Order Parameters --> 
                                <div class="col-md-6">
                                    <div class="card">
                                        <div class="card-header card-outline card-warning">
                                            <h5 class="mb-0">Parameter Pemesanan</h5>
                                        </div>
                                        <div class="card-body">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="minimal_order" class="font-weight-bold">Pesanan Minimal</label>
                                                        <small class="text-muted text-xs">*(Jumlah minimum yang dapat dipesan)</small>
                                                        <input value="{{ old('minimal_order', $data->minimal_order ?? 0) }}" type="number" step="0.01" class="form-control form-control-sm" name="minimal_order" id="minimal_order">
                                                    </div>
                                                </div>

                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="multiple_order" class="font-weight-bold">Kelipatan Pesanan</label>
                                                        <small class="text-muted text-xs">*(Pesanan harus dalam kelipatan nilai ini)</small>
                                                        <input value="{{ old('multiple_order', $data->multiple_order ?? 1) }}" type="number" step="0.01" class="form-control form-control-sm" name="multiple_order" id="multiple_order">
                                                    </div>
                                                </div>

                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="suggest_ratio" class="font-weight-bold">Rasio Saran</label>
                                                        <small class="text-muted text-xs">*(Rasio yang disarankan untuk kalkulasi pesanan)</small>
                                                        <input value="{{ old('suggest_ratio', $data->suggest_ratio ?? 0) }}" type="number" step="0.0001" class="form-control form-control-sm" name="suggest_ratio" id="suggest_ratio">
                                                    </div>
                                                </div>

                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <label for="latest_ratio" class="font-weight-bold">Rasio Terbaru</label>
                                                        <small class="text-muted text-xs">*(Rasio terbaru dari penggunaan/pengiriman aktual)</small>
                                                        <input value="{{ old('latest_ratio', $data->latest_ratio ?? 0) }}" type="number" step="0.0001" class="form-control form-control-sm" name="latest_ratio" id="latest_ratio">
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div> 
                            <!-- Action Buttons -->
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group">
                                        <button type="submit" class="btn btn-sm btn-info button-cek-permission" data-permission="raw_materials.store">
                                            <i class="fa fa-save"></i> Simpan
                                        </button>
                                        <a href="{{ route('raw_materials.index') }}" class="btn btn-sm btn-warning" data='back' onclick="return notificationBeforeBack(event, this);">
                                            <i class="fa fa-arrow-left"></i> Kembali
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    $(document).ready(function() {
        $('#formID').on('submit', function(e) {
            e.preventDefault();

            const form = this;
            const formData = new FormData(form);

            // Show loading
            Swal.fire({
                title: 'Menyimpan...',
                allowOutsideClick: false,
                didOpen: () => {
                    Swal.showLoading();
                }
            });

            $.ajax({
                type: "POST",
                url: $(form).attr('action'),
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    Swal.fire({
                        title: 'Berhasil!',
                        text: 'Bahan baku berhasil disimpan',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        window.location.href = "{{ route('raw_materials.index') }}";
                    });
                },
                error: function(xhr) {
                    let message = 'Terjadi kesalahan';
                    if (xhr.responseJSON && xhr.responseJSON.errors) {
                        const errors = xhr.responseJSON.errors;
                        message = Object.values(errors).flat().join('\n');
                    } else if (xhr.responseJSON && xhr.responseJSON.message) {
                        message = xhr.responseJSON.message;
                    }

                    Swal.fire({
                        title: 'Error!',
                        text: message,
                        icon: 'error',
                        confirmButtonText: 'OK'
                    });
                }
            });
        });
    });
</script>
@endsection