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

                            <!-- Basic Information -->
                            <div class="row">
                                <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="form-group">
                                                <label for="code">Kode <span class="text-danger">*</span></label>
                                                <input type="text"
                                                    class="form-control form-control-sm"
                                                    id="code"
                                                    name="code"
                                                    value="{{ old('code', $data->code ?? '') }}"
                                                    maxlength="20"
                                                    {{ $data->id > 0 ? 'readonly' : '' }}
                                                    required>
                                                <small class="form-text text-muted text-sm">
                                                    {{ $data->id > 0 ? 'Kode tidak dapat diubah' : 'Kode unik untuk supplier (maksimal 20 karakter)' }}
                                                </small>
                                            </div>

                                            <div class="form-group">
                                                <label for="name">Nama <span class="text-danger">*</span></label>
                                                <input type="text"
                                                    class="form-control form-control-sm"
                                                    id="name"
                                                    name="name"
                                                    value="{{ old('name', $data->name ?? '') }}"
                                                    maxlength="255"
                                                    required>
                                            </div>

                                            <div class="form-group">
                                                <label for="contact_person">Kontak Person</label>
                                                <input type="text"
                                                    class="form-control form-control-sm"
                                                    id="contact_person"
                                                    name="contact_person"
                                                    value="{{ old('contact_person', $data->contact_person ?? '') }}"
                                                    maxlength="255">
                                            </div>

                                            <div class="form-group">
                                                <label for="phone">Telepon</label>
                                                <input type="text"
                                                    class="form-control form-control-sm"
                                                    id="phone"
                                                    name="phone"
                                                    value="{{ old('phone', $data->phone ?? '') }}"
                                                    maxlength="20">
                                            </div>

                                            <div class="form-group">
                                                <label for="email">Email</label>
                                                <input type="email"
                                                    class="form-control form-control-sm"
                                                    id="email"
                                                    name="email"
                                                    value="{{ old('email', $data->email ?? '') }}"
                                                    maxlength="255">
                                            </div>

                                            <div class="form-group">
                                                <label for="address">Alamat</label>
                                                <textarea class="form-control form-control-sm"
                                                    id="address"
                                                    name="address"
                                                    rows="3">{{ old('address', $data->address ?? '') }}</textarea>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="card">
                                        <div class="card-header card-outline card-warning">
                                            <h5 class="mb-0">Jadwal Kontak</h5>
                                        </div>
                                        <div class="card-body">
                                            <div class="form-group">
                                                <label>Hari Kontak</label>
                                                <div class="row">
                                                    @php
                                                    $days = [
                                                    'monday' => 'Senin',
                                                    'tuesday' => 'Selasa',
                                                    'wednesday' => 'Rabu',
                                                    'thursday' => 'Kamis',
                                                    'friday' => 'Jumat',
                                                    'saturday' => 'Sabtu',
                                                    'sunday' => 'Minggu'
                                                    ];
                                                    $selectedDays = old('contact_days', $data->contact_days ?? []);
                                                    @endphp
                                                    @foreach($days as $value => $label)
                                                    <div class="col-6">
                                                        <div class="custom-control custom-checkbox">
                                                            <input type="checkbox"
                                                                class="custom-control-input"
                                                                id="day_{{ $value }}"
                                                                name="contact_days[]"
                                                                value="{{ $value }}"
                                                                {{ in_array($value, $selectedDays) ? 'checked' : '' }}>
                                                            <label class="custom-control-label" for="day_{{ $value }}">
                                                                {{ $label }}
                                                            </label>
                                                        </div>
                                                    </div>
                                                    @endforeach
                                                </div>
                                                <small class="form-text text-muted text-sm">Pilih hari ketika supplier dapat dihubungi</small>
                                            </div>

                                            <div class="row">
                                                <div class="col-6">
                                                    <div class="form-group">
                                                        <label for="contact_time_start">Waktu Kontak Mulai</label>
                                                        <input type="time"
                                                            class="form-control form-control-sm"
                                                            id="contact_time_start"
                                                            name="contact_time_start"
                                                            value="{{ old('contact_time_start', $data->contact_time_start ? $data->contact_time_start->format('H:i') : '') }}">
                                                    </div>
                                                </div>
                                                <div class="col-6">
                                                    <div class="form-group">
                                                        <label for="contact_time_end">Waktu Kontak Selesai</label>
                                                        <input type="time"
                                                            class="form-control form-control-sm"
                                                            id="contact_time_end"
                                                            name="contact_time_end"
                                                            value="{{ old('contact_time_end', $data->contact_time_end ? $data->contact_time_end->format('H:i') : '') }}">
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="form-group">
                                                <label for="lead_time_days">Lead Time (Hari) <span class="text-danger">*</span></label>
                                                <input type="number"
                                                    class="form-control form-control-sm"
                                                    id="lead_time_days"
                                                    name="lead_time_days"
                                                    value="{{ old('lead_time_days', $data->lead_time_days ?? 1) }}"
                                                    min="0"
                                                    max="365"
                                                    required>
                                                <small class="form-text text-muted text-sm">Jumlah hari dari kontak hingga pengiriman</small>
                                            </div>

                                            <div class="form-group">
                                                <label for="notes">Catatan</label>
                                                <textarea class="form-control form-control-sm"
                                                    id="notes"
                                                    name="notes"
                                                    rows="3">{{ old('notes', $data->notes ?? '') }}</textarea>
                                            </div>

                                            <div class="form-group">
                                                <div class="custom-control custom-checkbox">
                                                    <input type="checkbox"
                                                        class="custom-control-input"
                                                        id="is_active"
                                                        name="is_active"
                                                        value="1"
                                                        {{ old('is_active', $data->is_active ?? true) ? 'checked' : '' }}>
                                                    <label class="custom-control-label" for="is_active">
                                                        Aktif
                                                    </label>
                                                </div>
                                                <small class="form-text text-muted text-sm">Centang untuk mengaktifkan supplier ini</small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group mb-0">
                                        <button type="submit" class="btn btn-sm btn-info button-cek-permission" data-permission="shipment_suppliers.store">
                                            <i class="fas fa-save"></i>
                                            {{ $data->id ? 'Perbarui' : 'Buat' }} Supplier
                                        </button>
                                        <a href="{{ route('shipment_suppliers.index') }}" class="btn btn-sm btn-secondary ml-2">
                                            <i class="fas fa-times"></i> Batal
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    $(document).ready(function() {
        // Auto-generate code from name if creating new supplier
        @if(!$data->id)
        $('#name').on('input', function() {
            var name = $(this).val();
            var code = name.toUpperCase()
                .replace(/[^A-Z0-9\s]/g, '')
                .replace(/\s+/g, '')
                .substring(0, 20);
            if (code.length > 0) {
                code = 'SUP' + code.substring(0, 17);
            }
            $('#code').val(code);
        });
        @endif

        // Form submit with AJAX
        $('#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: 'Supplier berhasil disimpan',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        window.location.href = "{{ route('shipment_suppliers.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