<?php
/**
 * Admin view — main page.
 *
 * Variables available:
 *   $rules      array    All sort rules.
 *   $categories WP_Term[] Product categories.
 *
 * @package Smart_Product_Sort
 */

defined( 'ABSPATH' ) || exit;

$sort_options = SPSORT_Sort_Rules::get_sort_options();
?>
<div class="wrap spsort-wrap">

	<div id="spsort-notice" class="spsort-notice" style="display:none;"></div>

	<!-- Card container -->
	<div class="spsort-card">

		<!-- Card header -->
		<div class="spsort-card-header">
			<div class="spsort-card-header-left">
				<div class="spsort-logo-icon">
					<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
						<line x1="4" y1="7" x2="20" y2="7"></line>
						<line x1="4" y1="12" x2="16" y2="12"></line>
						<line x1="4" y1="17" x2="12" y2="17"></line>
					</svg>
				</div>
				<h1 class="spsort-card-title"><?php esc_html_e( 'Smart Product Sort', 'smart-product-sort' ); ?></h1>
			</div>
			<button type="button" class="spsort-btn spsort-btn-primary" id="spsort-add-rule">
				<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
					<line x1="12" y1="5" x2="12" y2="19"></line>
					<line x1="5" y1="12" x2="19" y2="12"></line>
				</svg>
				<?php esc_html_e( 'Add Sort Rule', 'smart-product-sort' ); ?>
			</button>
		</div>

		<!-- Rules table -->
		<div class="spsort-table-wrap">
			<table class="spsort-table">
				<thead>
					<tr>
						<th class="spsort-col-name"><?php esc_html_e( 'Rule Name', 'smart-product-sort' ); ?></th>
						<th class="spsort-col-sort"><?php esc_html_e( 'Sort By', 'smart-product-sort' ); ?></th>
						<th class="spsort-col-order"><?php esc_html_e( 'Order', 'smart-product-sort' ); ?></th>
						<th class="spsort-col-cat"><?php esc_html_e( 'Category', 'smart-product-sort' ); ?></th>
						<th class="spsort-col-prio"><?php esc_html_e( 'Priority', 'smart-product-sort' ); ?></th>
						<th class="spsort-col-status"><?php esc_html_e( 'Status', 'smart-product-sort' ); ?></th>
						<th class="spsort-col-actions"><?php esc_html_e( 'Actions', 'smart-product-sort' ); ?></th>
					</tr>
				</thead>
				<tbody id="spsort-rules-body">
				<?php if ( ! empty( $rules ) ) : ?>
					<?php foreach ( $rules as $rule ) : ?>
					<tr data-id="<?php echo esc_attr( $rule->id ); ?>">
						<td class="spsort-col-name">
							<strong><?php echo esc_html( $rule->rule_name ); ?></strong>
						</td>
						<td class="spsort-col-sort">
							<span class="spsort-sort-badge">
								<?php echo esc_html( isset( $sort_options[ $rule->sort_by ] ) ? $sort_options[ $rule->sort_by ] : $rule->sort_by ); ?>
							</span>
						</td>
						<td class="spsort-col-order">
							<span class="spsort-order-badge spsort-order-<?php echo esc_attr( strtolower( $rule->sort_order ) ); ?>">
								<?php if ( 'ASC' === $rule->sort_order ) : ?>
									<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>
								<?php else : ?>
									<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>
								<?php endif; ?>
								<?php echo esc_html( $rule->sort_order ); ?>
							</span>
						</td>
						<td class="spsort-col-cat">
							<?php
							if ( $rule->category_id ) {
								$rule_term = get_term( (int) $rule->category_id, 'product_cat' );
								echo esc_html( ( $rule_term && ! is_wp_error( $rule_term ) ) ? $rule_term->name : '—' );
							} else {
								echo '<span class="spsort-global-tag">' . esc_html__( 'Global', 'smart-product-sort' ) . '</span>';
							}
							?>
						</td>
						<td class="spsort-col-prio">
							<span class="spsort-priority-num"><?php echo esc_html( $rule->priority ); ?></span>
						</td>
						<td class="spsort-col-status">
							<button type="button"
								class="spsort-toggle-rule <?php echo esc_attr( $rule->is_active ? 'spsort-active' : 'spsort-inactive' ); ?>"
								data-id="<?php echo esc_attr( $rule->id ); ?>"
								aria-label="<?php esc_attr_e( 'Toggle rule status', 'smart-product-sort' ); ?>">
								<span class="spsort-status-dot"></span>
								<?php echo $rule->is_active ? esc_html__( 'Active', 'smart-product-sort' ) : esc_html__( 'Inactive', 'smart-product-sort' ); ?>
							</button>
						</td>
						<td class="spsort-col-actions">
							<div class="spsort-action-btns">
								<button type="button" class="spsort-btn-icon spsort-edit-rule"
									data-id="<?php echo esc_attr( $rule->id ); ?>"
									data-rule="<?php echo esc_attr( wp_json_encode( $rule ) ); ?>"
									title="<?php esc_attr_e( 'Edit', 'smart-product-sort' ); ?>">
									<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
										<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"></path>
										<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"></path>
									</svg>
								</button>
								<?php if ( function_exists( 'spsort_fs' ) && spsort_fs()->can_use_premium_code__premium_only() ) : ?>
								<button type="button" class="spsort-btn-icon spsort-duplicate-rule"
									data-id="<?php echo esc_attr( $rule->id ); ?>"
									title="<?php esc_attr_e( 'Duplicate', 'smart-product-sort' ); ?>">
									<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
										<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
										<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
									</svg>
								</button>
								<?php endif; ?>
								<button type="button" class="spsort-btn-icon spsort-btn-icon-danger spsort-delete-rule"
									data-id="<?php echo esc_attr( $rule->id ); ?>"
									title="<?php esc_attr_e( 'Delete', 'smart-product-sort' ); ?>">
									<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
										<polyline points="3 6 5 6 21 6"></polyline>
										<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path>
									</svg>
								</button>
							</div>
						</td>
					</tr>
					<?php endforeach; ?>
				<?php else : ?>
					<tr id="spsort-no-rules">
						<td colspan="7">
							<div class="spsort-empty-state">
								<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
									<rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
									<line x1="12" y1="8" x2="12" y2="16"></line>
									<line x1="8" y1="12" x2="16" y2="12"></line>
								</svg>
								<p><?php esc_html_e( 'No sort rules yet', 'smart-product-sort' ); ?></p>
								<span><?php esc_html_e( 'Click "Add Sort Rule" to create your first rule.', 'smart-product-sort' ); ?></span>
							</div>
						</td>
					</tr>
				<?php endif; ?>
				</tbody>
			</table>
		</div>

		<?php if ( function_exists( 'spsort_fs' ) && spsort_fs()->can_use_premium_code__premium_only() ) : ?>
		<!-- Pro: Import/Export toolbar -->
		<div class="spsort-pro-toolbar">
			<button type="button" class="spsort-btn-icon" id="spsort-export-rules" title="<?php esc_attr_e( 'Export Rules', 'smart-product-sort' ); ?>">
				<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
					<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
					<polyline points="7 10 12 15 17 10"></polyline>
					<line x1="12" y1="15" x2="12" y2="3"></line>
				</svg>
				<?php esc_html_e( 'Export', 'smart-product-sort' ); ?>
			</button>
			<button type="button" class="spsort-btn-icon" id="spsort-import-rules" title="<?php esc_attr_e( 'Import Rules', 'smart-product-sort' ); ?>">
				<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
					<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
					<polyline points="17 8 12 3 7 8"></polyline>
					<line x1="12" y1="3" x2="12" y2="15"></line>
				</svg>
				<?php esc_html_e( 'Import', 'smart-product-sort' ); ?>
			</button>
			<input type="file" id="spsort-import-file" accept=".json">
		</div>
		<?php elseif ( function_exists( 'spsort_fs' ) && spsort_fs()->is_not_paying() ) : ?>
		<!-- Free: Upsell teaser -->
		<div class="spsort-pro-teaser">
			<p><?php esc_html_e( 'Unlock stock sorting, custom fields, rule duplication, import/export, and more.', 'smart-product-sort' ); ?></p>
			<a href="<?php echo esc_url( spsort_fs()->get_upgrade_url() ); ?>" class="spsort-btn spsort-btn-primary">
				<?php esc_html_e( 'Upgrade to Pro', 'smart-product-sort' ); ?>
			</a>
		</div>
		<?php endif; ?>

	</div><!-- .spsort-card -->

	<!-- Add / Edit Modal -->
	<div id="spsort-modal-overlay" style="display:none;">
		<div id="spsort-modal">
			<div class="spsort-modal-header">
				<h2 id="spsort-modal-title"><?php esc_html_e( 'Add Sort Rule', 'smart-product-sort' ); ?></h2>
				<button type="button" class="spsort-modal-close" id="spsort-cancel-btn" aria-label="<?php esc_attr_e( 'Close', 'smart-product-sort' ); ?>">
					<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
						<line x1="18" y1="6" x2="6" y2="18"></line>
						<line x1="6" y1="6" x2="18" y2="18"></line>
					</svg>
				</button>
			</div>

			<form id="spsort-rule-form" novalidate>
				<input type="hidden" id="spsort-rule-id" name="rule_id" value="">

				<div class="spsort-form-body">
					<div class="spsort-field">
						<label for="spsort-rule-name" class="spsort-label">
							<?php esc_html_e( 'Rule Name', 'smart-product-sort' ); ?>
							<span class="spsort-required">*</span>
						</label>
						<input type="text" id="spsort-rule-name" name="rule_name" class="spsort-input" placeholder="<?php esc_attr_e( 'e.g. Premium Products First', 'smart-product-sort' ); ?>" required>
					</div>

					<div class="spsort-field-row">
						<div class="spsort-field">
							<label for="spsort-sort-by" class="spsort-label"><?php esc_html_e( 'Sort By', 'smart-product-sort' ); ?></label>
							<select id="spsort-sort-by" name="sort_by" class="spsort-select">
								<?php foreach ( SPSORT_Sort_Rules::get_sort_options() as $key => $label ) : ?>
									<option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $label ); ?></option>
								<?php endforeach; ?>
							</select>
						</div>
						<div class="spsort-field">
							<label for="spsort-sort-order" class="spsort-label"><?php esc_html_e( 'Sort Order', 'smart-product-sort' ); ?></label>
							<select id="spsort-sort-order" name="sort_order" class="spsort-select">
								<option value="DESC"><?php esc_html_e( 'High to Low (Z-A, Newest, Highest)', 'smart-product-sort' ); ?></option>
								<option value="ASC"><?php esc_html_e( 'Low to High (A-Z, Oldest, Lowest)', 'smart-product-sort' ); ?></option>
							</select>
						</div>
					</div>

					<div class="spsort-field-row">
						<div class="spsort-field">
							<label for="spsort-category" class="spsort-label"><?php esc_html_e( 'Category', 'smart-product-sort' ); ?></label>
							<select id="spsort-category" name="category_id" class="spsort-select">
								<option value=""><?php esc_html_e( 'All Products (Global)', 'smart-product-sort' ); ?></option>
								<?php if ( ! is_wp_error( $categories ) ) : ?>
									<?php foreach ( $categories as $category ) : ?>
										<option value="<?php echo esc_attr( $category->term_id ); ?>">
											<?php echo esc_html( $category->name ); ?>
										</option>
									<?php endforeach; ?>
								<?php endif; ?>
							</select>
							<span class="spsort-hint"><?php esc_html_e( 'Category rules override global rules.', 'smart-product-sort' ); ?></span>
						</div>
						<div class="spsort-field">
							<label for="spsort-priority" class="spsort-label"><?php esc_html_e( 'Priority', 'smart-product-sort' ); ?></label>
							<input type="number" id="spsort-priority" name="priority" value="10" min="0" max="999" class="spsort-input">
							<span class="spsort-hint"><?php esc_html_e( 'Lower = higher priority. Default: 10.', 'smart-product-sort' ); ?></span>
						</div>
					</div>

					<div class="spsort-field spsort-field-toggle">
						<label class="spsort-toggle-label">
							<input type="checkbox" id="spsort-is-active" name="is_active" value="1" checked>
							<span class="spsort-toggle-switch"></span>
							<span class="spsort-toggle-text"><?php esc_html_e( 'Active', 'smart-product-sort' ); ?></span>
						</label>
					</div>
				</div>

				<div class="spsort-modal-footer">
					<button type="button" class="spsort-btn spsort-btn-ghost" id="spsort-cancel-btn-footer">
						<?php esc_html_e( 'Cancel', 'smart-product-sort' ); ?>
					</button>
					<button type="submit" class="spsort-btn spsort-btn-primary" id="spsort-save-btn">
						<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
							<polyline points="20 6 9 17 4 12"></polyline>
						</svg>
						<?php esc_html_e( 'Save Rule', 'smart-product-sort' ); ?>
					</button>
				</div>
			</form>
		</div>
	</div>

</div><!-- .spsort-wrap -->
