<?php
if ( ! defined( 'ABSPATH' ) ) exit;
// phpcs:ignoreFile

use Anbarak\Input;
use Anbarak\Tables;

global $wpdb;

$post_per_page = 50;
$paged = (int) (anbarak_get_param('paged') ?: 1);
$paged = max(1, $paged);
$offset = ($paged - 1) * $post_per_page;

$q = anbarak_get_param('q');
$tag_id = (int) anbarak_get_param('tag');
$search_term = mb_strlen($q) > 2 ? '%' . $wpdb->esc_like($q) . '%' : '';

$offset = (int) $offset;
$post_per_page = (int) $post_per_page;
$expenses_table = $wpdb->prefix . Tables::EXPENSES;
$tags_table = $wpdb->prefix . Tables::EXPENSES_TAGS;

// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$expenses_lists = $wpdb->get_results($wpdb->prepare("SELECT 
		SQL_CALC_FOUND_ROWS 
		ex.*, 
		t.name as tag_name,
		t.color as tag_color
	FROM {$expenses_table} ex
		LEFT JOIN {$tags_table} t ON ex.tag_id = t.id
		WHERE ex.deleted_at IS NULL 
		AND ( %s = '' OR ex.notes LIKE %s )
			AND ( %d = 0 OR ex.tag_id = %d )
			ORDER BY ID DESC
			LIMIT %d,%d
		", $search_term, $search_term, $tag_id, $tag_id, $offset, $post_per_page));
// phpcs:enable

$sql_posts_total = $wpdb->get_var("SELECT FOUND_ROWS();");
$max_num_pages = ceil($sql_posts_total / $post_per_page);
$page_links = paginate_links(array(
	'base' => add_query_arg(
		array(
			'page'  => 'anbarak',
			'tab'   => 'finance',
			'view'  => 'expenses',
			'q'     => $q ?: false,
			'tag'   => $tag_id ?: false,
			'paged' => '%#%',
		)
	),
	'total' => $max_num_pages,
	'current' => max(1, $paged)
));

?>
<div class="anbarak-expenses-wrap">
	<header class="mb-5 flex gap-4 items-center">
		<div class="flex flex-grow">
			<div class="flex flex-grow">
				<form action="" class="w-full">
					<input type="hidden" name="page" value="anbarak">
					<input type="hidden" name="tab" value="finance">
					<input type="hidden" name="view" value="expenses">
					<div class="search-form-wrapper flex !flex-row w-full">
						<?php anbarak_render_expenses_tags_dropdown($tag_id) ?>
						<input name="q" type="text" class="form-control me-1 px-4" placeholder="<?php esc_attr_e('Search...', 'anbarak') ?>" value="<?php echo esc_attr($q) ?>">
						<button type="submit" class="button button--primary w-30"><i class="icon icon-search text-xl"></i></button>
					</div>
				</form>
			</div>
			<div class="new-expense-button-wrapper fixed lg:static bottom-8 right-8 z-10">
				<button data-micromodal-trigger="modal-3" class="button button--primary flex gap-2 items-center justify-center rounded-full lg:rounded-xl !size-12 lg:!size-auto p-0 lg:px-5 shadow-lg lg:shadow-none">
					<span class="icon icon-plus1 text-3xl lg:text-xl"></span>
					<span class="hidden xl:block"><?php esc_html_e('New Expense', 'anbarak'); ?></span>
				</button>
			</div>
		</div>

	</header>

	<div class="expenses-list-wrap table-container text-sm 2xl:text-base">
		<table class="table">
			<thead>
				<td><?php esc_html_e('Description', 'anbarak') ?></th>
				<td><?php esc_html_e('SKU', 'anbarak') ?></th>
				<td><?php esc_html_e('Product ID', 'anbarak') ?></th>
				<td><?php esc_html_e('Tag', 'anbarak') ?></th>
				<td><?php esc_html_e('Date', 'anbarak') ?></th>
				<td><?php esc_html_e('Price', 'anbarak') ?></th>
				<td><?php esc_html_e('Actions', 'anbarak') ?></th>
			</thead>
			<tbody>
				<?php foreach ($expenses_lists as $expense) : ?>
					<tr id="expense-<?php echo esc_attr($expense->id) ?>">
						<?php anbarak_render_expense_table_item($expense) ?>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

	<?php
	if ($page_links) {
		echo '<div class="pagination">' . wp_kses_post($page_links) . '</div>';
	}
	?>
	<div class="micro-modal micromodal-slide" id="modal-3" aria-hidden="false">
		<div class="modal__overlay" tabindex="-1" data-micromodal-close="">
			<div class="modal__container box" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
				<header class="modal__header">
					<h2 class="modal__title"><?php esc_html_e('Submit Expense', 'anbarak'); ?></h2>
					<button class="modal__close" type="button" aria-label="Close modal" data-micromodal-close></button>
				</header>
				<main class="modal__content">
					<div class="new-expense-from-wrap">
						<form name="frm-new-expense" action="" method="POST" class="w-full">
							<?php anbarak_nonce() ?>
								<input type="hidden" name="action" value="anbarak_register_expense">

							<div class="grid grid-cols-1 lg:grid-cols-2 gap-3">

								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'price',
										'id' => 'price',
										'parent_class' => 'w-full',
										'class' => 'thousand-separator',
										'label' => esc_html__('Item Cost', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::renderDateTimePicker([
										'name' => 'date',
										'parent_class' => 'w-full',
										'label' => esc_html__('Date', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<textarea class="w-full form-input" name="notes" placeholder="<?php esc_attr_e('Notes', 'anbarak') ?>" rows="3" required></textarea>
								</div>
								<div class="form-group mt-4">
									<?php anbarak_render_expenses_tags_dropdown(0, true) ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'buy_tax',
										'parent_class' => 'w-full',
										'class' => 'thousand-separator',
										'label' => esc_html__('Buy Tax', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'buy_discount',
										'parent_class' => 'w-full',
										'class' => 'thousand-separator',
										'label' => esc_html__('Buy Discount', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'invoice_number',
										'parent_class' => 'w-full',
										'label' => esc_html__('Invoice Number', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<select name="payment_method" id="" class="w-full">
										<option value="" selected><?php esc_html_e('Payment Method', 'anbarak') ?></option>
										<option value="card"><?php esc_html_e('Card', 'anbarak') ?></option>
										<option value="bank_transfer"><?php esc_html_e('Bank Transfer', 'anbarak') ?></option>
										<option value="cheque"><?php esc_html_e('Cheque', 'anbarak') ?></option>
										<option value="cash"><?php esc_html_e('Cash', 'anbarak') ?></option>
										<option value="other"><?php esc_html_e('Other', 'anbarak') ?></option>
									</select>
								</div>

							</div>
							<div class="form-group mt-5">
								<button type="submit" class="button button--primary"><?php esc_html_e('Submit Expense', 'anbarak'); ?></button>
							</div>
						</form>
					</div>
				</main>
			</div>
		</div>
	</div>
	<div class="micro-modal micromodal-slide edit-purchase-modal" id="modal-purchase-edit" aria-hidden="false">
		<div class="modal__overlay" tabindex="-1" data-micromodal-close="">
			<div class="modal__container box" role="dialog" aria-modal="true" aria-labelledby="modal-1-title">
				<header class="modal__header">
					<h2 class="modal__title"><?php esc_html_e('Edit Expense', 'anbarak'); ?></h2>
					<button class="modal__close" type="button" aria-label="Close modal" data-micromodal-close></button>
				</header>
				<main class="modal__content lg:w-[600px]">
					<div class="new-expense-from-wrap">
						<form name="frm-edit-expense" action="" method="POST" class="w-full">
							<?php  anbarak_nonce() ?>
							<input type="hidden" name="action" value="anbarak_edit_expense">
							<input type="hidden" name="id" value="">

							<div class="grid grid-cols-1 lg:grid-cols-2 gap-3">

								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'price',
										'id' => 'price',
										'parent_class' => 'w-full',
										'class' => 'thousand-separator',
										'label' => esc_html__('Item Cost', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::renderDateTimePicker([
										'name' => 'date',
										'parent_class' => 'w-full',
										'label' => esc_html__('Date', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<textarea class="w-full form-input" name="notes" placeholder="<?php esc_attr_e('Notes', 'anbarak') ?>" rows="3" required></textarea>
								</div>
								<div class="form-group mt-4">
									<?php anbarak_render_expenses_tags_dropdown() ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'buy_tax',
										'parent_class' => 'w-full',
										'class' => 'thousand-separator',
										'label' => esc_html__('Buy Tax', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'buy_discount',
										'parent_class' => 'w-full',
										'class' => 'thousand-separator',
										'label' => esc_html__('Buy Discount', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<?php echo wp_kses_post(Input::render([
										'name' => 'invoice_number',
										'parent_class' => 'w-full',
										'label' => esc_html__('Invoice Number', 'anbarak'),
									])); ?>
								</div>
								<div class="form-group mb-2">
									<select name="payment_method" id="" class="w-full">
										<option value="" selected><?php esc_html_e('Payment Method', 'anbarak') ?></option>
										<option value="card"><?php esc_html_e('Card', 'anbarak') ?></option>
										<option value="bank_transfer"><?php esc_html_e('Bank Transfer', 'anbarak') ?></option>
										<option value="cheque"><?php esc_html_e('Cheque', 'anbarak') ?></option>
										<option value="cash"><?php esc_html_e('Cash', 'anbarak') ?></option>
										<option value="other"><?php esc_html_e('Other', 'anbarak') ?></option>
									</select>
								</div>

							</div>
							<div class="form-group mt-5">
								<button type="submit" class="button button--primary"><?php esc_html_e('Update', 'anbarak'); ?></button>
							</div>
						</form>
					</div>
				</main>
			</div>
		</div>
	</div>

</div>
