<?php

namespace FSPoster\App\view;

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

defined( 'ABSPATH' ) or exit;

$accountsList = DB::DB()->get_results(DB::DB()->prepare("
	SELECT 
		*,
		(SELECT filter_type FROM ".DB::table('account_status')." WHERE account_id=tb1.id AND user_id=%d) is_active 
	FROM ".DB::table('accounts')." tb1 
	WHERE (user_id=%d OR is_public=1) AND driver='instagram' AND blog_id=%d",  [get_current_user_id(), get_current_user_id(), Helper::getBlogId()]) , ARRAY_A);

if( version_compare(PHP_VERSION, '5.6.0') < 0 )
{
	print '<div style="padding: 100px; font-size: 16px; font-weight: 500; color: #999; text-align: center">
				<div style="margin: 20px;"><i class="fa fa-warning fa-exclamation-triangle fa-5x" style="color: #ff9b90;"></i> </div>
				<div style="margin: 10px;">For using instagram account, please update your PHP version 5.6 or higher!</div>
				<div>Your current PHP version is: ' . PHP_VERSION . '</div>
			</div>';
	return;
}

?>

<div style="margin: 40px 80px;">
	<span style="color: #888; font-size: 17px; font-weight: 600; line-height: 36px;"><span id="accounts_count"><?php print count($accountsList);?></span> <?=__('instagram account added', 'fs-poster')?></span>
	<button type="button" class="ws_btn ws_bg_dark" style="float: right;" data-load-modal="add_instagram_account_case"><i class="fa fa-plus"></i> <?=esc_html__('ADD ACCOUNT', 'fs-poster')?></button>
</div>
<div class="ws_table_wraper">
	<table class="ws_table">
		<thead>
		<tr>
			<th><?=esc_html__('NAME', 'fs-poster')?> <i class="fa fa-caret-down"></i></th>
			<th style="width: 190px;"><?=esc_html__('USERNAME', 'fs-poster')?></th>
			<th style="width: 15%;"><?=esc_html__('ACTIVATE', 'fs-poster')?></th>
		</tr>
		</thead>
	</table>

	<?php
	foreach($accountsList AS $accountInf)
	{
		?>
		<table class="ws_table">
			<tbody>
				<tr data-id="<?=$accountInf['id']?>" data-type="account">
					<td>
						<img class="ws_img_style" src="<?=Helper::profilePic($accountInf)?>" onerror="this.src = FSPosterData.assets_url + 'images/no-photo.png';">
						<span style="vertical-align: middle;"><?php print esc_html($accountInf['name']);?></span>
						<?php
						if( !empty($accountInf['proxy']) )
						{
							?>
							<span style="padding-left: 10px; color: #74b9ff;" class="ws_tooltip" data-title="Proxy: <?=esc_html($accountInf['proxy'])?>"><i class="fa fa-globe"></i></span>
							<?php
						}
						?>
						<span style="padding-left: 10px; color: #ffbd72;<?=!$accountInf['is_public']?' display: none;':''?>" class="fs_account_is_public ws_tooltip" data-title="<?=esc_html__('This profile is public for all WordPress users.', 'fs-poster')?>"><i class="fa fa-star"></i></span>
						<a href="<?=Helper::profileLink($accountInf)?>" target="_blank" class="ws_btn ws_tooltip" data-title="<?=esc_html__('Profile link', 'fs-poster')?>" style="font-size: 13px; color: #fd79a8;"><i class="fa fa-external-link fa-external-link-alt"></i></a>
					</td>
					<td style="width: 190px;"><?=esc_html($accountInf['username']);?></td>
                    <td style="width: 20px;">
						<?php if ( $accountInf['status'] === 'error' ) { ?>
                            <div style="color: #ff0000; font-size: 20px;" class="ws_tooltip" data-title="<?php echo esc_html( $accountInf['error_msg'] ); ?>" type="button">
                                <i class="fas fa-exclamation-circle"></i>
                            </div>
						<?php } ?>
                    </td>
					<td style="width: 15%;">
                        <div class="fs_account_checkbox<?php echo $accountInf['is_active'] == '' ? '' : ' fs_account_checked' . ($accountInf['is_active']== 'no' ? '' : '2'); ?>">
                            <i class="fa fa-check"></i>
                        </div>
						<button class="more_options_account fs_more_options_design"><i class="fa fa-ellipsis-v"></i></button>
					</td>
				</tr>
			</tbody>
		</table>
		<?php
	}
	if( empty($accountsList) )
	{
		?>
		<table class="ws_table"><tbody><tr><td colspan="100%" style="color: #999;"><?=esc_html__('No account found', 'fs-poster')?></td></tr></tbody></table>
		<?php
	}
	?>
</div>