<div class="invoice-modern">
<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$created = can_action('51', 'created');
$edited = can_action('51', 'edited');
$deleted = can_action('51', 'deleted');

$invoice_info = isset($invoice_info) ? $invoice_info : null;
$project_info = isset($project_info) ? $project_info : null;
$invoice_id = !empty($invoice_info) ? $invoice_info->invoices_id : null;

$client_id = isset($client_id) ? $client_id : '';
if (empty($client_id)) {
	if (!empty($project_info) && !empty($project_info->client_id)) {
		$client_id = $project_info->client_id;
	} elseif (!empty($invoice_info) && !empty($invoice_info->client_id)) {
		$client_id = $invoice_info->client_id;
	} elseif (!empty($c_id)) {
		$client_id = $c_id;
	}
}
$client_label = isset($client_label) ? $client_label : '';
if (!empty($client_id) && empty($client_label)) {
	$client_row = $this->db->select('name')->where('client_id', $client_id)->get('tbl_client')->row();
	$client_label = !empty($client_row->name) ? $client_row->name : '';
}

$project_id = isset($project_id) ? $project_id : '';
if (empty($project_id)) {
	if (!empty($project_info) && !empty($project_info->project_id)) {
		$project_id = $project_info->project_id;
	} elseif (!empty($invoice_info) && !empty($invoice_info->project_id)) {
		$project_id = $invoice_info->project_id;
	}
}
$project_label = isset($project_label) ? $project_label : '';
if (!empty($project_id) && empty($project_label)) {
	$project_row = $this->db->select('project_name')->where('project_id', $project_id)->get('tbl_project')->row();
	$project_label = !empty($project_row->project_name) ? $project_row->project_name : '';
}

$reference_no = '';
if (!empty($invoice_info) && !empty($invoice_info->reference_no)) {
	$reference_no = $invoice_info->reference_no;
} else {
	if (empty(config_item('invoice_number_format'))) {
		$reference_no .= config_item('invoice_prefix');
	}
	if (config_item('increment_invoice_number') == 'FALSE') {
		$this->load->helper('string');
		$reference_no .= random_string('nozero', 6);
	} else {
		$reference_no .= $this->invoice_model->generate_invoice_number();
	}
}

$invoice_date = !empty($invoice_info) && !empty($invoice_info->invoice_date) ? $invoice_info->invoice_date : date('Y-m-d');
$due_date = !empty($invoice_info) && !empty($invoice_info->due_date) ? $invoice_info->due_date : date('Y-m-d', safe_strtotime('+' . config_item('invoices_due_after') . ' days'));

$recur_start_date = (!empty($invoice_info) && $invoice_info->recurring == 'Yes') ? date('Y-m-d', safe_strtotime($invoice_info->recur_start_date)) : date('Y-m-d');
$recur_end_date = (!empty($invoice_info) && $invoice_info->recurring == 'Yes') ? date('Y-m-d', safe_strtotime($invoice_info->recur_end_date)) : date('Y-m-d');
$recur_frequency = !empty($invoice_info) && !empty($invoice_info->recur_frequency) ? $invoice_info->recur_frequency : 'none';

$discount_options = array(
	'' => lang('no') . ' ' . lang('discount'),
	'before_tax' => lang('before_tax'),
	'after_tax' => lang('after_tax'),
);
$discount_type = !empty($invoice_info) && !empty($invoice_info->discount_type) ? $invoice_info->discount_type : '';

$all_user = $this->db->where('role_id != ', 2)->order_by('sort_user', 'ASC')->get('tbl_users')->result();
$agent_options = array('' => lang('select') . ' ' . lang('sales') . ' ' . lang('agent'));
if (!empty($all_user)) {
	foreach ($all_user as $v_user) {
		$profile_info = $this->db->where('user_id', $v_user->user_id)->get('tbl_account_details')->row();
		if (!empty($profile_info)) {
			$agent_options[$v_user->user_id] = $profile_info->fullname;
		}
	}
}
$selected_agent = !empty($invoice_info) && !empty($invoice_info->user_id) ? $invoice_info->user_id : $this->session->userdata('user_id');

$permissionL = !empty($invoice_info) && !empty($invoice_info->permission) ? $invoice_info->permission : null;

$warehouse_list = $this->db->where('status', 'published')->get('tbl_warehouse')->result();
$warehouse_options = array('' => lang('select') . ' ' . lang('warehouse'));
if (!empty($warehouse_list)) {
	foreach ($warehouse_list as $warehouse) {
		$warehouse_options[$warehouse->warehouse_id] = $warehouse->warehouse_name;
	}
}
$warehouse_selected = !empty($invoice_info) && !empty($invoice_info->warehouse_id) ? $invoice_info->warehouse_id : '';

$client_options = array('' => lang('select') . ' ' . lang('client'));
if (!empty($all_client)) {
	foreach ($all_client as $v_client) {
		$client_options[$v_client->client_id] = ucfirst($v_client->name);
	}
}
$client_selected = !empty($client_id) ? $client_id : '';

$project_options = array('' => lang('none'));
if (!empty($client_id)) {
	$all_project = $this->db->where('client_id', $client_id)->get('tbl_project')->result();
	if (!empty($all_project)) {
		foreach ($all_project as $v_project) {
			$project_options[$v_project->project_id] = $v_project->project_name;
		}
	}
}
$project_selected = !empty($project_id) ? $project_id : '';

$client_created = can_action('4', 'created');
$new_client_link = '';
if (!empty($client_created)) {
	$new_client_link = form_lookup_add_link('client_id', base_url('admin/client/new_client'), lang('new') . ' ' . lang('client'), 'md');
}

$notes = !empty($invoice_info) && !empty($invoice_info->notes) ? $invoice_info->notes : $this->config->item('default_terms');
?>

<?php if (!empty($created) || !empty($edited)) { ?>
	

	<div class="space-y-6">
		<div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
			<div class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
				<div>
					<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('recurring_invoice') ?></p>
					<h1 class="text-2xl font-semibold text-foreground"><?= !empty($invoice_id) ? lang('edit') . ' ' . lang('recurring_invoice') : lang('create_invoice') ?></h1>
					<p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('recurring_invoice') ?></p>
				</div>
				<div class="flex flex-wrap items-center gap-2">
					<a href="<?= base_url('admin/invoice/recurring_invoice') ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-sm font-medium text-foreground hover:bg-muted">
						<i data-lucide="list" class="h-4 w-4"></i>
						<?= lang('recurring_invoice') ?>
					</a>
					<?php if (!empty($invoice_id)) { ?>
						<a href="<?= base_url('admin/invoice/createrecuringinvoice') ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-sm font-medium text-foreground hover:bg-muted">
							<i data-lucide="plus" class="h-4 w-4"></i>
							<?= lang('create_invoice') ?>
						</a>
					<?php } ?>
				</div>
			</div>
		</div>

		<?php if (!empty($invoice_info) && !empty($invoices_to_merge)) { ?>
			<div class="rounded-2xl border border-border bg-card p-6 shadow-sm" id="invoice_top_info">
				<?php $this->load->view('admin/invoice/merge_invoice', array('invoices_to_merge' => $invoices_to_merge)); ?>
			</div>
		<?php } ?>

		<?= form_open(base_url('admin/invoice/save_invoice/' . $invoice_id), array('class' => 'space-y-6', 'data-parsley-validate' => '', 'novalidate' => '', 'enctype' => 'multipart/form-data')); ?>
		<div class="grid grid-cols-1 xl:grid-cols-12 gap-6">
			<div class="xl:col-span-8 space-y-6">
				<div class="rounded-xl border border-border bg-card p-6 space-y-5">
					<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
						<i data-lucide="file-text" class="h-5 w-5"></i>
						<h2 class="text-base font-semibold text-foreground"><?= lang('invoice') . ' ' . lang('details') ?></h2>
					</div>
					<div class="grid grid-cols-1 md:grid-cols-2 gap-5">
						<?= form_input_group('reference_no', lang('reference_no'), $reference_no, 'text', true, array('placeholder' => lang('reference_no'))); ?>
						<?= form_datepicker('invoice_date', lang('invoice_date'), $invoice_date, true, array('data-max' => '#due_date', 'data-auto-close' => 'true')); ?>
						<?= form_datepicker('due_date', lang('due_date'), $due_date, true, array('data-min' => '#invoice_date', 'data-auto-close' => 'true')); ?>
						<?= form_select_group('discount_type', lang('discount_type'), $discount_options, $discount_type, false, array('data-discount-type' => '1')); ?>
						<?php
						$client_lookup = form_lookup_group(
							'client_id',
							lang('client'),
							$client_id,
							$client_label,
							true,
							array('placeholder' => lang('select') . ' ' . lang('client')),
							'md:col-span-1',
							array(
								'type' => 'client',
								'append' => $new_client_link,
							)
						);
						$project_lookup = form_lookup_group(
							'project_id',
							lang('project'),
							$project_id,
							$project_label,
							false,
							array('placeholder' => lang('select') . ' ' . lang('project')),
							'md:col-span-1',
							array(
								'type' => 'project',
								'depends' => 'client_id',
							)
						);
						?>
						<?= $client_lookup ?>
						<?= $project_lookup ?>
						<?= form_select_group('warehouse_id', lang('warehouse'), $warehouse_options, $warehouse_selected, false, array('class' => 'select_box', 'data-live-search' => 'true')); ?>
						<?= form_select_group('user_id', lang('sales') . ' ' . lang('agent'), $agent_options, $selected_agent, true, array('class' => 'select_box', 'data-live-search' => 'true')); ?>
					</div>
				</div>

				<div class="rounded-xl border border-border bg-card p-6 space-y-5">
					<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
						<i data-lucide="repeat" class="h-5 w-5"></i>
						<h2 class="text-base font-semibold text-foreground"><?= lang('recurring') . ' ' . lang('details') ?></h2>
					</div>
					<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
						<?= form_select_group(
							'recuring_frequency',
							lang('recur_frequency'),
							array(
								'none' => lang('none'),
								'7D' => lang('week'),
								'1M' => lang('month'),
								'3M' => lang('quarter'),
								'6M' => lang('six_months'),
								'1Y' => lang('1year'),
								'2Y' => lang('2year'),
								'3Y' => lang('3year'),
							),
							$recur_frequency
						); ?>
						<?= form_datepicker('recur_start_date', lang('start_date'), $recur_start_date, false, array('data-format' => config_item('date_picker_format'))); ?>
						<?= form_datepicker('recur_end_date', lang('end_date'), $recur_end_date, false, array('data-format' => config_item('date_picker_format'))); ?>
					</div>
				</div>

				<div class="rounded-xl border border-border bg-card p-6 space-y-5">
					<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
						<i data-lucide="align-left" class="h-5 w-5"></i>
						<h2 class="text-base font-semibold text-foreground"><?= lang('notes') ?></h2>
					</div>
					<?= form_editor('notes', lang('notes'), $notes, false, array('rows' => 5)); ?>
				</div>
			</div>

			<aside class="xl:col-span-4 space-y-6">
				<div class="rounded-xl border border-border bg-card p-6 space-y-5">
					<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-3">
						<i data-lucide="settings" class="h-5 w-5"></i>
						<h2 class="text-base font-semibold text-foreground"><?= lang('settings') ?></h2>
					</div>
					<div class="grid grid-cols-1 gap-4">
						<?php
						$all_payment = get_result('tbl_online_payment');
						if (!empty($all_payment)) {
							foreach ($all_payment as $payment) {
								$allow_gateway = 'allow_' . slug_it(strtolower($payment->gateway_name));
								$gateway_status = slug_it(strtolower($payment->gateway_name)) . '_status';
								if (config_item($gateway_status) == 'active') {
						?>
							<div class="flex items-center gap-2 text-sm">
								<input type="checkbox" class="form-checkbox" name="<?= $allow_gateway ?>" value="Yes" <?= (!empty($invoice_info) && $invoice_info->$allow_gateway == 'Yes') ? 'checked' : '' ?> />
								<span><?= lang($allow_gateway) ?></span>
							</div>
						<?php
								}
							}
						}
						?>
					</div>
					<?php if (!empty($project_selected)) : ?>
						<div class="pt-2">
							<?= form_switch_radio('client_visible', lang('visible_to_client'), !empty($invoice_info) ? $invoice_info->client_visible : 'No'); ?>
						</div>
					<?php endif; ?>
				</div>

				<?= form_permission_modern($permission_user, $permissionL, lang('assigned_to')); ?>

				<div class="rounded-xl border border-border bg-card p-6 space-y-4">
					<div class="flex items-center gap-2 text-sm font-semibold text-foreground border-b border-border pb-2">
						<i data-lucide="list" class="h-5 w-5"></i>
						<h2 class="text-base font-semibold text-foreground"><?= lang('custom_field') ?></h2>
					</div>
					<?= custom_form_Fields(9, $invoice_id); ?>
				</div>
			</aside>
		</div>

		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<?php
			$pdata['itemType'] = 'invoice';
			if (!empty($invoice_info)) {
				$pdata['add_items'] = $this->invoice_model->ordered_items_by_id($invoice_info->invoices_id, 'invoices', true);
				$pdata['info'] = $invoice_info;
			}
			$this->load->view('admin/items/selectItem', $pdata);
			?>
		</div>

		<?= form_close(); ?>
	</div>
<?php } else { ?>
	<div class="rounded-2xl border border-border bg-card p-6 text-sm text-muted-foreground">
		<?= lang('you_dont_have_permission') ?>
	</div>
<?php } ?>

</div>
