/**
     * @param array $instance
     *
     * @return string|void
     */
    function form($instance)
    {
        if ($instance) {
            $title = esc_attr($instance['title']);
        } else {
            $title = __('Top Rated Users', 'cbratingsystem');
        }
        if (array_key_exists('timelimit', $instance)) {
            $timelimit = $instance['timelimit'];
        } else {
            $timelimit = '0';
        }
        if (array_key_exists('user_id', $instance)) {
            $user_id = $instance['user_id'];
        } else {
            $user_id = '';
        }
        if (array_key_exists('post_id', $instance)) {
            $post_id = $instance['post_id'];
        } else {
            $post_id = '';
        }
        if (array_key_exists('resultlimit', $instance)) {
            $limit = $instance['resultlimit'];
        } else {
            $limit = '10';
        }
        if (array_key_exists('posttype', $instance)) {
            $posttype = $instance['posttype'];
        } else {
            $posttype = '0';
        }
        if (array_key_exists('post_filter', $instance)) {
            $post_filter = $instance['post_filter'];
        } else {
            $post_filter = 'post_type';
        }
        if (array_key_exists('form_id', $instance)) {
            $form_id = $instance['form_id'];
        } else {
            $form_id = 'form_id';
        }
        if (array_key_exists('order', $instance)) {
            $order = $instance['order'];
        } else {
            $order = 'DESC';
        }
        if (array_key_exists('firstorder', $instance)) {
            $firstorder = $instance['firstorder'];
        } else {
            $firstorder = 'post_count';
        }
        ?>
        <p>
            <label for = "<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e("Title", 'cbratingsystem');
        ?>
:</label>
            <input class = "widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" type="text" name="<?php 
        echo $this->get_field_name('title');
        ?>
" value="<?php 
        echo $title;
        ?>
" />
        </p>
        <!--user limit  -->
        <p>
            <label for = "<?php 
        echo $this->get_field_id('user_id');
        ?>
"><?php 
        _e("User Ids ('Blank or comma separate ids ) ", 'cbratingsystem');
        ?>
:</label>
            <input class = "widefat" id="<?php 
        echo $this->get_field_id('user_id');
        ?>
" type="text" name="<?php 
        echo $this->get_field_name('user_id');
        ?>
" value="<?php 
        echo $user_id;
        ?>
" />
        </p>
        <!--time limit  -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('timelimit');
        ?>
"><?php 
        _e("Display Last", 'cbratingsystem');
        ?>
:</label>
            <select id="<?php 
        echo $this->get_field_id('timelimit');
        ?>
" name="<?php 
        echo $this->get_field_name('timelimit');
        ?>
" class="widefat" style="width:50%">
                <?php 
        //$no_of_days = array( 1 => '24 hours', 7 => 'Week', 15 => '15 Days', 30 => 'Month', 0 => 'All' );
        $no_of_days = array(1 => __('Last 24 hours', 'cbratingsystem'), 7 => __('Last 7 Days', 'cbratingsystem'), 15 => __('Last 15 Days', 'cbratingsystem'), 30 => __('Last 30 days', 'cbratingsystem'), 0 => __('All Time', 'cbratingsystem'));
        foreach ($no_of_days as $day => $day_label) {
            echo "<option value='{$day}'";
            if ($timelimit == $day) {
                echo "selected='selected'";
            }
            echo ">{$day_label}</option>";
        }
        ?>
            </select>
        </p>
        <p><?php 
        _e('Note: Based on Post creation date', 'cbratingsystem');
        ?>
</p>
        <!--result limit  -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('resultlimit');
        ?>
">No. To Display:</label>
            <input class="widefat" id="<?php 
        echo $this->get_field_id('resultlimit');
        ?>
" type="text" name="<?php 
        echo $this->get_field_name('resultlimit');
        ?>
" value="<?php 
        echo $limit;
        ?>
" />
        </p>
        <!--post id -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('post_id');
        ?>
"><?php 
        _e("Post Ids ('Blank or comma separate ids ) ", 'cbratingsystem');
        ?>
:</label>
            <input class="widefat" id="<?php 
        echo $this->get_field_id('post_id');
        ?>
" type="text" name="<?php 
        echo $this->get_field_name('post_id');
        ?>
" value="<?php 
        echo $post_id;
        ?>
" />
        </p>
        <!--post type id -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('posttype');
        ?>
"><?php 
        _e("Post Type", 'cbratingsystem');
        ?>
:</label>
            <select id="<?php 
        echo $this->get_field_id('posttype');
        ?>
" name="<?php 
        echo $this->get_field_name('posttype');
        ?>
" class="widefat" style="width: 55%">
                <?php 
        echo "<option value='0'";
        if ($posttype == 0) {
            echo "selected='selected'";
        }
        echo ">" . ucfirst('All') . "</option>";
        //////////////
        foreach (CBRatingSystem::post_types() as $argType => $postTypes) {
            echo '<optgroup label="' . $postTypes['label'] . '">';
            foreach ($postTypes['types'] as $type => $typeLabel) {
                echo "<option value='{$type}'";
                if ($posttype == $type) {
                    echo "selected='selected'";
                }
                echo ">" . ucfirst($typeLabel) . "</option>";
            }
            echo '</optgroup>';
        }
        ?>
            </select>
        </p>
        <!-- post type filter -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('post_filter');
        ?>
"><?php 
        _e("Post Filter", 'cbratingsystem');
        ?>
:</label>
            <select id="<?php 
        echo $this->get_field_id('post_filter');
        ?>
" name="<?php 
        echo $this->get_field_name('post_filter');
        ?>
" class="widefat" style="width:50%">
                <?php 
        $no_of_filter = array('post_type' => 'Post Type', 'post_id' => 'Post ID');
        foreach ($no_of_filter as $key => $label) {
            echo "<option value = '{$key}'";
            if ($post_filter == $key) {
                echo "selected='selected'";
            }
            echo "> {$label} </option>";
        }
        ?>
            </select>
        </p>
        <!-- order by  type filter -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('order');
        ?>
"><?php 
        _e("Order", 'cbratingsystem');
        ?>
:</label>
            <select id="<?php 
        echo $this->get_field_id('order');
        ?>
" name="<?php 
        echo $this->get_field_name('order');
        ?>
" class="widefat" style="width:50%">
                <?php 
        $no_of_filter = array('ASC' => 'Ascending', 'DESC' => 'Descending');
        foreach ($no_of_filter as $key => $label) {
            echo "<option value = '{$key}'";
            if ($order == $key) {
                echo "selected='selected'";
            }
            echo "> {$label} </option>";
        }
        ?>
            </select>
        </p>
        <!-- order by  type filter -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('firstorder');
        ?>
"><?php 
        _e("First Sort By", 'cbratingsystem');
        ?>
:</label>
            <select id="<?php 
        echo $this->get_field_id('firstorder');
        ?>
" name="<?php 
        echo $this->get_field_name('firstorder');
        ?>
" class="widefat" style="width:50%">
                <?php 
        $no_of_filter = array('rating' => 'Rating', 'post_count' => 'User post Number');
        foreach ($no_of_filter as $key => $label) {
            echo "<option value = '{$key}'";
            if ($firstorder == $key) {
                echo "selected='selected'";
            }
            echo "> {$label} </option>";
        }
        ?>
            </select>
        </p>


        <!--form id -->
        <p>
            <label for="<?php 
        echo $this->get_field_id('form_id');
        ?>
"><?php 
        _e("Form", 'cbratingsystem');
        ?>
:</label>
            <select id="<?php 
        echo $this->get_field_id('form_id');
        ?>
" name="<?php 
        echo $this->get_field_name('form_id');
        ?>
" class="widefat" style="width: 55%">
                <?php 
        $action = array('is_active' => true);
        $ratingForms = CBRatingSystemData::get_ratingForms(true, $action);
        $ratingFormToShow = intval($form_id);
        $default = CBRatingSystem::get_default_ratingFormId();
        if (!empty($ratingForms)) {
            echo "<option value='0'";
            if ($ratingFormToShow == 0) {
                echo "selected='selected'";
            }
            echo ">" . ucfirst('All') . "</option>";
            foreach ($ratingForms as $ratingForm) {
                if ($default == $ratingForm->id) {
                    $txt = ' (' . __('Default Form', 'cbratingsystem') . ')';
                } else {
                    $txt = '';
                }
                if ($ratingFormToShow == $ratingForm->id) {
                    echo '<option selected value="' . $ratingForm->id . '">' . $ratingForm->name . $txt . '</option>';
                } else {
                    echo '<option value="' . $ratingForm->id . '">' . $ratingForm->name . $txt . '</option>';
                }
            }
        }
        ?>
            </select>
        </p>
    <?php 
    }
    public static function post_meta_box_form($object, $box)
    {
        //echo '<pre>'; print_r($object); echo '</pre>';
        $postId = $object->ID;
        //var_dump($postId);
        $ratingFormEnabled = get_post_meta($postId, '_cbrating_enable_ratingForm', true);
        $defaultRatingFormEnabled = get_post_meta($postId, '_cbrating_is_default_ratingForm', true);
        $ratingFormToShow = get_post_meta($postId, '_cbrating_listing_ratingForm', true);
        $default = get_option('cbratingsystem_defaultratingForm');
        $default = CBRatingSystem::get_default_ratingFormId();
        $ratingFormToShow = (!empty($ratingFormToShow) and is_numeric($ratingFormToShow)) ? $ratingFormToShow : ((is_numeric($default) and !empty($default)) ? $default : '#NONE#');
        $action = array('is_active' => true, 'post_type' => $object->post_type);
        $ratingForms = CBRatingSystemData::get_ratingForms(true, $action);
        $adminUrl = admin_url('?page=rating');
        $option = '<select id="listing_ratingForm" name="listing_ratingForm" class="listing_ratingForm_select" style="width:50%">';
        if ($ratingFormToShow == '#NONE#') {
            $option .= '<option selected value="#NONE#">' . __('--Select rating form--', 'cbratingsystem') . '</option>';
        } else {
            $option .= '<option value="#NONE#">' . __('--Select rating form--', 'cbratingsystem') . '</option>';
        }
        if (!empty($ratingForms)) {
            foreach ($ratingForms as $ratingForm) {
                if ($default == $ratingForm->id) {
                    $txt = ' (' . __('Default Form', 'cbratingsystem') . ')';
                } else {
                    $txt = '';
                }
                if ($ratingFormToShow == $ratingForm->id) {
                    $option .= '<option selected value="' . $ratingForm->id . '">' . $ratingForm->name . $txt . '</option>';
                } else {
                    $option .= '<option value="' . $ratingForm->id . '">' . $ratingForm->name . $txt . '</option>';
                }
            }
        }
        $option .= '</select>';
        ?>
		<?php 
        wp_nonce_field(basename(__FILE__), 'rating_form_meta_box_nonce');
        ?>


		<p>
			<label for="enable_ratingForm"><?php 
        _e(sprintf("Display Form"), 'cbratingsystem');
        ?>
</label>
			<input type="checkbox" id="enable_ratingForm" name="enable_ratingForm" value="1" <?php 
        echo $ratingFormEnabled != '' ? $ratingFormEnabled == 1 ? 'checked' : '' : 'checked';
        ?>
/>
		</p>
		<p>
			<label for="is_default_ratingForm"><?php 
        _e(sprintf("Use Default"), 'cbratingsystem');
        ?>
</label>
			<input type="checkbox" id="is_default_ratingForm" name="is_default_ratingForm" value="1" <?php 
        echo $defaultRatingFormEnabled != '' ? $defaultRatingFormEnabled == 1 ? 'checked' : '' : 'checked';
        ?>
/>
		</p>
		<p class="cb_listing_ratingForm">
			<label for="listing_ratingForm"><?php 
        _e(sprintf("Select which form should work for this %s:", $object->post_type), 'cbratingsystem');
        ?>
</label>
			<br />
			<?php 
        echo $option;
        ?>
			<span class="description"><?php 
        _e(sprintf("Select which form should work for this %s. You can checkout rating forms at %s page.", $object->post_type, '<a target="_blank" href="' . $adminUrl . '">this</a>'), 'cbratingsystem');
        ?>
</span>
		</p>

		<script type="text/javascript">
			jQuery(document).ready(function ($) {
				$('input#is_default_ratingForm').change(function () {
					if ($(this).is(':checked')) {
						$('.cb_listing_ratingForm').hide();
					} else {
						$('.cb_listing_ratingForm').show();
					}
				});
				if ($('input#is_default_ratingForm').is(':checked')) {
					$('.cb_listing_ratingForm').hide();
				} else {
					$('.cb_listing_ratingForm').show();
				}
			});
		</script>
	<?php 
    }