<?php

function rentpress_register_taxonomy_amenity()
{
    $labels = [
        'name' => _x('Amenities', 'taxonomy general name', 'rentpress-for-websites'),
        'singular_name' => _x('Amenity', 'taxonomy singular name', 'rentpress-for-websites'),
        'menu_name' => __('Amenities', 'rentpress-for-websites'),
        'search_items' => __('Search Amenities', 'rentpress-for-websites'),
        'all_items' => __('All Amenities', 'rentpress-for-websites'),
        'parent_item' => __('Parent Amenity', 'rentpress-for-websites'),
        'parent_item_colon' => __('Parent Amenity:', 'rentpress-for-websites'),
        'edit_item' => __('Edit Amenity', 'rentpress-for-websites'),
        'view_item' => __('View Amenity', 'rentpress-for-websites'),
        'update_item' => __('Update Amenity', 'rentpress-for-websites'),
        'add_new_item' => __('Add New Amenity', 'rentpress-for-websites'),
        'new_item_name' => __('New Amenity Name', 'rentpress-for-websites'),
    ];
    $args = [
        'hierarchical' => false, // make it a tag
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => false,
        'query_var' => true,
        'rewrite' => ['slug' => 'amenity'],
    ];
    register_taxonomy('amenity', ['rentpress_property'], $args);
}
add_action('init', 'rentpress_register_taxonomy_amenity');

// remove columns from amenity
add_filter('manage_edit-amenity_columns', 'rentpress_remove_amenity_columns');

function rentpress_remove_amenity_columns($columns)
{
    if (isset($columns['description'])) {
        unset($columns['description']);
        unset($columns['slug']);
        unset($columns['posts']);

        $columns["description"] = "Short Description";
        $columns["posts"] = "Count";
    }

    return $columns;
}

// Add info to the new columns
add_action('manage_amenity_custom_column', 'rentpress_manage_amenity_custom_columns', 10, 3);

function rentpress_manage_amenity_custom_columns($string, $columns, $term_id)
{
    switch ($columns) {
        case 'rentpress_custom_field_amenity_extended_content':
            echo wp_kses_post(get_term_meta($term_id, 'rentpress_custom_field_amenity_extended_content', true));
            break;
        default:

        break;
    }
}

// add the field to the edit screen.
add_action('amenity_edit_form_fields', 'rentpress_edit_amenity_fields', 10, 2);
add_action('amenity_add_form_fields', 'rentpress_add_amenity_fields');

function rentpress_add_amenity_fields($term)
{
    require_once RENTPRESS_PLUGIN_ADMIN_DIR . 'posts/meta/metafields.php';
    $rpm = isset($term->term_id) ? get_term_meta($term->term_id) : array();
    ?>
<div class="form-field">
    <tr class="form-field">
        <th scope="row"><label for="rentpress_custom_field_amenity_image">Featured Image</label></th>
        <td>
            <?php echo wp_kses(rentpress_metaFieldImage('rentpress_custom_field_amenity_image', $rpm, 'text', '', 'true'), $rentpress_allowed_HTML); ?>
            <p class="description">Add an image to represent this amenity. For best results, use an image with
                landscape orientation and at least 1000px on the shortest side.</p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row"><label for="rentpress_custom_field_amenity_extended_content">Extended Description</label></th>
        <td>
            <?php echo wp_kses(rentpress_metaTextArea('rentpress_custom_field_amenity_extended_content', $rpm, '3'), $rentpress_allowed_HTML); ?>
            <p class="description">Add in more content or information about the amenity.</p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row"><label for="rentpress_custom_field_amenity_shortcode">Shortcode</label></th>
        <td>
            <?php echo wp_kses(rentpress_metaField('rentpress_custom_field_amenity_shortcode', $rpm, 'text'), $rentpress_allowed_HTML); ?>
            <p class="description">Insert a shortcode from a WordPress plugin or theme. This works great with a
                photo gallery, map, or contact form.</p>
        </td>
    </tr>
</div>
<?php
}

function rentpress_edit_amenity_fields($term)
{
    require_once RENTPRESS_PLUGIN_ADMIN_DIR . 'posts/meta/metafields.php';
    $rpm = isset($term->term_id) ? get_term_meta($term->term_id) : array();
?>

<h1 style=font-weight:700;><?php echo esc_attr($term->name); ?></h1>

<div class="rentpress-cpt-editor-container">

    <div class="rentpress-tabs">
        <div class="rentpress-tab-button" onclick="openTab(event, 'amenity-marketing')"><span class="fas fa-bullhorn"
                aria-hidden="true"></span> Marketing</div>
        <div class="rentpress-tab-button" onclick="openTab(event, 'amenity-info')"><span class="fas fa-info-circle"
                aria-hidden="true"></span> Info</div>
        <div id="rentpress-expand-all">Expand All</div>
    </div>

    <!-- tab start -->
    <div id="amenity-marketing" class="rentpress-tab-section">

        <div class="rentpress-accordion"><span class="fas fa-images" aria-hidden="true"></span> Featured Image</div>
        <div class="rentpress-panel">
            <p class="rentpress-panel-heading">Add an image to represent this amenity. For best results, use an image
                with landscape orientation and at least 1000px on the shortest side.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Featured Image</label>
                <?php echo wp_kses(rentpress_metaFieldImage('rentpress_custom_field_amenity_image', $rpm, 'text', '', 'true'), $rentpress_allowed_HTML); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-quote-left" aria-hidden="true"></span> Short Description
        </div>
        <div class="rentpress-panel">
            <p class="rentpress-panel-heading">Add a short summary description about this amenity. This description will
                also be used on property pages. For best results, keep less than 120 characters.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Short Description</label>
                <?php echo wp_kses(rentpress_termTextAreaField("description", $term), $rentpress_allowed_HTML); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-align-left" aria-hidden="true"></span> Extended Content
        </div>
        <div class="rentpress-panel">
            <p class="rentpress-panel-heading">Add in more content or information about the amenity.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Extended Content</label>
                <?php echo wp_kses(rentpress_metaTextArea('rentpress_custom_field_amenity_extended_content', $rpm, '25'), $rentpress_allowed_HTML); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-puzzle-piece" aria-hidden="true"></span> Shortcode</div>
        <div class="rentpress-panel">
            <p class="rentpress-panel-heading">Insert a shortcode from a WordPress plugin or theme. This works great
                with a photo gallery, map, or contact form.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Shortcode</label>
                <?php echo wp_kses(rentpress_metaField('rentpress_custom_field_amenity_shortcode', $rpm, 'text'), $rentpress_allowed_HTML); ?>
            </div>
        </div>

    </div>
    <!-- tab end -->

    <!-- tab start -->
    <div id="amenity-info" class="rentpress-tab-section">

        <div class="rentpress-accordion"><span class="fas fa-cogs" aria-hidden="true"></span> Term Info</div>
        <div class="rentpress-panel">
            <p class="rentpress-panel-heading">These fields represent the name and slug for this term. For terms
                maintained by a property data feed, you shouldn't need to edit these.
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Name</label>
                <?php echo wp_kses(rentpress_termTextField("name", $term), $rentpress_allowed_HTML); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Slug</label>
                <?php echo wp_kses(rentpress_termTextField("slug", $term), $rentpress_allowed_HTML); ?>
            </div>
        </div>


    </div>
    <!-- tab end -->

</div>

<script type="text/javascript">
document.getElementsByClassName('form-table')[0].remove();
</script>

<?php
}

add_action('created_amenity', 'rentpress_created_amenity_fields');
add_action('edited_amenity', 'rentpress_created_amenity_fields');

function rentpress_created_amenity_fields($term_id)
{
  if (isset($_POST['rentpress_custom_field_amenity_image']) ? $_POST['rentpress_custom_field_amenity_image'] : "") {
    update_term_meta($term_id, 'rentpress_custom_field_amenity_image', sanitize_text_field($_POST['rentpress_custom_field_amenity_image']));
  }
  if (isset($_POST['rentpress_custom_field_amenity_extended_content']) ? $_POST['rentpress_custom_field_amenity_extended_content'] : "") {
    update_term_meta($term_id, 'rentpress_custom_field_amenity_extended_content', wp_kses_post($_POST['rentpress_custom_field_amenity_extended_content']));
  }
  if (isset($_POST['rentpress_custom_field_amenity_shortcode']) ? $_POST['rentpress_custom_field_amenity_shortcode'] : "") {
    update_term_meta($term_id, 'rentpress_custom_field_amenity_shortcode', sanitize_text_field($_POST['rentpress_custom_field_amenity_shortcode']));
  }
}