<?php
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');
	if (!empty($client_info)) {
		$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);
$currency_symbol = is_object($currency) && !empty($currency->symbol) ? $currency->symbol : (is_string($currency) ? $currency : default_currency());
$proposal_id = $proposals_info->proposals_id;
$overdue_days = 0;
$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);
	$overdue_days = ceil(abs($end - $start) / 86400);
	$overdue_message = 'This proposal expired on ' . display_date($proposals_info->due_date) . ' and is currently overdue by ' . $overdue_days . ' days.';
}

$all_items = $this->proposal_model->ordered_items_by_id($proposal_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, $proposal_id);
$custom_field_markup = '';
if (!empty($custom_fields)) {
	foreach ($custom_fields as $c_label => $v_fields) {
		if (!empty($v_fields)) {
			$custom_field_markup .= '<span class="pdf-chip">' . html_escape($c_label) . ': ' . html_escape($v_fields) . '</span>';
		}
	}
}

$subtotal = $this->proposal_model->proposal_calculation('proposal_cost', $proposal_id);
$discount = $this->proposal_model->proposal_calculation('discount', $proposal_id);
$total = $this->proposal_model->proposal_calculation('total', $proposal_id);
$tax_info = json_decode($proposals_info->total_tax);
$tax_rows = array();
if (!empty($tax_info) && !empty($tax_info->tax_name) && is_array($tax_info->tax_name)) {
	foreach ($tax_info->tax_name as $i => $raw_tax) {
		$tax = explode('|', $raw_tax);
		$tax_rows[] = array(
			'label' => $tax[0] ?? '',
			'percent' => $tax[1] ?? '',
			'value' => !empty($tax_info->total_tax[$i]) ? $tax_info->total_tax[$i] : 0,
		);
	}
}

$logo = is_file(config_item('invoice_logo')) ? ROOTPATH . '/' . config_item('invoice_logo') : ROOTPATH . '/' . 'uploads/default_logo.png';
include APPPATH . 'views/shared/proposals/proposals_pdf_template.php';
