<?php
if ($this->session->userdata('user_type') == 1) {
	$h_s = config_item('proposal_state') === 'none' ? 'none' : 'block';
	$currency = $this->db->where(array('code' => config_item('default_currency')))->get('tbl_currencies')->row();
	$currency_symbol = !empty($currency->symbol) ? $currency->symbol : '';

	$expired = 0;
	$draft = 0;
	$total_draft = 0;
	$total_sent = 0;
	$total_declined = 0;
	$total_accepted = 0;
	$total_expired = 0;
	$sent = 0;
	$declined = 0;
	$accepted = 0;
	$pending = 0;
	$cancelled = 0;

	$all_proposals = $this->proposal_model->get_permission('tbl_proposals');
	if (!empty($all_proposals)) {
		$all_proposals = array_reverse($all_proposals);
		foreach ($all_proposals as $v_invoice) {
			$is_expired = (
				safe_strtotime($v_invoice->due_date) < safe_strtotime(date('Y-m-d'))
				&& ($v_invoice->status == 'pending' || $v_invoice->status == 'draft')
			);
			if ($is_expired) {
				$total_expired += $this->proposal_model->proposal_calculation('total', $v_invoice->proposals_id);
				$expired++;
			}
			if ($v_invoice->status == 'draft') {
				$draft++;
				$total_draft += $this->proposal_model->proposal_calculation('total', $v_invoice->proposals_id);
			}
			if ($v_invoice->status == 'sent') {
				$sent++;
				$total_sent += $this->proposal_model->proposal_calculation('total', $v_invoice->proposals_id);
			}
			if ($v_invoice->status == 'declined') {
				$declined++;
				$total_declined += $this->proposal_model->proposal_calculation('total', $v_invoice->proposals_id);
			}
			if ($v_invoice->status == 'accepted') {
				$accepted++;
				$total_accepted += $this->proposal_model->proposal_calculation('total', $v_invoice->proposals_id);
			}
			if ($v_invoice->status == 'pending') {
				$pending++;
			}
			if ($v_invoice->status == 'cancelled') {
				$cancelled++;
			}
		}
	}

	$total_count = !empty($all_proposals) ? count($all_proposals) : 0;
	$stats = array(
		'draft' => array('count' => $draft, 'amount' => $total_draft, 'tone' => 'violet'),
		'sent' => array('count' => $sent, 'amount' => $total_sent, 'tone' => 'sky'),
		'expired' => array('count' => $expired, 'amount' => $total_expired, 'tone' => 'rose'),
		'declined' => array('count' => $declined, 'amount' => $total_declined, 'tone' => 'amber'),
		'accepted' => array('count' => $accepted, 'amount' => $total_accepted, 'tone' => 'emerald'),
	);
	?>

	<div id="state_report" style="display: <?= $h_s ?>">
		<div class="mb-4 grid gap-3 sm:grid-cols-2 xl:grid-cols-5">
			<?php foreach ($stats as $key => $stat) { ?>
				<div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
					<div class="text-xs font-semibold uppercase tracking-wide text-slate-500 dark:text-slate-400"><?= lang($key) ?></div>
					<div class="mt-2 text-xl font-bold text-slate-900 dark:text-slate-100"><?= display_money($stat['amount'], $currency_symbol) ?></div>
				</div>
			<?php } ?>
		</div>

		<?php if (!empty($all_proposals)) { ?>
			<div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-5">
				<?php foreach ($stats as $key => $stat) {
					$percent = ($total_count > 0) ? round(($stat['count'] / $total_count) * 100, 2) : 0;
					$tone = $stat['tone'];
					?>
					<div class="rounded-xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-700 dark:bg-slate-900/60">
						<div class="flex items-center justify-between gap-2">
							<a class="filter_by_type text-sm font-semibold text-<?= $tone ?>-600 hover:underline dark:text-<?= $tone ?>-300"
							   search-type="<?= lang($key) ?>" id="<?= $key ?>" href="#">
								<?= lang($key) ?>
							</a>
							<span class="text-xs text-slate-500 dark:text-slate-400"><?= $stat['count'] ?> / <?= $total_count ?></span>
						</div>
						<div class="mt-3 h-2 w-full rounded-full bg-slate-100 dark:bg-slate-800">
							<div class="h-2 rounded-full bg-<?= $tone ?>-500" style="width: <?= $percent ?>%" data-toggle="tooltip" data-original-title="<?= $percent ?>%"></div>
						</div>
					</div>
				<?php } ?>
			</div>
		<?php } ?>
	</div>
<?php } ?>
