<div class="invoice-modern">
	<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900">
		<div class="flex items-center justify-between border-b border-slate-200 px-5 py-4 dark:border-slate-700">
			<div>
				<h4 class="text-sm font-semibold text-slate-900 dark:text-slate-100" id="myModalLabel"><?= lang('from_items') ?></h4>
				<p class="mt-1 text-xs text-slate-500 dark:text-slate-400">Pick saved catalog items to insert into this estimate.</p>
			</div>
		</div>

		<div class="p-5">
			<form role="form" id="from_items"
				action="<?= base_url() ?>admin/estimates/add_insert_items/<?= $estimates_id ?>" method="post"
				class="space-y-4" data-parsley-validate="" novalidate="">

				<div class="overflow-x-auto overflow-y-hidden rounded-xl border border-slate-200 dark:border-slate-700">
					<table class="min-w-full divide-y divide-slate-200 text-sm dark:divide-slate-700">
						<thead class="bg-slate-50 text-slate-600 dark:bg-slate-800 dark:text-slate-300">
							<tr>
								<th class="px-4 py-3 text-left">
									<label class="inline-flex items-center" data-toggle="tooltip" title="<?= lang('select') . ' ' . lang('all') ?>">
										<input id="parent_present" type="checkbox" class="h-4 w-4 rounded border-slate-300 text-primary focus:ring-primary dark:border-slate-600 dark:bg-slate-800">
									</label>
								</th>
								<th class="px-4 py-3 text-left font-semibold"><?= lang('item') ?></th>
								<?php if (!empty(config_item('invoice_view')) && config_item('invoice_view') == '2') { ?>
									<th class="px-4 py-3 text-left font-semibold"><?= lang('hsn_code') ?></th>
								<?php } ?>
								<th class="px-4 py-3 text-left font-semibold"><?= lang('qty') ?></th>
								<th class="px-4 py-3 text-left font-semibold"><?= lang('unit_price') ?></th>
								<th class="px-4 py-3 text-left font-semibold"><?= lang('tax') ?></th>
								<th class="px-4 py-3 text-left font-semibold"><?= lang('total') ?></th>
							</tr>
						</thead>
						<tbody class="divide-y divide-slate-100 dark:divide-slate-800">
							<?php
							$saved_items = $this->estimates_model->get_all_items();
							if (!empty($saved_items)) {
								$saved_items = array_reverse($saved_items, true);
								foreach ($saved_items as $group_id => $v_saved_items) {
									if ($group_id != 0) {
										$groupRow = $this->db->where('customer_group_id', $group_id)->get('tbl_customer_group')->row();
										$group = !empty($groupRow->customer_group) ? $groupRow->customer_group : '';
									} else {
										$group = '';
									}
							?>
									<tr>
										<th colspan="7" class="bg-slate-100 px-4 py-2 text-left text-sm font-semibold text-slate-700 dark:bg-slate-800 dark:text-slate-200"><?= html_escape($group) ?></th>
									</tr>
									<?php foreach ((array) $v_saved_items as $v_item) { ?>
										<tr>
											<td class="px-4 py-3">
												<input class="child_present h-4 w-4 rounded border-slate-300 text-primary focus:ring-primary dark:border-slate-600 dark:bg-slate-800"
													type="checkbox" name="saved_items_id[]" value="<?= $v_item->saved_items_id ?>" />
											</td>
											<td class="px-4 py-3 text-slate-700 dark:text-slate-200">
												<strong class="block"><?= html_escape($v_item->item_name) ?></strong>
												<span class="text-xs text-slate-500 dark:text-slate-400"><?= strip_html_tags(mb_substr($v_item->item_desc, 0, 200)) . '...'; ?></span>
											</td>
											<?php if (!empty(config_item('invoice_view')) && config_item('invoice_view') == '2') { ?>
												<td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= html_escape($v_item->hsn_code) ?></td>
											<?php } ?>
											<td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= $v_item->quantity . ' &nbsp; ' . html_escape($v_item->unit_type) ?></td>
											<td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= display_money($v_item->unit_cost) ?></td>
											<td class="px-4 py-3 text-xs text-slate-600 dark:text-slate-300">
													<?php
													$tax_rates = array();
													if (!empty($v_item->tax_rates_id)) {
														$decoded_taxes = is_numeric($v_item->tax_rates_id) ? array($v_item->tax_rates_id) : json_decode($v_item->tax_rates_id, true);
														if (!empty($decoded_taxes)) {
															$tax_ids = array();
															$collect_tax_ids = function ($value) use (&$collect_tax_ids, &$tax_ids) {
																if (is_array($value)) {
																	foreach (array('tax_rates_id', 'id', 'value') as $key) {
																		if (isset($value[$key]) && is_numeric($value[$key])) {
																			$tax_ids[] = (int) $value[$key];
																			return;
																		}
																	}
																	foreach ($value as $child) {
																		$collect_tax_ids($child);
																	}
																	return;
																}
																if (is_numeric($value)) {
																	$tax_ids[] = (int) $value;
																}
															};
															$collect_tax_ids($decoded_taxes);
															$tax_ids = array_values(array_unique(array_filter($tax_ids)));
															if (!empty($tax_ids)) {
																$tax_rates = $this->db->where_in('tax_rates_id', $tax_ids)->get('tbl_tax_rates')->result();
															}
														}
													}
													if (!empty($tax_rates)) {
														foreach ($tax_rates as $key => $taxes_info) {
															echo ($key + 1) . '. ' . html_escape($taxes_info->tax_rate_name) . ' (' . html_escape($taxes_info->tax_rate_percent) . '%)<br>';
														}
													}
													?>
											</td>
											<td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= display_money($v_item->total_cost) ?></td>
										</tr>
									<?php } ?>
							<?php
								}
							}
							?>
						</tbody>
					</table>
				</div>

				<div class="flex items-center justify-end gap-2 border-t border-slate-200 pt-4 dark:border-slate-700">
					<button type="button" data-dismiss="modal"
						class="inline-flex items-center rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm font-semibold text-slate-700 transition hover:bg-slate-50 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-200 dark:hover:bg-slate-700"><?= lang('close') ?></button>
					<button type="submit"
						class="inline-flex items-center rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white transition hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white"><?= lang('update') ?></button>
				</div>
			</form>
		</div>
	</div>
</div>

<script type="text/javascript">
	$(function() {
		$('#parent_present').on('change', function() {
			$('.child_present').prop('checked', $(this).prop('checked'));
		});
		$('.child_present').on('change', function() {
			$('#parent_present').prop('checked', $('.child_present').length > 0 && $('.child_present:checked').length === $('.child_present').length);
		});
	});
	</script>
