<?php
$eeror_message = $this->session->userdata('error');

if (!empty($eeror_message)):
	foreach ($eeror_message as $key => $message):
		?>
		<div class="alert alert-danger">
			<?php echo $message; ?>
		</div>
		<?php
	endforeach;
endif;
$this->session->unset_userdata('error');

$get_field = static function ($source, $key, $default = '') {
	if (is_array($source) && array_key_exists($key, $source)) {
		return $source[$key];
	}
	if (is_object($source) && property_exists($source, $key)) {
		return $source->$key;
	}
	return $default;
};

$login_info = isset($login_info) ? $login_info : null;
$profile_info = null;
$user_id = null;
$login_user_id = (int) $get_field($login_info, 'user_id', 0);
if (!empty($login_user_id)) {
	$profile_info = $this->user_model->check_by(array('user_id' => $login_user_id), 'tbl_account_details');
	$user_id = $login_user_id;
}

$profile_account_details_id = (int) $get_field($profile_info, 'account_details_id', 0);
$profile_fullname = (string) $get_field($profile_info, 'fullname', '');
$profile_phone = (string) $get_field($profile_info, 'phone', '');
$profile_mobile = (string) $get_field($profile_info, 'mobile', '');
$profile_skype = (string) $get_field($profile_info, 'skype', '');
$profile_locale = (string) $get_field($profile_info, 'locale', $this->config->item('locale'));
$profile_language = (string) $get_field($profile_info, 'language', $this->config->item('language'));

$login_username = (string) $get_field($login_info, 'username', '');
$login_email = (string) $get_field($login_info, 'email', '');
?>
<?= message_box('success'); ?>
<?= message_box('error'); ?>

<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-[0_12px_34px_-22px_rgba(15,23,42,0.45)]">
	<div class="border-b border-border bg-gradient-to-r from-slate-50/90 via-white to-slate-50/60 px-6 py-5 dark:from-slate-900/50 dark:via-card dark:to-slate-900/40">
		<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
			<div>
				<p class="text-xs font-semibold uppercase tracking-[0.14em] text-muted-foreground"><?= lang('user') ?></p>
				<h2 class="text-xl font-semibold text-foreground md:text-2xl"><?= !empty($user_id) ? lang('update') . ' ' . lang('user') : lang('create') . ' ' . lang('user') ?></h2>
				<p class="mt-1 text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('user') . ' ' . lang('details') ?></p>
			</div>
			<span class="inline-flex items-center rounded-full border border-indigo-200 bg-indigo-50 px-3 py-1 text-xs font-semibold uppercase tracking-wide text-indigo-700 dark:border-indigo-500/30 dark:bg-indigo-500/15 dark:text-indigo-200">
				<?= !empty($user_id) ? lang('edit') : lang('new') ?>
			</span>
		</div>
	</div>

	<div class="p-6 md:p-7">
		<?= form_open(base_url('client/user/save_user'), array('class' => 'space-y-6', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'role' => 'form')); ?>

		<div class="grid grid-cols-1 gap-6 xl:grid-cols-3">
			<div class="space-y-6 xl:col-span-2">
				<div class="rounded-xl border border-border bg-card p-5 shadow-sm transition-shadow duration-200 hover:shadow-md">
					<div class="mb-4 flex items-center gap-2 border-b border-border pb-3 text-sm font-semibold text-foreground">
						<i data-lucide="user-cog" class="h-4 w-4"></i>
						<span><?= lang('general') ?></span>
					</div>

					<input type="hidden" id="user_id" name="user_id" value="<?= !empty($login_user_id) ? (int) $login_user_id : '' ?>">
					<input type="hidden" name="account_details_id" value="<?= !empty($profile_account_details_id) ? (int) $profile_account_details_id : '' ?>">
					<input type="hidden" id="username_flag" value="">

					<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
						<?= form_input_group('fullname', lang('full_name'), $profile_fullname, 'text', true); ?>
						<?= form_input_group('username', lang('username'), $login_username, 'text', true); ?>
						<?= form_input_group('email', lang('email'), $login_email, 'email', true); ?>
						<?= form_input_group('password', lang('password'), '', 'password', empty($user_id)); ?>
						<?= form_input_group('confirm_password', lang('confirm_password'), '', 'password', empty($user_id)); ?>
						<?= form_input_group('phone', lang('phone'), $profile_phone, 'text', true); ?>
						<?= form_input_group('mobile', lang('mobile'), $profile_mobile, 'text', false); ?>
						<?= form_input_group('skype', lang('skype'), $profile_skype, 'text', false); ?>

						<?php
						$local_opts = array();
						$locals = $this->db->get('tbl_locales')->result();
						if (!empty($locals)) {
							foreach ($locals as $loc) {
								$local_opts[$loc->locale] = ucfirst($loc->locale);
							}
						}
						?>
						<?= form_select_group('locale', lang('locale'), $local_opts, $profile_locale, false); ?>

						<?php
						$lang_opts = array();
						if (!empty($languages)) {
							foreach ($languages as $lang) {
								$lang_opts[$lang->name] = ucfirst($lang->name);
							}
						}
						?>
						<?= form_select_group('language', lang('language'), $lang_opts, $profile_language, false); ?>
					</div>
				</div>
			</div>

			<div class="space-y-6">
				<div class="rounded-xl border border-border bg-card p-5 shadow-sm transition-shadow duration-200 hover:shadow-md">
					<div class="mb-4 flex items-center gap-2 border-b border-border pb-3 text-sm font-semibold text-foreground">
						<i data-lucide="image" class="h-4 w-4"></i>
						<span><?= lang('profile_image') ?></span>
					</div>
					<?= form_attachment_group('image', lang('image'), 'photo'); ?>
				</div>
			</div>
		</div>

		<div class="flex items-center gap-2 border-t border-border pt-4">
			<?= btn_update(!empty($user_id) ? lang('update') : lang('save'), 'md') ?>
			<?= btn_cancel('client/user/user_list', 'cancel', 'md', 'x', array('class' => 'mb-0 !h-9')); ?>
		</div>

		<?= form_close(); ?>
	</div>
</div>
