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

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="name">Nama Hari Libur <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>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="date">Tanggal <span class="text-danger">*</span></label>
                                        <input type="date"
                                            class="form-control form-control-sm"
                                            id="date"
                                            name="date"
                                            value="{{ old('date', $data->date ? $data->date->format('Y-m-d') : '') }}"
                                            required>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="type">Tipe <span class="text-danger">*</span></label>
                                        <select class="form-control form-control-sm select2 select2-warning" data-dropdown-css-class="select2-warning" name="type" id="type" required>
                                            <option value="">Pilih Tipe</option>
                                            @foreach($variables['types'] as $value => $label)
                                            <option value="{{ $value }}"
                                                {{ old('type', $data->type ?? '') == $value ? 'selected' : '' }}>
                                                {{ $label }}
                                            </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="recurring_type">Tipe Pengulangan <span class="text-danger">*</span></label>
                                        <select class="form-control form-control-sm select2 select2-warning" data-dropdown-css-class="select2-warning" name="recurring_type" id="recurring_type" required>
                                            @foreach($variables['recurring_types'] as $value => $label)
                                            <option value="{{ $value }}"
                                                {{ old('recurring_type', $data->recurring_type ?? 'None') == $value ? 'selected' : '' }}>
                                                {{ $label }}
                                            </option>
                                            @endforeach
                                        </select>
                                        <small class="form-text text-muted text-sm">Pilih jika hari libur ini berulang setiap tahun atau bulan</small>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="description">Deskripsi</label>
                                        <textarea class="form-control form-control-sm"
                                            id="description"
                                            name="description"
                                            rows="3">{{ old('description', $data->description ?? '') }}</textarea>
                                        <small class="form-text text-muted text-sm">Deskripsi opsional tentang hari libur</small>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <div class="custom-control custom-checkbox">
                                            <input type="checkbox"
                                                class="custom-control-input"
                                                id="is_recurring"
                                                name="is_recurring"
                                                value="1"
                                                {{ old('is_recurring', $data->is_recurring ?? false) ? 'checked' : '' }}>
                                            <label class="custom-control-label" for="is_recurring">
                                                Hari Libur Berulang
                                            </label>
                                        </div>
                                        <small class="form-text text-muted text-sm">Centang jika hari libur ini berulang setiap tahun/bulan</small>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <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 hari libur ini</small>
                                    </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="holidays.store">
                                            <i class="fas fa-save"></i>
                                            {{ $data->id ? 'Perbarui' : 'Buat' }} Hari Libur
                                        </button>
                                        <a href="{{ route('holidays.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() {
        // Handle recurring checkbox
        $('#is_recurring').on('change', function() {
            if ($(this).is(':checked')) {
                $('#recurring_type').prop('disabled', false);
                if ($('#recurring_type').val() === 'None') {
                    $('#recurring_type').val('Yearly');
                }
            } else {
                $('#recurring_type').val('None').prop('disabled', true);
            }
        });

        // Initialize recurring type state
        if (!$('#is_recurring').is(':checked')) {
            $('#recurring_type').val('None').prop('disabled', true);
        }

        // 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: 'Hari libur berhasil disimpan',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(() => {
                        window.location.href = "{{ route('holidays.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