<?php
    /*
 *  Save the meta box’s post metadata.
 */
    function rentpress_save_property_meta($post_id, $post)
    {
        // Get the post type object
        $post_type = get_post_type_object($post->post_type);

        // Verify the nonce before proceeding and check if the current user has permission to edit the post
        if (isset($_POST[ 'rentpress_custom_field_property_nonce' ]) &&
            wp_verify_nonce($_POST[ 'rentpress_custom_field_property_nonce' ], basename(__FILE__)) &&
            current_user_can($post_type->cap->edit_post, $post_id)) {
            // firstly mark this property as no longer a feed property, this will be undone immediately during next resync if it is in the feed still
            update_post_meta($post_id, 'rentpress_custom_field_property_is_feed', false);
            // get all the post meta
            $rpm                         = get_post_meta($post_id);
            $rentpress_add_on_field_args = get_option('rentpress_add_on_field_args');
            $rentpress_add_on_field_args = $rentpress_add_on_field_args ? json_decode($rentpress_add_on_field_args) : false;

            // delete the override post meta if it doesn't exist in the form submit
            foreach ($rpm as $meta_override_key => $meta_override_value) {
                if (strpos($meta_override_key, 'rentpress_custom_field_property') !== false &&
                    strpos($meta_override_key, 'override') !== false) {

                    // if the override doesn't exist in the request delete the unused meta
                    if (! isset($_POST[ $meta_override_key ])) {
                        delete_post_meta($post_id, $meta_override_key);
                    }
                }
            }

            // set up the meta arrays to save them based on type
            $post_emails      = rentpress_createPropertyEmailMetaList($rentpress_add_on_field_args);
            $post_text_fields = rentpress_createPropertyTextFieldMetaList($rentpress_add_on_field_args);
            $post_text_area   = rentpress_createPropertyTextAreaMetaList($rentpress_add_on_field_args);
            $post_urls        = rentpress_createPropertyURLMetaList($rentpress_add_on_field_args);

            // if there is an override set in the request, set the value
            foreach ($_POST as $override_key => $override_value) {
                if (strpos($override_key, '_override') !== false &&
                    strpos($override_key, 'rentpress_custom_field_property') !== false &&
                    $override_value == 'on') {

                    // remove override from the name of the key
                    $meta_key       = str_replace('_override', '', $override_key);
                    $new_meta_value = '';

                    /* Get the posted data and sanitize it for use as an HTML class. */
                    if (in_array($meta_key, $post_emails)) {
                        $new_meta_value = (isset($_POST[ $meta_key ]) ? sanitize_email($_POST[ $meta_key ]) : '');
                    } elseif (in_array($meta_key, $post_text_fields)) {
                        $new_meta_value = (isset($_POST[ $meta_key ]) ? sanitize_text_field($_POST[ $meta_key ]) : '');
                    } elseif (in_array($meta_key, $post_text_area)) {
                        $new_meta_value = (isset($_POST[ $meta_key ]) ? wp_filter_post_kses($_POST[ $meta_key ]) : '');
                    } elseif (in_array($meta_key, $post_urls)) {
                        $new_meta_value = (isset($_POST[ $meta_key ]) ? esc_url_raw($_POST[ $meta_key ]) : '');
                    }

                    update_post_meta($post_id, $meta_key, $new_meta_value);
                    update_post_meta($post_id, $override_key, $override_value);
                }
            }

            // check to see if the pricing has been overridden
            if (isset($_POST[ 'rentpress_custom_field_property_rent_type_selection' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_rent_type_selection', sanitize_text_field($_POST[ 'rentpress_custom_field_property_rent_type_selection' ]));
                if ($_POST[ 'rentpress_custom_field_property_rent_type_selection' ] == 'Global Setting') {
                    delete_post_meta($post_id, 'rentpress_custom_field_property_rent_type_selection_override');
                } else {
                    update_post_meta($post_id, 'rentpress_custom_field_property_rent_type_selection_override', 'on');
                }
            }

            // the the hours are overridden, then save them correctly
            if (isset($_POST[ 'rentpress_custom_field_property_office_hours_checkbox' ]) && $_POST[ 'rentpress_custom_field_property_office_hours_checkbox' ] === 'on') {
                update_post_meta($post_id, 'rentpress_custom_field_property_office_hours_checkbox', 'on');
            } else {
                delete_post_meta($post_id, 'rentpress_custom_field_property_office_hours_checkbox');
            }
            $office_hour_days = [
                'monday',
                'tuesday',
                'wednesday',
                'thursday',
                'friday',
                'saturday',
                'sunday',
             ];
            foreach ($office_hour_days as $time_of_setting) {
                $time_of_setting_open  = 'rentpress_custom_field_property_' . $time_of_setting . '_open';
                $time_of_setting_close = 'rentpress_custom_field_property_' . $time_of_setting . '_close';
                $time_replace_setting  = 'rentpress_custom_field_property_' . $time_of_setting . '_replace';
                $time_status_setting   = 'rentpress_custom_field_property_' . $time_of_setting . '_status';
                $time_append_setting   = 'rentpress_custom_field_property_' . $time_of_setting . '_append';
                $time_text_setting     = 'rentpress_custom_field_property_' . $time_of_setting . '_append_text';
                $time_override_setting = 'rentpress_custom_field_property_' . $time_of_setting . '_override';

                if (isset($_POST[ $time_override_setting ]) && $_POST[ $time_override_setting ] === 'on') {
                    // time fields
                    if (! empty($_POST[ $time_of_setting_open ])) {
                        $time = date_create_from_format('H:i', $_POST[ $time_of_setting_open ])->format('g:i a');
                        update_post_meta($post_id, $time_of_setting_open, $time);
                    } else {
                        update_post_meta($post_id, $time_of_setting_open, '');
                    }
                    if (! empty($_POST[ $time_of_setting_close ])) {
                        $time = date_create_from_format('H:i', $_POST[ $time_of_setting_close ])->format('g:i a');
                        update_post_meta($post_id, $time_of_setting_close, $time);
                    } else {
                        update_post_meta($post_id, $time_of_setting_close, '');
                    }

                    // select field
                    if (isset($_POST[ $time_status_setting ])) {
                        update_post_meta($post_id, $time_status_setting, sanitize_text_field($_POST[ $time_status_setting ]));
                    } else {
                        update_post_meta($post_id, $time_status_setting, '');
                    }
                }

                // checkboxies
                if (isset($_POST[ $time_replace_setting ])) {
                    update_post_meta($post_id, $time_replace_setting, 'on');
                } else {
                    delete_post_meta($post_id, $time_replace_setting);
                }
                if (isset($_POST[ $time_append_setting ])) {
                    update_post_meta($post_id, $time_append_setting, 'on');
                } else {
                    delete_post_meta($post_id, $time_append_setting);
                }
                if (isset($_POST[ $time_override_setting ])) {
                    update_post_meta($post_id, $time_override_setting, 'on');
                } else {
                    delete_post_meta($post_id, $time_override_setting);
                }

                // text fields
                if (isset($_POST[ $time_text_setting ])) {
                    update_post_meta($post_id, $time_text_setting, wp_kses_post($_POST[ $time_text_setting ]));
                } else {
                    delete_post_meta($post_id, $time_text_setting);
                }
            }

            if (isset($_POST[ 'rentpress_custom_field_property_accent_color' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_accent_color', sanitize_text_field($_POST[ 'rentpress_custom_field_property_accent_color' ]));
            }

            if (isset($_POST['rentpress_custom_field_property_accent_color_use_property_branding'])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_accent_color_use_property_branding', 'on');
            } else {
                delete_post_meta($post_id, 'rentpress_custom_field_property_accent_color_use_property_branding');
            }

            if (isset($_POST[ 'rentpress_custom_field_property_gallery_images' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_gallery_images', sanitize_text_field($_POST[ 'rentpress_custom_field_property_gallery_images' ]));
            }

            if (isset($_POST[ 'rentpress_custom_field_property_disable_pricing' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_disable_pricing', 'on');
            } else {
                delete_post_meta($post_id, 'rentpress_custom_field_property_disable_pricing');
            }

            if (isset($_POST[ 'rentpress_custom_field_property_link_options' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_link_options', sanitize_text_field($_POST[ 'rentpress_custom_field_property_link_options' ]));
            } else {
                update_post_meta($post_id, 'rentpress_custom_field_property_link_options', 'Default apply link');
            }

            if (isset($_POST[ 'rentpress_custom_field_property_contact_type' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_contact_type', sanitize_text_field($_POST[ 'rentpress_custom_field_property_contact_type' ]));
            } else {
                update_post_meta($post_id, 'rentpress_custom_field_property_contact_type', 'Default apply link');
            }

            if (isset($_POST[ 'rentpress_custom_field_property_deposit_settings' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_deposit_settings', sanitize_text_field($_POST[ 'rentpress_custom_field_property_deposit_settings' ]));
            } else {
                update_post_meta($post_id, 'rentpress_custom_field_property_deposit_settings', 1);
            }

            // neighborhoods
            if (isset($_POST[ 'rentpress_custom_field_property_neighborhood_post_id' ])) {
                update_post_meta($post_id, 'rentpress_custom_field_property_neighborhood_post_id', sanitize_text_field($_POST[ 'rentpress_custom_field_property_neighborhood_post_id' ]));

                $neighborhoods = null;
                if (empty($GLOBALS[ 'rentpress_neighborhood_data' ])) {
                    require_once RENTPRESS_PLUGIN_ADMIN_POSTS . 'neighborhood/neighborhood_post_type_data.php';
                    $neighborhoods                            = rentpress_getAllNeighborhoods();
                    $GLOBALS[ 'rentpress_neighborhood_data' ] = $neighborhoods;
                } else {
                    $neighborhoods = $GLOBALS[ 'rentpress_neighborhood_data' ];
                }

                $neighborhood_name = array_search(intval($_POST[ 'rentpress_custom_field_property_neighborhood_post_id' ]), $neighborhoods);
                update_post_meta($post_id, 'rentpress_custom_field_property_neighborhood_post_name', sanitize_text_field($neighborhood_name));
            } else {
                update_post_meta($post_id, 'rentpress_custom_field_property_neighborhood_post_id', '');
                update_post_meta($post_id, 'rentpress_custom_field_property_neighborhood_post_name', '');
            }

            require_once RENTPRESS_PLUGIN_DATA_SYNC . 'property/single_property_refresh.php';
            rentpress_syncFeedAndWPPropertyMeta($post_id);
        }
    }
    add_action('save_post', 'rentpress_save_property_meta', 10, 2);

    /*
 *  Create meta box to hold all of the viewable meta data fields
 */
    function rentpress_add_custom_property_data_box()
    {
        $box_title = 'RentPress - Property Editor';

        add_meta_box(
            'rentpress_custom_property_data_box',      // Unique ID
            $box_title,                                // Box title
            'rentpress_custom_property_data_box_html', // Content callback, must be of type callable
            'rentpress_property'                       // Post type
        );
    }
    add_action('add_meta_boxes', 'rentpress_add_custom_property_data_box');

    /*
 *  Create HTML that will show in the meta box
 */
    function rentpress_custom_property_data_box_html($post)
    {
        wp_nonce_field(basename(__FILE__), 'rentpress_custom_field_property_nonce');
        require_once RENTPRESS_PLUGIN_ADMIN_DIR . 'posts/meta/metafields.php';
        require_once RENTPRESS_PLUGIN_ADMIN_POSTS . 'floorplan/floorplan_post_type_data.php';

        $rentpress_add_on_field_args = get_option('rentpress_add_on_field_args');
        $rentpress_add_on_field_args = $rentpress_add_on_field_args ? json_decode($rentpress_add_on_field_args) : false;
        $rpm                         = get_post_meta($post->ID); // Get RentPress Property Meta
        $overrides                   = [  ];
        $floorplan_posts             = [  ];
        $rentpress_options           = get_option('rentpress_options');
        $selected_price_type         = (isset($rentpress_options[ 'rentpress_pricing_display_settings_section_price_display_selection' ])) ? $rentpress_options[ 'rentpress_pricing_display_settings_section_price_display_selection' ] : 'Best Price';
        $selected_deposit_type       = (isset($rentpress_options[ 'rentpress_deposit_settings_section_deposit_settings' ])) ? $rentpress_options[ 'rentpress_deposit_settings_section_deposit_settings' ] : '';
        $global_price_disable        = (isset($rentpress_options[ 'rentpress_disable_pricing_section_disable_pricing' ])) ? $rentpress_options[ 'rentpress_disable_pricing_section_disable_pricing' ] : '';
        if (isset($rpm[ 'rentpress_custom_field_property_code' ]) ? $rpm[ 'rentpress_custom_field_property_code' ] : '') {
            require_once RENTPRESS_PLUGIN_DATA_ACCESS . 'data_layer.php';
            $property_data   = rentpress_getAllPropertyDataWithCodeOrPostID($rpm[ 'rentpress_custom_field_property_code' ][ 0 ]);
            $floorplan_posts = rentpress_getAllFloorplanPostsForPropertyCode($rpm[ 'rentpress_custom_field_property_code' ][ 0 ]);
        }

        foreach ($rpm as $key => $value) {
            if (strpos($key, 'override') !== false &&
                strpos($key, 'rentpress_custom_field_property') !== false &&
                $value[ 0 ] == 'on') {
                $overrides[ $key ] = $value[ 0 ];
            }
        }

        // neighborhoods
        $neighborhoods = null;
        if (empty($GLOBALS[ 'rentpress_neighborhood_data' ])) {
            require_once RENTPRESS_PLUGIN_ADMIN_POSTS . 'neighborhood/neighborhood_post_type_data.php';
            $neighborhoods                            = rentpress_getAllNeighborhoods();
            $GLOBALS[ 'rentpress_neighborhood_data' ] = $neighborhoods;
        } else {
            $neighborhoods = $GLOBALS[ 'rentpress_neighborhood_data' ];
        }

        // Pricing Selector
        $pricing_options        = [ 'Global Setting', 'Base Rent', 'Market Rent', 'Term Rent', 'Effective Rent', 'Best Price', 'Minimum - Maximum' ];
        $pricing_selector_value = (isset($rpm[ 'rentpress_custom_field_property_rent_type_selection' ][ 0 ])) ? $rpm[ 'rentpress_custom_field_property_rent_type_selection' ][ 0 ] : 'Global Setting';

        $pricing_selector_str = "<select name='rentpress_custom_field_property_rent_type_selection' id='rentpress_custom_field_property_rent_type_selection' class='rentpress-settings-select'>";
        foreach ($pricing_options as $option) {
            if ($pricing_selector_value == $option) {
                $pricing_selector_str .= "<option value='$option' selected>$option</option>";
            } else {
                $pricing_selector_str .= "<option value='$option'>$option</option>";
            }
        }
        $pricing_selector_str .= "</select>";

    ?>

<div class="rentpress-cpt-editor-container">
    <div class="rentpress-tabs">
        <div class="rentpress-tab-button" onclick="openTab(event, 'prop-marketing')"><span class="fas fa-bullhorn"
                aria-hidden="true"></span>
            Marketing</div>
        <div class="rentpress-tab-button" onclick="openTab(event, 'prop-info')"><span class="fas fa-info-circle"
                aria-hidden="true"></span> Info
        </div>
        <div class="rentpress-tab-button" onclick="openTab(event, 'prop-integrations')">
            <span class="fas fa-plug" aria-hidden="true"></span> Integrations
        </div>
        <?php
        if (isset($rentpress_add_on_field_args->rentpress_property_meta) ? count((array) $rentpress_add_on_field_args->rentpress_property_meta) : '') {
                foreach ($rentpress_add_on_field_args->rentpress_property_meta as $property_meta_values) {
                    foreach ($property_meta_values as $property_meta) {
                        if (isset($property_meta->tab_callback) && isset($property_meta->new_tab) ? $property_meta->new_tab : '' && function_exists($property_meta->tab_callback)) {
                            call_user_func($property_meta->tab_callback);
                        }
                    }
                }
            }
        ?>
        <div class="rentpress-tab-button" onclick="openTab(event, 'prop-floorplans')"><span
                class="wp-menu-image dashicons-before dashicons-layout" aria-hidden="true"></span> Floor
            Plans</div>
        <div id="rentpress-expand-all">Expand All</div>
    </div>

    <div id="prop-marketing" class="rentpress-tab-section">
        <div class="rentpress-accordion"><span class="fas fa-tags" aria-hidden="true"></span> Special</div>
        <div class="rentpress-panel">
            <p class="rentpress-panel-heading">If your property is running a special discount, enter that information
                here.
                Keep under 120 characters. You can also optionally add a link and an expiration date to automatically
                remove the special from display.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Special Text</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_special_text', $rpm, 'text'); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Special Link</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_special_link', $rpm, 'url', ''); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Special Expiration</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_special_expiration', $rpm, 'date'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span aria-hidden="true" class="fas fa-edit"></span> Contact Form</div>
        <div class="rentpress-panel">
            <p>Set a destination for the "Request Info" CTA for this property and its floor plans. <br /><br />
                The default <strong>Global Setting</strong> will use the value set in the "Contact Page URL" option from
                <a
                    href="/wp-admin/admin.php?page=rentpress&settingstab=Appearance#rentpress-default-urls-settings-section">RentPress
                    settings</a>.<br /><br />
                The <strong>Contact Page URL</strong> option will allow you to set a URL for a contact page specific to
                this
                property.<br /><br />
                Choose <strong>Gravity Form Shortcode</strong> to enter the shortcode for a Gravity Form you have
                configured.
                The form will appear in a modal on page. Make sure to add <code>ajax="true"</code> to your shortcode for
                best
                results.<br /><br />
            </p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Choose Destination</label>
                <?php echo rentpress_metaFieldSelector('rentpress_custom_field_property_contact_type', $rpm, [ 'Global Setting' => 1, 'Contact Page URL' => 2, 'Gravity Form Shortcode' => 3 ]); ?>
            </div>

            <div class="rentpress-settings-group" id="rentpress_custom_field_property_specific_contact_link_group">
                <label class="rentpress-settings-title">Contact Page URL</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_specific_contact_link', $rpm, 'url', 'ex: https://somecontactform.page/'); ?>
            </div>

            <div class="rentpress-settings-group" id="rentpress_custom_field_property_specific_gravity_form_group">
                <label class="rentpress-settings-title">Gravity Form Shortcode <span>‎ ‎</span></label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_specific_gravity_form', $rpm, 'text', 'ex: [gravityforms id="1" ajax="true"]'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-share-alt" aria-hidden="true"></span> Social Profiles</div>
        <div class="rentpress-panel">
            <p>Add links to social profiles to invite shoppers to connect with your property or management company on
                social
                networks.</p>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Facebook Profile URL</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_facebook_url', $rpm, 'url'); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Instagram Profile URL</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_instagram_url', $rpm, 'url'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="far fa-images" aria-hidden="true"></span> Photo Gallery</div>
        <div class="rentpress-panel">
            <p>Add a WordPress shortcode from a photo or video gallery plugin. When active, the "Gallery" section will
                show on
                this property's page.</p>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Gallery Shortcode</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_gallery_shortcode', $rpm, 'text'); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Gallery Images</label>
                <?php echo rentpress_metaFieldImage('rentpress_custom_field_property_gallery_images', $rpm, 'text', 'Choose Images To Display In Property Gallery', 'false'); ?>
            </div>
        </div>

        <!-- TODO: add reviews section to property template -->
        <!-- <div class="rentpress-accordion"><span class="far fa-comments" aria-hidden="true"></span> Reviews Shortcode</div>
        <div class="rentpress-panel">
            <p>Add a shortcode from RentPress: Reviews Add-on to display resident reviews. Slider view is recommended. More information is available at the <a href="https://support.30lines.com" rel="noopener noreferrer" target="_blank">Support Site >></a></p>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Reviews Shortcode</label> -->
        <?php // echo rentpress_metaField('rentpress_custom_field_property_reviews_shortcode', $rpm, 'text'); ?>
        <!-- </div>
        </div> -->

        <div class="rentpress-accordion"><span class="fas fa-paw" aria-hidden="true"></span> Pet Policy Details</div>
        <div class="rentpress-panel">
            <p>Add details about the property's pet policy. This will show if the property has a Pets taxonomy selected.
                Supports HTML tags.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Pet Policy Details</label>
                <?php echo rentpress_metaTextArea('rentpress_custom_field_property_pet_policy', $rpm, '10'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="wp-menu-image dashicons-before dashicons-location-alt"
                aria-hidden="true"></span> Neighborhood Details</div>
        <div class="rentpress-panel">
            <p>Properties can exist within multiple neighborhoods. <br />
                <br />
                First <a href="/wp-admin/edit.php?post_type=rentpress_hood">create your neighborhoods</a> and asssign
                properties into them. You can then choose to assign this property's
                primary neighborhood. If no neighborhood is set as primary, the property's city will be used.
            </p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Primary Neighborhood </label>
                <?php echo rentpress_metaFieldNeighborhoodSelector('rentpress_custom_field_property_neighborhood_post_id', $rpm, $neighborhoods); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-certificate" aria-hidden="true"></span> Branding</div>
        <div class="rentpress-panel">
            <p>Add branding to represent the property. Choose a color then choose to override the Global Accent Color
                for this
                property.</p>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Property Accent Color</label>
                <?php echo rentpress_colorMetaField('rentpress_custom_field_property_accent_color', $rpm); ?>
                <div style="width:20px;"></div>
                <?php echo rentpress_checkboxMetaField('rentpress_custom_field_property_accent_color_use_property_branding', $rpm, 'Use Property Accent Where Available'); ?>
            </div>

            <!-- TODO: add logo and tagline to property template -->
            <!-- <div class="rentpress-settings-group">
        <label class="rentpress-settings-title">Property Logo</label> -->
            <?php // echo rentpress_metaField('rentpress_custom_field_property_logo', $rpm, 'url', 'https://photourl.com/'); ?>
            <!-- </div> -->

            <!-- <div class="rentpress-settings-group">
        <label class="rentpress-settings-title">Property Tagline</label> -->
            <?php // echo rentpress_metaField('rentpress_custom_field_property_tagline', $rpm, 'text', 'this is your tagline'); ?>
            <!-- </div> -->

        </div>

        <div class="rentpress-accordion"><span class="fas fa-search" aria-hidden="true"></span> Additional Search
            Keywords
        </div>
        <div class="rentpress-panel">
            <p>Add additional search keywords for this property. Keep each keyword separated by a comma. These keywords
                will be used in searches, but not shown to shoppers.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Search Keywords</label>
                <?php echo rentpress_metaTextArea('rentpress_custom_field_property_search_keywords', $rpm, '3', 'Downtown, Westside, Near Bus Line'); ?>
            </div>
        </div>
    </div>

    <div id="prop-info" class="rentpress-tab-section">
        <div class="rentpress-accordion"><span class="fas fa-phone" aria-hidden="true"></span> Phone Number</div>
        <div class="rentpress-panel">
            <p>The phone number of the property. If you have a tracking number, enter it here.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Phone Number</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_phone', $rpm, $overrides, 'phone', 'ex: (555) 555-1234'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-align-left" aria-hidden="true"></span> Property Description
        </div>
        <div class="rentpress-panel">
            <p>Enter a description about the property. Limit to around 100-120 words.Supports HTML tags.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Property Description</label>
                <?php echo rentpress_overrideMetaTextArea('rentpress_custom_field_property_description', $rpm, $overrides, '7'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-file-signature" aria-hidden="true"></span> Application Link
        </div>
        <div class="rentpress-panel">
            <p>Enter the URL for property's online application and select how you would like the link to affect the
                floor plans and units. If box is left empty, the "Default Application Link" from <a
                    href="/wp-admin/admin.php?page=rentpress&settingstab=Appearance#rentpress-default-urls-settings-section">RentPress
                    settings</a> page is used.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Apply Link</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_apply_link', $rpm, $overrides, 'url', ''); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Link Options</label>
                <?php echo rentpress_metaFieldSelector('rentpress_custom_field_property_link_options', $rpm, [ 'Default apply link', 'Override every apply link' ]); ?>
            </div>

        </div>

        <div class="rentpress-accordion"><span class="fas fa-user-circle" aria-hidden="true"></span> Residents Link
        </div>
        <div class="rentpress-panel">
            <p>Enter the URL for the property's online resident portal. If not set, link will not display.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Residents Link</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_residents_link', $rpm, $overrides, 'url'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-envelope" aria-hidden="true"></span> Email Address</div>
        <div class="rentpress-panel">
            <p>The email address for the property. If you have a tracking email address, enter it here.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Email Address</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_email', $rpm, $overrides, 'email'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-link" aria-hidden="true"></span> Website</div>
        <div class="rentpress-panel">
            <p>The address for a standalone property website.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Website Address</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_website', $rpm, $overrides, 'url'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-map-marker-alt" aria-hidden="true"></span> Location</div>
        <div class="rentpress-panel">
            <p>The address information for your property.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Street Address</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_address', $rpm, $overrides, 'text'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">City</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_city', $rpm, $overrides, 'text'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">State Abbreviation</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_state', $rpm, $overrides, 'text'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Zip Code</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_zip', $rpm, $overrides, 'text'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Longitude</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_longitude', $rpm, $overrides, 'text'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Latitude</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_latitude', $rpm, $overrides, 'text'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-clock" aria-hidden="true"></span> Office Hours</div>
        <div class="rentpress-panel">
            <p>Enter the daily hours the property is open. If you would like to override the hours from a feed, click
                Replace to adjust the time or swap with a text note. If you would like to display hours and text
                simultaneously,
                chose Append.</p>
            <div class="rentpress-settings-group">
                <?php echo rentpress_timeMetaFields('rentpress_custom_field_property_monday', $rpm, 'Monday'); ?>
            </div>
            <div class="rentpress-settings-group">
                <?php echo rentpress_timeMetaFields('rentpress_custom_field_property_tuesday', $rpm, 'Tuesday'); ?>
            </div>
            <div class="rentpress-settings-group">
                <?php echo rentpress_timeMetaFields('rentpress_custom_field_property_wednesday', $rpm, 'Wednesday'); ?>
            </div>
            <div class="rentpress-settings-group">
                <?php echo rentpress_timeMetaFields('rentpress_custom_field_property_thursday', $rpm, 'Thursday'); ?>
            </div>
            <div class="rentpress-settings-group">
                <?php echo rentpress_timeMetaFields('rentpress_custom_field_property_friday', $rpm, 'Friday'); ?>
            </div>
            <div class="rentpress-settings-group">
                <?php echo rentpress_timeMetaFields('rentpress_custom_field_property_saturday', $rpm, 'Saturday'); ?>
            </div>
            <div class="rentpress-settings-group">
                <?php echo rentpress_timeMetaFields('rentpress_custom_field_property_sunday', $rpm, 'Sunday'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-cogs" aria-hidden="true"></span> Configuration</div>
        <div class="rentpress-panel">
            <p class="rentpress-override-bottom">Configuration for the property data.</p>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Property Code</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_code', $rpm, $overrides, 'text'); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Import Source</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_import_source', $rpm, $overrides, 'text'); ?>
            </div>
        </div>

        <div class="rentpress-accordion"><span class="fas fa-dollar-sign" aria-hidden="true"></span> Pricing</div>
        <div class="rentpress-panel">
            <p>Values are calculated from the floor plans at this property. You can select which price you would like to
                display or optionally override each value.</p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Minimum Rent</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_rent_min', $rpm, $overrides, 'number', '0'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Maximum Rent</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_rent_max', $rpm, $overrides, 'number', '0'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Base Rent</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_rent_base', $rpm, $overrides, 'number', '0'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Market Rent</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_rent_market', $rpm, $overrides, 'number', '0'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Term Rent</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_rent_term', $rpm, $overrides, 'number', '0'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Effective Rent</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_rent_effective', $rpm, $overrides, 'number', '0'); ?>
            </div>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Best Price</label>
                <?php echo rentpress_overrideMetaField('rentpress_custom_field_property_rent_best', $rpm, $overrides, 'number', '0'); ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Price to Display</label>
                <?php echo $pricing_selector_str; ?>
                <?php if (! isset($rpm['rentpress_custom_field_property_rent_type_selection_override'][0])) : ?>
                <div style="margin-left:16px;margin-top:4px;">
                    Global Setting: <?php echo esc_html($selected_price_type); ?>
                </div>
                <?php endif; ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Hide Pricing</label>
                <?php echo rentpress_checkboxMetaField('rentpress_custom_field_property_disable_pricing', $rpm, 'Hide pricing at this property');?>
                <?php if ( $global_price_disable ) : ?>
                <div style="margin-left:8px">|</div>
                <div style="margin-left:8px;margin-top:1px;">
                    <?php echo esc_html('Global Setting: Hide Pricing'); ?>
                </div>
                <?php endif; ?>
            </div>

            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Floor Plan Deposits</label>
                <?php echo rentpress_metaFieldSelector('rentpress_custom_field_property_deposit_settings',$rpm,
                        [
                            'Global Setting'      => 1,
                            'Always Show'         => 2,
                            'Exclude $0 Deposits' => 3,
                            'Show None'           => 4,
                        ]
                    ); ?>

                <?php if ( $selected_deposit_type ) : ?>
                <div style="margin-left:16px;margin-top:4px;">
                    <?php echo esc_html( "Global Setting: $selected_deposit_type" ); ?>
                </div>
                <?php endif; ?>
            </div>

        </div>

        <?php
        if (isset($rentpress_add_on_field_args->rentpress_property_meta) ? count((array) $rentpress_add_on_field_args->rentpress_property_meta) : '') {
                foreach ($rentpress_add_on_field_args->rentpress_property_meta as $property_meta_values) {
                    foreach ($property_meta_values as $property_meta) {
                        if (isset($property_meta->callback) && isset($property_meta->new_tab) ? ! $property_meta->new_tab : '' && function_exists($property_meta->callback)) {
                            call_user_func($property_meta->callback, [ 'rpm' => $rpm, 'rentpress_allowed_HTML' => $rentpress_allowed_HTML ]);
                        }
                    }
                }
            }
        ?>
    </div>

    <?php
      if (isset($rentpress_add_on_field_args->rentpress_property_meta) ? count((array) $rentpress_add_on_field_args->rentpress_property_meta) : '') {
              foreach ($rentpress_add_on_field_args->rentpress_property_meta as $property_meta_values) {
                  foreach ($property_meta_values as $property_meta) {
                      if (isset($property_meta->callback) && isset($property_meta->new_tab) ? $property_meta->new_tab : '' && function_exists($property_meta->callback)) {
                          call_user_func($property_meta->callback, [ 'rpm' => $rpm, 'rentpress_allowed_HTML' => $rentpress_allowed_HTML ]);
                      }
                  }
              }
          }
      ?>

    <div id="prop-integrations" class="rentpress-tab-section">
        <div class="rentpress-accordion"><span class="fas fa-search-location" aria-hidden="true"></span> Engrain
            SightMap
        </div>
        <div class="rentpress-panel">
            <?php if (intval($property_data->property_engrain_enabled)): ?>
            <h3>SightMap for RentPress</h3>
            <p>
                Enter the direct link to your Engrain SightMap URL. <br /><br />
                Your URL was provided when your SightMap was created. Contact Engrain support if you do not have your
                SightMap URL available.
            </p>
            <div class="rentpress-settings-group">
                <label class="rentpress-settings-title">Engrain SightMap URL</label>
                <?php echo rentpress_metaField('rentpress_custom_field_property_engrain_map', $rpm, 'text', 'Example: https://sightmap.com/embed/e8ywkqrqwlx/'); ?>
            </div>
            <?php else: ?>
            <h3>SightMap for RentPress</h3>
            <p>RentPress can display a property’s Engrain SightMap integrated in your website’s floor plan and property
                pages.
                To activate your subscription, please <a href="https://via.30lines.com/2Q9ajYa9" target="_blank"
                    rel="noopener noreferrer">Contact 30 Lines</a>.</p>
            <p><a href="https://via.30lines.com/lfIruuAv" target="_blank" rel="noopener noreferrer">Learn more about
                    this Integration »</a></p>
            <?php endif; ?>
        </div>

        <?php // if (intval($property_data->property_scheduler_enabled)): ?>
        <div class="rentpress-accordion"><span class="fas fa-regular fa-calendar-alt" aria-hidden="true"></span>
            Appointment Scheduler
        </div>
        <div class="rentpress-panel">
            <?php if (intval($property_data->property_scheduler_enabled)): ?>
            <h3>RentPress Appointment Scheduler</h3>
            <p>RentPress Scheduler is active for this property. Configure and add the shortcode to a page to use it.</p>
            <p><a href="https://via.30lines.com/kB9ddSaI" target="_blank" rel="noopener noreferrer">Learn more about
                    this Integration »</a></p>
            <?php else: ?>
            <h3>RentPress Appointment Scheduler</h3>
            <p>RentPress Scheduler creates a seamless scheduling solution for your multifamily website.
                No third-party links or extra steps. Just clean, CRM-integrated booking to elevate the leasing
                experience.</p>
            <p>To activate your subscription, please <a href="https://via.30lines.com/2Q9ajYa9" target="_blank"
                    rel="noopener noreferrer">Contact 30 Lines</a>.</p>
            <p><a href="https://via.30lines.com/kB9ddSaI" target="_blank" rel="noopener noreferrer">Learn more about
                    this Integration »</a></p>
            <?php endif; ?>
        </div>
        <?php // endif; ?>
    </div>

    <div id="prop-floorplans" class="rentpress-tab-section">
        <div class="rentpress-accordion"><span class="fas fa-clipboard-list" aria-hidden="true"></span> Floor Plan
            Stats
        </div>
        <div class="rentpress-panel">
            <?php if ($property_data->property_floorplan_count > 0) : ?>
            <h3>Floor Plan Starting Price</h3>
            <?php endif; ?>
            <ul>
                <?php
            $matrix = json_decode($property_data->property_availability_matrix);
            foreach ($matrix as $data): if ((isset($data->name) ? $data->name : '') && (isset($data->price) ? $data->price : '')): ?>
                <li><strong><?php echo esc_attr($data->name); ?></strong> : $<?php echo esc_attr($data->price); ?></li>
                <?php endif;endforeach; ?>
            </ul>
        </div>
        <div class="rentpress-accordion"><span class="fas fa-list" aria-hidden="true"></span> Associated Floor Plans
        </div>
        <div class="rentpress-panel">
            <p class="rentpress-panel-heading">Floor plans that have this property listed as their parent will appear
                here for quick access.</p>
            <?php foreach ($floorplan_posts as $fp_key => $fp): ?>
            <p><a href='<?php echo esc_url($fp->guid); ?>'><?php echo esc_html($fp->post_title); ?></a> — <a
                    href="post.php?post=<?php echo esc_attr($fp->ID); ?>&action=edit">Edit »</a></p>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<?php
    }

    function rentpress_createPropertyEmailMetaList($rentpress_add_on_field_args)
    {
        $EmailMetaList = [
            'rentpress_custom_field_property_email',
         ];
        if (isset($rentpress_add_on_field_args->rentpress_property_meta) ? count((array) $rentpress_add_on_field_args->rentpress_property_meta) : '') {
            foreach ($rentpress_add_on_field_args->rentpress_property_meta as $property_meta_values) {
                foreach ($property_meta_values as $property_meta) {
                    if (isset($property_meta->meta_type) ? $property_meta->meta_type === 'email' : '') {
                        $EmailMetaList[  ] = $property_meta->meta_key;
                    }
                }
            }
        }
        return $EmailMetaList;
    }

    function rentpress_createPropertyTextFieldMetaList($rentpress_add_on_field_args)
    {
        $TextFieldMetaList = [
            'rentpress_custom_field_property_special_text',
            'rentpress_custom_field_property_special_expiration',
            'rentpress_custom_field_property_gallery_shortcode',
            'rentpress_custom_field_property_accent_color',
            'rentpress_custom_field_property_neighborhood_post_id',
            'rentpress_custom_field_property_neighborhood_post_ids',
            'rentpress_custom_field_property_search_keywords',
            'rentpress_custom_field_property_phone',
            'rentpress_custom_field_property_address',
            'rentpress_custom_field_property_city',
            'rentpress_custom_field_property_state',
            'rentpress_custom_field_property_zip',
            'rentpress_custom_field_property_longitude',
            'rentpress_custom_field_property_latitude',
            'rentpress_custom_field_property_code',
            'rentpress_custom_field_property_import_source',
            'rentpress_custom_field_property_rent_min',
            'rentpress_custom_field_property_rent_max',
            'rentpress_custom_field_property_rent_base',
            'rentpress_custom_field_property_rent_market',
            'rentpress_custom_field_property_rent_term',
            'rentpress_custom_field_property_rent_effective',
            'rentpress_custom_field_property_rent_best',
            'rentpress_custom_field_property_specific_gravity_form',
            "rentpress_custom_field_property_engrain_map",
            // 'rentpress_custom_field_property_monday_append_text',
            // 'rentpress_custom_field_property_tuesday_append_text',
            // 'rentpress_custom_field_property_wednesday_append_text',
            // 'rentpress_custom_field_property_thursday_append_text',
            // 'rentpress_custom_field_property_friday_append_text',
            // 'rentpress_custom_field_property_saturday_append_text',
            // 'rentpress_custom_field_property_sunday_append_text',
         ];
        if (isset($rentpress_add_on_field_args->rentpress_property_meta) ? count((array) $rentpress_add_on_field_args->rentpress_property_meta) : '') {
            foreach ($rentpress_add_on_field_args->rentpress_property_meta as $property_meta_values) {
                foreach ($property_meta_values as $property_meta) {
                    if (isset($property_meta->meta_type) ? $property_meta->meta_type === 'text' : '') {
                        $TextFieldMetaList[  ] = $property_meta->meta_key;
                    }
                }
            }
        }
        return $TextFieldMetaList;
    }

    function rentpress_createPropertyTextAreaMetaList($rentpress_add_on_field_args)
    {
        $TextAreaMetaList = [
            'rentpress_custom_field_property_pet_policy',
            'rentpress_custom_field_property_description',
         ];
        if (isset($rentpress_add_on_field_args->rentpress_property_meta) ? count((array) $rentpress_add_on_field_args->rentpress_property_meta) : '') {
            foreach ($rentpress_add_on_field_args->rentpress_property_meta as $property_meta_values) {
                foreach ($property_meta_values as $property_meta) {
                    if (isset($property_meta->meta_type) ? $property_meta->meta_type === 'textarea' : '') {
                        $TextAreaMetaList[  ] = $property_meta->meta_key;
                    }
                }
            }
        }
        return $TextAreaMetaList;
    }

    function rentpress_createPropertyURLMetaList($rentpress_add_on_field_args)
    {
        $URLMetaList = [
            'rentpress_custom_field_property_special_link',
            'rentpress_custom_field_property_facebook_url',
            'rentpress_custom_field_property_instagram_url',
            'rentpress_custom_field_property_apply_link',
            'rentpress_custom_field_property_residents_link',
            'rentpress_custom_field_property_website',
            'rentpress_custom_field_property_specific_contact_link',
         ];
        if (isset($rentpress_add_on_field_args->rentpress_property_meta) ? count((array) $rentpress_add_on_field_args->rentpress_property_meta) : '') {
            foreach ($rentpress_add_on_field_args->rentpress_property_meta as $property_meta_values) {
                foreach ($property_meta_values as $property_meta) {
                    if (isset($property_meta->meta_type) ? $property_meta->meta_type === 'url' : '') {
                        $URLMetaList[  ] = $property_meta->meta_key;
                    }
                }
            }
        }
    return $URLMetaList;
}