<?php

namespace FSPoster\App\view;

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

defined( 'ABSPATH' ) or exit;
?>

<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

<div class="fs_setting_item" style="width: 50%;">
	<div class="fs_setting_item_label">
		<div><?=esc_html__('Show "FS Poster" column on post list table:' , 'fs-poster')?></div>
		<div class="fs_s_help"><?=__('If you don\'t want to show FS Poster <i class="fa fa-question-circle" style="padding: 5px; cursor: pointer; color: #FF7784;" title="Click to learn more" data-open-img="'.Helper::assets('images/fs_poster_column_help.png').'"></i> column on posts table, you can disable this option.' , 'fs-poster')?></div>
	</div>
	<div class="fs_s_input">
		<div class="fs_onoffswitch">
			<input type="checkbox" onautocomplete="off" name="fs_show_fs_poster_column" class="fs_onoffswitch-checkbox" id="fs_show_fs_poster_column"<?=Helper::getOption('show_fs_poster_column', '1')?' checked':''?>>
			<label class="fs_onoffswitch-label" for="fs_show_fs_poster_column"></label>
		</div>
	</div>
</div>

<div style="display: flex;">
    <div style="width: 50%;">
        <div class="fs_setting_item">
            <div class="fs_setting_item_label">
                <div><?php print esc_html__('Check accounts:' , 'fs-poster')?></div>
                <div class="fs_s_help"><?php print esc_html__('Enable the option to check the status of all active accounts daily.' , 'fs-poster')?></div>
            </div>
            <div class="fs_s_input">
                <div class="fs_onoffswitch">
                    <input type="checkbox" onautocomplete="off" name="fs_check_accounts" class="fs_onoffswitch-checkbox" id="fs_check_accounts"<?php print Helper::getOption( 'check_accounts', 1 ) ? ' checked' : ''; ?> onchange="if(jQuery(this).is(':checked')){ jQuery('#hide_12').fadeIn(fadeSpeed); }else{ jQuery('#hide_12').fadeOut(fadeSpeed); }">
                    <label class="fs_onoffswitch-label" for="fs_check_accounts"></label>
                </div>
            </div>
        </div>
    </div>
    <div style="width: 50%;<?php print Helper::getOption('check_accounts', '1') ? '' : 'display: none;'?>" id="hide_12">
        <div class="fs_setting_item">
            <div class="fs_setting_item_label">
                <div><?php print esc_html__('Disable failed accounts:' , 'fs-poster')?></div>
                <div class="fs_s_help"><?php print esc_html__('Enable the option to deactivate failed accounts. If you have enabled the option to check the status of your accounts and if the connection to the account is unable, the plugin will not share posts on those failed accounts.' , 'fs-poster')?></div>
            </div>
            <div class="fs_s_input">
                <div class="fs_onoffswitch">
                    <input type="checkbox" onautocomplete="off" name="fs_check_accounts_disable" class="fs_onoffswitch-checkbox" id="fs_check_accounts_disable"<?php print Helper::getOption( 'check_accounts_disable', 0 ) ? ' checked':''?>>
                    <label class="fs_onoffswitch-label" for="fs_check_accounts_disable"></label>
                </div>
            </div>
        </div>
    </div>
</div>

<div style="width: 70%;">
	<div class="fs_setting_item">
		<div class="fs_setting_item_label">
			<div><?=esc_html__('Custom post types:' , 'fs-poster')?></div>
			<div class="fs_s_help"><?=esc_html__('Add post types which you want to share.' , 'fs-poster')?></div>
		</div>
		<div class="fs_s_input" style="width: 300px;">
			<select class="ws_form_element select2-init" id="fs_allowed_post_types" name="fs_allowed_post_types[]" style="width: 300px;" multiple onautocomplete="off">
				<?php
				$selecttedTypes = explode( '|' , Helper::getOption('allowed_post_types' , 'post|page|attachment|product') );
				foreach( get_post_types() AS $post_type )
				{
					print '<option value="' . htmlspecialchars($post_type) . '"' . (in_array( $post_type , $selecttedTypes ) ? ' selected' : '') . '>' . htmlspecialchars($post_type) . '</option>';
				}
				?>
			</select>
		</div>
	</div>
</div>

<div style="width: 70%;">
	<div class="fs_setting_item">
		<div class="fs_setting_item_label">
			<div><?=esc_html__('Hide FS Poster for:' , 'fs-poster')?></div>
			<div class="fs_s_help"><?=esc_html__('If you want to hide the FS Poster plugin for some user roles, then this option is for you. You can select multiple user roles to hide the FS Poster menu for them.' , 'fs-poster')?></div>
		</div>
		<div class="fs_s_input" style="width: 300px;">
			<select class="ws_form_element select2-init" id="fs_hide_for_roles" name="fs_hide_for_roles[]" style="width: 300px;" multiple onautocomplete="off">
				<?php
				$hideForRoles = explode('|' , Helper::getOption('hide_menu_for' , ''));
				$wp_roles = get_editable_roles();
				foreach( $wp_roles AS $roleId => $roleInf )
				{
					if( $roleId == 'administrator' )
						continue;

					print '<option value="' . htmlspecialchars($roleId) . '"' . (in_array( $roleId , $hideForRoles ) ? ' selected' : '') . '>' . htmlspecialchars($roleInf['name']) . '</option>';
				}
				?>
			</select>
		</div>
	</div>
</div>

<div class="fs_setting_item" style="width: 50%;">
	<div class="fs_setting_item_label">
		<div><?=esc_html__('Collect FS Poster statistics:' , 'fs-poster')?></div>
		<div class="fs_s_help"><?=esc_html__('Disabling the option prevents you from getting statistics in the Insights menu. By enabling, "feed_id" parameter appends automatically each time you share a post link.' , 'fs-poster')?></div>
	</div>
	<div class="fs_s_input">
		<div class="fs_onoffswitch">
			<input type="checkbox" onautocomplete="off" name="fs_collect_statistics" class="fs_onoffswitch-checkbox" id="fs_collect_statistics"<?=Helper::getOption('collect_statistics', '1')?' checked':''?>>
			<label class="fs_onoffswitch-label" for="fs_collect_statistics"></label>
		</div>
	</div>
</div>

<div class="fs_setting_item">
	<div class="fs_setting_item_label" style="width: 50%;">
		<div><?=esc_html__('Cron Job settings:' , 'fs-poster')?></div>
		<div class="fs_s_help"><?=__('You can follow this <a href="https://www.fs-poster.com/documentation/how-to-set-up-a-cron-job-on-wordpress-fs-poster-wp-plugin" target="_blank">documentation</a> and configure your WordPress Cron Jobs.' , 'fs-poster')?></div>
	</div>
	<div class="fs_s_input" style="width: 50%; display: block;">
		<?php
		$lastCronTaskRunnedOn = Helper::getOption('cron_job_runned_on', 0);

		if( empty( $lastCronTaskRunnedOn ) )
		{
			print '<span style="color: #ff7784;">Not runned! Your schedule posts and background share may be not work! Please follow this <a href="https://www.fs-poster.com/documentation/how-to-set-up-a-cron-job-on-wordpress-fs-poster-wp-plugin" target="_blank">documentation</a> and configure your WordPress Cron Jobs.</span>';
		}
		else
		{
			$calcTime = Date::epoch() - $lastCronTaskRunnedOn;
			$calcTime = (int)($calcTime / 60);
			$calcTime = $calcTime > 0 ? $calcTime : 1;
			print Date::dateTime( $lastCronTaskRunnedOn ) . ' ( ' . $calcTime . ' minute(s) ago )';

			if( $calcTime > 3 )
			{
				print '<div style="color: #ff7784;">It looks like your Cron job runs with delay. Therefore your Scheduled posts and background shares may post with delay!</div>';
			}
		}
		?>
	</div>
</div>

<div class="fs_warning_box" style="margin: 0;">
	Cron command is:<br>
	<b style="color: #289be5;">wget -O /dev/null <?php print site_url()?>/wp-cron.php?doing_wp_cron > /dev/null 2>&1</b>
</div>

<script>
	var fadeSpeed = 0;
	jQuery(document).ready(function()
	{
		if( typeof $ == 'undefined' ) var $ = typeof jQuery == 'undefined' ? null : jQuery;

		$("#save_btn").click(function()
		{
			var data = fsCode.serialize($(".settings_form"));

			fsCode.ajax('settings_general_save' , data , function(result)
			{
				fsCode.toast("<?=esc_html__('Save successful!' , 'fs-poster')?>" , 'success');
			});
		});

		fadeSpeed = 200;

		$(".select2-init").select2();

		$("[data-open-img]").click(function ()
		{
			var imgSrc = $(this).data('open-img');

			fsCode.modal('<div><img src="'+imgSrc+'" style="width: 100%;"></div><div style="border-top: 1px solid #999; text-align: center; padding: 10px; margin-top: 9px;"><button type="button" data-modal-close="true" class="ws_btn ws_bg_danger">Close modal</button></div>' , {'width': '60%'});
		});
	});
</script>