<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>

<?php
    use EHxMe\Helpers\Helper;

    $ehxme_content_restriction = get_post_meta($post->ID, 'ehxme_content_restriction', true);
    $access_settings = isset($ehxme_content_restriction['_ehx_custom_access_settings']) ? $ehxme_content_restriction['_ehx_custom_access_settings'] : null;
    $accessible = isset($ehxme_content_restriction['_ehx_accessible']) ? $ehxme_content_restriction['_ehx_accessible'] : null;
    $access_roles = isset($ehxme_content_restriction['_ehx_access_roles']) ? $ehxme_content_restriction['_ehx_access_roles'] : [];
    $hide_from_queries = isset($ehxme_content_restriction['_ehx_access_hide_from_queries']) ? $ehxme_content_restriction['_ehx_access_hide_from_queries'] : null;
?>

<table class="form-table ehx-members-metabox">
    <input type="hidden" name="<?php echo esc_html(Helper::NONCE_NAME) ?>" value="<?php echo esc_html(wp_create_nonce(Helper::NONCE_NAME)) ?>">
    <tbody>

        <tr class="ehxme-forms-line" data-prefix="ehxme_content_restriction" data-field_type="checkbox">
            <th>
                <label for="ehxme_content_restriction__ehx_custom_access_settings">
                    <?php esc_html_e('Restrict access to this post?', 'ehx-members') ?>
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="<?php esc_html_e('Activate individual content restriction settings for this post. Then ignore all terms restriction content settings if they exists.', 'ehx-members') ?>"></span>
                </label>
            </th>
            <td>
                <input type="checkbox" id="ehxme_content_restriction__ehx_custom_access_settings" class="ehxme-forms-field" name="ehxme_content_restriction[_ehx_custom_access_settings]" value="1" <?php checked(1, $access_settings) ?> />
            </td>
        </tr>

        <tr class="ehxme-forms-line"  style="display: <?php echo empty($access_settings) ? 'none':'table-row' ?>;" data-field="ehxme_content_restriction__ehx_accessible">
            <th>
                <label for="ehxme_content_restriction__ehx_accessible">Who can access this post? </label>
            </th>
            <td>
                <select id="ehxme_content_restriction__ehx_accessible" name="ehxme_content_restriction[_ehx_accessible]" class="ehxme-forms-field ehx-long-field" data-field_id="_ehx_accessible">
                    <?php foreach([esc_html__('Everyone', 'ehx-members'), esc_html__('Logged out users', 'ehx-members'),esc_html__('Logged in users', 'ehx-members')] as $key => $item): ?>
                        <option value="<?php echo esc_attr($key); ?>" <?php selected($key, $accessible) ?>><?php echo esc_html($item); ?></option>
                    <?php endforeach ?>
                </select>
            </td>
        </tr>

        <tr class="ehxme-forms-line ehxme-forms-logged-in" style="display: <?php echo $accessible != 2 ? 'none':'table-row' ?>;">
            <th>
                <label for="ehxme_content_restriction__ehx_access_roles">
                    <?php esc_html_e('Select which roles can access this post', 'ehx-members') ?>
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="<?php esc_html_e('Leave empty if you want to display a post for all logged in users', 'ehx-members') ?>"></span>
                </label>
            </th>
            <td>
                <span class="ehxme-form-fields-section" style="width:100% !important;">
                    <?php foreach(wp_roles()->roles as $key => $role): ?>
                        <label for="ehxme_content_restriction__ehx_access_roles_<?php echo esc_attr($key); ?>">
                            <input type="checkbox" id="ehxme_content_restriction__ehx_access_roles_<?php echo esc_attr($key); ?>" name="ehxme_content_restriction[_ehx_access_roles][]" value="<?php echo esc_attr($key); ?>" class="ehxme-forms-field" <?php checked(true, in_array($key, $access_roles)) ?> />
                            <span><?php echo esc_html($role['name']); ?></span>
                        </label>
                    <?php endforeach ?>
                </span>
            </td>
        </tr>

        <tr class="ehxme-forms-line ehxme-forms-line-conditioned" style="display: <?php echo empty($access_settings) ? 'none':'table-row' ?>;">
            <th>
                <label for="ehxme_content_restriction__ehx_access_hide_from_queries">
                <?php esc_html_e('Would you like to display a 404 page for users who do not have access to this page?', 'ehx-members') ?>
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="<?php esc_html_e('Recommended to be enabled. Restricted post will be hidden by exclusion from WP Query. The safest and most effective method that hides post and its comments from all requests, RSS feeds, etc. on your site', 'ehx-members') ?>"></span>
                </label>
            </th>
            <td>
                <input type="hidden" name="ehxme_content_restriction[_ehx_access_hide_from_queries]" value="1">
                <input type="checkbox" id="ehxme_content_restriction__ehx_access_hide_from_queries" class="ehxme-forms-field" disabled checked >
            </td>
        </tr>

        <!-- <tr class="ehxme-forms-line ehxme-forms-line-conditioned" style="display: none;" data-field="_ehx_access_hide_from_queries">
            <th>
                <label for="ehxme_content_restriction__ehx_noaccess_action">
                    What happens when users without access try to view the post?
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="Action when users without access tries to view the post"></span>
                </label>
            </th>
            <td>
                <select id="ehxme_content_restriction__ehx_noaccess_action" name="ehxme_content_restriction[_ehx_noaccess_action]" class="ehxme-forms-field  ehx-long-field" data-field_id="_ehx_noaccess_action">
                    <option value="0" selected="selected">Show access restricted message</option>
                    <option value="1">Redirect user</option>
                </select>
            </td>
        </tr>
        <tr class="ehxme-forms-line ehxme-forms-line-conditioned" style="display: none;" data-field="_ehx_access_hide_from_queries">
            <th>
                <label for="ehxme_content_restriction__ehx_restrict_by_custom_message">
                    Restricted access message type
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="Would you like to use the global default message or apply a custom message to this post?"></span>
                </label>
            </th>
            <td>
                <select id="ehxme_content_restriction__ehx_restrict_by_custom_message" name="ehxme_content_restriction[_ehx_restrict_by_custom_message]" class="ehxme-forms-field  ehx-long-field" data-field_id="_ehx_restrict_by_custom_message">
                    <option value="0" selected="selected">Global default message</option>
                    <option value="1">Custom message</option>
                </select>
            </td>
        </tr>

        <tr class="ehxme-forms-line" style="display: none;">
            <th>
                <label for="ehxme_content_restriction__ehx_restrict_custom_message">
                    Custom restricted access message
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="You may replace global restricted access message here"></span>

                </label>
            </th>
            <td>
                <div id="wp-ehxme_content_restriction__ehx_restrict_custom_message-wrap" class="wp-core-ui wp-editor-wrap html-active">
                    <div id="wp-ehxme_content_restriction__ehx_restrict_custom_message-editor-tools" class="wp-editor-tools hide-if-no-js">
                        <div class="wp-editor-tabs">
                            <button type="button" id="ehxme_content_restriction__ehx_restrict_custom_message-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="ehxme_content_restriction__ehx_restrict_custom_message">Visual</button>
                            <button type="button" id="ehxme_content_restriction__ehx_restrict_custom_message-html" aria-pressed="true" class="wp-switch-editor switch-html" data-wp-editor-id="ehxme_content_restriction__ehx_restrict_custom_message">Text</button>
                        </div>
                    </div>
                    <div id="wp-ehxme_content_restriction__ehx_restrict_custom_message-editor-container" class="wp-editor-container">
                        <div id="qt_ehxme_content_restriction__ehx_restrict_custom_message_toolbar" class="quicktags-toolbar hide-if-no-js">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_strong" class="ed_button button button-small" aria-label="Bold" value="b">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_em" class="ed_button button button-small" aria-label="Italic" value="i">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_link" class="ed_button button button-small" aria-label="Insert link" value="link">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_block" class="ed_button button button-small" aria-label="Blockquote" value="b-quote">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_del" class="ed_button button button-small" aria-label="Deleted text (strikethrough)" value="del">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_ins" class="ed_button button button-small" aria-label="Inserted text" value="ins">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_img" class="ed_button button button-small" aria-label="Insert image" value="img">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_ul" class="ed_button button button-small" aria-label="Bulleted list" value="ul">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_ol" class="ed_button button button-small" aria-label="Numbered list" value="ol">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_li" class="ed_button button button-small" aria-label="List item" value="li">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_code" class="ed_button button button-small" aria-label="Code" value="code">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_more" class="ed_button button button-small" aria-label="Insert Read More tag" value="more">
                            <input type="button" id="qt_ehxme_content_restriction__ehx_restrict_custom_message_close" class="ed_button button button-small" title="Close all open tags" value="close tags">
                        </div>
                        <textarea class="ehx-long-field wp-editor-area" style="height: 425px" autocomplete="off" cols="40" name="ehxme_content_restriction[_ehx_restrict_custom_message]" id="ehxme_content_restriction__ehx_restrict_custom_message"></textarea>
                    </div>
                </div>
            </td>
        </tr>

        <tr class="ehxme-forms-line" style="display: none;" data-field="_ehx_access_redirect">
            <th>
                <label for="ehxme_content_restriction__ehx_access_redirect">
                    Where should users be redirected to?
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="Select redirect to page when user hasn't access to post"></span>
                </label>
            </th>
            <td>
                <select id="ehxme_content_restriction__ehx_access_redirect" name="ehxme_content_restriction[_ehx_access_redirect]" class="ehxme-forms-field  ehx-long-field" data-field_id="_ehx_access_redirect">
                    <option value="0" selected="selected">Login page</option>
                    <option value="1">Custom URL</option>
                </select>
            </td>
        </tr>

        <tr class="ehxme-forms-line" style="display: none;">
            <th>
                <label for="ehxme_content_restriction__ehx_access_redirect_url">
                    Redirect URL
                    <span class="ehx_tooltip dashicons dashicons-editor-help" title="Set full URL where do you want to redirect the user"></span>
                </label>
            </th>
            <td>
                <input type="text" id="ehxme_content_restriction__ehx_access_redirect_url" class="ehxme-forms-field ehx-long-field" name="ehxme_content_restriction[_ehx_access_redirect_url]" data-field_id="_ehx_access_redirect_url" value="">
            </td>
        </tr> -->

    </tbody>
</table>