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

<?php
$all_customer_group = array();
$all_client = array();
$created = can_action('57', 'created');
$edited = can_action('57', 'edited');

$projects_id = !empty($project_info) ? $project_info->project_id : null;
$project_no = '';
if (!empty($project_info->project_no)) {
	$project_no = $project_info->project_no;
} else {
	if (empty(config_item('projects_number_format'))) {
		$project_no .= config_item('projects_prefix');
	}
	$project_no .= $this->project_model->generate_projects_number();
}

$project_permissions = $this->db->get('tbl_project_settings')->result();
$get_permissions = !empty($project_info->project_settings)
	? normalize_project_settings($project_info->project_settings)
	: array();

$permissionL = !empty($project_info->permission) ? $project_info->permission : null;
$progress_value = !empty($project_info) ? $this->project_model->get_project_progress($project_info->project_id) : 0;
$estimate_hours = '';
if (!empty($project_info->estimate_hours)) {
	$parts = explode(':', $project_info->estimate_hours);
	$estimate_hours = isset($parts[0]) ? $parts[0] : '0';
	if (isset($parts[1])) {
		$estimate_hours .= '.' . $parts[1];
	}
}

$billing_type = !empty($project_info->billing_type) ? $project_info->billing_type : 'fixed_rate';
$status_val = !empty($project_info->project_status) ? $project_info->project_status : 'started';
$back_uri = !empty($project_info->project_id)
	? 'admin/projects/project_details/' . $project_info->project_id
	: 'admin/projects';
$project_tags_value = !empty($project_info->tags) ? $project_info->tags : '';

$selected_client_id = !empty($project_info->client_id) ? $project_info->client_id : (!empty($client_id) ? $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();
	if (!empty($client_row)) {
		$selected_client_label = $client_row->name;
	}
}
$selected_category_id = !empty($project_info->category_id) ? $project_info->category_id : '';
$selected_category_label = '';
if (!empty($selected_category_id)) {
	$category_row = $this->db->select('customer_group')->where('customer_group_id', $selected_category_id)->get('tbl_customer_group')->row();
	if (!empty($category_row)) {
		$selected_category_label = $category_row->customer_group;
	}
}

$new_client_html = form_lookup_add_link(
	'client_id',
	base_url('admin/client/new_client'),
	lang('create') . ' ' . lang('client'),
);

$new_category_html = form_lookup_add_link(
	'category_id',
	base_url('admin/projects/new_category'),
	lang('create') . ' ' . lang('categories'),
	'sm'
);
?>

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

<?php if (!empty($created) || !empty($edited)) : ?>
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="px-6 py-5 flex justify-between items-start border-b border-border">
			<div class="flex items-center">
				<div>
					<h3 class="text-xl font-bold text-foreground"><?= lang('new_project'); ?></h3>
					<p class="text-sm mt-1 text-muted-foreground"><?= lang('fill_form_fields'); ?></p>
				</div>
			</div>
			<div class="flex flex-wrap gap-2">
				<?= btn_back($back_uri); ?>
				<?= btn_info('admin/projects/import', lang('import') . ' ' . lang('project'), '', 'sm'); ?>

			</div>
		</div>

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

		<div class="grid min-w-0 grid-cols-1 gap-6 lg:grid-cols-3">
			<!-- Left: primary fields -->
			<div class="min-w-0 lg:col-span-2 space-y-5">
				<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-5 mb-4">
						<?= form_input_group('project_name', lang('project_name'), !empty($project_info->project_name) ? $project_info->project_name : '', 'text', true, ['placeholder' => 'e.g. Q4 Marketing Campaign']); ?>
						<?= form_input_group('project_no', lang('project_no'), $project_no, 'text', true, ['readonly' => true]); ?>
						<?= form_lookup_group('client_id', lang('client'), $selected_client_id, $selected_client_label, true, array(), '', array('type' => 'client', 'append' => $new_client_html)); ?>
						<?= form_lookup_group('category_id', lang('categories'), $selected_category_id, $selected_category_label, false, array(), '', array('type' => 'category', 'extra' => ['type' => 'projects'], 'append' => $new_category_html)); ?>
						<?= form_select_group('project_status', lang('status'), array(
							'started' => lang('started'),
							'in_progress' => lang('in_progress'),
							'on_hold' => lang('on_hold'),
							'cancel' => lang('cancel'),
							'completed' => lang('completed'),
						), $status_val, true, array('multiple' => true, 'data-search' => true)); ?>
						<?= form_input_group('demo_url', lang('demo_url'), !empty($project_info->demo_url) ? $project_info->demo_url : '', 'url', false); ?>
						<?= form_datepicker('start_date', lang('start_date'), !empty($project_info->start_date) ? date('Y-m-d', safe_strtotime($project_info->start_date)) : '', true, ['id' => 'start-date', 'data-max' => '#end-date', 'data-auto-close' => 'true']); ?>
						<?= form_datepicker('end_date', lang('end_date'), !empty($project_info->end_date) ? date('Y-m-d', safe_strtotime($project_info->end_date)) : '', true, ['id' => 'end-date', 'data-min' => '#start-date', 'data-auto-close' => 'true']); ?>
					</div>
					<?= form_editor('description', lang('description'), !empty($project_info->description) ? $project_info->description : '', false); ?>
					<div class="flex items-center gap-2 mt-6">
						<?= btn_update(!empty($project_info) ? lang('updates') : lang('save'), 'md') ?>
						<?= btn_cancel('admin/client/manage_client', 'cancel', 'md', 'x', array('class' => 'mb-0 !h-9')); ?>
					</div>
				</div>
			</div>

			<!-- Right: side panels -->
			<div class="min-w-0 space-y-5">
				<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="dollar-sign" class="h-5 w-5"></i>
						<?= lang('financials'); ?>
					</h4>
					<div class="space-y-3">
						<?= form_input_group('project_cost', lang('project_cost'), !empty($project_info->project_cost) ? $project_info->project_cost : '', 'number', false, ['step' => '0.01'], '$'); ?>
						<?= form_input_group('hourly_rate', lang('project_hourly_rate'), !empty($project_info->hourly_rate) ? $project_info->hourly_rate : '', 'number', false, ['step' => '0.01'], '$'); ?>
						<?= form_select_group('billing_type', lang('billing_type'), array(
							'fixed_rate' => lang('fixed_rate'),
							'project_hours' => lang('only') . ' ' . lang('project_hours'),
							'tasks_hours' => lang('only') . ' ' . lang('tasks_hours'),
							'tasks_and_project_hours' => lang('tasks_and_project_hours'),
						), $billing_type, true); ?>
						<?= form_input_group('estimate_hours', lang('estimate_hours'), $estimate_hours, 'number', false, ['step' => '0.01'], '', 'hrs'); ?>
						<?= form_progress_slider('progress', lang('progress'), $progress_value, 0, 100, ['id' => 'progress_slider_main']); ?>

						<div class="flex items-center gap-3">
							<?= form_checkbox('calculate_progress', lang('through_tasks'), !empty($project_info->calculate_progress) && $project_info->calculate_progress == 'through_tasks' ? 'checked' : '', false, ['class' => 'calculate_progress select_one']); ?>
							<?= form_checkbox('calculate_progress', lang('through_project_hours'), !empty($project_info->calculate_progress) && $project_info->calculate_progress == 'through_project_hours' ? 'checked' : '', false, ['class' => 'calculate_progress select_one']); ?>
						</div>
						<?= form_tags_group($project_tags_value); ?>
					</div>
				</div>
				<?= form_permission_modern($assign_user, $permissionL, lang('assined_to')); ?>



				<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="settings" class="h-5 w-5"></i>
						<?= lang('project_settings'); ?>
					</h4>
					<div class="space-y-2">
						<?php foreach ($project_permissions as $v_permissions) :
							$checked = '';
							if (!empty($project_info->project_settings)) {
								if (in_array($v_permissions->settings, $get_permissions)) {
									$checked = 'checked';
								}
							} else {
								$checked = 'checked';
							}
						?>
							<?= form_checkbox($v_permissions->settings_id, lang($v_permissions->settings), $checked); ?>
						<?php endforeach; ?>
					</div>
				</div>

				<?= custom_form_Fields(4, $projects_id); ?>
			</div>
		</div>

		<script>
			// 
			<?php if (empty($project_info)) { ?>
				$('#hourly_rate').closest('label').hide();
			<?php } ?>
			// document on change billing type
			$('#billing_type').on('change', function() {
				var val = $(this).val();
				if (val == 'fixed_rate') {
					$('#project_cost').closest('label').show();
					$('#hourly_rate').closest('label').hide();
				} else if (val == 'tasks_hours') {
					$('#hourly_rate').closest('label').hide();
					$('#project_cost').closest('label').hide();
				} else {
					$('#hourly_rate').closest('label').show();
					$('#project_cost').closest('label').hide();
				}
				if (val == 'project_hours') {
					// hide based on tasks hour
				}
			});

			// click to calculate_progress checkbox then disable the progress slider
			$('.calculate_progress').on('click', function() {
				$('.calculate_progress').not(this).prop('checked', false);
				if ($('.calculate_progress:checked').length > 0) {
					$('#progress_slider_main').prop('disabled', true);
					// add opacity to the progress slider
					$('#progress_slider_main').closest('.form-group').css('opacity', '0.5');
				} else {
					$('#progress_slider_main').prop('disabled', false);
				}
			});
		</script>

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