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

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

$estimate_info = isset($estimates_info) ? $estimates_info : null;
$project_info = isset($project_info) ? $project_info : null;

$estimate_id = !empty($estimate_info) && !empty($estimate_info->estimates_id) ? $estimate_info->estimates_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($estimate_info) && !empty($estimate_info->client_id)) {
		$client_id = $estimate_info->client_id;
	} elseif (!empty($c_id)) {
		$client_id = $c_id;
	}
}

$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($estimate_info) && !empty($estimate_info->project_id)) {
		$project_id = $estimate_info->project_id;
	}
}

$client_label = isset($client_label) ? $client_label : '';
if (empty($client_label) && !empty($client_id)) {
	$client_label = client_name($client_id);
}

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

$reference_no = '';
if (!empty($estimate_info) && !empty($estimate_info->reference_no)) {
	$reference_no = $estimate_info->reference_no;
} else {
	if (empty(config_item('estimate_number_format'))) {
		$reference_no .= config_item('estimate_prefix');
	}
	if (config_item('increment_estimate_number') == 'FALSE') {
		$this->load->helper('string');
		$reference_no .= random_string('nozero', 6);
	} else {
		$reference_no .= $this->estimates_model->generate_estimate_number();
	}
}

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

$status_selected = !empty($estimate_info) && !empty($estimate_info->status) ? $estimate_info->status : 'draft';

$warehouse_list = $this->db->where('status', 'published')->get('tbl_warehouse')->result();
$warehouse_selected = !empty($estimate_info) && !empty($estimate_info->warehouse_id) ? $estimate_info->warehouse_id : '';
$warehouse_label = '';
if (!empty($warehouse_selected)) {
	$warehouse_label = get_row('tbl_warehouse', array('warehouse_id' => $warehouse_selected), 'warehouse_name');
}

$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($estimate_info) && !empty($estimate_info->user_id) ? $estimate_info->user_id : $this->session->userdata('user_id');
$selected_agent_label = '';
if (!empty($selected_agent)) {
	$selected_agent_label = get_row('tbl_account_details', array('user_id' => $selected_agent), 'fullname');
}

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

$tags_value = !empty($estimate_info) && !empty($estimate_info->tags) ? $estimate_info->tags : '';
$notes_value = !empty($estimate_info) && !empty($estimate_info->notes) ? $estimate_info->notes : $this->config->item('estimate_terms');

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

$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');
}

$estimate_status_options = array(
	'draft' => lang('draft'),
	'sent' => lang('sent'),
	'expired' => lang('expired'),
	'declined' => lang('declined'),
	'accepted' => lang('accepted'),
	'pending' => lang('pending'),
	'cancelled' => lang('cancelled'),
);

$pdata['itemType'] = 'estimates';
if (!empty($estimate_info)) {
	$pdata['add_items'] = $this->estimates_model->ordered_items_by_id($estimate_info->estimates_id, true);
	$pdata['info'] = $estimate_info;
}
?>

    <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">
        <?php if (!empty($created) || !empty($edited)) { ?>
        <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('estimate') ?></p>
                    <h1 class="text-2xl font-semibold text-foreground">
                        <?= !empty($estimate_id) ? lang('edit') . ' ' . lang('estimate') : lang('create_estimate') ?>
                    </h1>
                    <p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('estimate') ?></p>
                </div>
                <div class="flex flex-wrap items-center gap-2">
                    <a href="<?= base_url('admin/estimates') ?>"
                        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_estimates') ?>
                    </a>
                    <?php if (!empty($estimate_id)) { ?>
                    <a href="<?= base_url('admin/estimates/create') ?>"
                        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_estimate') ?>
                    </a>
                    <?php } ?>
                </div>
            </div>
        </div>

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

        <?= form_open(base_url('admin/estimates/save_estimates/' . $estimate_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('estimate') . ' ' . 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('estimate_date', lang('estimate_date'), $estimate_date, true, array('data-max' => '#due_date', 'data-auto-close' => 'true')); ?>
                        <?= form_datepicker('due_date', lang('due_date'), $due_date, true, array('data-min' => '#estimate_date', 'data-auto-close' => 'true')); ?>
                        <?= form_select_group('status', lang('status'), $estimate_status_options, $status_selected, false); ?>
                        <?= 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 ?>

                        <?php if (!empty($warehouse_list)) { ?>
                        <?= form_lookup_group(
								'warehouse_id',
								lang('warehouse'),
								$warehouse_selected,
								$warehouse_label,
								false,
								array('placeholder' => lang('select') . ' ' . lang('warehouse')),
								'',
								array('type' => 'warehouse')
							); ?>
                        <?php } ?>
                    </div>
                </div>



                <div class="rounded-xl border border-border bg-card p-6 space-y-3">
                    <div class="flex items-center gap-2 text-sm font-semibold text-foreground">
                        <i data-lucide="sticky-note" class="h-5 w-5"></i>
                        <h2 class="text-base font-semibold text-foreground"><?= lang('notes') ?></h2>
                    </div>
                    <?= form_textarea_group('notes', '', $notes_value, 5, false, array('class' => 'textarea_'), 'stacked'); ?>
                </div>
            </div>

            <div 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">
                        <i data-lucide="user-check" class="h-5 w-5"></i>
                        <h2 class="text-base font-semibold text-foreground"><?= lang('assignment') ?></h2>
                    </div>
                    <?= form_lookup_group(
						'user_id',
						lang('sales') . ' ' . lang('agent'),
						$selected_agent,
						$selected_agent_label,
						false,
						array('placeholder' => lang('select') . ' ' . lang('sales') . ' ' . lang('agent')),
						'',
						array('type' => 'staff')
					); ?>
                    <?= form_tags_group($tags_value); ?>
                    <?php
					$estimates_id = !empty($estimate_info) ? $estimate_info->estimates_id : null;
					echo custom_form_Fields(10, $estimates_id);
					?>
                </div>

                <div class="rounded-xl border border-border bg-card p-6">
                    <?= form_permission_modern($permission_user, $permissionL); ?>
                </div>

                <?php if (!empty($project_id)) { ?>
                <div class="rounded-xl border border-border bg-card p-6 space-y-3">
                    <div class="flex items-center gap-2 text-sm font-semibold text-foreground">
                        <i data-lucide="eye" class="h-5 w-5"></i>
                        <h2 class="text-base font-semibold text-foreground"><?= lang('visible_to_client') ?></h2>
                    </div>
                    <label class="inline-flex items-center gap-3 text-sm text-muted-foreground">
                        <input data-toggle="toggle" name="client_visible" value="Yes" <?php if (!empty($estimate_info->client_visible) && $estimate_info->client_visible == 'Yes') {
								echo 'checked';
							} ?> data-on="<?= lang('yes') ?>" data-off="<?= lang('no') ?>" data-onstyle="success" data-offstyle="danger"
                            type="checkbox">
                        <span><?= lang('show_to_client') ?></span>
                    </label>
                </div>
                <?php } ?>
            </div>
        </div>

        <?php $this->load->view('admin/items/selectItem', $pdata); ?>


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

    <script>
    $(document).ready(function() {
        if (window.ins_data) {
            window.ins_data(base_url + 'admin/estimates/estimates_state_report');
        }
    });
    </script>
</div>
