{# src/templates/_settings/general.twig #}

{% extends '_layouts/cp' %}
{% import '_includes/forms' as forms %}
{% import 'blocksmith/_includes/macros.twig' as macros %}

{% set currentEdition = craft.app.plugins.getPlugin('blocksmith').edition %}

{% set readOnly = not craft.app.config.general.allowAdminChanges %}
{% set fullPageForm = not readOnly %}

{% if readOnly %}
	{% set contentNotice = readOnlyNotice() %}
{% endif %}

{% set crumbs = [
    {
        label: "Blocksmith",
        url: url('blocksmith/settings/general')
    }
] %}

{% block sidebar %}
	{% include 'blocksmith/_settings/sidebar.twig' %}
{% endblock %}

{% block content %}
	<input type="hidden" name="pluginHandle" value="blocksmith">
	{{ actionInput('blocksmith/blocksmith/save-settings') }}

	<div class="blocksmith-settings-wrapper">
		<div class="blocksmith-settings-content">

			<h1>{{ "General Settings"|t('blocksmith') }}</h1>

			{{ forms.lightswitchField({
                label: 'Enable Cards support'|t('blocksmith'),
                name: 'enableCardsSupport',
                id: 'enableCardsSupport',
                instructions: 'Enable support for Matrix fields using the “As cards” view mode. This allows adding new cards via the "New entry" button and inserting cards above existing ones through the enhanced context menu.'|t('blocksmith'),
                on: settings.enableCardsSupport,
                disabled: readOnly or ('enableCardsSupport' in overrides),
                warning: 'enableCardsSupport' in overrides ? macros.configWarning('enableCardsSupport'),
            }) }}

			{% if craft.app.plugins.getPlugin('blocksmith').edition != 'pro' %}
				<div class="blocksmith-hint" style="margin-top: 2rem;">
					<strong>{{ "Note: "|t('blocksmith') }}</strong>
					{{ "The following options apply to the <strong>Preview Modal</strong> UI mode, which is only available in the <strong>Pro Edition</strong>."|t('blocksmith')|raw }}
				</div>
			{% endif %}

			{% set craftSupportsEntryGroups = craft.app.version >= '5.8.0' %}

			{{ forms.lightswitchField({
                label: 'Organize Button Group entries using Craft’s native Entry Type Groups'|t('blocksmith'),
                name: 'useEntryTypeGroups',
                id: 'useEntryTypeGroups',
                instructions: craftSupportsEntryGroups
                ? 'Enable this option to group buttons in Blocksmith’s Button Groups based on the Entry Type Groups (if defined) in Craft’s Matrix Field settings.'|t('blocksmith')
                : '<span class="error">' ~ 'This feature requires Craft CMS 5.8 or newer.'|t('blocksmith') ~ '</span>',
                on: settings.useEntryTypeGroups,
                disabled: not craftSupportsEntryGroups or readOnly or ('useEntryTypeGroups' in overrides),
                })
            }}


			{{ forms.lightswitchField({
                label: 'Use Handle-based Previews (Recommended for deployment workflows)'|t('blocksmith'),
                labelClass: (currentEdition != 'pro') ? 'text-light' : null,
                name: 'useHandleBasedPreviews',
                id: 'useHandleBasedPreviews',
                instructions: macros.instructions(
                    'Automatically assign preview images based on block handles instead of selecting them manually via the Asset Browser. Supports deployment-friendly, version-controlled previews if images are stored in @webroot/blocksmith/previews/.'|t('blocksmith'),
                    'blocksmith_previewInstructionsHtml'|t('blocksmith')|raw
                    ),
                on: settings.useHandleBasedPreviews,
                disabled: readOnly or ('useHandleBasedPreviews' in overrides),
            })}}

			{% if settings.useHandleBasedPreviews %}

				{{ forms.radioGroupField({
                label: 'Preview Image Storage Mode'|t('blocksmith'),
                instructions: 'Choose where the preview images will be loaded from.'|t('blocksmith'),
                name: 'previewStorageMode',
                options: {
                    'volume': 'Asset Volume'|t('blocksmith'),
                    'web': '@webroot/blocksmith/previews/'|t('blocksmith') ~ ' (Recommended)'|t('blocksmith'),
                    },
                value: settings.previewStorageMode ?? 'volume',
                disabled: readOnly or ('previewStorageMode' in overrides),
                warning: 'previewStorageMode' in overrides ? macros.configWarning('previewStorageMode'),
                }) }}

			{% endif %}


			{% if settings.useHandleBasedPreviews and settings.previewStorageMode == 'volume' %}

				{{ forms.selectField({
                    label: 'Preview Image Volume'|t('blocksmith'),
                    name: 'previewImageVolume',
                    id: 'previewImageVolume',
                    instructions: 'Choose the volume where preview images will be stored.'|t('blocksmith'),
                    options: volumeOptions,
                    value: settings.previewImageVolume,
                    disabled: readOnly or ('previewImageVolume' in overrides),
                    warning: 'previewImageVolume' in overrides ? macros.configWarning('previewImageVolume'),
                }) }}

				{{ forms.textField({
                    label: 'Subfolder (optional)'|t('blocksmith'),
                    name: 'previewImageSubfolder',
                    id: 'previewImageSubfolder',
                    instructions: 'Specify an optional subfolder within the selected volume for storing images.'|t('blocksmith'),
                    value: settings.previewImageSubfolder,
                    disabled: readOnly or ('previewImageSubfolder' in overrides),
                    warning: 'previewImageSubfolder' in overrides ? macros.configWarning('previewImageSubfolder'),
                }) }}
			{% endif %}


			{{ forms.lightswitchField({
                label: 'Preview Modal: Show 3 Blocks per Row (Default 2) in Wide Viewport'|t('blocksmith'),
                name: 'wideViewFourBlocks',
                id: 'wideViewFourBlocks',
                instructions: 'Enable this option to display 3 blocks per row for browser widths of 1178px or more.'|t('blocksmith'),
                on: settings.wideViewFourBlocks,
                disabled: readOnly or ('wideViewFourBlocks' in overrides),
            }) }}

		</div>
	</div>
{% endblock %}


