<?php

namespace FSPoster\App\view;

use FSPoster\App\Providers\Date;
use FSPoster\App\Providers\DB;
use FSPoster\App\Providers\Helper;

if ( ! defined( 'ABSPATH' ) )
{
	exit;
}

if ( isset( $_GET['share'] ) && ! empty( $_GET['share'] ) && $_GET['share'] == '1' )
{
	if ( ! empty( $_GET['background'] ) )
	{
		?>
        <script>
            jQuery(document).ready(function () {
                fsCode.toast("<?=esc_html__( 'Post will be share in background!', 'fs-poster' )?>");
            });
        </script>
		<?php
	}
	else
	{
		$checkNotSendedFeeds = DB::DB()->get_row( DB::DB()->prepare( "SELECT count(0) AS cc FROM " . DB::table( 'feeds' ) . " WHERE post_id=%d AND is_sended=0 AND `blog_id`=%d", [ (int) $parameters['post']->ID, Helper::getBlogId() ] ), ARRAY_A );
	}
}

$feeds = DB::fetchAll( 'feeds', [
	'blog_id' => Helper::getBlogId(),
	'post_id' => $parameters['post']->ID
] );
?>

<style>
    .share_box_items_edit a {
        text-decoration: none;
        color: #0984e3;
        outline: none !important;
        box-shadow: none !important;
        text-shadow: none !important;
    }

    .share_box_items_edit {
        margin-top: 10px;
        border: 1px solid #DDD;
        background: #FFF;
        -webkit-border-radius: 3px;
        -moz-border-radius: 3px;
        border-radius: 3px;

        max-height: 300px;
        overflow: auto;
    }

    .share_box_node_edit {
        display: flex;
        align-items: center;
        padding: 5px;
        height: 35px;
        border-bottom: 1px solid #DDD;
    }

    .node_img {
        width: 30px;
    }

    .node_img > img {
        width: 30px;
        height: 30px;
        -webkit-border-radius: 30px;
        -moz-border-radius: 30px;
        border-radius: 30px;
    }

    .node_label {
        margin-left: 10px;
        font-size: 14px;
        overflow: hidden;
        white-space: nowrap;
    }

    .node_actions {
        width: 50px;
        height: 100%;
        background: #FFF;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .node_actions > .status_action {
        width: 19px;
        line-height: 18px;
        height: 19px;
        text-align: center;
        -webkit-border-radius: 3px;
        -moz-border-radius: 3px;
        border-radius: 3px;
        font-size: 10px;
    }

    .node_label_help {
        font-size: 11px;
        color: #888;
    }

    .post_link {
        padding-left: 5px;
        color: #fdcb6e !important;
        cursor: pointer;
    }
</style>

<div>
    <div id="share_box1_edit">
        <div>Shared on:</div>
        <div class="share_box_items_edit">
			<?php
			$feedsCount = 0;
			foreach ( $feeds as $feedInf )
			{
				$nodeInfTable = $feedInf['node_type'] == 'account' ? 'accounts' : 'account_nodes';

				$nodeInf = DB::fetch( $nodeInfTable, $feedInf['node_id'] );

				if ( empty( $nodeInf ) )
				{
					continue;
				}

				if ( $feedInf['node_type'] == 'account' )
				{
					$nodeInf['node_type'] = 'account';
				}

				$feedsCount++;

				?>
                <div class="share_box_node_edit">
                    <div class="node_img"><img src="<?= Helper::profilePic( $nodeInf ) ?>"
                                               onerror="this.src = FSPosterData.assets_url + 'images/no-photo.png';">
                    </div>
                    <div class="node_label" style="width: 100%;">
                        <div><a href="<?= Helper::profileLink( $nodeInf ) ?>"
                                target="_blank"><?= esc_html( $nodeInf['name'] ); ?></a></div>
                        <div class="node_label_help"><?= esc_html( ucfirst( $nodeInf['driver'] ) . ' > ' . $nodeInf['node_type'] . ( $feedInf['feed_type'] == 'story' ? ' > Story' : '' ) ); ?></div>
                    </div>
                    <div class="node_actions">
						<?php
						if ( $feedInf['status'] == 'ok' )
						{
							if ( $nodeInf['driver'] == 'google_b' )
							{
								$username = isset( $nodeInf['node_id'] ) ? $nodeInf['node_id'] : '';
							}
							else
							{
								if ( $nodeInf['driver'] == 'wordpress' )
								{
									$username = isset( $nodeInf['options'] ) ? $nodeInf['options'] : '';
								}
								else
								{
									$username = ( isset( $nodeInf['screen_name'] ) ? $nodeInf['screen_name'] : '' );
								}
							}
							?>
                            <span class="ws_bg_success status_action"
                                  title="<?= esc_html__( 'Posted successfully', 'fs-poster' ) ?>">
								<i class="fa fa-check"></i>
							</span>
                            <a href="<?= Helper::postLink( $feedInf['driver_post_id'], $feedInf['driver'] . ( $feedInf['driver'] == 'instagram' ? $feedInf['feed_type'] : '' ), $username ) ?>"
                               target="_blank" class="post_link"
                               title="<?= esc_html__( 'Go to the post', 'fs-poster' ) ?>"><i
                                        class="fa fa-external-link fa-external-link-alt"></i></a>
							<?php
						}
						else
						{
							if ( $feedInf['is_sended'] == '0' )
							{
								$message       = esc_html__( 'Going to share in a minute', 'fs-poster' );
								$shareTimerSec = Date::epoch( $feedInf['send_time'] ) - Date::epoch();
								if ( $shareTimerSec > 60 )
								{
									$message = sprintf( esc_html__( 'Going to share after %d minute(s)', 'fs-poster' ), (int) ( $shareTimerSec / 60 ) );
								}

								?>
                                <span class="ws_bg_warning status_action" title="<?php
								print $message ?>">
								<i class="fa fa-clock"></i>
							</span>
								<?php
							}
							else
							{
								?>
                                <span class="ws_bg_danger status_action" title="<?php
								printf( esc_html__( 'Post failed! %s', 'fs-poster' ), esc_html( $feedInf['error_msg'] ) ) ?>">
								<i class="fa fa-exclamation-triangle"></i>
							</span>
								<?php
							}
						}
						?>
                    </div>
                </div>
				<?php
			}

			if ( ! $feedsCount )
			{
				print '<div style="margin: 10px; color: #b05858; text-align: center;">This post not shared any social network!</div>';
			}
			?>
        </div>
		<?php
		if ( get_post_status( $parameters['post']->ID ) === 'publish' )
		{
			?>
            <div style="margin-top: 10px; display: flex; justify-content: space-between;">
                <button type="button" class="button button-primary" style="width: 49%;"
                        data-load-modal="share_saved_post"
                        data-parameter-post_id="<?= $parameters['post']->ID ?>"><?= empty( $feeds ) ? 'Share' : 'Share again' ?></button>
                <button type="button" class="button button-secondary"
                        style="width: 49%; background: #e84393; color: #FFF; border: 0;" data-load-modal="add_schedule"
                        data-parameter-post_ids="<?= $parameters['post']->ID ?>">Schedule
                </button>
            </div>
			<?php
		}
		else
		{
			if ( get_post_status( $parameters['post']->ID ) === 'future' )
			{
				?>
                <div style="margin-top: 10px;">
                    <button type="button" class="button button-secondary"
                            style="width: 100%; background: #e84393; color: #FFF; border: 0;"
                            data-load-modal="edit_wp_native_schedule"
                            data-parameter-post_id="<?= $parameters['post']->ID ?>">Edit schedule
                    </button>
                </div>
				<?php
			}
		}
		?>
    </div>
</div>

<script>
    jQuery(document).ready(function () {
        if (typeof $ == 'undefined') var $ = typeof jQuery == 'undefined' ? null : jQuery;
		<?php
		if(isset( $checkNotSendedFeeds ) && $checkNotSendedFeeds['cc'] > 0)
		{
		?>
        fsCode.loadModal('share_feeds', {'post_id': '<?=(int) $parameters['post']->ID?>'})
		<?php
		}
		?>
    });
</script>
