@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>
                    <form id="formID" action="{{ route('store_raw_materials.store', $data->id) }}" method="POST">
                        @csrf
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="store_id" class="font-weight-bold">Store <span class="text-danger">*</span></label>
                                        <select class="form-control form-control-sm select2 select2-warning"
                                            data-dropdown-css-class="select2-warning"
                                            name="store_id"
                                            id="store_id"
                                            required
                                            {{ $data->id ? 'disabled' : '' }}>
                                            <option value="">Pilih Store</option>
                                            @foreach($variables['stores'] as $store)
                                            <option value="{{ $store->id }}" {{ old('store_id', $data->store_id ?? '') == $store->id ? 'selected' : '' }}>
                                                {{ $store->code }} - {{ $store->name }}
                                            </option>
                                            @endforeach
                                        </select>
                                        @if($data->id)
                                        <input type="hidden" name="store_id" value="{{ $data->store_id }}">
                                        @endif
                                    </div>

                                    <div class="form-group">
                                        <label for="raw_material_id" class="font-weight-bold">Item <span class="text-danger">*</span></label>
                                        <select class="form-control form-control-sm select2 select2-warning"
                                            data-dropdown-css-class="select2-warning"
                                            name="raw_material_id"
                                            id="raw_material_id"
                                            required
                                            {{ $data->id ? 'disabled' : '' }}>
                                            <option value="">Pilih Item</option>
                                            @foreach($variables['raw_materials'] as $rm)
                                            <option value="{{ $rm->id }}"
                                                data-tipe-barang="{{ $rm->tipe_barang }}"
                                                {{ old('raw_material_id', $data->raw_material_id ?? '') == $rm->id ? 'selected' : '' }}>
                                                {{ $rm->code }} - {{ $rm->name }} ({{ $rm->tipe_barang }})
                                            </option>
                                            @endforeach
                                        </select>
                                        @if($data->id)
                                        <input type="hidden" name="raw_material_id" value="{{ $data->raw_material_id }}">
                                        @endif
                                    </div>
  
                                    <div class="form-group">
                                        <label class="font-weight-bold">Flag Status</label>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="lsm_flag" id="lsm_flag" value="1" {{ old('lsm_flag', $data->lsm_flag ?? true) ? 'checked' : '' }}>
                                            <label class="form-check-label" for="lsm_flag">
                                                <strong>Flag LSM</strong> - Masukkan dalam Stock Opname (LSM)
                                            </label>
                                        </div>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="spb_flag" id="spb_flag" value="1" {{ old('spb_flag', $data->spb_flag ?? true) ? 'checked' : '' }}>
                                            <label class="form-check-label" for="spb_flag">
                                                <strong>Flag SPB</strong> - Izinkan dalam Permintaan Item (SPB)
                                            </label>
                                        </div>
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="inv_matrix_flag" id="inv_matrix_flag" value="1" {{ old('inv_matrix_flag', $data->inv_matrix_flag ?? false) ? 'checked' : '' }}>
                                            <label class="form-check-label" for="inv_matrix_flag">
                                                <strong>Matriks Inventori</strong> - Tampilkan field penyesuaian
                                            </label>
                                        </div>
                                        <small class="form-text text-muted text-xs">Pengaturan ini spesifik untuk store ini</small>
                                    </div>

                                    <div class="form-group">
                                        <label for="period_id" class="font-weight-bold">Periode Pemesanan</label>
                                        <select class="form-control form-control-sm select2 select2-warning" data-dropdown-css-class="select2-warning" name="period_id" id="period_id">
                                            <option value="">Pilih Periode</option>
                                            @foreach($variables['periods'] as $period)
                                            <option value="{{ $period->id }}"
                                                {{ old('period_id', $data->period_id ?? '') == $period->id ? 'selected' : '' }}>
                                                {{ $period->code }} - {{ $period->name }}
                                            </option>
                                            @endforeach
                                        </select>
                                        <small class="form-text text-muted">Periode pemesanan untuk item ini di store ini</small>
                                    </div>

                                    <div class="form-group">
                                        <label for="tipe_dokumen" class="font-weight-bold">Tipe Dokumen <span class="text-danger">*</span></label>
                                        <small class="form-text text-muted text-xs mb-2">
                                            ⚠️ Menentukan jenis dokumen untuk item ini di store ini. Item yang sama bisa punya tipe dokumen berbeda di store lain.
                                        </small>
                                        <select class="form-control form-control-sm select2 select2-warning"
                                            data-dropdown-css-class="select2-warning"
                                            name="tipe_dokumen"
                                            id="tipe_dokumen"
                                            required>
                                            <option value="DO" {{ old('tipe_dokumen', $data->tipe_dokumen ?? 'DO') == 'DO' ? 'selected' : '' }}>
                                                DO - Delivery Order (Warehouse)
                                            </option>
                                            <option value="PO" {{ old('tipe_dokumen', $data->tipe_dokumen ?? '') == 'PO' ? 'selected' : '' }}>
                                                PO - Purchase Order (Supplier)
                                            </option>
                                            <option value="PR" {{ old('tipe_dokumen', $data->tipe_dokumen ?? '') == 'PR' ? 'selected' : '' }}>
                                                PR - Purchase Request (Approval)
                                            </option>
                                        </select>
                                        <small class="form-text text-muted text-xs">
                                            <strong>Info:</strong> Tipe Dokumen menentukan bagaimana SPB akan di-generate.
                                            Dalam 1 SO, items akan di-group berdasarkan kombinasi <strong>Tipe Dokumen × Tipe Barang</strong>,
                                            <br>sehingga 1 SO bisa menghasilkan maksimal 6 SPB (3 tipe dokumen × 2 tipe barang).
                                        </small>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="min_stock" class="font-weight-bold">Stok Minimum</label>
                                        <input type="number"
                                            class="form-control form-control-sm"
                                            name="min_stock"
                                            id="min_stock"
                                            step="0.01"
                                            min="0"
                                            value="{{ old('min_stock', $data->min_stock ?? 0) }}">
                                        <small class="form-text text-muted">Level stok minimum untuk item ini di toko ini</small>
                                    </div>

                                    <div class="form-group">
                                        <label for="max_stock" class="font-weight-bold">Stok Maksimum</label>
                                        <input type="number"
                                            class="form-control form-control-sm"
                                            name="max_stock"
                                            id="max_stock"
                                            step="0.01"
                                            min="0"
                                            value="{{ old('max_stock', $data->max_stock ?? 0) }}">
                                        <small class="form-text text-muted">Level stok maksimum untuk item ini di toko ini</small>
                                    </div>

                                    <div class="form-group">
                                        <label for="reorder_point" class="font-weight-bold">Reorder Point</label>
                                        <input type="number"
                                            class="form-control form-control-sm"
                                            name="reorder_point"
                                            id="reorder_point"
                                            step="0.01"
                                            min="0"
                                            value="{{ old('reorder_point', $data->reorder_point ?? 0) }}">
                                        <small class="form-text text-muted">Level stok yang memicu pemesanan ulang</small>
                                    </div>

                                    <div class="form-group">
                                        <label for="notes" class="font-weight-bold">Catatan</label>
                                        <textarea class="form-control form-control-sm"
                                            name="notes"
                                            id="notes"
                                            rows="3">{{ old('notes', $data->notes ?? '') }}</textarea>
                                        <small class="form-text text-muted">Catatan tambahan untuk pemetaan ini</small>
                                    </div>
                                </div>
                            </div> 
                        </div> 
                        <div class="card-footer">
                            <button type="submit" class="btn btn-sm btn-info">
                                <i class="fa fa-save"></i> Simpan
                            </button>
                            <a href="{{ route('store_raw_materials.index') }}" class="btn btn-sm btn-warning">
                                <i class="fa fa-arrow-left"></i> Kembali
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    $(function() {
        // Initialize Select2
        $('.select2').select2();

        // Form validation
        $('#formID').validate({
            rules: {
                store_id: {
                    required: true
                },
                raw_material_id: {
                    required: true
                },
                tipe_dokumen: {
                    required: true
                },
                min_stock: {
                    number: true,
                    min: 0
                },
                max_stock: {
                    number: true,
                    min: 0
                },
                reorder_point: {
                    number: true,
                    min: 0
                }
            },
            errorElement: 'span',
            errorPlacement: function(error, element) {
                error.addClass('invalid-feedback');
                element.closest('.form-group').append(error);
            },
            highlight: function(element, errorClass, validClass) {
                $(element).addClass('is-invalid');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass('is-invalid');
            },
            submitHandler: function(form) {
                var formData = $(form).serialize();

                $.ajax({
                    url: $(form).attr('action'),
                    type: 'POST',
                    data: formData,
                    success: function(response) {
                        Swal.fire({
                            icon: 'success',
                            title: 'Berhasil!',
                            text: response[1],
                            showConfirmButton: false,
                            timer: 1500
                        }).then(function() {
                            window.location.href = '{{ route("store_raw_materials.index") }}';
                        });
                    },
                    error: function(xhr) {
                        var errorMsg = 'Terjadi kesalahan';
                        if (xhr.responseJSON && xhr.responseJSON[1]) {
                            errorMsg = xhr.responseJSON[1];
                        }

                        Swal.fire({
                            icon: 'error',
                            title: 'Gagal!',
                            text: errorMsg
                        });
                    }
                });

                return false;
            }
        });
    });
</script>
@endsection