<?php

namespace FSPoster\App\view;

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

defined('MODAL') or exit();
?>

<style>
	.select2-search__field::placeholder
	{
		color: #999;
		font-weight: 600;
		font-size: 14px;
		line-height: 21px;
	}

	.btn_select > div
	{
		width: 139px;
		text-align: center;
		height: 90px;
		background: #EEE;
		color: #999;
		font-width: 600 !important;;
		font-size: 14px !important;
		padding: 5px;
		cursor: pointer;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
	}

	.btn_select > .selected
	{
		background: #fdcb6e;
		color: #FFF;
	}

	.btn_select
	{
		display: flex;
		justify-content: space-between;
		width: 300px;
		margin-top: 10px;
	}

	.select2-selection__rendered
	{
		white-space: normal !important;
	}

	.select2-results__option[aria-selected=true]
	{
		display: none;
	}
</style>
<div style="width: 100%; height: 100%; display: flex; align-items: center; flex-direction: column; justify-content: center; position: absolute;">
	<div style="margin-bottom: 20px; text-align: center;font-size: 17px;color: #888;font-weight: 600;">
		<?php print esc_html__('Set conditions for the account', 'fs-poster')?>
	</div>

	<div style="width: 300px;">
		<select class="ws_form_element select2-init categories_filter" style="width: 300px;" multiple>
			<?php
			$taxonomies		= get_terms( [ 'category' ], [ 'hide_empty' => false ] );

			print '<optgroup label="Post categories">';
			foreach( $taxonomies AS $categ )
			{
				print '<option value="' . htmlspecialchars($categ->term_id) . '"' . (in_array((string)$categ->term_id, $parameters['categories']) ? ' selected' : '') . '>' . htmlspecialchars($categ->name) . '</option>';
			}
			print '</optgroup>';

			if( taxonomy_exists('product_cat') )
			{
				$productTaxs	= get_terms( [ 'product_cat' ], [ 'hide_empty' => false ] );

				print '<optgroup label="Product categories">';
				foreach( $productTaxs AS $categ )
				{
					print '<option value="' . htmlspecialchars($categ->term_id) . '"' . (in_array((string)$categ->term_id, $parameters['categories']) ? ' selected' : '') . '>' . htmlspecialchars($categ->name) . '</option>';
				}
				print '</optgroup>';
			}

			$otherTaxonomies = get_terms( [ 'hide_empty' => false, 'orderby' => 'taxonomy' ] );
			print '<optgroup label="Custom categories"></optgroup>';

			$saveLastTaxonomyName = false;
			foreach( $otherTaxonomies AS $categ )
			{
				if( in_array( $categ->taxonomy, ['category', 'product_cat'] ) )
					continue;

				if( $saveLastTaxonomyName !== $categ->taxonomy )
				{
					print '</optgroup><optgroup label="' . htmlspecialchars( $categ->taxonomy ) . '">';
					$saveLastTaxonomyName = $categ->taxonomy;
				}

				print '<option value="' . htmlspecialchars($categ->term_id) . '"' . (in_array((string)$categ->term_id, $parameters['categories']) ? ' selected' : '') . '>' . htmlspecialchars($categ->name) . '</option>';
			}
			print $saveLastTaxonomyName ? '</optgroup>' : '';

			?>
		</select>
	</div>

	<div class="btn_select filter_type">
		<div<?php print $parameters['filter_type'] == 'in' ? ' class="selected"' : ''?> data-name="in">
			<div><i class="far fa-check-circle"></i></div>
			<div>Share only the posts of the selected categories, tags, etc...</div>
		</div>
		<div<?php print $parameters['filter_type'] == 'ex' ? ' class="selected"' : ''?> data-name="ex">
			<div><i class="fa fa-ban"></i></div>
			<div>Do not share the posts of the selected categories, tags, etc...</div>
		</div>
	</div>

	<div style="margin: 15px; width: 250px; text-align: center;"><button type="button" class="ws_btn ws_bg_danger save_btn" style="width: 130px;"><?=esc_html__('ACTIVATE', 'fs-poster')?></button> </div>
</div>

<span class="close" data-modal-close="true">&times;</span>

<script>

	jQuery(document).ready(function ()
	{
		if( typeof $ == 'undefined' ) var $ = typeof jQuery == 'undefined' ? null : jQuery;
		$("#proModal<?=$mn?> .save_btn").click(function()
		{
			var cats = $("#proModal<?=$mn?> .categories_filter").val(),
				filter_type = $("#proModal<?=$mn?> .filter_type > .selected").attr('data-name');

			if( cats == '' )
			{
				cats = $("#proModal<?=$mn?> .categories_filter").select2('val');
			}

			fsCode.ajax( '<?=$parameters['ajaxUrl']?>' , {'id': '<?=(int)$parameters['id']?>', 'checked': 1 , 'categories': cats, 'filter_type': filter_type}, function()
			{
				$("tr[data-id=\"<?=(int)$parameters['id']?>\"][data-type=\"<?=($parameters['ajaxUrl'] == 'account_activity_change' ? 'account' : 'community')?>\"] .fs_account_checkbox")
					.removeClass('fs_account_checked')
					.addClass('fs_account_checked2');

				fsCode.modalHide( $("#proModal<?=$mn?>") );
			});
		});

		$("#proModal<?=$mn?> .filter_type > div").click(function ()
		{
			$("#proModal<?=$mn?> .filter_type > .selected").removeClass('selected');
			$(this).addClass('selected');
		});

		$("#proModal<?=$mn?> .select2-init").select2({
			'placeholder': 'Select...'
		});
	});

</script>