@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('shipment_deliveries.store', ['prm_id' => $data->id]) }}">
                            @csrf

                            <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" name="store_id" id="store_id" required>
                                            <option value="">Pilih Store</option>
                                            @foreach($variables['stores'] as $store)
                                            <option value="{{ $store->id }}" {{ old('store_id', $data->store_id ?? '') == $store->id ? 'selected' : '' }}>
                                                {{ $store->name }}
                                            </option>
                                            @endforeach
                                        </select>
                                    </div>

                                    <div class="form-group">
                                        <label for="armada_type_id" class="font-weight-bold">Tipe Armada <span class="text-danger">*</span></label>
                                        <select class="form-control form-control-sm select2" name="armada_type_id" id="armada_type_id" required>
                                            <option value="">Pilih Tipe Armada</option>
                                            @foreach($variables['armada_types'] as $armadaType)
                                            <option value="{{ $armadaType->id }}" {{ old('armada_type_id', $data->armada_type_id ?? '') == $armadaType->id ? 'selected' : '' }}>
                                                {{ $armadaType->name }}
                                            </option>
                                            @endforeach
                                        </select>
                                    </div>

                                    <div class="form-group">
                                        <label for="type" class="font-weight-bold">Tipe <span class="text-danger">*</span></label>
                                        <small class="text-muted text-xs">*(Tipe pengiriman DCKP atau DSLK)</small>
                                        <select class="form-control form-control-sm" name="type" id="type" required>
                                            <option value="">Pilih Tipe</option>
                                            @foreach($variables['types'] as $type)
                                            <option value="{{ $type }}" {{ old('type', $data->type ?? 'DCKP') == $type ? 'selected' : '' }}>
                                                {{ $type }}
                                            </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="day_name" class="font-weight-bold">Hari <span class="text-danger">*</span></label>
                                        <select class="form-control form-control-sm" name="day_name" id="day_name" required>
                                            <option value="">Pilih Hari</option>
                                            @foreach($variables['days'] as $day)
                                            <option value="{{ $day }}" {{ old('day_name', $data->day_name ?? '') == $day ? 'selected' : '' }}>
                                                {{ $day }}
                                            </option>
                                            @endforeach
                                        </select>
                                    </div>

                                    <div class="form-group">
                                        <label for="arrival_time" class="font-weight-bold">Waktu Kedatangan</label>
                                        <input value="{{ old('arrival_time', $data->arrival_time ?? '') }}" type="time" class="form-control form-control-sm" name="arrival_time" id="arrival_time">
                                    </div>

                                    <div class="form-group">
                                        <label for="departure_time" class="font-weight-bold">Waktu Keberangkatan</label>
                                        <input value="{{ old('departure_time', $data->departure_time ?? '') }}" type="time" class="form-control form-control-sm" name="departure_time" id="departure_time">
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="h_minus" class="font-weight-bold">H-Minus (Hari) <span class="text-danger">*</span></label>
                                        <small class="text-muted text-xs">*(Jumlah hari sebelum pengiriman)</small>
                                        <input value="{{ old('h_minus', $data->h_minus ?? 2) }}" type="number" min="1" max="7" class="form-control form-control-sm" name="h_minus" id="h_minus" required>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="status" class="font-weight-bold">Status <span class="text-danger">*</span></label>
                                        <select class="form-control form-control-sm" name="status" id="status" required>
                                            <option value="Active" {{ old('status', $data->status ?? 'Active') == 'Active' ? 'selected' : '' }}>Aktif</option>
                                            <option value="Inactive" {{ old('status', $data->status ?? '') == 'Inactive' ? 'selected' : '' }}>Nonaktif</option>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <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="shipment_deliveries.store">
                                            <i class="fa fa-save"></i> Simpan
                                        </button>
                                        <a href="{{ route('shipment_deliveries.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() {
        // Initialize select2
        $('.select2').select2({
            width: '100%'
        });

        $('#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: 'Pengiriman berhasil disimpan',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        window.location.href = "{{ route('shipment_deliveries.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