{#
	Code snippet listing on the Customizations tab in Toolset Settings.


#}

{% extends "@toolset/listing.twig" %}

{% import "@toolset/listing.twig" as listing %}
{% import "@toolset/forms.twig" as forms %}

{# We already have the section header for this purpose. #}
{% block title %}{% endblock %}

{% block columnHeaders %}
	{{ listing.columnHeader( __( 'Name', 'wpv-views' ), true, 'displayName', 'column-title column-primary') }}
	{{ listing.columnHeader( __( 'Status', 'wpv-views' ), false ) }}
	{{ listing.columnHeader( __( 'Run', 'wpv-views' ), false ) }}
	{{ listing.columnHeader( __( 'Context', 'wpv-views' ), false ) }}
	{{ listing.columnHeader( __( 'Description', 'wpv-views' ), false ) }}
{% endblock %}


{# Row actions which will be embedded in the main column. #}
{% block rowActions %}
	<div data-bind="visible: ! isEditing()">
		<span class="edit">
			<a data-bind="click: onEdit">{{ __( 'Edit', 'wpv-views' ) }}</a>
		</span>
		|
		<span class="edit" data-bind="visible: ! isActive()">
			<a data-bind="click: onActivate">{{ __( 'Activate', 'wpv-views' ) }}</a>
		</span>
		<span class="edit" data-bind="visible: isActive">
			<a data-bind="click: onDeactivate">{{ __( 'Deactivate', 'wpv-views' ) }}</a>
		</span>
		|
		<span class="edit">
			<a data-bind="click: onRunOnce">{{ __( 'Run now', 'wpv-views' ) }}</a>
		</span>
		|
		<span class="delete">
			<a data-bind="click: onDelete">{{ __( 'Delete', 'wpv-views' ) }}</a>
		</span>
	</div>
{% endblock %}


{% block afterDisplayName %}
	{# The slug doesn't deserve its own column but we still want to show it, it's relevant especially for ondemand snippets. #}
	<br />
	<code data-bind="text: slug"></code>
{% endblock %}


{# Other table cells besides the main one (with displayName and row actions) #}
{% block tdCells %}
	<td>
		<div data-bind="text: display.activityStatus"></div>
		<div data-bind="visible: display.hasLastError">
			<span class="toolset_code_snippets__error_indication">{{ __( 'Error', 'wpv-views' ) }}</span>
		</div>
        <div data-bind="visible: display.hasWarning">
            <span class="toolset_code_snippets__warning_indication">{{ __( 'Warning', 'wpv-views' ) }}</span>
        </div>
	</td>
	<td data-bind="text: display.runMode"></td>
	<td data-bind="text: display.runContexts"></td>
	<td data-bind="text: description"></td>
{% endblock %}

{% block afterNormalRow %}
	{# Another row with Edit Snippet GUI. #}
	<tr data-bind="visible: isEditing">
		<td></td> {# this is for the checkbox column #}
		<td colspan="5">
			<div class="toolset_code_snippets__snippet_edit_area">
				<table class="widefat toolset-field-table toolset_code_snippets--no-border">
					<tr>
						<td><label>{{ __( 'Name', 'wpv-views' ) }}</label></td>
						<td><input class="large-text" type="text" title="{{ __( 'Name', 'wpv-views' ) }}" data-bind="value: displayName"/></td>
					</tr>
					<tr>
						<td><label>{{ __( 'Slug', 'wpv-views' ) }}</label></td>
						<td><input class="large-text" type="text" title="{{ __( 'Slug', 'wpv-views' ) }}" data-bind="value: slug"/></td>
					</tr>
					<tr>
						<td><label>{{ __( 'Path to snippet file', 'wpv-views' ) }}</label></td>
						<td><code data-bind="text: filePath"></code></td>
					</tr>
					<tr>
						<td><label>{{ __( 'Run mode', 'wpv-views') }}</label></td>
						<td>
							<p>
								{% for	option in [
									{ 'value': 'always', 'label': __( 'Run always', 'wpv-views' ) },
									{ 'value': 'ondemand', 'label': __( 'On demand', 'wpv-views' ) },
								] %}
									{{ forms.radio( 'checked: runMode', option.value, option.label, '', '', 'toolset_code_snippets__option' ) }}
								{% endfor %}
							</p>
							<p data-bind="visible: ! isActive()">
								<strong>{{ __( 'Note: The snippet is inactive.', 'wpv-views') }}</strong>
								{{ __( 'The run mode will have no effect until you activate it or run the snippet manually.', 'wpv-views' ) }}<br />
								<a data-bind="click: onSetActive">{{ __( 'Activate', 'wpv-views' ) }}</a>
							</p>
							<p data-bind="visible: 'ondemand' == runMode()">
								{{ __( 'The script will be executed only if you add a specific request parameter:', 'wpv-views' ) }}
								<code data-bind="text: '{{ strings.site_url }}?{{ strings.ondemand_run_trigger }}=' + slug()"></code>
							</p>
						</td>
					</tr>
					<tr>
						<td><label>{{ __( 'Run context', 'wpv-views' ) }}</label></td>
						<td>
							{% for option in [
								{ 'value': 'frontend', 'label': __( 'Front-end', 'wpv-views' ) },
								{ 'value': 'admin', 'label': __( 'WordPress admin', 'wpv-views' ) },
								{ 'value': 'ajax', 'label': __( 'AJAX calls', 'wpv-views' ) },
							] %}
								{{ forms.checkbox( 'checked: runContexts', option.value, option.label, '', '', 'toolset_code_snippets__option' ) }}
							{% endfor %}

							<p data-bind="visible: runContexts().length === 0">
								<strong>{{ __( 'Note: The script doesn\'t have a run context configured.', 'wpv-views' ) }}</strong>
								{{ __( 'It will not be executed anywhere unless you choose at least one context.', 'wpv-views' ) }}
							</p>
						</td>
					</tr>
					<tr data-bind="visible: display.hasLastError">
						<td><label class="toolset_code_snippets__error_indication">{{ __( 'Last error', 'wpv-views' ) }}</label></td>
						<td>
							<p data-bind="html: lastError"></p>
							<p>
								{{ __( 'Note: Depending on the nature of the error, you may need to run the script in a particular scenario to make sure the issue is gone.', 'wpv-views' ) }}
							</p>
							<p>{{ forms.button('click: onTryAgain', __( 'Save the snippet and try again', 'wpv-views' )) }}</p>
						</td>
					</tr>
                    <tr data-bind="visible: ! hasSecurityCheck()">
                        <td><label class="toolset_code_snippets__warning_indication">{{ __( 'Warning', 'wpv-views' ) }}</label></td>
                        <td>
                            <p>
                                {# translators: A warning message on the Custom Code tab of Toolset Settings, displayed when editing a PHP code snippet. #}
                                {{ __( 'The snippet is missing a security check which means that it could be executed directly by anyone who knows the file\'s URL. We highly recommend adding the following line at the beginning of this snippet:', 'wpv-views' ) }}
                            </p>
                            <p><code>toolset_snippet_security_check() or die( 'Direct access is not allowed' );</code></p>
                        </td>
                    </tr>
				</table>

				<div class="notice notice-warning inline" data-bind="visible: ! isCodeEditable()">
					<p>{{ __( 'Unfortunately, this snippet cannot be edited directly. Check file permissions and wp-config.php constants DISALLOW_FILE_EDIT and DISALLOW_FILE_MODS. Alternatively, you can edit the file manually.', 'wpv-views' ) }}</p>
				</div>

				<div class="toolset_code_snippets__codemirror_wrap">
					<textarea
							data-bind="
								value: code,
								readonly: ! isCodeEditable(),
								attr: { id: 'toolset_code_snippets__editor-' + slug() }"
							class="toolset_code_snippets__code_area toolset_code_snippets--fullwidth"
							title="Content of the snippet file"
					></textarea>
				</div>

				<div class="toolset_code_snippets__buttonpane">
					<div class="toolset_code_snippets--left">
						{{ forms.button( 'click: onCancelEdit', __( 'Cancel', 'wpv-views' ) ) }}
					</div>
					<div class="toolset_code_snippets--right">
						{{ forms.button( 'click: onSave', __( 'Save', 'wpv-views' ) ) }}
						{{ forms.button( 'click: onTryAgain', __( 'Save and test', 'wpv-views' ) ) }}
						{{ forms.button( 'click: onSaveAndClose', __( 'Save and close', 'wpv-views' ), 'primary' ) }}
					</div>
				</div>
			</div>
		</td>
	</tr>
{% endblock %}


{% block topPagination %}
	{# No need for it here, the listing is too narrow already. #}
{% endblock %}