@extends('incharge.layout.page-app')
@section('page_title', __('label.working_time'))
@section('tab_title', __('label.working_time'))

@section('content')
    @include('incharge.layout.sidebar')

    <div class="right-content">
        @include('incharge.layout.header')

        <div class="body-content">
            <!-- mobile title -->
            <h1 class="page-title-sm">{{__('label.working_time')}}</h1>

            <div class="border-bottom row mb-3">
                <div class="col-sm-12">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item">
                            <a href="{{ route('incharge.dashboard') }}">{{__('label.dashboard')}}</a>
                        </li>
                        <li class="breadcrumb-item active" aria-current="page">{{__('label.working_time')}}</li>
                    </ol>
                </div>
            </div>

            <div class="card custom-border-card mt-3">
                <div class="card-body">
                    <form id="working_time" autocomplete="off" enctype="multipart/form-data">
                        <input type="hidden" name="incharge_id" value="{{$incharge_id}}">
                        @php
                            $days = [
                                1 => __('label.monday'),
                                2 => __('label.tuesday'),
                                3 => __('label.wednesday'),
                                4 => __('label.thursday'),
                                5 => __('label.friday'),
                                6 => __('label.saturday'),
                                7 => __('label.sunday'),
                            ];
                        @endphp
                        @foreach($days as $dayNumber => $dayLabel)
                            @php
                                $startTime = $work_time[$dayNumber - 1]['start_time'] ?? '00:00';
                                $endTime = $work_time[$dayNumber - 1]['end_time'] ?? '00:00';
                                $status = $work_time[$dayNumber - 1]['status'] ?? 0;
                            @endphp
                            <div class="form-row">
                                <div class="col-2">
                                    <h6 class="pl-3 pt-1">{{ $dayLabel }}</h6>
                                </div>
                                <div class="col-3">
                                    <div class="form-group">
                                        <div class="radio-group">
                                            <div class="custom-control custom-radio">
                                                <input type="radio" name="day_{{ $dayNumber }}" id="day_{{ $dayNumber }}_off" class="custom-control-input" value="0" {{ ($status == '0')? "checked" : "" }}>
                                                <label class="custom-control-label" for="day_{{ $dayNumber }}_off">{{ __('label.off') }}</label>
                                            </div>
                                            <div class="custom-control custom-radio">
                                                <input type="radio" name="day_{{ $dayNumber }}" id="day_{{ $dayNumber }}_on" class="custom-control-input" value="1" {{ ($status == '1')? "checked" : "" }}>
                                                <label class="custom-control-label" for="day_{{ $dayNumber }}_on">{{ __('label.on') }}</label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-3">
                                    <div class="form-group">
                                        <input name="start_time_{{ $dayNumber }}" type="time" value="{{ $startTime }}" class="form-control" {{ ($status == '0')? "readonly" : "" }}>
                                    </div>
                                </div>
                                <div class="col-1 text-center">
                                    <h6 class="pt-3">{{ __('label.to') }}</h6>
                                </div>
                                <div class="col-3">
                                    <div class="form-group">
                                        <input name="end_time_{{ $dayNumber }}" type="time"  value="{{ $endTime }}" class="form-control" {{ ($status == '0')? "readonly" : "" }}>
                                    </div>
                                </div>
                            </div>
                        @endforeach
                        <div class="border-top pt-3 text-right">
                            <button type="button" class="btn btn-default mw-120" onclick="save_working_time()">{{__('label.save')}}</button>
                            <input type="hidden" name="_method" value="PATCH">
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('pagescript')
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    
    <script>
        $(document).ready(function() {

            // Working Time
            const days = ['day_1', 'day_2', 'day_3', 'day_4', 'day_5', 'day_6', 'day_7'];
            days.forEach((day, index) => {
                const dayOnRadio = document.getElementById(`${day}_on`);
                const dayOffRadio = document.getElementById(`${day}_off`);
                const startTime = document.querySelector(`input[name="start_time_${index + 1}"]`);
                const endTime = document.querySelector(`input[name="end_time_${index + 1}"]`);

                // Enable/disable time fields based on radio selection
                const toggleTimeFields = () => {
                    if (dayOnRadio.checked) {
                        startTime.removeAttribute('readonly');
                        endTime.removeAttribute('readonly');
                    } else {
                        startTime.setAttribute('readonly', 'readonly');
                        endTime.setAttribute('readonly', 'readonly');
                        startTime.value = "00:00";
                        endTime.value = "00:00";
                    }
                };

                // Add event listeners to toggle fields
                dayOnRadio.addEventListener('change', toggleTimeFields);
                dayOffRadio.addEventListener('change', toggleTimeFields);
            });
        });

        function save_working_time() {

            var Demo_Mode = '<?php echo Demo_Mode(); ?>';
            if(Demo_Mode == 1){

                $("#dvloader").show();
                var formData = new FormData($("#working_time")[0]);
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    enctype: 'multipart/form-data',
                    type: 'POST',
                    url: '{{route("inworkingtime.update", [$incharge_id])}}',
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(resp) {
                        $("#dvloader").hide();
                        get_responce_message(resp, 'working_time', '{{ route("inworkingtime.index") }}');
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $("#dvloader").hide();
                        toastr.error(errorThrown, textStatus);
                    }
                });
            } else {
                showError();
            }
        }
    </script>
@endsection