/**
     * Widget settings
     * @param $instance
     */
    function form($instance)
    {
        //$this->skin->saveCallbackJs4SkinChangeEvent('console.log(skin);');
        //$this->skin->saveCallbackJs4SkinChangeEvent('console.log("jkehrerdgb");');
        //for showing/hiding advanced options; wordpress moves this script to where it needs to go
        wp_enqueue_script('jquery');
        //create feature tog
        if (class_exists('HW_ButtonToggle_widget')) {
            $btn_tog = new HW_ButtonToggle_widget($this, $instance);
        }
        ?>

        <?php 
        $terms_data = array();
        // instance exist? if not set defaults
        if ($instance) {
            $title = $instance['title'];
            $this_taxonomy = $instance['taxonomy'];
            $orderby = $instance['orderby'];
            $ascdsc = $instance['ascdsc'];
            $exclude = $instance['exclude'];
            $childof = $instance['childof'];
            $showcount = isset($instance['count']) ? (bool) $instance['count'] : false;
            $hierarchical = isset($instance['hierarchical']) ? (bool) $instance['hierarchical'] : false;
            $dropdown = isset($instance['dropdown']) ? (bool) $instance['dropdown'] : false;
            $childof_current_term = isset($instance['childof_current_term']) ? (bool) $instance['childof_current_term'] : false;
            $show_subcat_by_parent = isset($instance['show_subcat_by_parent']) ? (bool) $instance['show_subcat_by_parent'] : false;
            $categories_by_current_post = isset($instance['categories_by_current_post']) ? (bool) $instance['categories_by_current_post'] : false;
            $hide_empty = isset($instance['hide_empty']) ? (bool) $instance['hide_empty'] : false;
            $depth = isset($instance['depth']) ? $instance['depth'] : 0;
            $use_skin = isset($instance['use_skin']) ? (bool) $instance['use_skin'] : false;
            $extend_skin_options = isset($instance['extend_skin_options']) ? (bool) $instance['extend_skin_options'] : false;
            $skin = isset($instance['skin']) ? $instance['skin'] : '';
            //saved active hash skin
            //total skin setting
            $skin_setting = isset($instance['skin_settings']) ? $instance['skin_settings'] : '';
            $skin_condition = isset($instance['skin_condition']) ? $instance['skin_condition'] : '';
            $show_option_none = isset($instance['show_option_none']) ? $instance['show_option_none'] : '';
            //get terms by select tax
            if (!empty($this_taxonomy)) {
                $terms_data = HW_POST::list_tax_terms($this_taxonomy);
            }
        } else {
            //These are our defaults
            $title = '';
            $orderby = 'count';
            $ascdsc = 'desc';
            $exclude = '';
            $childof = '';
            $this_taxonomy = 'category';
            //this will display the category taxonomy, which is used for normal, built-in posts
            $hierarchical = true;
            $showcount = true;
            $dropdown = false;
            $childof_current_term = false;
            $show_subcat_by_parent = false;
            $categories_by_current_post = false;
            $hide_empty = false;
            //default hide if empty
            $depth = 0;
            //unlimit nested level
            $use_skin = false;
            $skin = '';
            $skin_setting = '';
            $skin_condition = '';
            $extend_skin_options = false;
            $show_option_none = '';
        }
        // The widget form
        $terms_holder_id = 'holder-' . $this->get_field_id('terms');
        $exclude_terms_holder_id = 'holder-' . $this->get_field_id('exclude_terms');
        ?>
		
		
			<p>
				<label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        echo __('Tiêu đề:');
        ?>
</label>
				<input id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" type="text" value="<?php 
        echo $title;
        ?>
" class="widefat" />
			</p>
			<p>
				<label for="<?php 
        echo $this->get_field_id('taxonomy');
        ?>
"><?php 
        echo __('Chọn Taxonomy:');
        ?>
</label>
				<select name="<?php 
        echo $this->get_field_name('taxonomy');
        ?>
" id="<?php 
        echo $this->get_field_id('taxonomy');
        ?>
" class="widefat" style="height: auto;" size="4" onchange="__hwlct_object.change_taxonomy(this, '#<?php 
        echo $terms_holder_id;
        ?>
', 'hwlct_change_taxonomy_cbs','<?php 
        //echo $link
        ?>
')">
			<?php 
        $args = array('public' => true, '_builtin' => false);
        $output = 'names';
        // or objects
        $operator = 'and';
        // 'and' or 'or'
        $taxonomies = get_taxonomies($args, $output, $operator);
        $taxonomies[] = 'category';
        $taxonomies[] = 'post_tag';
        $taxonomies[] = 'post_format';
        foreach ($taxonomies as $taxonomy) {
            ?>
				<option value="<?php 
            echo $taxonomy;
            ?>
" <?php 
            if ($taxonomy == $this_taxonomy) {
                echo 'selected="selected"';
            }
            ?>
><?php 
            echo $taxonomy;
            ?>
</option>
			<?php 
        }
        ?>
			</select>
			</p>
			<?php 
        if (class_exists('HW_SKIN')) {
            ?>
			    <p><?php 
            //echo $this->skin->get_skins_select_tag('skin');
            //$this->skin->get_skins_listview('skin1',$instance['skin1']);
            //$this->skin->get_skin_instance('t1')->get_skins_select_tag('skin2',null,array('class'=>'widefat'),HW_SKIN::DROPDOWN_DDSSLICK_THEME,HW_SKIN::SKIN_LINKS);
            ?>
</p>
            <p>
                <input type="checkbox" name="<?php 
            echo $this->get_field_name('use_skin');
            ?>
" id="<?php 
            echo $this->get_field_id('use_skin');
            ?>
" <?php 
            checked($use_skin);
            ?>
/>
                <label for="<?php 
            echo $this->get_field_id('use_skin');
            ?>
"><?php 
            _e('Kích hoạt Giao diện');
            ?>
</label>

            </p>
            <p>
                <input type="checkbox" name="<?php 
            echo $this->get_field_name('extend_skin_options');
            ?>
" id="<?php 
            echo $this->get_field_id('extend_skin_options');
            ?>
" <?php 
            selected($extend_skin_options ? 1 : 0);
            ?>
 />
                <label for="<?php 
            echo $this->get_field_id('extend_skin_options');
            ?>
"><?php 
            _e('Cho phép sử dụng skin options');
            ?>
</label>
                <br/>
                <span><em>Cho phép sử dụng skin options mặc dù không kích hoạt sử dụng giao diện riêng.</em></span>
            </p>
			<p>
			     <label for="<?php 
            echo $this->get_field_id('skin');
            ?>
"><strong><?php 
            _e('Giao diện');
            ?>
</strong></label>
				  <?php 
            echo $this->skin->get_skins_select_tag('skin', null, array('class' => 'widefat'), false);
            #$this->skin->addCallbackSkinChangeEvent('console.log(3);');
            ?>
                <!-- show skin options,skin condition field if exists -->
                <?php 
            echo $this->skin->get_skin_template_condition_selector('skin_condition', $skin_condition);
            echo $this->skin->prepare_skin_options_fields('skin_settings', $skin_setting, $skin);
            /*echo $this->skin->create_total_skin_selector('skin', array('hash_skin' => $skin, 'skin_settings' => $skin_setting), null,array(
                  'show_main_skin' =>0,
                  'show_condition_field'=>1,
                  'show_skin_options' => 1
              ));*/
            ?>
			</p>
			
			<?php 
        }
        ?>
			<?php 
        if (isset($btn_tog)) {
            $btn_tog->set_button_toggle_start_wrapper('Nâng cao...');
        }
        ?>
				
				<input type="checkbox" class="checkbox" id="<?php 
        echo $this->get_field_id('count');
        ?>
" name="<?php 
        echo $this->get_field_name('count');
        ?>
"<?php 
        checked($showcount);
        ?>
 />
				<label for="<?php 
        echo $this->get_field_id('count');
        ?>
"><?php 
        _e('Hiển thị số lượng posts bên cạnh');
        ?>
</label><br />
				<input type="checkbox" class="checkbox" id="<?php 
        echo $this->get_field_id('hierarchical');
        ?>
" name="<?php 
        echo $this->get_field_name('hierarchical');
        ?>
"<?php 
        checked($hierarchical);
        ?>
 />
				<label for="<?php 
        echo $this->get_field_id('hierarchical');
        ?>
"><?php 
        _e('Hiển thị đa tầng');
        ?>
</label><br/>

        <input type="checkbox" class="checkbox" id="<?php 
        echo $this->get_field_id('dropdown');
        ?>
" name="<?php 
        echo $this->get_field_name('dropdown');
        ?>
"<?php 
        checked($dropdown);
        ?>
 />
        <label for="<?php 
        echo $this->get_field_id('dropdown');
        ?>
"><?php 
        _e('Hiển thị dạng Dropdown');
        ?>
</label><br/>

        <input type="checkbox" class="checkbox" id="<?php 
        echo $this->get_field_id('hide_empty');
        ?>
" name="<?php 
        echo $this->get_field_name('hide_empty');
        ?>
" <?php 
        checked($hide_empty);
        ?>
 />
        <label for="<?php 
        echo $this->get_field_id('hide_empty');
        ?>
"><?php 
        _e('Ẩn mục không có posts');
        ?>
</label><br/>

        <input type="text" class="text" size="5" id="<?php 
        echo $this->get_field_id('depth');
        ?>
" name="<?php 
        echo $this->get_field_name('depth');
        ?>
" value="<?php 
        echo isset($instance['depth']) ? $instance['depth'] : '0';
        ?>
" />
        <label for="<?php 
        echo $this->get_field_id('depth');
        ?>
"><?php 
        _e('depth');
        ?>
</label><br/>
        <span><em>=1: Nếu chỉ muốn hiện tất cả những danh mục chính.</em></span><br/>
        <span><em>Yêu cầu: bật tùy chọn "Hiển thị đa tầng".</em></span>

				<p>
					<label for="<?php 
        echo $this->get_field_id('orderby');
        ?>
"><?php 
        echo __('Xắp xếp:');
        ?>
</label>
					<select name="<?php 
        echo $this->get_field_name('orderby');
        ?>
" id="<?php 
        echo $this->get_field_id('orderby');
        ?>
" class="widefat" >
						<option value="ID" <?php 
        if ($orderby == 'ID') {
            echo 'selected="selected"';
        }
        ?>
>ID</option>
						<option value="name" <?php 
        if ($orderby == 'name') {
            echo 'selected="selected"';
        }
        ?>
>Name</option>
						<option value="slug" <?php 
        if ($orderby == 'slug') {
            echo 'selected="selected"';
        }
        ?>
>Slug</option>
						<option value="count" <?php 
        if ($orderby == 'count') {
            echo 'selected="selected"';
        }
        ?>
>Count</option>
						<option value="term_group" <?php 
        if ($orderby == 'term_group') {
            echo 'selected="selected"';
        }
        ?>
>Term Group</option>
					</select>
				</p>
				<p>
					<label><input type="radio" name="<?php 
        echo $this->get_field_name('ascdsc');
        ?>
" value="asc" <?php 
        if ($ascdsc == 'asc') {
            echo 'checked';
        }
        ?>
/> Tăng dần</label><br/>
					<label><input type="radio" name="<?php 
        echo $this->get_field_name('ascdsc');
        ?>
" value="desc" <?php 
        if ($ascdsc == 'desc') {
            echo 'checked';
        }
        ?>
/> Giảm dần</label>
				</p>
				<p id="<?php 
        echo $exclude_terms_holder_id;
        ?>
">
					<label for="<?php 
        echo $this->get_field_id('exclude');
        ?>
">Loại trừ ID (danh sách IDs ngăn cách dấu phẩy)</label><br/>
					<select multiple class="widefat" name="<?php 
        echo $this->get_field_name('exclude');
        ?>
[]" >
                        <?php 
        if (!empty($terms_data)) {
            foreach ($terms_data as $id => $name) {
                $selected = in_array($id, $exclude) ? 'selected="selected"' : '';
                printf('<option %s value="%s">%s</option>', $selected, $id, $name);
            }
        }
        ?>
					</select>
				</p>
        <hr/>
				<p id="<?php 
        echo $terms_holder_id;
        ?>
">
					<label for="<?php 
        echo $this->get_field_id('childof');
        ?>
">Chỉ hiển thị sub-category của category mẹ (category id)</label><br/>
                    <?php 
        //echo $childof;
        ?>
					<select class="widefat" data-id="<?php 
        echo $this->number;
        ?>
" name="<?php 
        echo $this->get_field_name('childof');
        ?>
" id="<?php 
        echo $this->get_field_id('childof');
        ?>
">
                        <option value="">---- Chọn ----</option>
                        <?php 
        if (!empty($terms_data)) {
            foreach ($terms_data as $id => $name) {
                printf('<option %s value="%s">%s</option>', selected($id, $childof, false), $id, $name);
            }
        }
        ?>
					</select>

				</p>
                <p>
                    <input type="checkbox" name="<?php 
        echo $this->get_field_name('childof_current_term');
        ?>
" id="<?php 
        echo $this->get_field_id('childof_current_term');
        ?>
" <?php 
        checked($childof_current_term ? 1 : 0);
        ?>
/>
                    <label for="<?php 
        echo $this->get_field_id('childof_current_term');
        ?>
"><?php 
        _e('Lấy category mẹ hiện tại(category id)');
        ?>
</label><br/>
                    <span><em>Chỉ hiển thị sub-category của category hiện tại.</em></span>
                </p>
        <hr/>
                <p>
                    <input type="checkbox" name="<?php 
        echo $this->get_field_name('show_subcat_by_parent');
        ?>
" id="<?php 
        echo $this->get_field_id('show_subcat_by_parent');
        ?>
" <?php 
        checked($show_subcat_by_parent ? 1 : 0);
        ?>
/>
                    <label for="<?php 
        echo $this->get_field_id('show_subcat_by_parent');
        ?>
" style="text-decoration: line-through;"><?php 
        _e('Hiển thị sub-category khi vào trang mẹ');
        ?>
</label><br/>

                    <span><em>Hiển thị sub-category con của trang category mẹ nhưng vẫn dữ toàn bộ danh mục</em></span>
                </p>
        <hr/>
                <p>
                    <label for="<?php 
        echo $this->get_field_id('show_option_none');
        ?>
"><strong><?php 
        _e('Nội dung không có dữ liệu');
        ?>
</strong></label><br/>
                    <input type="text" name="<?php 
        echo $this->get_field_name('show_option_none');
        ?>
" id="<?php 
        echo $this->get_field_id('show_option_none');
        ?>
" value="<?php 
        echo $show_option_none;
        ?>
"/>

                </p>
            <p>
                <input type="checkbox" name="<?php 
        echo $this->get_field_name('categories_by_current_post');
        ?>
" id="<?php 
        echo $this->get_field_id('categories_by_current_post');
        ?>
" <?php 
        checked($categories_by_current_post ? 1 : 0);
        ?>
/>
                <label for="<?php 
        echo $this->get_field_id('categories_by_current_post');
        ?>
"><?php 
        _e('Hiển thị cho post hiện tại');
        ?>
</label><br/>
                <span><em>Hiển thị tất cả những taxonomies, taxonomies nào thuộc về post hiện tại.</em></span>
            </p>

			<script>
                /**
                 * hwlct_change_taxonomy_cbs callbacks
                 */
                if(typeof __hwlct_object.add_callback == 'function')
                __hwlct_object.add_callback('<?php 
        echo $this->number;
        ?>
', 'hwlct_change_taxonomy_cbs', {
                    before_ajax : function() {
                        var select_tag = jQuery('#<?php 
        echo $this->get_field_id('childof');
        ?>
:eq(0)'),
                            exclude_select_tag = jQuery('#<?php 
        echo $this->get_field_id('exclude');
        ?>
:eq(0)');

                        select_tag.html(' ').append(jQuery('<option>', {value:'',text : 'Loading..'}));
                        exclude_select_tag.html(' ').append(jQuery('<option>', {value:'',text : 'Loading..'}));
                    },
                    after_ajax : function(data) {
                        var select_tag = jQuery('#<?php 
        echo $this->get_field_id('childof');
        ?>
:eq(0)'),
                            exclude_select_tag = jQuery('#<?php 
        echo $this->get_field_id('exclude');
        ?>
:eq(0)');

                        jQuery(select_tag).add(exclude_select_tag).html(' ').append(jQuery('<option>', {
                            value: "",
                            text: "------Select------"  //.data[value]
                        }));

                        if(data.posts)
                            jQuery.each(data.terms, function(value, text) {
                                jQuery(select_tag).add(exclude_select_tag).append(jQuery('<option>', {
                                    value: value,
                                    text: text  //.data[value]
                                }));
                            });

                    }
                });
			</script>
			<?php 
        if (isset($btn_tog)) {
            $btn_tog->set_button_toggle_end_wrapper();
        }
        //close feature tog
    }
/**
 * list terms taxonomy
 * @param $tax
 * @param $args
 * @param $field default get term id as select value
 * @return array
 */
function hwlct_list_tax_terms($tax, $args = array(), $field = 'id')
{
    HW_HOANGWEB::load_class('HW_POST');
    return HW_POST::list_tax_terms($tax, $args, $field);
}