<?php defined( 'ABSPATH' ) || exit; ?>
<div class="wrap wcp-admin-wrap">
	<h1>
		<img src="<?php echo esc_url( WOOCOM_PAY_PLUGIN_URL . 'assets/logo.PNG' ); ?>" alt="Woocom-Pay">
		<?php esc_html_e( 'UPI Payment Verification', 'woocom-pay' ); ?>
		<a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="wcp-home-btn" title="<?php esc_attr_e( 'Go to store home', 'woocom-pay' ); ?>">
			<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></svg>
			<?php esc_html_e( 'Home', 'woocom-pay' ); ?>
		</a>
	</h1>

	<?php settings_errors( 'woocom_pay' ); ?>

	<?php if ( $order_id ) : ?>
		<?php
		$order = wc_get_order( $order_id );
		$txns  = $order ? Woocom_Pay_DB::get_by_order( $order_id ) : [];

		if ( ! $order ) :
		?>
			<div class="notice notice-error"><p><?php esc_html_e( 'Order not found.', 'woocom-pay' ); ?></p></div>
		<?php else : ?>
			<div class="wcp-verify-grid">

				<!-- Order Info Card -->
				<div class="wcp-card">
					<h2><?php echo esc_html( sprintf( __( 'Order #%s', 'woocom-pay' ), $order->get_order_number() ) ); ?></h2>

					<table class="widefat striped">
						<tr><th><?php esc_html_e( 'Customer', 'woocom-pay' ); ?></th><td><?php echo esc_html( $order->get_formatted_billing_full_name() ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Email', 'woocom-pay' ); ?></th><td><?php echo esc_html( $order->get_billing_email() ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Phone', 'woocom-pay' ); ?></th><td><?php echo esc_html( $order->get_billing_phone() ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Order Total', 'woocom-pay' ); ?></th><td><?php echo wc_price( $order->get_total() ); ?></td></tr>
						<?php
						$paid    = $order->get_meta( '_wcp_paid_amount' );
						$balance = $order->get_meta( '_wcp_balance_amount' );
						$total   = $order->get_meta( '_wcp_product_total' );
						if ( $total ) :
						?>
						<tr><th><?php esc_html_e( 'Product Total', 'woocom-pay' ); ?></th><td><?php echo wc_price( $total ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Amount Paid', 'woocom-pay' ); ?></th><td style="color:#27ae60;"><?php echo wc_price( $paid ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Balance Due', 'woocom-pay' ); ?></th><td style="color:#e74c3c;"><?php echo wc_price( $balance ); ?></td></tr>
						<?php endif; ?>
						<tr><th><?php esc_html_e( 'Status', 'woocom-pay' ); ?></th><td><?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?></td></tr>
					</table>

					<h3><?php esc_html_e( 'Products', 'woocom-pay' ); ?></h3>
					<ul style="margin:0;padding-left:16px;">
					<?php foreach ( $order->get_items() as $item ) : ?>
						<li><?php echo esc_html( $item->get_name() ); ?> &times; <?php echo (int) $item->get_quantity(); ?></li>
					<?php endforeach; ?>
					</ul>
				</div>

				<!-- Transaction Cards -->
				<?php foreach ( $txns as $txn ) : ?>
				<div class="wcp-card">
					<h2>
						<?php echo esc_html( sprintf(
							__( '%s Transaction', 'woocom-pay' ),
							ucfirst( $txn['payment_type'] )
						) ); ?>
						<span class="wcp-badge wcp-badge-<?php echo esc_attr( $txn['verification_status'] ); ?>">
							<?php echo esc_html( ucfirst( $txn['verification_status'] ) ); ?>
						</span>
					</h2>

					<table class="widefat striped">
						<tr><th><?php esc_html_e( 'Transaction ID', 'woocom-pay' ); ?></th><td><code><?php echo esc_html( $txn['transaction_id'] ?: '—' ); ?></code></td></tr>
						<tr><th><?php esc_html_e( 'Amount Paid', 'woocom-pay' ); ?></th><td><?php echo wc_price( $txn['paid_amount'] ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Balance', 'woocom-pay' ); ?></th><td><?php echo wc_price( $txn['balance_amount'] ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Remarks', 'woocom-pay' ); ?></th><td><?php echo nl2br( esc_html( $txn['remarks'] ?: '—' ) ); ?></td></tr>
						<tr><th><?php esc_html_e( 'Submitted', 'woocom-pay' ); ?></th><td><?php echo esc_html( $txn['created_at'] ); ?></td></tr>
						<?php if ( $txn['verified_at'] ) : ?>
						<tr><th><?php esc_html_e( 'Verified At', 'woocom-pay' ); ?></th><td><?php echo esc_html( $txn['verified_at'] ); ?></td></tr>
						<?php endif; ?>
					</table>

					<?php if ( $txn['screenshot_path'] ) : ?>
					<div style="margin-top:14px;">
						<strong><?php esc_html_e( 'Payment Screenshot:', 'woocom-pay' ); ?></strong>
						<a href="<?php echo esc_url( $txn['screenshot_path'] ); ?>" target="_blank">
							<img src="<?php echo esc_url( $txn['screenshot_path'] ); ?>"
								class="wcp-screenshot-preview">
						</a>
					</div>
					<?php endif; ?>

					<?php if ( $txn['verification_status'] === 'pending' ) : ?>
					<div class="wcp-verify-actions" style="margin-top:20px;">
						<form method="post" style="display:inline-block;">
							<?php wp_nonce_field( 'wcp_verify_action', 'wcp_verify_nonce' ); ?>
							<input type="hidden" name="wcp_action" value="approve">
							<input type="hidden" name="wcp_txn_id" value="<?php echo (int) $txn['id']; ?>">
							<input type="hidden" name="wcp_order_id" value="<?php echo (int) $order_id; ?>">
							<?php if ( $txn['payment_type'] === 'partial' ) : ?>
							<label style="display:block;margin-bottom:8px;">
								<input type="checkbox" name="send_balance_link" value="1" checked>
								<?php esc_html_e( 'Send balance payment link to customer', 'woocom-pay' ); ?>
							</label>
							<?php endif; ?>
							<?php submit_button( __( 'Approve Payment', 'woocom-pay' ), 'primary', 'submit', false ); ?>
						</form>

						&nbsp;

						<button type="button" class="button button-link-delete" id="wcp-reject-btn-<?php echo (int) $txn['id']; ?>">
							<?php esc_html_e( 'Reject Payment', 'woocom-pay' ); ?>
						</button>

						<div id="wcp-reject-form-<?php echo (int) $txn['id']; ?>" class="wcp-reject-form" style="display:none;">
							<form method="post">
								<?php wp_nonce_field( 'wcp_verify_action', 'wcp_verify_nonce' ); ?>
								<input type="hidden" name="wcp_action" value="reject">
								<input type="hidden" name="wcp_txn_id" value="<?php echo (int) $txn['id']; ?>">
								<input type="hidden" name="wcp_order_id" value="<?php echo (int) $order_id; ?>">
								<label>
									<strong><?php esc_html_e( 'Rejection Reason:', 'woocom-pay' ); ?></strong><br>
									<textarea name="rejection_reason" rows="3" style="width:100%;margin-top:4px;" placeholder="<?php esc_attr_e( 'Reason for rejection (sent to customer)...', 'woocom-pay' ); ?>"></textarea>
								</label>
								<?php submit_button( __( 'Confirm Rejection', 'woocom-pay' ), 'delete', 'submit', false ); ?>
							</form>
						</div>
					</div>

					<script>
					document.getElementById( 'wcp-reject-btn-<?php echo (int) $txn['id']; ?>' ).addEventListener( 'click', function() {
						var form = document.getElementById( 'wcp-reject-form-<?php echo (int) $txn['id']; ?>' );
						form.style.display = form.style.display === 'none' ? 'block' : 'none';
					} );
					</script>
					<?php endif; ?>
				</div>
				<?php endforeach; ?>

				<?php if ( empty( $txns ) ) : ?>
				<div class="wcp-card">
					<p><?php esc_html_e( 'No payment transactions recorded for this order yet.', 'woocom-pay' ); ?></p>
				</div>
				<?php endif; ?>
			</div>

			<p style="margin-top:16px;">
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=woocom-pay-verify' ) ); ?>" class="button">
					&larr; <?php esc_html_e( 'Back to All Transactions', 'woocom-pay' ); ?>
				</a>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-orders&id=' . $order_id ) ); ?>" class="button" style="margin-left:8px;">
					<?php esc_html_e( 'View Full Order', 'woocom-pay' ); ?>
				</a>
			</p>
		<?php endif; ?>

	<?php else : ?>

		<!-- All Transactions List -->
		<?php
		$status_filter = sanitize_text_field( $_GET['status'] ?? '' );
		$paged         = max( 1, (int) ( $_GET['paged'] ?? 1 ) );
		$per_page      = 20;
		$offset        = ( $paged - 1 ) * $per_page;
		$args          = [ 'limit' => $per_page, 'offset' => $offset ];
		if ( $status_filter ) {
			$args['verification_status'] = $status_filter;
		}
		$txns  = Woocom_Pay_DB::get_all( $args );
		$total = Woocom_Pay_DB::count( $status_filter );
		$pages = ceil( $total / $per_page );

		$pending  = Woocom_Pay_DB::count( 'pending' );
		$approved = Woocom_Pay_DB::count( 'approved' );
		$rejected = Woocom_Pay_DB::count( 'rejected' );
		?>

		<div class="wcp-stats-bar">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=woocom-pay-verify' ) ); ?>" class="wcp-stat <?php echo ! $status_filter ? 'active' : ''; ?>">
				<?php echo esc_html( sprintf( __( 'All (%d)', 'woocom-pay' ), $total ) ); ?>
			</a>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=woocom-pay-verify&status=pending' ) ); ?>" class="wcp-stat wcp-stat-pending <?php echo $status_filter === 'pending' ? 'active' : ''; ?>">
				<?php echo esc_html( sprintf( __( 'Pending (%d)', 'woocom-pay' ), $pending ) ); ?>
			</a>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=woocom-pay-verify&status=approved' ) ); ?>" class="wcp-stat wcp-stat-approved <?php echo $status_filter === 'approved' ? 'active' : ''; ?>">
				<?php echo esc_html( sprintf( __( 'Approved (%d)', 'woocom-pay' ), $approved ) ); ?>
			</a>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=woocom-pay-verify&status=rejected' ) ); ?>" class="wcp-stat wcp-stat-rejected <?php echo $status_filter === 'rejected' ? 'active' : ''; ?>">
				<?php echo esc_html( sprintf( __( 'Rejected (%d)', 'woocom-pay' ), $rejected ) ); ?>
			</a>
		</div>

		<div class="wcp-table-wrap">
		<table class="wp-list-table widefat fixed striped wcp-transactions-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Order', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Customer', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Type', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Paid', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Balance', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Transaction ID', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Status', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Date', 'woocom-pay' ); ?></th>
					<th><?php esc_html_e( 'Actions', 'woocom-pay' ); ?></th>
				</tr>
			</thead>
			<tbody>
			<?php if ( empty( $txns ) ) : ?>
				<tr><td colspan="9" style="text-align:center;padding:30px;"><?php esc_html_e( 'No transactions found.', 'woocom-pay' ); ?></td></tr>
			<?php else : ?>
				<?php foreach ( $txns as $txn ) :
					$order = wc_get_order( $txn['order_id'] );
				?>
				<tr>
					<td>
						<?php if ( $order ) : ?>
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=woocom-pay-verify&order_id=' . $txn['order_id'] ) ); ?>">
							#<?php echo esc_html( $order->get_order_number() ); ?>
						</a>
						<?php else : ?>
						#<?php echo (int) $txn['order_id']; ?>
						<?php endif; ?>
					</td>
					<td><?php echo $order ? esc_html( $order->get_formatted_billing_full_name() ) : '—'; ?></td>
					<td><?php echo esc_html( ucfirst( $txn['payment_type'] ) ); ?></td>
					<td><?php echo wc_price( $txn['paid_amount'] ); ?></td>
					<td><?php echo wc_price( $txn['balance_amount'] ); ?></td>
					<td><code><?php echo esc_html( $txn['transaction_id'] ?: '—' ); ?></code></td>
					<td>
						<span class="wcp-badge wcp-badge-<?php echo esc_attr( $txn['verification_status'] ); ?>">
							<?php echo esc_html( ucfirst( $txn['verification_status'] ) ); ?>
						</span>
					</td>
					<td><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $txn['created_at'] ) ) ); ?></td>
					<td>
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=woocom-pay-verify&order_id=' . $txn['order_id'] ) ); ?>" class="button button-small">
							<?php esc_html_e( 'Review', 'woocom-pay' ); ?>
						</a>
					</td>
				</tr>
				<?php endforeach; ?>
			<?php endif; ?>
			</tbody>
		</table>
		</div><!-- .wcp-table-wrap -->

		<?php if ( $pages > 1 ) : ?>
		<div class="tablenav bottom">
			<div class="tablenav-pages">
				<?php
				$base_url = admin_url( 'admin.php?page=woocom-pay-verify' . ( $status_filter ? '&status=' . urlencode( $status_filter ) : '' ) . '&paged=%#%' );
				echo paginate_links( [
					'base'    => $base_url,
					'format'  => '',
					'current' => $paged,
					'total'   => $pages,
				] );
				?>
			</div>
		</div>
		<?php endif; ?>

	<?php endif; ?>
</div>
