<?php
global $campoal_options;
global $post;

$filter_category = isset($campoal_options['conikal_f_category_field']) ? $campoal_options['conikal_f_category_field'] : 1;
$filter_topic = isset($campoal_options['conikal_f_topic_field']) ? $campoal_options['conikal_f_topic_field'] : 1;
$filter_address = isset($campoal_options['conikal_f_address_field']) ? $campoal_options['conikal_f_address_field'] : 1;
$filter_country = isset($campoal_options['conikal_f_country_field']) ? $campoal_options['conikal_f_country_field'] : 1;
$filter_state = isset($campoal_options['conikal_f_state_field']) ? $campoal_options['conikal_f_state_field'] : 1;
$filter_city = isset($campoal_options['conikal_f_city_field']) ? $campoal_options['conikal_f_city_field'] : 1;
$filter_neighborhood = isset($campoal_options['conikal_f_neighborhood_field']) ? $campoal_options['conikal_f_neighborhood_field'] : 1;

$search_save = get_post_meta($post->ID, 'search_save', true);
$search_save = !$search_save && isset($_GET['search_save']) ? $_GET['search_save'] : $search_save;
$search_hide = get_post_meta($post->ID, 'search_hide', true);
$search_hide = !$search_hide && isset($_GET['search_hide']) ? $_GET['search_hide'] : $search_hide;
$search_invert = get_post_meta($post->ID, 'search_invert', true);
$search_invert = !$search_invert && isset($_GET['search_invert']) ? $_GET['search_invert'] : $search_invert;

$search_key = isset($_GET['search_key']) ? $_GET['search_key'] : '';
$search_address = isset($_GET['search_address']) ? $_GET['search_address'] : '';
$search_neighborhood = isset($_GET['search_neighborhood']) ? $_GET['search_neighborhood'] : '';
$search_city = isset($_GET['search_city']) ? $_GET['search_city'] : '';
$search_state = isset($_GET['search_state']) ? $_GET['search_state'] : '';
$search_country = isset($_GET['search_country']) ? $_GET['search_country'] : '';
$search_category = isset($_GET['search_category']) ? $_GET['search_category'] : '';
$search_topics = isset($_GET['search_topics']) && $_GET['search_topics'] != '' ? explode(',', $_GET['search_topics']) : '';


if (!isset($_GET['search_key']) && !isset($_GET['search_address']) && !isset($_GET['search_neighborhood']) && !isset($_GET['search_city']) && !isset($_GET['search_state']) && !isset($_GET['search_country']) && !isset($_GET['search_category']) && !isset($_GET['search_topics'])) {
    $search_key = get_post_meta($post->ID, 'search_key', true);
    $search_address = get_post_meta($post->ID, 'search_address', true);
    $search_neighborhood = get_post_meta($post->ID, 'search_neighborhood', true);
    $search_city = get_post_meta($post->ID, 'search_city', true);
    $search_state = get_post_meta($post->ID, 'search_state', true);
    $search_country = get_post_meta($post->ID, 'search_country', true);
    $search_category = get_post_meta($post->ID, 'search_category', true);
    $search_topics = get_post_meta($post->ID, 'search_topics', true);
    $search_topics = $search_topics != '0' ? $search_topics : '';
}

// get categories
$cat_taxonomies = array( 
    'petition_category'
);
$cat_args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false
); 
$cat_terms = get_terms($cat_taxonomies, $cat_args);
?>

<div class="wrapper petition ui vertical white color very padded segment" id="search-form-section">
    <div class="ui container search-form-container">
        <h1 class="ui header"><?php echo esc_html(get_the_title($post->ID)) ?></h1>

        <?php if ($search_hide != 'on' || current_user_can('editor') || current_user_can('administrator')) { ?>

        <form class="ui form" id="search-petition">
            <div class="field">
                <div class="ui icon large input">
                    <input type="text" name="search_key" id="search_key" value="<?php echo esc_attr($search_key) ?>" placeholder="<?php esc_attr_e('Type some words related to the campaign', 'campoal'); ?>">
                    <i class="search icon"></i>
                </div>
            </div>
            <div class="equal width fields">
                <?php if ($filter_address == 1) { ?>
                <div class="six wide computer sixteen wide tablet sixteen wide mobile field">
                    <div class="ui icon large input">
                        <input type="text" name="search_address" id="addressSearch" value="<?php echo esc_attr($search_address) ?>" placeholder="<?php esc_attr_e('Address. eg, City, State, Country,...', 'campoal'); ?>">
                        <i class="map marker icon"></i>
                    </div>
                </div>
                <?php } ?>
                <?php if($filter_topic == 1) { ?>
                <div class="four wide computer eight wide tablet sixteen wide mobile field">
                    <div class="ui fluid multiple search selection large dropdown" id="topics-search">
                        <input name="search_topics" id="search_topics" type="hidden">
                        <i class="dropdown icon"></i>
                        <div class="default text"><?php esc_html_e('Topics', 'campoal') ?></div>
                        <div class="menu">
                        </div>

                        <?php if ($search_topics != '') { ?>
                            <?php foreach($search_topics as $topic) { ?>
                                <input type="hidden" name="current_topics[]" id="current_topics" value="<?php echo esc_attr($topic != '' ? $topic : '') ?>"> 
                            <?php }
                        } ?>
                    </div>
                </div>
                <?php }
                if ($filter_category == 1) { ?>
                <div class="four wide computer eight wide tablet sixteen wide mobile field">
                    <select class="ui dropdown fluid large input search-category" name="search_category" id="search_category">
                        <option value=""><?php esc_html_e('Category', 'campoal') ?></option>
                        <option value="0"><?php esc_html_e('None', 'campoal') ?></option>
                        <?php foreach($cat_terms as $cat_term) { ?>
                            <option value="<?php echo esc_attr($cat_term->term_id); ?>"<?php echo esc_attr($cat_term->term_id == (int)$search_category ? ' selected' : '') ?>><?php echo esc_html($cat_term->name); ?></option>
                        <?php } ?>
                    </select>
                </div>
                <?php } ?>
                <div class="two wide computer sixteen wide tablet sixteen wide mobile field">
                    <input type="submit" value="<?php esc_attr_e('Search', 'campoal') ?>" class="ui primary large fluid button" id="submit-search">
                </div>
            </div>
            <?php if (is_user_logged_in() && current_user_can('editor') || current_user_can('administrator')) { ?>
                <div class="inline fields">
                    <div class="field">
                        <div class="ui slider checkbox <?php echo esc_attr($search_save == 'on' ? 'checked' : '') ?>">
                            <input type="checkbox" name="search_save" <?php echo esc_attr($search_save == 'on' ? 'checked' : '') ?>>
                            <label><?php esc_html_e('Save search for this page', 'campoal') ?></label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui slider checkbox <?php echo esc_attr($search_hide == 'on' ? 'checked' : '') ?>">
                            <input type="checkbox" name="search_hide" <?php echo esc_attr($search_hide == 'on' ? 'checked' : '') ?>>
                            <label><?php esc_html_e('Hide search for font-end users', 'campoal') ?></label>
                        </div>
                    </div>
                    <div class="field">
                        <div class="ui slider checkbox <?php echo esc_attr($search_invert == 'on' ? 'checked' : '') ?>">
                            <input type="checkbox" name="search_invert" <?php echo esc_attr($search_invert == 'on' ? 'checked' : '') ?>>
                            <label><?php esc_html_e('Invert this search', 'campoal') ?></label>
                        </div>
                    </div>
                </div>
            <?php } ?>
                <input type="hidden" name="search_neighborhood" id="neighborhoodSearch" value="<?php echo esc_attr($search_neighborhood) ?>" tabindex="0" class="hidden">
                <input type="hidden" name="search_city" id="citySearch" value="<?php echo esc_attr($search_city) ?>" tabindex="0" class="hidden">
                <input type="hidden" name="search_state" id="stateSearch" value="<?php echo esc_attr($search_state) ?>" tabindex="0" class="hidden">
                <input type="hidden" name="search_country" id="countrySearch" value="<?php echo esc_attr($search_country) ?>" tabindex="0" class="hidden">
                <input type="hidden" name="post_id" id="posd_id" value="<?php echo esc_attr($post->ID) ?>" tabindex="0" class="hidden">
        </form>

        <?php } ?>

    </div>
</div>
