<?php
/* Adds Maskitto_Services widget. */
class Maskitto_Projects extends WP_Widget {

    /* Register widget with WordPress. */
    function __construct() {
        parent::__construct(
            'maskitto_projects',
            __('Maskitto: Portfolio', 'maskitto-light'),
            array(
                'description' => __( 'Only for page builder.', 'maskitto-light' ),
                'panels_icon' => 'dashicons dashicons-media-interactive',
                'panels_groups' => 'theme-widgets'
            )
        );
    }

    /* Front-end display of widget. */
    public function widget( $args, $instance ) { ?>

        <?php if( ( isset( $instance['title'] ) && $instance['title'] ) && ( isset( $instance['subtitle'] ) && $instance['subtitle'] ) ) : ?>
            <div class="page-section" style="padding-bottom: 0;">
                <div class="container">
                    <div class="row projects-list">
                        <div class="section-title text-center">
                            <h3 style="<?php echo $style2; ?>"><?php echo $instance['title']; ?></h3>
                            <?php if( isset( $subtitle ) && $subtitle ) : ?>
                                <div class="subtitle"><p><?php echo $instance['subtitle']; ?></p></div>
                            <?php endif; ?>
                            <div class="section-title-line"></div>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>
        <div class="page-section" style="padding: 0;">
            <div class="row portfolio-list">
                <?php
                    if( !isset($instance['limit']) ||  !$instance['limit'] || $instance['limit'] == '-1' ) :
                        $instance['limit'] = 200;
                    endif;

                    
                    $count_portfolio = wp_count_posts( 'portfolio-item' );
                    if( isset( $count_portfolio->publish ) && $count_portfolio->publish > 0) :
                        $loop = new WP_Query( array('post_type' => 'portfolio-item', 'posts_per_page' => $instance['limit'] ) );
                    else :
                        $loop = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => $instance['limit'] ) );
                    endif;


                    while ( $loop->have_posts() ) : $loop->the_post();

                        $style1 = (string) NULL;
                        $image = esc_url( get_post_meta( get_the_ID(), 'wpcf-background-image', true ));
                        $caption = esc_attr( get_post_meta( get_the_ID(), 'wpcf-caption', true ));
                        $url = esc_url( get_post_meta( get_the_ID(), 'wpcf-url', true ));

                        if( $image ) :
                            $style1.= "background-image: url($image);";
                        endif;

                        if( $url ) :
                            $image = $url;
                        endif;

                ?>
                <a href="<?php echo $image; ?>" alt="<?php the_title(); ?>" class="col-md-3 col-sm-6 portfolio-item">
                    <div class="portfolio-thumb" style="<?php echo $style1; ?>"></div>
                    <div class="portfolio-details">
                        <div class="portfolio-details-align">
                            <div class="portfolio-title"><?php the_title(); ?></div>
                            <?php if( $caption ) : ?>
                                <div class="portfolio-info"><?php echo $caption; ?></div>
                            <?php endif; ?>
                            <?php
                            $cat = get_the_category();
                            if( count($cat) && $cat[0]->name ) : ?>
                                <div class="portfolio-line"></div>
                                <div class="portfolio-cat"><?php echo $cat[0]->name; ?></div>
                            <?php endif; ?>
                        </div>
                    </div>
                </a>
                <?php endwhile; ?>
            </div>
        </div>

    <?php }


    /* Back-end widget form. */
    public function form( $instance ) {
        $title = (string) NULL;
        $subtitle = (string) NULL;
        $limit = (string) NULL;

        if ( isset( $instance[ 'title' ] ) ) :
            $title = $instance[ 'title' ];
        endif;

        if ( isset( $instance[ 'subtitle' ] ) ) :
            $subtitle = $instance[ 'subtitle' ];
        endif;

        if ( isset( $instance[ 'limit' ] ) ) :
            $limit = $instance[ 'limit' ];
        endif;

        ?>

        <div class="widget-option">
            <div class="widget-th">
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><b><?php _e( 'Title', 'maskitto-light' ); ?></b></label> 
            </div>
            <div class="widget-td">
                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
                <p><?php _e( 'This field is optional', 'maskitto-light' ); ?></p>
            </div>
            <div class="clearfix"></div>
        </div>


        <div class="widget-option">
            <div class="widget-th">
                <label for="<?php echo $this->get_field_id( 'subtitle' ); ?>"><b><?php _e( 'Subitle', 'maskitto-light' ); ?></b></label> 
            </div>
            <div class="widget-td">
                <input class="widefat" id="<?php echo $this->get_field_id( 'subtitle' ); ?>" name="<?php echo $this->get_field_name( 'subtitle' ); ?>" type="text" value="<?php echo esc_attr( $subtitle ); ?>">
                <p><?php _e( 'This field is optional', 'maskitto-light' ); ?></p>
            </div>
            <div class="clearfix"></div>
        </div>

        <div class="widget-option">
            <div class="widget-th">
                <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><b><?php _e( 'Limit items', 'maskitto-light' ); ?></b></label> 
            </div>
            <div class="widget-td">
                <select id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>"> 
                    <option value="4" <?php if( $limit == '4' ) echo 'selected'; ?>><?php _e( '4 items', 'maskitto-light' ); ?></option>
                    <option value="8" <?php if( $limit == '8' ) echo 'selected'; ?>><?php _e( '8 items', 'maskitto-light' ); ?></option>
                    <option value="12" <?php if( $limit == '12' ) echo 'selected'; ?>><?php _e( '12 items', 'maskitto-light' ); ?></option>
                    <option value="-1" <?php if( $limit == '-1' ) echo 'selected'; ?>><?php _e( 'No limits', 'maskitto-light' ); ?></option>
                </select>
                <p><?php _e( 'This field is optional', 'maskitto-light' ); ?></p>
            </div>
            <div class="clearfix"></div>
        </div>

        <link rel='stylesheet' id='options-css'  href='<?php echo get_template_directory_uri() . '/css/options.css'; ?>' type='text/css' media='all' />

        <?php 
    }


    /* Sanitize widget form values as they are saved. */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['subtitle'] = ( ! empty( $new_instance['subtitle'] ) ) ? strip_tags( $new_instance['subtitle'] ) : '';
        $instance['limit'] = ( ! empty( $new_instance['limit'] ) ) ? strip_tags( $new_instance['limit'] ) : '';

        return $instance;
    }

}