<?php if ( ! defined( 'ABSPATH' ) ) {
	exit;} ?>

<div class="mt-2 content-header dfy-rules-header">
	<div class="title mr-1"><?php esc_html_e( 'Discount Rules', 'discountify' ); ?></div>
	<div class="dfy-header-actions">
		<a href="<?php echo esc_url( admin_url() . 'admin.php?page=discountify-rules&rule=new-rule' ); ?>" target="_self" class="dfy-btn dfy-btn-add">
			<span class="dashicons dashicons-plus-alt2"></span>
			<?php esc_html_e( 'Add Discount Rules', 'discountify' ); ?>
		</a>
		<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=discountify-rules&discountify_action=export_rules' ), 'discountify_export_rules' ) ); ?>" class="dfy-btn dfy-btn-export">
				<span class="dashicons dashicons-database-export"></span>
				<?php esc_html_e( 'Export Rules', 'discountify' ); ?>
			</a>
			<button type="button" class="dfy-btn dfy-btn-import" id="dfy-import-toggle">
				<span class="dashicons dashicons-database-import"></span>
				<?php esc_html_e( 'Import Rules', 'discountify' ); ?>
			</button>
	</div>
</div>

<div class="dfy-import-panel" id="dfy-import-panel" style="display:none;">
	<form method="post" enctype="multipart/form-data" class="dfy-import-form">
		<input type="hidden" name="discountify_action" value="import_rules" />
		<?php wp_nonce_field( 'discountify_import_rules' ); ?>
		<div class="dfy-import-inner">
			<div class="dfy-import-icon">
				<span class="dashicons dashicons-upload"></span>
			</div>
			<div class="dfy-import-content">
				<p class="dfy-import-title"><?php esc_html_e( 'Import Discount Rules', 'discountify' ); ?></p>
				<p class="dfy-import-desc"><?php esc_html_e( 'Upload a .json file previously exported from Discountify. Rules will be added alongside existing ones.', 'discountify' ); ?></p>
			</div>
			<div class="dfy-import-actions">
				<input type="file" name="discountify_import_file" id="discountify_import_file" accept=".json" class="dfy-import-file" />
				<button type="submit" class="dfy-btn dfy-btn-add dfy-btn-sm">
					<span class="dashicons dashicons-yes-alt"></span>
					<?php esc_html_e( 'Upload & Import', 'discountify' ); ?>
				</button>
			</div>
		</div>
	</form>
</div>
<script>
(function(){
	var toggle = document.getElementById('dfy-import-toggle');
	var panel = document.getElementById('dfy-import-panel');
	if(toggle && panel){
		toggle.addEventListener('click', function(){
			if(panel.style.display === 'none'){
				panel.style.display = 'block';
				toggle.classList.add('dfy-btn-active');
			} else {
				panel.style.display = 'none';
				toggle.classList.remove('dfy-btn-active');
			}
		});
	}
})();
</script>

<div class="view-discount-rules" style="margin-top:16px;">
<?php
	/**
	 * Shows Rules
	 */
	discountify_all_rules();
function discountify_all_rules() {
	$columns = array(
		'cb'              => '<input name="bulk-delete[]" type="checkbox" />',
		'discount_name'   => esc_html__( 'Discount Name', 'discountify' ),
		'rules_type_name' => esc_html__( 'Rules Type', 'discountify' ),
		'discount_in'     => esc_html__( 'Discount In', 'discountify' ),
		'enable_rules'    => esc_html__( 'Rules Status', 'discountify' ),
		'discount_valid'  => esc_html__( 'Validation Period', 'discountify' ),
	);

	$rules_list = array(
		'singular_name' => esc_html__( 'All Discount Rule', 'discountify' ),
		'plural_name'   => esc_html__( 'All Discount Rules', 'discountify' ),
		'columns'       => $columns,
	);

	?>
			<div class="report-list">
				<form method="POST">
				<?php
					$table = new \Discountify\Core\Admin\Settings\Table( $rules_list );
					$table->preparing_items();
					$table->display();
				?>
				</form>
			</div>
		<?php
}
?>
</div>
