<?php
$can_edit = $this->items_model->can_action('tbl_leads', 'edit', array('leads_id' => $leads_details->leads_id));
$can_delete = $this->items_model->can_action('tbl_leads', 'delete', array('leads_id' => $leads_details->leads_id));

$edited = can_action('55', 'edited');
$deleted = can_action('55', 'deleted');
$all_proposals_info = get_result('tbl_proposals', array('module_id' => $leads_details->leads_id));

?>

<div class="space-y-4 pt-4">
    <div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/50">
        <div class="flex flex-wrap items-center justify-between gap-3 border-b border-slate-200 px-5 py-4 dark:border-slate-700">
            <div>
                <h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('all_proposals') ?></h3>
                <p class="text-xs text-slate-500 dark:text-slate-400"><?= count((array) $all_proposals_info) ?> <?= lang('all_proposals') ?></p>
            </div>
            <?php if (!empty($edited)) { ?>
                <a href="<?= base_url() ?>admin/proposals/createproposal/<?= $leads_details->leads_id ?>"
                   class="inline-flex items-center gap-2 rounded-lg bg-slate-900 px-3 py-2 text-xs font-semibold text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white">
                    <i data-lucide="plus" class="h-4 w-4"></i>
                    <?= lang('create_proposal') ?>
                </a>
            <?php } ?>
        </div>

		<table class="min-w-full text-sm">
			<thead class="bg-slate-50 text-slate-500 dark:bg-slate-800/60 dark:text-slate-300">
				<tr>
					<th class="px-4 py-3 text-left"><?= lang('proposal') ?> #</th>
					<th class="px-4 py-3 text-left"><?= lang('proposal_date') ?></th>
					<th class="px-4 py-3 text-left"><?= lang('expire_date') ?></th>
					<th class="px-4 py-3 text-left"><?= lang('status') ?></th>
					<?php if (!empty($edited) || !empty($deleted)) { ?>
						<th class="px-4 py-3 text-left"><?= lang('action') ?></th>
					<?php } ?>
				</tr>
			</thead>
			<tbody class="divide-y divide-slate-100 dark:divide-slate-800">
				<?php if (!empty($all_proposals_info)) {
					foreach ($all_proposals_info as $v_proposals) {
						$can_edit = $this->items_model->can_action('tbl_proposals', 'edit', array('proposals_id' => $v_proposals->proposals_id));
						$can_delete = $this->items_model->can_action('tbl_proposals', 'delete', array('proposals_id' => $v_proposals->proposals_id));

						if ($v_proposals->status == 'pending') {
							$status_class = 'bg-sky-100 text-sky-700 dark:bg-sky-900/40 dark:text-sky-300';
						} elseif ($v_proposals->status == 'accepted') {
							$status_class = 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300';
						} else {
							$status_class = 'bg-rose-100 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300';
						}
						$is_expired = ((safe_strtotime($v_proposals->due_date) < safe_strtotime(date('Y-m-d'))) && ($v_proposals->status == 'pending' || $v_proposals->status == 'draft'));
				?>
						<tr id="leads_proposals_<?= $v_proposals->proposals_id ?>">
							<td class="px-4 py-3">
								<a class="font-medium text-info dark:text-sky-300 dark:hover:text-sky-200 hover:underline" href="<?= base_url() ?>admin/proposals/index/proposals_details/<?= $v_proposals->proposals_id ?>">
									<?= html_escape($v_proposals->reference_no) ?>
								</a>
								<?php if ($v_proposals->convert == 'Yes') {
									if ($v_proposals->convert_module == 'invoice') {
										$c_url = base_url() . 'admin/invoice/manage_invoice/invoice_details/' . $v_proposals->convert_module_id;
										$text = lang('invoiced');
									} else {
										$text = lang('estimated');
										$c_url = base_url() . 'admin/estimates/create/estimates_details/' . $v_proposals->convert_module_id;
									}
									if (!empty($c_url)) { ?>
										<p class="mt-1 text-xs">
											<a class="font-semibold text-emerald-600 hover:underline dark:text-emerald-300" href="<?= $c_url ?>">
												<?= $text ?>
											</a>
										</p>
								<?php }
								} ?>
							</td>
							<td class="px-4 py-3 text-slate-700 dark:text-slate-200"><?= display_date($v_proposals->proposal_date) ?></td>
							<td class="px-4 py-3 text-slate-700 dark:text-slate-200">
								<?= display_date($v_proposals->due_date) ?>
								<?php if ($is_expired) { ?>
									<span class="ml-2 inline-flex items-center rounded-full bg-rose-100 px-2 py-0.5 text-xs font-semibold text-rose-700 dark:bg-rose-900/40 dark:text-rose-300">
										<?= lang('expired') ?>
									</span>
								<?php } ?>
							</td>
							<td class="px-4 py-3">
								<span class="inline-flex items-center rounded-full px-2.5 py-1 text-xs font-semibold <?= $status_class ?>">
									<?= lang($v_proposals->status) ?>
								</span>
							</td>
							<?php if (!empty($edited) || !empty($deleted)) { ?>
								<td class="px-4 py-3">
									<div class="flex flex-wrap items-center gap-0">
										<?php if (!empty($can_edit) && !empty($edited)) { ?>
											<?= btn_edit('admin/proposals/createproposal/' . $v_proposals->proposals_id) ?>
										<?php } ?>
										<?php if (!empty($can_delete) && !empty($deleted)) { ?>
											<?= ajax_anchor(
												base_url("admin/proposals/delete/delete_proposals/" . $v_proposals->proposals_id),
												"",
												array(
													"class" => "inline-flex items-center justify-center rounded-lg border border-rose-200 bg-rose-50 p-2 text-rose-600 hover:bg-rose-100 dark:border-rose-900/50 dark:bg-rose-900/20 dark:text-rose-300 dark:hover:bg-rose-900/40 mr-1",
													"title" => lang('delete'),
													"data-fade-out-on-success" => "#leads_proposals_" . $v_proposals->proposals_id
												)
											); ?>
										<?php } ?>
										<?php if (!empty($can_edit) && !empty($edited)) { ?>
											<details class="relative inline-flex">
												<summary class="inline-flex h-7 mb-1 cursor-pointer list-none items-center justify-center gap-1 rounded-md border border-slate-200 bg-white px-2 py-1 text-slate-500 shadow-sm transition-colors hover:bg-slate-50 hover:text-slate-900 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:hover:bg-gray-700 dark:hover:text-gray-200">
													<i data-lucide="chevron-down" class="h-3 w-3"></i>
												</summary>
												<ul class="absolute right-0 top-full z-50 mt-1 min-w-[170px] overflow-hidden rounded-lg border border-slate-200 bg-white py-1 text-xs shadow-lg dark:border-gray-700 dark:bg-gray-800">
													<li><a class="block px-3 py-2 text-slate-700 hover:bg-slate-50 dark:text-slate-300 dark:hover:bg-gray-700" href="<?= base_url() ?>admin/proposals/index/email_proposals/<?= $v_proposals->proposals_id ?>"><?= lang('send_email') ?></a></li>
													<li><a class="block px-3 py-2 text-slate-700 hover:bg-slate-50 dark:text-slate-300 dark:hover:bg-gray-700" href="<?= base_url() ?>admin/proposals/index/proposals_details/<?= $v_proposals->proposals_id ?>"><?= lang('view_details') ?></a></li>
													<li><a class="block px-3 py-2 text-slate-700 hover:bg-slate-50 dark:text-slate-300 dark:hover:bg-gray-700" href="<?= base_url() ?>admin/proposals/index/proposals_history/<?= $v_proposals->proposals_id ?>"><?= lang('history') ?></a></li>
													<li><a class="block px-3 py-2 text-slate-700 hover:bg-slate-50 dark:text-slate-300 dark:hover:bg-gray-700" href="<?= base_url() ?>admin/proposals/change_status/declined/<?= $v_proposals->proposals_id ?>"><?= lang('declined') ?></a></li>
													<li><a class="block px-3 py-2 text-slate-700 hover:bg-slate-50 dark:text-slate-300 dark:hover:bg-gray-700" href="<?= base_url() ?>admin/proposals/change_status/accepted/<?= $v_proposals->proposals_id ?>"><?= lang('accepted') ?></a></li>
												</ul>
											</details>
										<?php } ?>
									</div>
								</td>
							<?php } ?>
						</tr>
				<?php }
				} else { ?>
					<tr>
						<td colspan="<?= (!empty($edited) || !empty($deleted)) ? '5' : '4' ?>" class="px-4 py-8 text-center text-sm text-slate-500 dark:text-slate-400">
							<?= lang('no_data_found') ?>
						</td>
					</tr>
				<?php } ?>
			</tbody>
		</table>
    </div>
</div>
