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

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

$config_map = array(
	'leads' => array(
		'save' => 'admin/leads/saved_call',
	),
	'opportunities' => array(
		'save' => 'admin/opportunities/saved_call',
	),
	'deals' => array(
		'save' => 'admin/deals/saved_call',
	),
);

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;
}

$call_info = null;
if (!empty($call_id)) {
	$call_info = get_row('tbl_calls', array('calls_id' => $call_id));
}

$selected_client_id = !empty($call_info->client_id) ? $call_info->client_id : '';
$selected_client_label = '';
if (!empty($selected_client_id)) {
	$client_row = $this->db->select('name')->where('client_id', $selected_client_id)->get('tbl_client')->row();
	$selected_client_label = !empty($client_row->name) ? $client_row->name : '';
}

$selected_user_id = !empty($call_info->user_id) ? $call_info->user_id : '';
$selected_user_label = '';
if (!empty($selected_user_id)) {
	$user_row = $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.user_id', $selected_user_id)
		->get()->row();
	if (!empty($user_row)) {
		$selected_user_label = !empty($user_row->fullname) ? $user_row->fullname : $user_row->username;
	}
}

$form_action = base_url($config_map[$module]['save'] . '/' . $module_id . '/' . (!empty($call_info->calls_id) ? $call_info->calls_id : ''));
$form_label = !empty($call_info) ? lang('updates') : lang('save');
?>

<?php echo form_open($form_action, array('class' => 'modal-form', 'data-parsley-validate' => '')); ?>
<div class="grid gap-4">
	<div class="grid gap-4 md:grid-cols-2">
		<?= form_datepicker('date', lang('date'), (!empty($call_info->date) ? $call_info->date : date('Y-m-d')), true, array('data-type' => 'date', 'data-format' => 'Y-m-d')); ?>
		<?= form_input_group('call_summary', lang('call_summary'), (!empty($call_info->call_summary) ? $call_info->call_summary : ''), 'text', true); ?>
		<?= form_lookup_group('client_id', lang('contact'), $selected_client_id, $selected_client_label, false, array(), 'md:col-span-1', array('type' => 'client')); ?>
		<?= form_lookup_group('user_id', lang('responsible'), $selected_user_id, $selected_user_label, true, array(), 'md:col-span-1', array('type' => 'staff')); ?>
	</div>
</div>

<div class="mt-6 flex items-center justify-end gap-2">
	<button type="button" class="cancel-upload inline-flex items-center gap-2 rounded-lg border border-slate-200 bg-white px-4 py-2 text-xs font-semibold text-slate-600 hover:bg-slate-50" data-dismiss="modal">
		<i data-lucide="x" class="h-4 w-4"></i>
		<?= lang('close'); ?>
	</button>
	<button type="submit" class="start-upload inline-flex items-center gap-2 rounded-lg bg-slate-900 px-4 py-2.5 text-xs font-semibold text-white hover:bg-slate-800 dark:bg-slate-700 dark:hover:bg-slate-600">
		<i data-lucide="check-circle" class="h-4 w-4"></i>
		<?= $form_label; ?>
	</button>
</div>
<?php echo form_close(); ?>
