<?= message_box('success'); ?>
<?= message_box('error'); ?>

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

$purchase_info = isset($purchase_info) ? $purchase_info : null;
$purchase_id = (!empty($purchase_info) && isset($purchase_info->purchase_id)) ? $purchase_info->purchase_id : null;

$reference_no = '';
if (!empty($purchase_info) && !empty($purchase_info->reference_no)) {
	$reference_no = $purchase_info->reference_no;
} else {
	if (empty(config_item('proposal_number_format'))) {
		$reference_no .= config_item('purchase_prefix');
	}
	if (config_item('increment_purchase_number') == 'FALSE') {
		$this->load->helper('string');
		$reference_no .= random_string('nozero', 6);
	} else {
		$reference_no .= $this->purchase_model->generate_purchase_number();
	}
}

$purchase_date = !empty($purchase_info) && !empty($purchase_info->purchase_date) ? $purchase_info->purchase_date : date('Y-m-d');
$due_date = !empty($purchase_info) && !empty($purchase_info->due_date) ? $purchase_info->due_date : date('Y-m-d');

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

$supplier_selected = !empty($purchase_info->supplier_id) ? $purchase_info->supplier_id : '';
$supplier_label = '';
if (!empty($supplier_selected) && !empty($all_supplier)) {
	foreach ($all_supplier as $v_supplier) {
		if ((string) $v_supplier->supplier_id === (string) $supplier_selected) {
			$supplier_label = $v_supplier->name;
			break;
		}
	}
}

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

$_created = can_action('151', 'created');
$new_supplier_link = '';
if (!empty($_created)) {
	$new_supplier_link = form_lookup_add_link('supplier_id', base_url('admin/supplier/new_supplier'), lang('new') . ' ' . lang('supplier'), 'md');
}

$all_user = get_staff_details();
$selected_agent = !empty($purchase_info->user_id) ? $purchase_info->user_id : $this->session->userdata('user_id');
$selected_agent_label = '';
if (!empty($selected_agent)) {
	$agent_profile = $this->db->where('user_id', $selected_agent)->get('tbl_account_details')->row();
	if (!empty($agent_profile)) {
		$selected_agent_label = $agent_profile->fullname;
	}
}

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

$notes = '';
if (!empty($purchase_info) && !empty($purchase_info->notes)) {
	$notes = $purchase_info->notes;
} else {
	$notes = $this->config->item('purchase_notes');
}

$warehouse_options = array('' => lang('select') . ' ' . lang('warehouse'));
$warehouse_selected = !empty($purchase_info->warehouse_id) ? $purchase_info->warehouse_id : '';
$usertype = profile();
if (!empty($usertype) && $usertype->role_id == 3) {
	$warehouse_list = $this->admin_model->select_data('tbl_warehouse', 'warehouse_id', 'warehouse_name', array('status' => 'published', 'warehouse_id' => $this->session->userdata['warehouse_id']));
	$warehouse_selected = $this->session->userdata['warehouse_id'];
} else {
	$warehouse_list = $this->admin_model->select_data('tbl_warehouse', 'warehouse_id', 'warehouse_name', array('status' => 'published'));
}
if (!empty($warehouse_list)) {
	foreach ($warehouse_list as $key => $label) {
		$warehouse_options[$key] = $label;
	}
}

$new_warehouse_link = '';
if (!empty($usertype) && $usertype->role_id == 1) {
	$new_warehouse_link = form_lookup_add_link('warehouse_id', base_url('admin/warehouse/create/0/from_warehouse_id'), lang('new') . ' ' . lang('warehouse'), 'xl');
}
		
$warehouse_label = '';
if (!empty($warehouse_selected) && !empty($warehouse_options[$warehouse_selected])) {
	$warehouse_label = $warehouse_options[$warehouse_selected];
}
?>

<?php if (!empty($created) || !empty($edited)) { ?>
	<script src="<?php echo base_url(); ?>assets/plugins/bootstrap-tagsinput/fm.tagator.jquery.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.js"></script>

	<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('purchase') ?></p>
					<h1 class="text-2xl font-semibold text-foreground"><?= !empty($purchase_id) ? lang('edit') . ' ' . lang('purchase') : lang('new') . ' ' . lang('purchase') ?></h1>
					<p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('purchase') ?></p>
				</div>
				<div class="flex flex-wrap items-center gap-2">
					<?php if (!empty($project_selected)) { ?>
						<a href="<?= base_url('admin/projects/project_details/' . $project_selected . '/purchase') ?>" 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="briefcase" class="h-4 w-4"></i>
							<?= lang('project') ?>
						</a>
					<?php } ?>
					<a href="<?= base_url('admin/purchase') ?>" 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('all') . ' ' . lang('purchase') ?>
					</a>
					<?php if (!empty($purchase_id)) { ?>
						<a href="<?= base_url('admin/purchase/new_purchase') ?>" 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('new') . ' ' . lang('purchase') ?>
						</a>
					<?php } ?>
				</div>
			</div>
		</div>

		<?= form_open(base_url('admin/purchase/save_purchase/' . $purchase_id), array('class' => 'space-y-6', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => '', 'role' => 'form')); ?>
		<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('purchase') . ' ' . 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_lookup_group(
							'supplier_id',
							lang('supplier'),
							$supplier_selected,
							$supplier_label,
							true,
							array('placeholder' => lang('search') . ' ' . lang('supplier')),
							'md:col-span-1',
							array(
								'type' => 'supplier',
								'append' => $new_supplier_link,
							)
						); ?>
						<?= form_lookup_group(
							'project_id',
							lang('project'),
							$project_selected,
							$project_label,
							false,
							array('placeholder' => lang('search') . ' ' . lang('project')),
							'md:col-span-1',
							array('type' => 'project')
						); ?>
						<?= form_datepicker('purchase_date', lang('purchase') . ' ' . lang('date'), $purchase_date, true, array('data-max' => '#due_date', 'data-auto-close' => 'true')); ?>
						<?= form_datepicker('due_date', lang('due_date'), $due_date, false, array('data-min' => '#purchase_date', 'data-auto-close' => 'true')); ?>
						<?= form_lookup_group(
							'warehouse_id',
							lang('warehouse'),
							$warehouse_selected,
							$warehouse_label,
							true,
							array('placeholder' => lang('search') . ' ' . lang('warehouse')),
							'md:col-span-1',
							array(
								'type' => 'warehouse',
								'append' => $new_warehouse_link,
							)
						); ?>
						<?= form_lookup_group(
							'user_id',
							lang('purchase') . ' ' . lang('agent'),
							$selected_agent,
							$selected_agent_label,
							true,
							array('placeholder' => lang('search') . ' ' . lang('agent')),
							'md:col-span-1',
							array('type' => 'staff')
						); ?>
					</div>
					<input type="hidden" name="warehouseId" class="WarehouseValue" value="<?= html_escape($warehouse_selected) ?>">
				</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="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 md:grid-cols-2 gap-5">
						<?= form_switch_radio('update_stock', lang('update_stock'), (!empty($purchase_info) && isset($purchase_info->update_stock)) ? $purchase_info->update_stock : 'Yes'); ?>
						<?= form_select_group('discount_type', lang('discount_type'), $discount_options, $discount_type, false, array('data-discount-type' => '1')); ?>
						<?= form_tags_group($tags); ?>
					</div>
					<?= form_editor('notes', lang('notes'), $notes, false, array('rows' => 5)); ?>
				</div>
			</div>

			<aside class="xl:col-span-4 space-y-6">
				<?= 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(20, $purchase_id); ?>
				</div>
			</aside>
		</div>

		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<?php
			$pdata['itemType'] = 'purchase';
			if (!empty($purchase_info) && !empty($purchase_id)) {
				$pdata['add_items'] = $this->purchase_model->ordered_items_by_id($purchase_info->purchase_id, true);
				$pdata['info'] = $purchase_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 } ?>
