<div class="panel panel-custom">

	<div class="modal-body wrap-modal wrap">
		<div class="space-y-4">
			<?php
			$invoice_options = array();
			if (!empty($all_invoices)) {
				foreach ($all_invoices as $v_invoices) {
					$payment_status = $this->invoice_model->get_payment_status($v_invoices->invoices_id);
					if ($payment_status != lang('fully_paid') && $payment_status != 'fully_paid') {
						$currency = $this->invoice_model->client_currency_symbol($v_invoices->client_id);
						if (empty($currency)) {
							$currency = $this->invoice_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
						}
						$total_amount = display_money($this->invoice_model->calculate_to('total', $v_invoices->invoices_id), $currency->symbol);
						$due_amount = display_money($this->invoice_model->calculate_to('invoice_due', $v_invoices->invoices_id), $currency->symbol);
						$invoice_options[base_url('client/invoice/select_item/' . $v_invoices->invoices_id)] = '(' . $total_amount . ') ' . $v_invoices->reference_no . ' - ' . lang('due_amount') . ': ' . $due_amount;
					}
				}
			}

			if (!empty($invoice_options)) {
				echo form_select_group(
					'item_select',
					lang('select') . ' ' . lang('invoice'),
					$invoice_options,
					'',
					true,
					array(
						'id' => 'item_select',
						'data-live-search' => 'true',
						'data-placeholder' => lang('select') . ' ' . lang('invoice'),
						'data-refund-invoice-select' => 'true',
						'onchange' => 'if (this.value) { window.location.href = this.value; }',
					)
				);
			} else { ?>
				<div class="rounded-xl border border-border bg-muted/30 p-4 text-sm text-muted-foreground">
					<?= lang('no_data_found') ?>
				</div>
			<?php } ?>
		</div>
		<div class="modal-footer mt-4">
			<button type="button" class="inline-flex items-center rounded-lg border border-border bg-muted/30 px-4 py-2 text-sm font-medium text-foreground hover:bg-muted" data-dismiss="modal">
				<?= lang('close') ?>
			</button>
		</div>
	</div>
</div>
<script type="text/javascript">
	(function () {
		function initRefundInvoiceSelect(root) {
			root = root || document;
			var select = root.querySelector('[data-refund-invoice-select]');
			if (!select || select.dataset.refundSelectReady === 'true') {
				return;
			}
			select.dataset.refundSelectReady = 'true';
			select.addEventListener('change', function () {
				if (select.value) {
					window.location.href = select.value;
				}
			});
		}

		if (document.readyState === 'loading') {
			document.addEventListener('DOMContentLoaded', function () {
				initRefundInvoiceSelect(document);
			});
		} else {
			initRefundInvoiceSelect(document);
		}

		document.addEventListener('app:modal-loaded', function (event) {
			initRefundInvoiceSelect(event.detail && event.detail.bodyEl ? event.detail.bodyEl : document);
		});
	})();

	document.addEventListener('DOMContentLoaded', function () {
		var modal = document.getElementById('myModal');
		if (!modal) {
			return;
		}
		modal.addEventListener('hide.bs.modal', function () {
			location.reload();
		});
	});
</script>
