<div class="invoice-modern">
<?= message_box('success') ?>
<?= message_box('error'); ?>
<?php
$can_edit = $this->proposal_model->can_action('tbl_proposals', 'edit', array('proposals_id' => $proposals_info->proposals_id));
$can_delete = $this->proposal_model->can_action('tbl_proposals', 'delete', array('proposals_id' => $proposals_info->proposals_id));
$proposal_url = base_url() . 'frontend/proposals/' . url_encode($proposals_info->proposals_id);

if ($proposals_info->module == 'client') {
	$client_info = $this->proposal_model->check_by(array('client_id' => $proposals_info->module_id), 'tbl_client');
	$currency = $this->proposal_model->client_currency_symbol($proposals_info->module_id);
	$client_lang = !empty($client_info->language) ? $client_info->language : config_item('language');
} elseif ($proposals_info->module == 'leads') {
	$client_info = $this->proposal_model->check_by(array('leads_id' => $proposals_info->module_id), 'tbl_leads');
	$client_info->name = $client_info->lead_name;
	$client_info->zipcode = null;
	$client_lang = 'english';
	$currency = $this->proposal_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
} else {
	$client_info = null;
	$client_lang = 'english';
	$currency = $this->proposal_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
}

unset($this->lang->is_loaded[5]);
$language_info = $this->lang->load('sales_lang', $client_lang, true, false, '', true);

$status_label = !empty($proposals_info->status_label) ? $proposals_info->status_label : lang($proposals_info->status);
$status_value = strtolower((string) ($proposals_info->status ?? ''));
$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
if ($status_value === 'accepted') {
	$status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif (in_array($status_value, array('declined', 'cancelled'), true)) {
	$status_class = 'bg-rose-50 text-rose-700 border-rose-200';
} elseif ($status_value === 'expired' || $status_value === 'open') {
	$status_class = 'bg-amber-50 text-amber-700 border-amber-200';
} elseif ($status_value === 'sent' || $status_value === 'revised') {
	$status_class = 'bg-blue-50 text-blue-700 border-blue-200';
}

$overdue_message = '';
if (safe_strtotime($proposals_info->due_date) < safe_strtotime(date('Y-m-d')) && $proposals_info->status == 'draft') {
	$start = safe_strtotime(date('Y-m-d'));
	$end = safe_strtotime($proposals_info->due_date);
	$days_between = ceil(abs($end - $start) / 86400);
	$overdue_message = lang('proposal_overdue') . ' ' . lang('by') . ' ' . $days_between . ' ' . lang('days');
}

$all_items = $this->proposal_model->ordered_items_by_id($proposals_info->proposals_id);

if (!empty($client_info)) {
	$client_name = $client_info->name;
	$address = $client_info->address;
	$city = $client_info->city;
	$zipcode = $client_info->zipcode;
	$country = $client_info->country;
	$phone = $client_info->phone;
	$vat = $client_info->vat ?? '';
} else {
	$client_name = '-';
	$address = '-';
	$city = '-';
	$zipcode = '-';
	$country = '-';
	$phone = '-';
	$vat = '';
}

$custom_fields = custom_form_label(11, $proposals_info->proposals_id);
$custom_field_markup = '';
if (!empty($custom_fields)) {
	foreach ($custom_fields as $c_label => $v_fields) {
		if (!empty($v_fields)) {
			$custom_field_markup .= '<div>' . html_escape($c_label) . ': ' . html_escape($v_fields) . '</div>';
		}
	}
}

$sales_info = (object) array(
	'reference_no' => $proposals_info->reference_no,
	'status' => $proposals_info->status,
	'start_date' => $language_info['proposal_date'] . ': ' . display_date($proposals_info->proposal_date),
	'end_date' => $language_info['due_date'] . ': ' . display_date($proposals_info->due_date),
	'sales_agent' => !empty($proposals_info->user_id) ? lang('sales') . ' ' . lang('agent') . ': ' . fullname($proposals_info->user_id) : null,
	'custom_field' => $custom_field_markup,
	'client_id' => $proposals_info->module == 'client' ? $proposals_info->module_id : null,
	'name' => $client_name,
	'address' => $address,
	'city' => $city,
	'zipcode' => $zipcode,
	'country' => $country,
	'phone' => $phone,
	'vat' => $vat,
	'notes' => $proposals_info->notes,
	'sub_total' => $this->proposal_model->proposal_calculation('proposal_cost', $proposals_info->proposals_id),
	'discount' => $this->proposal_model->proposal_calculation('discount', $proposals_info->proposals_id),
	'discount_percent' => $proposals_info->discount_percent,
	'total_tax' => $proposals_info->total_tax,
	'adjustment' => $proposals_info->adjustment,
	'total' => $this->proposal_model->proposal_calculation('total', $proposals_info->proposals_id),
	'overdue_days' => $overdue_message,
);
?>

<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 lg:flex-row lg:items-center lg:justify-between">
			<div class="space-y-2">
				<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('proposal') ?></p>
				<div class="flex flex-wrap items-center gap-3">
					<h1 class="text-2xl font-semibold text-foreground">#<?= html_escape($proposals_info->reference_no) ?></h1>
					<span class="inline-flex items-center rounded-full border px-3 py-1 text-xs font-semibold <?= $status_class ?>">
						<?= html_escape($status_label) ?>
					</span>
					<span class="inline-flex items-center rounded-full border border-border px-2.5 py-1 text-xs font-semibold text-muted-foreground">
						<?= display_date($proposals_info->proposal_date) ?>
					</span>
					<span class="inline-flex items-center rounded-full border border-border px-2.5 py-1 text-xs font-semibold text-muted-foreground">
						<?= lang('expire_date') ?>: <?= display_date($proposals_info->due_date) ?>
					</span>
				</div>
			</div>
			<div class="flex flex-wrap items-center gap-2">
				<?php if ($proposals_info->status != 'accepted') { ?>
					<a class="inline-flex items-center gap-2 rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-xs font-semibold text-emerald-700 hover:bg-emerald-100"
					   href="<?= base_url() ?>client/proposals/change_status/accepted/<?= $proposals_info->proposals_id ?>">
						<i data-lucide="check" class="h-4 w-4"></i>
						<?= lang('accepted') ?>
					</a>
					<a class="inline-flex items-center gap-2 rounded-lg border border-rose-200 bg-rose-50 px-3 py-2 text-xs font-semibold text-rose-600 hover:bg-rose-100"
					   href="<?= base_url() ?>client/proposals/change_status/declined/<?= $proposals_info->proposals_id ?>">
						<i data-lucide="x" class="h-4 w-4"></i>
						<?= lang('declined') ?>
					</a>
				<?php } ?>
			</div>
		</div>
	</div>

	<div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
		<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
			<div class="flex-1">
				<label class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('copy_unique_url') ?></label>
				<div class="mt-2 flex items-center gap-2">
					<input id="proposal-url" type="text" value="<?= $proposal_url ?>" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm" readonly>
					<button type="button" data-copy-target="#proposal-url" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
						<i data-lucide="copy" class="h-4 w-4"></i>
						<?= lang('copy') ?>
					</button>
				</div>
			</div>
			<div class="flex flex-wrap items-center gap-2">
				<a href="<?= base_url() ?>client/proposals/pdf_proposals/<?= $proposals_info->proposals_id ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
					<i data-lucide="file-text" class="h-4 w-4"></i>
					PDF
				</a>
				<a href="#" onclick="print_sales_details('sales_details')" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
					<i data-lucide="printer" class="h-4 w-4"></i>
					<?= lang('print') ?>
				</a>
			</div>
		</div>
	</div>

	<?php
	$this->load->view('admin/common/sales_details_modern', array(
		'sales_info' => $sales_info,
		'all_items' => $all_items,
		'document_label' => lang('proposal'),
		'payment_status' => $status_label,
	));
	?>
</div>

<script>
	document.addEventListener('click', function(event) {
		var button = event.target.closest('[data-copy-target]');
		if (!button) return;
		var target = button.getAttribute('data-copy-target');
		var input = document.querySelector(target);
		if (!input) return;
		input.select();
		input.setSelectionRange(0, 99999);
		try {
			document.execCommand('copy');
		} catch (err) {}
	});

	function print_sales_details(print_section_id) {
		var printContents = document.getElementById(print_section_id).innerHTML;
		var originalContents = document.body.innerHTML;
		document.body.innerHTML = printContents;
		window.print();
		document.body.innerHTML = originalContents;
	}
</script>
</div><?= message_box('success') ?>
<?= message_box('error'); ?>
<div class="row mb">

    <div class="col-sm-8">
        <?php

        $can_edit = $this->proposal_model->can_action('tbl_proposals', 'edit', array('proposals_id' => $proposals_info->proposals_id));
        $can_delete = $this->proposal_model->can_action('tbl_proposals', 'delete', array('proposals_id' => $proposals_info->proposals_id));
        if ($proposals_info->module == 'client') {
            $client_info = $this->proposal_model->check_by(array('client_id' => $proposals_info->module_id), 'tbl_client');
            $currency = $this->proposal_model->client_currency_symbol($proposals_info->module_id);
            $client_lang = $client_info->language;
        } else if ($proposals_info->module == 'leads') {
            $client_info = $this->proposal_model->check_by(array('leads_id' => $proposals_info->module_id), 'tbl_leads');
            $client_info->name = $client_info->lead_name;
            $client_info->zipcode = null;
            $client_lang = 'english';
            $currency = $this->proposal_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
        } else {
            $client_lang = 'english';
            $currency = $this->proposal_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
        }
        unset($this->lang->is_loaded[5]);
        $language_info = $this->lang->load('sales_lang', $client_lang, TRUE, FALSE, '', TRUE);

        if ($proposals_info->status != 'accepted') {
            ?>
            <a class="btn btn-xs btn-purple"
               href="<?= base_url() ?>client/proposals/change_status/accepted/<?= $proposals_info->proposals_id ?>">
                <i data-lucide="check"></i> <?= lang('accepted') ?></a>

            <a class="btn btn-xs btn-warning"
               href="<?= base_url() ?>client/proposals/change_status/declined/<?= $proposals_info->proposals_id ?>"><i
                    data-lucide="x"></i> <?= lang('declined') ?></a>
        <?php } ?>

    </div>
    <div class="col-sm-4 pull-right">
        <a onclick="print_proposals('print_proposals')" href="#" data-toggle="tooltip" data-placement="top" title=""
           data-original-title="<?= lang('print') ?>" class="mr-sm btn btn-xs btn-danger pull-right">
            <i data-lucide="printer"></i>
        </a>

        <a href="<?= base_url() ?>client/proposals/pdf_proposals/<?= $proposals_info->proposals_id ?>"
           data-toggle="tooltip" data-placement="top" title="" data-original-title="<?= lang('pdf') ?>"
           class="btn btn-xs btn-success pull-right mr-sm">
            <i data-lucide="file-text"></i>
        </a>

    </div>
</div>
<!-- Start Display Details -->
<?php
if (safe_strtotime($proposals_info->due_date) < safe_strtotime(date('Y-m-d')) && $proposals_info->status == 'draft') {
    $start = safe_strtotime(date('Y-m-d'));
    $end = safe_strtotime($proposals_info->due_date);
    $days_between = ceil(abs($end - $start) / 86400);
    ?>
    <div class="alert bg-danger-light hidden-print">
        <button type="button" class="close" data-dismiss="alert">Ã—</button>
        <i data-lucide="triangle-alert"></i>
        <?= lang('proposal_overdue') . ' ' . lang('by') . ' ' . $days_between . ' ' . lang('days') ?>
    </div>
    <?php
}

if (is_file(config_item('invoice_logo'))) {
    $img = base_url() . config_item('invoice_logo');
} else {
    $img = base_url() . 'uploads/default_logo.png';
}
?>
<!-- Main content -->
<div class="panel" id="print_proposals">

    <!-- info row -->
    <div class="panel-body">

        <div class="row">
            <div class="col-lg-6 hidden-xs">
                <img class="pl-lg" style="width: 233px;height: 120px;"
                     src="<?= $img ?>">
            </div>
            <div class="col-lg-6 col-xs-12 ">

                <?php

                if (!empty($client_info)) {
                    $client_name = $client_info->name;
                    $address = $client_info->address;
                    $city = $client_info->city;
                    $zipcode = $client_info->zipcode;
                    $country = $client_info->country;
                    $phone = $client_info->phone;

                } else {
                    $client_name = '-';
                    $address = '-';
                    $city = '-';
                    $zipcode = '-';
                    $country = '-';
                    $phone = '-';
                }
                ?>

                <div class="pull-right pr-lg">
                    <h4 class="mb0"><?= lang('proposals') . ' : ' . $proposals_info->reference_no ?></h4>
                    <?= $language_info['proposal_date'] ?>
                    : <?= display_date($proposals_info->proposal_date); ?>
                    <br><?= $language_info['due_date'] ?>
                    : <?= display_date($proposals_info->due_date); ?>
                    <?php if (!empty($proposals_info->user_id)) { ?>
                        <br><?= lang('sales') . ' ' . lang('agent') ?>:<?php echo fullname($proposals_info->user_id); ?>
                        <?php
                    }
                    if ($proposals_info->status == 'accepted') {
                        $label = 'success';
                    } else {
                        $label = 'danger';
                    }
                    ?>
                    <br><?= lang('proposals') . '  ' . lang('status') ?>: <span
                        class="label label-<?= $label ?>"><?= lang($proposals_info->status) ?></span>

                    <?php $show_custom_fields = custom_form_label(11, $proposals_info->proposals_id);
                    if (!empty($show_custom_fields)) {
                        foreach ($show_custom_fields as $c_label => $v_fields) {
                            if (!empty($v_fields)) {
                                ?>
                                <br><?= $c_label ?>: <?= $v_fields; ?>
                            <?php }
                        }
                    }
                    ?>
                </div>
            </div>

        </div>

        <div class="row mb-lg">
            <div class="col-lg-6 col-xs-6">
                <h5 class="p-md bg-items mr-15">
                    <?= lang('our_info') ?>:
                </h5>
                <div class="pl-sm">
                    <h4 class="mb0"><?= (config_item('company_legal_name_' . $client_lang) ? config_item('company_legal_name_' . $client_lang) : config_item('company_legal_name')) ?></h4>
                    <?= (config_item('company_address_' . $client_lang) ? config_item('company_address_' . $client_lang) : config_item('company_address')) ?>
                    <br><?= (config_item('company_city_' . $client_lang) ? config_item('company_city_' . $client_lang) : config_item('company_city')) ?>
                    , <?= config_item('company_zip_code') ?>
                    <br><?= (config_item('company_country_' . $client_lang) ? config_item('company_country_' . $client_lang) : config_item('company_country')) ?>
                    <br/><?= $language_info['phone'] ?> : <?= config_item('company_phone') ?>
                    <br/><?= lang('vat_number') ?> : <?= config_item('company_vat') ?>
                </div>
            </div>
            <div class="col-lg-6 col-xs-6 ">
                <h5 class="p-md bg-items ml-13">
                    <?= lang('customer') ?>:
                </h5>
                <div class="pl-sm">
                    <?php

                    if (!empty($client_info)) {
                        $client_name = $client_info->name;
                        $address = $client_info->address;
                        $city = $client_info->city;
                        $zipcode = $client_info->zipcode;
                        $country = $client_info->country;
                        $phone = $client_info->phone;

                    } else {
                        $client_name = '-';
                        $address = '-';
                        $city = '-';
                        $zipcode = '-';
                        $country = '-';
                        $phone = '-';
                    }
                    ?>
                    <h4 class="mb0"><?= $client_name ?></h4>
                    <?= $address ?>
                    <br> <?= $city ?>, <?= $zipcode ?>
                    <br><?= $country ?>
                    <br><?= $language_info['phone'] ?>: <?= $phone ?>
                    <?php if (!empty($client_info->vat)) { ?>
                        <br><?= lang('vat_number') ?>: <?= $client_info->vat ?>
                    <?php } ?>
                </div>
            </div>

        </div>

        <div class="table-responsive mb-lg " style="margin-top: 25px">
            <table class="table items proposal-items-preview" page-break-inside: auto;>
                <thead class="bg-items">
                <tr>
                    <th><?= $language_info['items'] ?></th>
                    <?php
                    $invoice_view = config_item('invoice_view');
                    if (!empty($invoice_view) && $invoice_view == '2') {
                        ?>
                        <th><?= $language_info['hsn_code'] ?></th>
                    <?php } ?>
                    <?php
                    $qty_heading = $language_info['qty'];
                    if (isset($proposals_info) && $proposals_info->show_quantity_as == 'hours' || isset($hours_quantity)) {
                        $qty_heading = lang('hours');
                    } else if (isset($proposals_info) && $proposals_info->show_quantity_as == 'qty_hours') {
                        $qty_heading = lang('qty') . '/' . lang('hours');
                    }
                    ?>
                    <th><?php echo $qty_heading; ?></th>
                    <th class="col-sm-1"><?= $language_info['price'] ?></th>
                    <th class="col-sm-2"><?= $language_info['tax'] ?></th>
                    <th class="col-sm-1"><?= $language_info['total'] ?></th>
                </tr>
                </thead>
                <tbody>
                <?php
                $invoice_items = $this->proposal_model->ordered_items_by_id($proposals_info->proposals_id);
                if (!empty($invoice_items)) :
                    foreach ($invoice_items as $key => $v_item) :
                        $item_name = $v_item->item_name ? $v_item->item_name : $v_item->item_desc;
                        $item_tax_name = json_decode($v_item->item_tax_name);
                        ?>
                        <tr class="sortable item" data-item-id="<?= $v_item->proposals_items_id ?>">
                            <td><strong class="block"><?= $item_name ?></strong>
                                <?= nl2br($v_item->item_desc) ?>
                            </td>
                            <?php
                            $invoice_view = config_item('invoice_view');
                            if (!empty($invoice_view) && $invoice_view == '2') {
                                ?>
                                <td><?= $v_item->hsn_code ?></td>
                            <?php } ?>
                            <td><?= $v_item->quantity . '   &nbsp' . $v_item->unit ?></td>
                            <td><?= display_money($v_item->unit_cost) ?></td>
                            <td><?php
                                if (!empty($item_tax_name)) {
                                    foreach ($item_tax_name as $v_tax_name) {
                                        $i_tax_name = explode('|', $v_tax_name);
                                        echo '<small class="pr-sm">' . $i_tax_name[0] . ' (' . $i_tax_name[1] . ' %)' . '</small>' . display_money($v_item->total_cost / 100 * $i_tax_name[1]) . ' <br>';
                                    }
                                }
                                ?></td>
                            <td><?= display_money($v_item->total_cost) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="8"><?= lang('nothing_to_display') ?></td>
                    </tr>
                <?php endif ?>
                </tbody>
            </table>
        </div>
        <div class="row" style="margin-top: 35px">
            <div class="col-xs-8">
                <p class="well well-sm mt">
                    <?= $proposals_info->notes ?>
                </p>
            </div>
            <div class="col-sm-4 pv">
                <div class="clearfix">
                    <p class="pull-left"><?= $language_info['sub_total'] ?></p>
                    <p class="pull-right mr">
                        <?= display_money($this->proposal_model->proposal_calculation('proposal_cost', $proposals_info->proposals_id)); ?>
                    </p>
                </div>
                <?php if ($proposals_info->discount_total > 0): ?>
                    <div class="clearfix">
                        <p class="pull-left"><?= $language_info['discount'] ?>
                            (<?php echo $proposals_info->discount_percent; ?>
                            %)</p>
                        <p class="pull-right mr">
                            <?= display_money($this->proposal_model->proposal_calculation('discount', $proposals_info->proposals_id)); ?>
                        </p>
                    </div>
                <?php endif ?>
                <?php
                $tax_info = json_decode($proposals_info->total_tax);
                $tax_total = 0;
                if (!empty($tax_info)) {
                    $tax_name = $tax_info->tax_name;
                    $total_tax = $tax_info->total_tax;
                    if (!empty($tax_name)) {
                        foreach ($tax_name as $t_key => $v_tax_info) {
                            $tax = explode('|', $v_tax_info);
                            $tax_total += $total_tax[$t_key];
                            ?>
                            <div class="clearfix">
                                <p class="pull-left"><?= $tax[0] . ' (' . $tax[1] . ' %)' ?></p>
                                <p class="pull-right mr">
                                    <?= display_money($total_tax[$t_key]); ?>
                                </p>
                            </div>
                        <?php }
                    }
                } ?>
                <?php if ($tax_total > 0): ?>
                    <div class="clearfix">
                        <p class="pull-left"><?= $language_info['total'] . ' ' . $language_info['tax'] ?></p>
                        <p class="pull-right mr">
                            <?= display_money($tax_total); ?>
                        </p>
                    </div>
                <?php endif ?>
                <?php if ($proposals_info->adjustment > 0): ?>
                    <div class="clearfix">
                        <p class="pull-left"><?= $language_info['adjustment'] ?></p>
                        <p class="pull-right mr">
                            <?= display_money($proposals_info->adjustment); ?>
                        </p>
                    </div>
                <?php endif;
                $total_proposal = $this->proposal_model->proposal_calculation('total', $proposals_info->proposals_id);
                ?>

                <div class="clearfix">
                    <p class="pull-left"><?= $language_info['total'] ?></p>
                    <p class="pull-right mr">
                        <?= display_money($total_proposal, $currency->symbol); ?>
                    </p>
                </div>
                <?php if (config_item('amount_to_words') == 'Yes') { ?>
                    <div class="clearfix">
                        <p class="pull-right h4"><strong class="h3"><?= lang('num_word') ?>
                                : </strong> <?= number_to_word($proposals_info->module_id, $total_proposal); ?></p>
                    </div>
                <?php } ?>

            </div>
        </div>
    </div>
    <?= !empty($invoice_view) && $invoice_view > 0 ? $this->gst->summary($invoice_items) : ''; ?>
</div>
<?php include_once 'assets/js/sales.php'; ?>
<script type="text/javascript">
    $(document).ready(function () {
        init_items_sortable(true);
    });
    function print_proposals(print_proposals) {
        var printContents = document.getElementById(print_proposals).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
    }
</script>

