<?php

namespace DropshippingExportFreeVendor;

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportMapperViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportOptionsViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportStatusViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportManagerViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\ExportManagerForm;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\View\ExportTypeViewAction;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Entity\Export;
use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Action\Loader\Plugin\PluginLinksLoaderAction;
/**
 * @var string $title
 * @var string $import_url
 * @var string $header_url
 * @var WPDesk\View\Renderer\Renderer $renderer
 * @var \Core\Infrastructure\View\FormView $form
 * @var \Core\Helper\PluginHelper $plugin_helper
 * @var Export[] $exports
 * @var string $nonce
 * @var string $elements number of all import items
 */
$is_pl_lang = PluginLinksLoaderAction::is_pl_lang();
$display_export_status = function (Export $export) {
    $status = $class = $error_message = '';
    // phpcs:ignore
    switch ($export->get_status()) {
        case Export::STATUS_WAITING:
            $class = 'color-success';
            $status = \__('Synchronization active', 'dropshipping-export-free');
            break;
        case Export::STATUS_STOPPED:
            $class = 'color-error';
            $status = \__('Synchronization stopped', 'dropshipping-export-free');
            break;
        case Export::STATUS_ERROR:
            $class = 'color-error';
            $status = \__('Synchronization error', 'dropshipping-export-free');
            $error_message = '<p class="' . $class . '">' . $export->get_error_message() . '</p>';
            break;
        default:
            $class = 'color-success';
            $status = \__('Synchronization in progress', 'dropshipping-export-free');
            break;
    }
    return \str_replace(['{class}', '{status}', '{error}'], [$class, $status, $error_message], '<span class="{class}"><strong>{status}</strong>{error}</span>');
};
$display_action_url = function (Export $export) use ($plugin_helper, $nonce) {
    switch ($export->get_status()) {
        case Export::STATUS_STOPPED:
        case Export::STATUS_ERROR:
            $anchor = \__('Activate synchronization', 'dropshipping-export-free');
            $url = $plugin_helper->generate_url_by_view(ExportManagerViewAction::class, ['activate' => $export->get_id(), 'nonce' => $nonce]);
            break;
        default:
            $anchor = \__('Stop synchronization', 'dropshipping-export-free');
            $url = $plugin_helper->generate_url_by_view(ExportManagerViewAction::class, ['stop' => $export->get_id(), 'nonce' => $nonce]);
            break;
    }
    return \str_replace(['{url}', '{anchor}'], [$url, $anchor], '	<a href="{url}">{anchor}</a>');
};
$renderer->output_render('Header', ['title' => $title, 'header_url' => $header_url]);
$docs_url = $is_pl_lang ? 'https://wpdesk.link/export-docs-manager-pl' : 'https://wpdesk.link/export-docs-manager';
$faq_url = $is_pl_lang ? 'https://wpdesk.link/export-docs-faq-pl' : 'https://wpdesk.link/export-docs-faq';
?>

<p><?php 
echo \wp_kses_post(\sprintf(\__('<a href="%s" target="_blank">Docs of Export Manager</a>', 'dropshipping-export-free'), $docs_url));
?></p>
<p><?php 
echo \wp_kses_post(\sprintf(\__('Have you encountered any problems with the export, or do you want to know more? <a href="%s" target="_blank">Visit FAQ →</a>.', 'dropshipping-export-free'), $faq_url));
?></p>


<?php 
$form->form_start();
?>
<div class="tablenav top">
	<div class="alignleft actions bulkactions">
		<?php 
$form->show_field('manage');
?>
		<?php 
$form->show_field('apply');
?>
	</div>
	<div class="tablenav-pages one-page">
		<span class="displaying-num"><?php 
echo \esc_html($elements);
?></span>
		<br class="clear">
	</div>
	<table class="wp-list-table widefat fixed odd-blue posts">
		<thead>
			<tr>
				<td id="cb" class="manage-column column-cb check-column">
					<label class="screen-reader-text" for="cb-select-all-1"><?php 
echo \esc_html(\__('Select all', 'dropshipping-export-free'));
?></label>
					<input id="cb-select-all-1" type="checkbox">
				</td>
				<th scope="col" id="id" class="manage-column column-id desc">
					<span><?php 
echo \esc_html(\__('Export name', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="schedule" class="manage-column column-schedule">
					<span><?php 
echo \esc_html(\__('Cron schedule', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="actions" class="manage-column column-actions">
					<span><?php 
echo \esc_html(\__('Actions', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="export-status" class="manage-column column-export-status">
					<span><?php 
echo \esc_html(\__('Status', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="export-stats" class="manage-column column-export-status">
					<span><?php 
echo \esc_html(\__('Export statistics', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="date" class="manage-column column-date asc">
					<span><?php 
echo \esc_html(\__('Created', 'dropshipping-export-free'));
?></span>
				</th>
			</tr>
		</thead>

		<tbody id="the-list">
			<?php 
if (empty($exports)) {
    ?>
				<tr>
					<td colspan="7">
						<span>
						<?php 
    /* TRANSLATORS: %s: http url to export page */
    echo \wp_kses_post(\sprintf(\__('Exports list is empty, <a href="%s" target="_self">please create your first export.</a>', 'dropshipping-export-free'), \esc_url($import_url)));
    ?>
						</span>
					</td>
				</tr>
			<?php 
} else {
    ?>

				<?php 
    foreach ($exports as $export_file) {
        ?>
					<tr id="post-<?php 
        echo \esc_html($export_file->get_id());
        ?>" class="iedit level-0 status-publish hentry">
						<th scope="row" class="check-column">
							<input id="cb-select-<?php 
        echo \esc_html($export_file->get_id());
        ?>" type="checkbox" name="<?php 
        echo \esc_html(ExportManagerForm::ID);
        ?>[id][]" value="<?php 
        echo \esc_html($export_file->get_id());
        ?>">
						</th>
						<td class="id column-id has-row-actions column-primary">
							<?php 
        if (!empty($export_file->get_export_name())) {
            echo '<strong><span class="row-file-name">' . \esc_html($export_file->get_export_name()) . '</span></strong><br>';
            echo '<small><span class="row-file">' . \esc_url($export_file->get_url()) . '</span></small>';
        } else {
            echo '<strong><span class="row-file">' . \esc_url($export_file->get_url()) . '</span></strong>';
        }
        ?>
							<div class="row-actions">
							<span class="edit">
									<a href="
									<?php 
        echo \esc_url($plugin_helper->generate_url_by_view(ExportTypeViewAction::class, ['uid' => $export_file->get_uid(), 'mode' => 'edit_all']));
        ?>
									"><?php 
        echo \esc_html(\__('Edit', 'dropshipping-export-free'));
        ?></a> |
								</span>
								<span class="edit">
									<a href="
									<?php 
        echo \esc_url($plugin_helper->generate_url_by_view(ExportMapperViewAction::class, ['uid' => $export_file->get_uid(), 'mode' => 'edit']));
        ?>
												"><?php 
        echo \esc_html(\__('Edit mapper', 'dropshipping-export-free'));
        ?></a> |
								</span>
								<span class="edit">
									<a href="
									<?php 
        echo \esc_url($plugin_helper->generate_url_by_view(ExportOptionsViewAction::class, ['uid' => $export_file->get_uid(), 'mode' => 'edit']));
        ?>
												"><?php 
        echo \esc_html(\__('Edit options', 'dropshipping-export-free'));
        ?></a> |
								</span>
								<span class="edit">
									<a href="
									<?php 
        echo \esc_url($plugin_helper->generate_url_by_view(ExportManagerViewAction::class, ['clone' => $export_file->get_uid(), 'nonce' => $nonce]));
        ?>
												"><?php 
        echo \esc_html(\__('Clone', 'dropshipping-export-free'));
        ?></a> |
								</span>
								<?php 
        $file_url = $file_locator->get_url_to_file($export_file->get_uid());
        if (!empty($file_url)) {
            ?>
								<span class="edit">
									<a href="
									<?php 
            echo \esc_url($file_url);
            ?>
												"><?php 
            echo \esc_html(\__('File url', 'dropshipping-export-free'));
            ?></a> |
								</span>
								<?php 
        }
        ?>
								<?php 
        $zip_file_url = $file_locator->get_url_to_zip_file($export_file->get_uid());
        if (!empty($zip_file_url)) {
            ?>
								<span class="edit">
									<a href="
									<?php 
            echo \esc_url($zip_file_url);
            ?>
												"><?php 
            echo \esc_html(\__('ZIP images', 'dropshipping-export-free'));
            ?></a> |
								</span>
								<?php 
        }
        ?>								
								<span class="trash">
									<a href="
									<?php 
        echo \esc_url($plugin_helper->generate_url_by_view(ExportManagerViewAction::class, ['delete' => $export_file->get_id(), 'nonce' => $nonce]));
        ?>
												"><?php 
        echo \esc_html(\__('Delete', 'dropshipping-export-free'));
        ?></a> |
								</span>
							</div>
						</td>
						<td class="id column-id">
							<span title="Cron Schedule"><?php 
        echo \esc_html($export_file->get_cron_schedule());
        ?></span>
						</td>
						<td class="id column-id">
							<ul>
								<li>
									<strong>
										<a href="<?php 
        echo \esc_url($plugin_helper->generate_url_by_view(ExportStatusViewAction::class, ['uid' => $export_file->get_uid()]));
        ?>"><?php 
        echo \esc_html(\__('Export now', 'dropshipping-export-free'));
        ?></a>
									</strong>
								</li>
								<li>
									<strong>
										<?php 
        echo \wp_kses_post($display_action_url($export_file));
        ?>
									</strong>
								</li>
							</ul>
						</td>
						<td class="id column-id">
							<ul>
								<li>
									<?php 
        echo \wp_kses_post($display_export_status($export_file));
        ?>
								</li>
							</ul>
						</td>
						<td class="id column-id">
							<ul>
								<li>
									<?php 
        echo \esc_html(\__('Exported', 'dropshipping-export-free'));
        ?>: <strong><?php 
        echo \esc_html($export_file->get_created());
        ?></strong>
								</li>
							</ul>
						</td>
						<td class="date column-date"><span title="<?php 
        echo \esc_html($export_file->get_date_created());
        ?>"><?php 
        echo \esc_html($export_file->get_date_created());
        ?></span></td>
					</tr>
				<?php 
    }
    ?>
			<?php 
}
?>

		</tbody>
		<tfoot>
			<tr>
				<td id="cb2" class="manage-column column-cb check-column">
					<label class="screen-reader-text" for="cb-select-all-1"><?php 
echo \esc_html(\__('Select all', 'dropshipping-export-free'));
?></label>
					<input id="cb-select-all-1" type="checkbox">
				</td>
				<th scope="col" id="id2" class="manage-column column-id desc">
					<span><?php 
echo \esc_html(\__('Export name', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="schedule2" class="manage-column column-schedule">
					<span><?php 
echo \esc_html(\__('Cron schedule', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="actions2" class="manage-column column-actions">
					<span><?php 
echo \esc_html(\__('Actions', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="export-status2" class="manage-column column-export-status">
					<span><?php 
echo \esc_html(\__('Status', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="export-stats2" class="manage-column column-export-status">
					<span><?php 
echo \esc_html(\__('Export statistics', 'dropshipping-export-free'));
?></span>
				</th>
				<th scope="col" id="date2" class="manage-column column-date asc">
					<span><?php 
echo \esc_html(\__('Created', 'dropshipping-export-free'));
?></span>
				</th>
			</tr>
		</tfoot>
	</table>

	<div class="tablenav bottom">
		<div class="alignleft actions bulkactions">
			<?php 
$form->show_field('manage2');
?>
			<?php 
$form->show_field('apply2');
?>
		</div>
		<div class="alignleft actions"></div>
		<div class="tablenav-pages one-page"><span class="displaying-num"><?php 
echo \esc_html($elements);
?></span><br class="clear"></div>
	</div>
	<?php 
$form->form_fields_complete();
$form->form_end();
$renderer->output_render('Footer');
