<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?php
$module = isset($module) ? $module : '';
$module_id = isset($id) ? $id : null;

$config_map = array(
	'leads' => array(
		'perm' => '55',
		'field' => 'leads_id',
		'base' => 'admin/leads/leads_details',
		'save' => 'admin/leads/saved_metting',
		'delete' => 'admin/leads/delete_leads_mettings',
		'where' => array(),
	),
	'opportunities' => array(
		'perm' => '56',
		'field' => 'opportunities_id',
		'base' => 'admin/opportunities/opportunity_details',
		'details' => 'admin/opportunities/meeting_details',
		'save' => 'admin/opportunities/saved_metting',
		'delete' => 'admin/opportunities/delete_opportunity_mettings',
		'where' => array(),
	),
	'deals' => array(
		'perm' => '56',
		'field' => 'module_field_id',
		'base' => 'admin/deals/details',
		'details' => 'admin/deals/meeting_details',
		'save' => 'admin/deals/saved_metting',
		'delete' => 'admin/deals/delete_deals_mettings',
		'where' => array('module' => 'deals'),
	),
);

if (empty($module) || empty($module_id) || !isset($config_map[$module])) {
	echo '<div class="text-sm text-muted-foreground">' . lang('there_in_no_value') . '</div>';
	return;
}

$config = $config_map[$module];
$edited = can_action($config['perm'], 'edited');
$deleted = can_action($config['perm'], 'deleted');

$where = array_merge(array($config['field'] => $module_id), !empty($config['where']) ? $config['where'] : array());
$meetings = get_result('tbl_mettings', $where);

$user_map = array();
$user_rows = $this->db
	->select('tbl_users.user_id, tbl_users.username, tbl_account_details.fullname')
	->from('tbl_users')
	->join('tbl_account_details', 'tbl_account_details.user_id = tbl_users.user_id', 'left')
	->where('tbl_users.role_id !=', '2')
	->get()->result();
if (!empty($user_rows)) {
	foreach ($user_rows as $user) {
		$label = !empty($user->fullname) ? $user->fullname : $user->username;
		$user_map[$user->user_id] = $label;
	}
}
?>

<div class="space-y-6">
	<div class="rounded-3xl border border-border bg-card p-5 shadow-sm md:p-6">
		<div class="flex flex-wrap items-center justify-between gap-3 border-b border-border pb-4">
			<div>
				<p class="text-[11px] uppercase tracking-wider text-muted-foreground"><?= lang('all') . ' ' . lang('mettings') ?></p>
				<h3 class="mt-1 text-base font-semibold text-foreground"><?= lang('mettings') ?></h3>
			</div>
			<div class="flex items-center gap-2.5">
				<span class="inline-flex items-center rounded-md border border-border bg-muted/40 px-2.5 py-1 text-xs font-medium text-muted-foreground"><?= !empty($meetings) ? count($meetings) : 0; ?> <?= lang('records') ?></span>
				<?php if (!empty($edited)) { ?>
					<?= btn_modal(
						'admin/common/new_meeting/' . $module . '/' . $module_id,
						lang('new') . ' ' . lang('mettings'),
						lang('new') . ' ' . lang('mettings'),
						'lg',
						'plus',
						array('class' => 'inline-flex items-center gap-2 rounded-md bg-indigo-600 px-3 py-2 text-xs font-semibold text-white hover:bg-indigo-500 dark:bg-indigo-700 dark:hover:bg-indigo-600')
					); ?>
				<?php } ?>
			</div>
		</div>

		<div class="mt-6 grid grid-cols-1 gap-4 xl:grid-cols-2">
			<?php if (!empty($meetings)) { ?>
				<?php foreach ($meetings as $meeting) { ?>
					<?php
					$meeting_title = !empty($meeting->meeting_subject) ? $meeting->meeting_subject : lang('mettings');
					$responsible_label = !empty($meeting->user_id) && isset($user_map[$meeting->user_id]) ? $user_map[$meeting->user_id] : '-';
					$meeting_start = !empty($meeting->start_date) ? display_datetime($meeting->start_date, true) : '-';
					$meeting_end = !empty($meeting->end_date) ? display_datetime($meeting->end_date, true) : '-';
					$meeting_location = !empty($meeting->location) ? $meeting->location : '-';
					$meeting_notes = !empty($meeting->description) ? $meeting->description : '-';
					$details_url = !empty($config['details']) ? base_url($config['details'] . '/' . $meeting->mettings_id) : '';
					?>
					<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm transition hover:shadow-md" id="meeting-row-<?= $meeting->mettings_id; ?>">
						<div class="grid grid-cols-[6px_1fr]">
							<div class="bg-slate-300 dark:bg-slate-700"></div>
							<div class="space-y-3 p-4">
								<div class="flex flex-wrap items-start justify-between gap-3">
									<div class="space-y-2">
										<p class="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground"><?= lang('mettings'); ?></p>
										<?php if (!empty($details_url)) { ?>
											<a href="#"
											   data-modal-url="<?= $details_url; ?>"
											   data-modal-title="<?= lang('meeting_details'); ?>"
											   data-modal-size="md"
											   class="text-sm font-semibold text-foreground hover:underline md:text-base">
												<?= html_escape($meeting_title); ?>
											</a>
										<?php } else { ?>
											<h4 class="text-sm font-semibold text-foreground md:text-base"><?= html_escape($meeting_title); ?></h4>
										<?php } ?>
										<div class="flex flex-wrap items-center gap-2 text-xs text-muted-foreground">
											<span class="inline-flex items-center gap-1 rounded-full bg-muted px-2.5 py-1">
												<i data-lucide="calendar" class="h-3.5 w-3.5"></i>
												<?= $meeting_start; ?>
											</span>
											<span class="inline-flex items-center gap-1 rounded-full bg-muted px-2.5 py-1">
												<i data-lucide="clock" class="h-3.5 w-3.5"></i>
												<?= $meeting_end; ?>
											</span>
											<span class="inline-flex items-center gap-1 rounded-full bg-muted px-2.5 py-1">
												<i data-lucide="map-pin" class="h-3.5 w-3.5"></i>
												<?= html_escape($meeting_location); ?>
											</span>
											<span class="inline-flex items-center gap-1 rounded-full bg-muted px-2.5 py-1">
												<i data-lucide="user-check" class="h-3.5 w-3.5"></i>
												<?= html_escape($responsible_label); ?>
											</span>
										</div>
									</div>
									<?php if (!empty($edited) || !empty($deleted) || !empty($details_url)) { ?>
										<div class="flex items-center">
											<?php if (!empty($details_url)) { ?>
												<a href="#"
												   class="inline-flex h-7 w-7 items-center justify-center rounded-md border border-border bg-muted/40 mb-1 text-foreground hover:bg-muted"
												   data-placement="top"
												   data-modal-url="<?= $details_url; ?>"
												   data-modal-title="<?= lang('meeting_details'); ?>"
												   data-modal-size="md"
												   title="<?= lang('details'); ?>">
													<i data-lucide="list" class="h-3.5 w-3.5"></i>
												</a>
											<?php } ?>
											<?php if (!empty($edited)) { ?>
												<?= btn_modal(
													'admin/common/new_meeting/' . $module . '/' . $module_id . '/' . $meeting->mettings_id,
													'',
													lang('edit') . ' ' . lang('mettings'),
													'lg',
													'pencil',
													array('class' => 'btn btn-xs btn-primary mb-1 ml-1')
												); ?>
											<?php } ?>
											<?php if (!empty($deleted)) { ?>
												
												
												 <?= ajax_anchor(base_url($config['delete'] . '/' . $module_id . '/' . $meeting->mettings_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", "title" => lang('delete'), "data-fade-out-on-success" => "#meeting-row-" . $meeting->mettings_id)); ?>

												
											<?php } ?>
										</div>
									<?php } ?>
								</div>
								<div class="rounded-xl border border-border bg-muted/25 p-3">
									<p class="text-[11px] uppercase tracking-wide text-muted-foreground"><?= lang('notes'); ?></p>
									<p class="mt-1 text-sm leading-relaxed text-foreground"><?= $meeting_notes; ?></p>
								</div>
							</div>
						</div>
					</div>
				<?php } ?>
			<?php } else { ?>
				<div class="rounded-xl border border-dashed border-border bg-muted/30 px-4 py-10 text-center text-sm text-muted-foreground xl:col-span-2"><?= lang('nothing_to_display') ?></div>
			<?php } ?>
		</div>
	</div>
</div>
