<?php
echo message_box('success');
echo message_box('error');

$all_customer_group = get_result('tbl_customer_group', array('type' => 'client'));
$curency = $this->client_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$created = can_action('4', 'created');
$edited  = can_action('4', 'edited');
$deleted = can_action('4', 'deleted');

$client_id    = !empty($client_info) ? $client_info->client_id : null;
$permissionL  = !empty($client_info->permission) ? $client_info->permission : null;
?>

<?php if (!empty($created) || !empty($edited)) : ?>
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<!-- Header -->
		<div class="px-6 py-5 flex justify-between items-start border-b border-border">
			<div>
				<h1 class="text-2xl font-bold text-foreground"><?= !empty($client_info) ? lang('edit') : lang('new_client'); ?></h1>
				<p class="text-sm text-muted-foreground"><?= lang('fill_form_fields'); ?></p>
			</div>
			<div class="flex flex-wrap gap-2">
				<?= btn_back('admin/client/manage_client'); ?>
				<?= btn_info('admin/client/import', lang('import') . ' ' . lang('client'), '', 'sm'); ?>
			</div>
		</div>

		<?php echo form_open(base_url('admin/client/save_client/' . $client_id), array('class' => 'p-6 space-y-6 ', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'role' => 'form')); ?>

		<div class="grid min-w-0 grid-cols-1 gap-8 xl:grid-cols-4">
			<!-- Main columns -->
			<div class="min-w-0 space-y-4 xl:col-span-2">

				<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
					<h4 class="text-sm font-semibold text-foreground flex items-center gap-2 font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
						<i data-lucide="info" class="h-5 w-5"></i>
						<?= lang('general'); ?>
					</h4>
					<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
						<?= form_input_group('name', lang('company_name'), !empty($client_info->name) ? $client_info->name : '', 'text', true, ['placeholder' => lang('company_name')]); ?>
						<?= form_input_group('email', lang('company_email'), !empty($client_info->email) ? $client_info->email : '', 'email', true); ?>
						<?= form_input_group('vat', lang('company_vat'), !empty($client_info->vat) ? $client_info->vat : '', 'text'); ?>
						<?php
						// $group_options = [];
						// if (!empty($all_customer_group)) {
						// 	foreach ($all_customer_group as $group) {
						// 		$group_options[$group->customer_group_id] = $group->customer_group;
						// 	}
						// }
						$selected_category_id = !empty($client_info->customer_group_id) ? $client_info->customer_group_id : null;
						$selected_category_label = null;
						if (!empty($client_info->customer_group_id)) {
							$group_info = $this->db->where('customer_group_id', $client_info->customer_group_id)->get('tbl_customer_group')->row();
							if (!empty($group_info)) {
								$selected_category_label = $group_info->customer_group;
							}
						}
						// inline add new customer group modal
						$new_group_html = form_lookup_add_link(
							'customer_group_id',
							base_url('admin/client/customer_group'),
							lang('create') . ' ' . lang('customer_group'),
							'sm'
						);
						?>
						<?= form_lookup_group('customer_group_id', lang('customer_group'), $selected_category_id, $selected_category_label, false, array(), '', array('type' => 'category', 'extra' => ['type' => 'client'], 'append' => $new_group_html)); ?>
						<?php
						$lang_opts = [];
						if (!empty($languages)) {
							foreach ($languages as $lang) {
								$lang_opts[$lang->name] = ucfirst($lang->name);
							}
						}
						?>
						<?= form_select_group('language', lang('language'), $lang_opts, !empty($client_info->language) ? $client_info->language : $this->config->item('language'), false); ?>
						<?php
						$curr_opts = [];
						if (!empty($currencies)) {
							foreach ($currencies as $currency) {
								$curr_opts[$currency->code] = $currency->name;
							}
						}
						?>
						<?= form_select_group(
							'currency',
							lang('currency'),
							$curr_opts,
							!empty($client_info->currency) ? $client_info->currency : config_item('default_currency'),
							false,
							['data-live-search' => 'true']
						); ?>

						<?= form_textarea_group('short_note', lang('short_note'), !empty($client_info->short_note) ? $client_info->short_note : '', 3, false); ?>
						<div class="flex items-center h-full">
							<label class="inline-flex items-center cursor-pointer gap-3 text-sm font-medium text-foreground">
								<input type="checkbox" name="sms_notification" value="1" class="sr-only peer"
									<?php if (!empty($client_info->sms_notification) && $client_info->sms_notification == 1) echo 'checked'; ?>>
								<span class="relative w-11 h-6 bg-slate-200 dark:bg-slate-600 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-blue-600"></span>
								<span><?= lang('received') . ' ' . lang('sms') . ' ' . lang('notification'); ?></span>
							</label>
						</div>
					</div>
				</div>
				<!-- Assign User Permission -->
				<?= form_permission_modern($assign_user, $permissionL); ?>
				<?= custom_form_Fields(12, $client_id); ?>
				<div class="space-y-5">
					<div class="bg-card p-5 rounded-xl border border-border shadow-sm space-y-3">
						<h4 class="text-sm font-semibold text-foreground flex items-center gap-2 font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
							<i data-lucide="settings" class="h-5 w-5"></i>
							<?= lang('actions'); ?>
						</h4>
						<div class="flex flex-col gap-2">
							<button type="submit" class="btn btn-primary btn-sm"><?= !empty($client_info) ? lang('updates') : lang('save'); ?></button>
							<?php if (empty($client_info)) : ?>
								<button type="submit" name="save_and_create_contact" value="1" class="btn btn-info btn-sm"><?= lang('save_and_create_contact'); ?></button>
							<?php endif; ?>
							<?= btn_cancel('admin/client/manage_client','cancel', 'md', 'x', array('class' => 'mb-0 !h-9')); ?>
						</div>
					</div>
				</div>
			</div>
			<div class="min-w-0 space-y-4 xl:col-span-2">
				<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700">
					<h4 class="font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700 flex items-center gap-2 font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
						<i data-lucide="phone" class="h-5 w-5"></i> <?= lang('client_contact') . ' ' . lang('details'); ?>
					</h4>
					<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
						<?= form_input_group('phone', lang('company_phone'), !empty($client_info->phone) ? $client_info->phone : '', 'text'); ?>
						<?= form_input_group('mobile', lang('company_mobile'), !empty($client_info->mobile) ? $client_info->mobile : '', 'text'); ?>
						<?= form_input_group('zipcode', lang('zipcode'), !empty($client_info->zipcode) ? $client_info->zipcode : '', 'text'); ?>
						<?= form_input_group('city', lang('company_city'), !empty($client_info->city) ? $client_info->city : '', 'text'); ?>
						<?php
						$country_opts = [];
						if (!empty($countries)) {
							foreach ($countries as $c) {
								$country_opts[$c->value] = $c->value;
							}
						}
						$default_country   = $this->config->item('company_country');
						$selected_country  = !empty($client_info->country) ? $client_info->country : $default_country;
						?>
						<?= form_select_group('country', lang('company_country'), $country_opts, $selected_country, false); ?>
						<?= form_input_group('fax', lang('company_fax'), !empty($client_info->fax) ? $client_info->fax : '', 'text'); ?>
						<?= form_textarea_group('address', lang('company_address'), !empty($client_info->address) ? $client_info->address : '', 2, false); ?>
						<?= form_input_group('latitude', lang('latitude') . ' (' . lang('google_map') . ')', !empty($client_info->latitude) ? $client_info->latitude : '', 'text'); ?>
						<?= form_input_group('longitude', lang('longitude') . ' (' . lang('google_map') . ')', !empty($client_info->longitude) ? $client_info->longitude : '', 'text'); ?>
					</div>
				</div>
				<!-- Web and Hosting -->
				<div class="grid grid-cols-1 gap-6 xl:grid-cols-2">
					<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700 space-y-2">
						<h4 class="font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700 flex items-center gap-2 font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
							<i data-lucide="globe" class="h-5 w-5"></i> <?= lang('web'); ?>
						</h4>
						<?= form_input_group('website', lang('company_domain'), !empty($client_info->website) ? $client_info->website : '', 'text'); ?>
						<?= form_input_group('skype_id', lang('skype_id'), !empty($client_info->skype_id) ? $client_info->skype_id : '', 'text'); ?>
						<?= form_input_group('facebook', lang('facebook_profile_link'), !empty($client_info->facebook) ? $client_info->facebook : '', 'text'); ?>
						<?= form_input_group('twitter', lang('twitter_profile_link'), !empty($client_info->twitter) ? $client_info->twitter : '', 'text'); ?>
						<?= form_input_group('linkedin', lang('linkedin_profile_link'), !empty($client_info->linkedin) ? $client_info->linkedin : '', 'text'); ?>
					</div>

					<div class="bg-slate-50 p-4 rounded-xl border border-slate-200 dark:bg-slate-800 dark:border-slate-700 space-y-2">
						<h4 class="font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700 flex items-center gap-2 font-semibold text-sm text-slate-900 mb-3 border-b border-slate-200 pb-2 dark:text-slate-100 dark:border-slate-700">
							<i data-lucide="server" class="h-5 w-5"></i> <?= lang('hosting'); ?>
						</h4>
						<?= form_input_group('hosting_company', lang('hosting_company'), !empty($client_info->hosting_company) ? $client_info->hosting_company : '', 'text'); ?>
						<?= form_input_group('hostname', lang('hostname'), !empty($client_info->hostname) ? $client_info->hostname : '', 'text'); ?>
						<?= form_input_group('username', lang('username'), !empty($client_info->username) ? $client_info->username : '', 'text'); ?>
						<?php
						$password_placeholder = '';
						if (!empty($client_info->password)) {
							$password_placeholder = str_repeat('*', strlen(decrypt($client_info->password)));
						}
						?>
						<?= form_input_group('password', lang('password'), '', 'password', false, ['placeholder' => $password_placeholder]); ?>
						<?php if (!empty($client_info->password)) : ?>
							<a class="text-xs text-primary hover:underline" data-toggle="modal" data-target="#myModal" href="<?= base_url('admin/client/see_password/c_' . $client_info->client_id); ?>"><?= lang('see_password'); ?></a>
						<?php endif; ?>
						<?= form_input_group('port', lang('port'), !empty($client_info->port) ? $client_info->port : '', 'text'); ?>
						
					</div>
				</div>
			</div>

		</div>

		<!-- Side actions -->

	</div>

	<?php echo form_close(); ?>
	</div>
<?php endif; ?>
