<?php
/**
 * Template Name: Search Page
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Biuro
 * @since 1.0
 * @version 1.0
 */
?>
<?php
    get_header();

    $search = get_query_var('search');
    $cityID = get_query_var('city');
    $periodID = get_query_var('period');
    $fieldID = get_query_var('field');
    $typeID = get_query_var('type');

// function print()

// $cities = get_terms( 'city', array(
//     'hide_empty' => false,
// ));

// debug($cityID)

 // if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
 //     echo '<ul>';
 //     foreach ( $terms as $term ) {
 //       echo '<li>' . $term->name . '</li>';

 //     }
 //     echo '</ul>';
 // }

?>

<form id="filter-form" action="<?php echo esc_url( $searchPageURL ); ?>" method="get" enctype="application/x-www-form-urlencoded">
<ul class="search_box">
    <li>
        <label for="label">Paieška:</label>
        <div id="search-input-block">
            <input type="text" name="search" id="search" value="<?php echo $search; ?>" class="input text nolabel" />
            <button aria-label="Ieškoti" type="submit" id="filter-action" class="submit action" ></button>
        </div>
        <a href="/darbo-pasiulymai/#" id="filter-expand" class="url close">Išplėstinė paieška</a></li>
    <li class="filter-additional">
        <label for="city">Miestas:</label>
        <select name="city" id="city" class="el_130">
            <option value="">Visi miestai</option>
<?php
            $cities = get_terms( 'city', array(
                'hide_empty' => false,
            ));

            if ( ! empty( $cities ) && ! is_wp_error( $cities ) ):
                foreach ( $cities as $city ):
                    $name = $city->name;
                    $termID = $city->term_id;
                    $selected = ($cityID == $termID) ? 'selected="selected"' : '';
?>
                    <option value="<?php echo $termID; ?>" <?php echo $selected; ?>><?php echo $name; ?></option>
<?php
                endforeach;
             endif;
?>
        </select>
        <label for="period" style="opacity: 0.45">Laikotarpis:</label>
        <select name="period" id="period" class="el_130" style="opacity: 0.45">
            <option value="">Nesvarbu</option>
            <option value="1">Šiandienos</option>
            <option value="7">Savaitės</option>
            <option value="30">Mėnesio</option>
        </select>
    </li>
    <li class="filter-additional">
        <label for="field">Darbo sritis:</label>
        <select name="field" id="field" class="el_130">
            <option value="">Visos sritys</option>
<?php
            $fields = get_terms( 'field', array(
                'hide_empty' => false,
            ));

            if ( ! empty( $fields ) && ! is_wp_error( $fields ) ):
                foreach ( $fields as $field ):
                    $name = $field->name;
                    $termID = $field->term_id;
                    $selected = ($fieldID == $termID) ? 'selected="selected"' : '';
?>
                    <option value="<?php echo $termID; ?>" <?php echo $selected; ?>><?php echo $name; ?></option>
<?php
                endforeach;
             endif;
?>
        </select>
        <label for="type">Darbo rūšis:</label>
        <select name="type" id="type" class="el_130">
            <option value="">Visos rūšys</option>
<?php
            $types = get_terms( 'type', array(
                'hide_empty' => false,
            ));

            if ( ! empty( $types ) && ! is_wp_error( $types ) ):
                foreach ( $types as $type ):
                    $name = $type->name;
                    $termID = $type->term_id;
                    $selected = ($typeID == $termID) ? 'selected="selected"' : '';
?>
                    <option value="<?php echo $termID; ?>" <?php echo $selected; ?>><?php echo $name; ?></option>
<?php
                endforeach;
             endif;
?>
        </select>
        <input type="button" value="Ieškoti" name="find" id="find" class="filter-button">
        <input type="button" value="Išvalyti" name="find" data-href="<?php echo esc_url( $searchPageURL ); ?>" id="reset-filter" class="filter-button">
    </li>
</ul>
</form>

<?php
    // Use for search
    // https://pods.io/docs/code/pods/find/

    $where = 't.post_title LIKE "%' . $search . '%"';
    // 'where' => 't.post_title LIKE "%' . $keyword . '%" OR my_field.meta_value LIKE "%' . $keyword . '%"'

    if ($cityID):
        $where = $where . ' AND city.term_id = ' . $cityID;
    endif;

    if ($fieldID):
        $where = $where . ' AND field.term_id = ' . $fieldID;
    endif;

    if ($typeID):
        $where = $where . ' AND type.term_id = ' . $typeID;
    endif;


    $params = array(
        'where' => $where,
        'limit'   => -1
    );

    $jobs = pods( 'job', $params );

    if ( 0 < $jobs->total() ):
        $i = 0;
?>

<table cellspacing="0" cellpadding="0" class="advert_table">
    <tr>
        <th>Pozicija </th>
        <th>Vietovė</th>
        <th>Data</th>
        <th>Rūšis</th>
    </tr>

<?php
        while ( $jobs->fetch() ) :
            $i++;
?>
    <tr <?php if ( $i % 2 == 0 ) { echo 'class="bg"'; } ?>>
        <td>
            <a href="<?php echo get_post_permalink( $jobs->display( 'ID' ) ); ?>" target="_blank" title="<?php echo $jobs->display( 'name' ); ?>">
                <strong><?php echo $jobs->display( 'name' ); ?></strong>
            </a>
        </td>
        <td>
            <span style="opacity: 0.25;">Šiauliai</span>
        </td>
        <td>
            <span style="opacity: 0.25;">Paskelbta: 2018 11 30 <br> Galioja iki: 2018 12 28</span>
        </td>
        <td>
            <span style="opacity: 0.25;">Nuolatinis darbas</span>
    </td>
    </tr>

<?php
        endwhile;
?>

</table>

<?php
    endif;
?>

<?php get_footer();
