<div class="description">
    <p>
        {!! __('calendar.settings.calendar.subtitle', ['calendar' => $data['instance']['displayname']]) !!}
    </p>
</div>

@php
    /** @var \App\Models\CalendarInstance $instance */
    $instance = $data['instance'];
    $meta     = $data['meta'] ?? null;

    $isRemote = (bool) ($meta?->is_remote ?? false);
    $isShared = (bool) ($meta?->is_shared ?? false);
    $shareUrl = $data['shareUrl'] ?? null;

    $color = old('color', $instance->resolvedColor());

    $timezone = old('timezone',
        $instance->timezone
        ?? ($data['userTz'] ?? 'UTC')
    );
@endphp

<form method="post"
      action="{{ route('calendar.settings.calendars.update', $instance->uri) }}"
      class="settings mt-8">
    @csrf
    @method('patch')

    <div class="input-row input-row--1">
        <div class="input-cell">
            <x-input.label for="displayname" :value="__('calendar.name')" />
            <x-input.text id="displayname"
                          name="displayname"
                          type="text"
                          required="true"
                          :value="old('displayname', $instance->displayname)" />
            <x-input.error :messages="$errors->get('displayname')" />
        </div>
    </div>

    <div class="input-row input-row--1">
        <div class="input-cell">
            <x-input.textarea-label
                :label="__('calendar.description')"
                id="description"
                name="description"
                placeholder="Brief description for this calendar..."
                :value="old('description', $instance->description)"
            />
            <x-input.error :messages="$errors->get('description')" />
        </div>
    </div>

    <div class="input-row input-row--1-1">
        <div class="input-cell">
            <x-input.label for="timezone" :value="__('common.timezone')" />
            <x-input.select
                id="timezone"
                name="timezone"
                placeholder="{{ __('common.timezone_select') }}"
                :value="$timezone"
                :options="$timezones"
                :selected="old('timezone', $instance->timezone ?? $user->timezone)"
                :description="__('calendar.timezone_help')" />
            <x-input.error :messages="$errors->get('timezone')" />
        </div>

        <div class="input-cell">
            <x-input.label for="color" :value="__('calendar.color')" />
            <x-input.color-picker
                name="color"
                :value="$color"
                id="color"
            />
            <x-input.error :messages="$errors->get('color')" />
        </div>
    </div>

    @if (!$isRemote)
        <div class="input-row input-row--1">
            <div class="input-cell">
                <input type="hidden" name="is_shared" value="0">
                <x-input.checkbox-label
                    name="is_shared"
                    value="1"
                    :checked="old('is_shared', $isShared)"
                    :label="__('calendar.ics.share')"
                />
                <p class="text-sm text-gray-500 mt-1">{{ __('calendar.ics.share_help') }}</p>
            </div>
        </div>

        @if ($shareUrl)
        <div class="input-row input-row--1">
            <div class="input-cell">
                <x-input.text-label
                    :label="__('calendar.ics.public_url')"
                    id="ics_public_url"
                    name="ics_public_url"
                    type="url"
                    :value="$shareUrl"
                    disabled="true"
                    :description="__('calendar.ics.public_url_help')"
                />
            </div>
        </div>
        @endif
    @endif

    @if ($isRemote)
        <div class="input-row input-row--1">
            <div class="input-cell">
                <x-input.text-label
                    :label="__('calendar.ics.url')"
                    id="ics_url"
                    name="ics_url"
                    type="url"
                    :value="$data['icsUrl'] ?? ''"
                    disabled="true"
                    :description="__('calendar.ics.url_help')"
                />
            </div>
        </div>
    @endif

    <div class="input-row input-row--actions input-row--start sticky-bottom">
        <x-button variant="primary" type="submit">{{ __('common.save_changes') }}</x-button>
        <x-button type="anchor"
                  variant="tertiary"
                  href="{{ route('calendar.settings.calendars.show', $data['instance']['uri']) }}">{{ __('common.cancel') }}</x-button>
    </div>
</form>
