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

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


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

        $style1 = (string) NULL;
        $style2 = (string) NULL;
        $style3 = (string) NULL;
        global $maskitto_light;

        global $wp_query;
        $post_obj = $wp_query->get_queried_object();
        $page = get_post( (int)$instance['pageid'] );

        if( $instance['pageid'] > 0 && $page && $instance['pageid'] != $post_obj->ID ) {

            $subtitle = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-subtitle', true ));
            $titlebar = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-title-layout', true ));
            $title_color = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-title-color', true ));
            $background_color = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-background-color', true ));
            $background_image = esc_url( get_post_meta( $instance['pageid'], 'wpcf-background-image', true ));
            $padding = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-page-padding', true ));
            $readmore = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-read-more', true ));

            $button_name = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-button-name', true ));
            $button_url = esc_url( get_post_meta( $instance['pageid'], 'wpcf-button-url', true ));
            if( !$button_url ) $button_url = '#';
            $button_icon = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-button-icon', true ));
            $button_type = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-button-tone', true ));

            $googlemaps_image = esc_url( get_post_meta( $instance['pageid'], 'wpcf-google-maps-image', true ));
            $googlemaps_url = esc_url( get_post_meta( $instance['pageid'], 'wpcf-google-maps-url', true ));
            $googlemaps_height = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-google-maps-height', true ));
            $sociallinks = esc_attr( get_post_meta( $instance['pageid'], 'wpcf-social-links', true ));

            if( $padding == 'small' ) {
                $style1.= "padding: 35px 0;";
            } else if( $padding == 'large' ) {
                $style1.= "padding: 150px 0;";
            }

            if( $background_color ) {
                $style1.= "background-color: $background_color;";
            }

            if( $background_image ) {
                $style1.= "background-image: url($background_image);";
            }

            if( $title_color ) {
                $style2.= "color: $title_color;";
            }

            if( $googlemaps_height == 'extrasmall' ) {
                $style3.= "height: 200px;";
            } else if( $googlemaps_height == 'small' ) {
                $style3.= "height: 325px;";
            } else if( $googlemaps_height == 'large' ) {
                $style3.= "height: 550px;";
            } else if( $googlemaps_height == 'extralarge' ) {
                $style3.= "height: 750px;";
            } else {
                $style3.= "height: 450px;";
            }

        ?>
            <div class="page-section" style="<?php echo $style1; ?>">
                <div class="container page-list">

                    <?php if( $titlebar == 'small' ) { ?>
                        <h4 class="text-center" style="<?php echo $style2; ?>"><?php echo $page->post_title; ?><?php echo maskitto_light_admin_edit($instance['pageid']); ?></h4>
                    <?php } else if( $titlebar == 'large' ) { ?>
                        <div class="section-title-large text-center">
                            <div class="title" style="<?php echo $style2; ?>"><?php echo $page->post_title; ?><?php echo maskitto_light_admin_edit($instance['pageid']); ?></div>
                        </div>
                        <?php if( $page->post_content ) { ?>
                            <div class="section-content-large" style="<?php echo $style2; ?>"><?php echo $page->post_content; ?></div>
                        <?php } ?>
                    <?php } else { ?>
                        <div class="section-title text-center">
                            <h3 style="<?php echo $style2; ?>"><?php echo $page->post_title; ?><?php echo maskitto_light_admin_edit($instance['pageid']); ?></h3>
                            <?php if( $subtitle ){ ?>
                                <div class="subtitle"><p><?php echo $subtitle; ?></p></div>
                            <?php } ?>
                            <div class="section-title-line"></div>
                        </div>
                        <?php if( $page->post_content && !$googlemaps_height ) { ?>
                            <div class="section-content" style="<?php echo $style2; ?>"><?php echo $page->post_content; ?></div>
                        <?php } ?>
                    <?php } ?>

                    <?php if( $googlemaps_height ) { ?>
                        <div class="contact-section">
                            <div class="contact-subtitle"><?php _e( 'Information', 'maskitto-light' ); ?></div>
                            <div><?php echo wpautop($page->post_content); ?></div>

                            <?php if( isset( $sociallinks ) && $sociallinks == '1' ) { ?>
                                <div class="contact-social-icons">  
                                    <?php echo maskitto_light_social_icons(); ?>
                                </div>
                            <?php } ?>
                        </div>
                    <?php } ?>            

                    <?php if( $button_name ) { ?>
                        <a href="<?php echo $button_url; ?>" class="btn btn-default section-button section-button-<?php echo $button_type; ?>">
                            <?php if( $button_icon ) { ?>
                                <i class="fa <?php echo $button_icon; ?>"></i>
                            <?php } ?>
                            <?php echo $button_name; ?>
                        </a>
                    <?php } ?>

                    <?php if( $readmore ) { ?>
                        <a href="<?php echo esc_url( get_permalink( $instance['pageid'] ) ); ?>" class="btn btn-default"><i class="fa fa-angle-right"></i><?php _e( 'Read more', 'maskitto-light' ); ?></a>
                    <?php } ?>

                </div>
            </div>
            <?php if( $googlemaps_url ) { ?>
            <div class="page-section googlemap" style="<?php echo $style3; ?>">
                <a href="<?php echo $googlemaps_url; ?>" target="_blank" class="google-maps-image" style="background-image: url(<?php echo $googlemaps_image; ?>);"></a>
                <div class="googlemap-loading"><i class="fa fa-circle-o-notch fa-spin"></i></div>
            </div>
            <?php } ?>
        <?php } ?>
    <?php }


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

        if ( isset( $instance[ 'pageid' ] ) ) {
            $pageid = $instance[ 'pageid' ];
        }

        if ( isset( $instance[ 'subtitle' ] ) ) {
            $subtitle = $instance[ 'subtitle' ];
        }
        ?>

        <div class="widget-option">
            <div class="widget-th">
                <label for="<?php echo $this->get_field_id( 'pageid' ); ?>"><b><?php _e( 'Pages', 'maskitto-light' ); ?></b></label> 
            </div>
            <div class="widget-td">
                <select id="<?php echo $this->get_field_id( 'pageid' ); ?>" name="<?php echo $this->get_field_name( 'pageid' ); ?>"> 
                    <?php $args = array(
                        'sort_order' => 'ASC',
                        'sort_column' => 'post_title',
                        'hierarchical' => 1,
                        'exclude' => '',
                        'include' => '',
                        'meta_key' => '',
                        'meta_value' => '',
                        'authors' => '',
                        'child_of' => 0,
                        'parent' => -1,
                        'exclude_tree' => '',
                        'number' => '',
                        'offset' => 0,
                        'post_type' => 'page',
                        'post_status' => 'publish'
                    ); 
                    $pages = get_pages($args);
                        foreach ( $pages as $page ) {
                            if( $page->ID == $pageid ) {
                                $selected = ' selected';
                            }

                            $option = '<option value="' . $page->ID . '"'.$selected.'>';
                            $option .= $page->post_title;
                            $option .= '</option>';
                            echo $option;

                            $selected = '';
                        }
                    ?>
                </select>
                <p><?php _e( 'Choose page you want to include', '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();
        (int)$instance['pageid'] = ( ! empty( $new_instance['pageid'] ) ) ? strip_tags( $new_instance['pageid'] ) : '';
        if( $instance['pageid'] > 0 ) {
            $title = get_the_title( $instance['pageid'] );
            if( $title ) {
                $instance['title'] = $title;
            } else {
                $instance['title'] = 'Unknown';
            }
        }

        return $instance;
    }

}