\documentclass{klartex-base}
\providecommand{\orgname}{}

\VAR{page_template_source}

\BLOCK{if lang == 'en'}
\setdoclang{en}
\BLOCK{endif}

\setdoctitle{\VAR{title}}

\begin{document}

\BLOCK{for comp in components}
%# ---- Component: \VAR{comp.type} ----
\BLOCK{if comp.type == 'heading'}
\begin{center}
{\Large\bfseries \VAR{comp.data.get('title', '')}}\\[0.5em]
\ifdefempty{\orgname}{}{{\normalsize \orgname}\\[0.3em]}
\BLOCK{if comp.data.get('subtitle')}
{\normalsize \VAR{comp.options.get('subtitle_prefix', '')}\VAR{comp.data.subtitle}}\\[1em]
\BLOCK{else}
\vspace{1em}
\BLOCK{endif}
\end{center}

\BLOCK{elif comp.type == 'metadata_table'}
\noindent
\begin{tabular}{@{}ll}
\BLOCK{for meta in metadata}
\textbf{\VAR{meta.label}} & \BLOCK{if meta.value is iterable and meta.value is not string}\VAR{meta.value | join(', ')}\BLOCK{else}\VAR{meta.value}\BLOCK{endif} \\
\BLOCK{endfor}
\end{tabular}

\BLOCK{elif comp.type == 'attendees'}
\vspace{1em}
\noindent\textbf{Narvarande:} \VAR{comp.data.get('attendees', []) | join(', ')}

\BLOCK{if comp.data.get('adjusters')}
\noindent\textbf{Justerare:} \VAR{comp.data.adjusters | join(', ')}
\BLOCK{endif}

\vspace{1em}

\BLOCK{elif comp.type == 'klausuler'}
\begin{clauses}
\BLOCK{set items = comp.data.get('items', []) }
\BLOCK{set title_field = comp.options.get('item_title_field', 'title') }
\BLOCK{set body_field = comp.options.get('item_body_field', 'body') }
\BLOCK{set decision_field = comp.options.get('item_decision_field', '') }
\BLOCK{set decision_label = comp.options.get('decision_label', 'Beslut:') }
\BLOCK{for item in items}
\clause{\VAR{item[title_field]}}
\BLOCK{if item.get(body_field)}
\VAR{item[body_field]}
\BLOCK{endif}
\BLOCK{if decision_field and item.get(decision_field)}

\textbf{\VAR{decision_label}} \VAR{item[decision_field]}
\BLOCK{endif}
\BLOCK{if item.get('subclauses')}
\begin{subclauses}
\BLOCK{for sub in item.subclauses}
\item \VAR{sub}
\BLOCK{endfor}
\end{subclauses}
\BLOCK{endif}
\BLOCK{endfor}
\end{clauses}

\BLOCK{elif comp.type == 'signaturblock'}
\BLOCK{set _p = comp.data.parties}
\signatureblock{\VAR{_p[0].name}}{\VAR{_p[1].name}}{\VAR{_p[0].get('signatory', _p[0].name)}}{\VAR{_p[1].get('signatory', _p[1].name)}}{\VAR{_p[0].get('title', '')}}{\VAR{_p[1].get('title', '')}}

\BLOCK{elif comp.type == 'titelsida'}
\makedoctitle{\VAR{comp.data.get('party1', '')}}{\VAR{comp.data.get('party2', '')}}{\VAR{comp.data.get('title', '')}}

\BLOCK{elif comp.type == 'adjuster_signatures'}
\BLOCK{if comp.data.get('adjusters')}
\vspace{2cm}
\noindent
\BLOCK{for adjuster in comp.data.adjusters}
\begin{minipage}[t]{0.45\textwidth}
Justerare\\[1cm]
\dottedline\\[0.3cm]
\VAR{adjuster}
\end{minipage}\BLOCK{if not loop.last}\hfill\BLOCK{endif}
\BLOCK{endfor}
\BLOCK{endif}

\BLOCK{elif comp.type == 'invoice_header'}
\begin{flushright}
{\Large\bfseries FAKTURA}\\[0.5em]
{\normalsize Nr: \VAR{comp.data.get('invoice_number', '')}}\\
{\normalsize Datum: \VAR{comp.data.get('date', '')}}\\
{\normalsize Förfallodatum: \VAR{comp.data.get('due_date', '')}}
\end{flushright}

\vspace{1em}

\BLOCK{elif comp.type == 'invoice_recipient'}
\noindent
\begin{minipage}[t]{0.45\textwidth}
\textbf{Mottagare}\\[0.3em]
\VAR{comp.data.get('recipient', {}).get('name', '')}\\
\BLOCK{if comp.data.get('recipient', {}).get('org_number')}
Org.nr: \VAR{comp.data.recipient.org_number}\\
\BLOCK{endif}
\BLOCK{if comp.data.get('recipient', {}).get('address_line1')}
\VAR{comp.data.recipient.address_line1}\\
\BLOCK{endif}
\BLOCK{if comp.data.get('recipient', {}).get('address_line2')}
\VAR{comp.data.recipient.address_line2}
\BLOCK{endif}
\end{minipage}%
\hfill
\begin{minipage}[t]{0.45\textwidth}
\BLOCK{if comp.data.get('reference')}
\textbf{Er referens:} \VAR{comp.data.reference}\\
\BLOCK{endif}
\BLOCK{if comp.data.get('our_reference')}
\textbf{Vår referens:} \VAR{comp.data.our_reference}\\
\BLOCK{endif}
\BLOCK{if comp.data.get('payment_terms')}
\textbf{Betalningsvillkor:} \VAR{comp.data.payment_terms}
\BLOCK{endif}
\end{minipage}

\vspace{2em}

\BLOCK{elif comp.type == 'invoice_table'}
\begin{tabularx}{\textwidth}{Xrrrrr}
\toprule
\textbf{Beskrivning} & \textbf{Antal} & \textbf{Enhet} & \textbf{À-pris} & \textbf{Moms} & \textbf{Belopp} \\
\midrule
\BLOCK{for line in comp.data.get('lines', [])}
\VAR{line.description} & \VAR{line.quantity} & \VAR{line.get('unit', 'st')} & \VAR{"{:,.2f}".format(line.unit_price)} & \VAR{line.get('vat_percent', 25)}\% & \VAR{"{:,.2f}".format(line.quantity * line.unit_price)} \\
\BLOCK{endfor}
\bottomrule
\end{tabularx}

\vspace{1em}

\BLOCK{set subtotal = namespace(val=0) }
\BLOCK{set vat_total = namespace(val=0) }
\BLOCK{for line in comp.data.get('lines', [])}
\BLOCK{set subtotal.val = subtotal.val + line.quantity * line.unit_price}
\BLOCK{set vat_total.val = vat_total.val + line.quantity * line.unit_price * line.get('vat_percent', 25) / 100}
\BLOCK{endfor}

\begin{flushright}
\begin{tabular}{lr}
\textbf{Summa exkl. moms:} & \VAR{"{:,.2f}".format(subtotal.val)} \VAR{comp.data.get('currency', 'SEK')} \\
\textbf{Moms:} & \VAR{"{:,.2f}".format(vat_total.val)} \VAR{comp.data.get('currency', 'SEK')} \\
\midrule
\textbf{Att betala:} & \textbf{\VAR{"{:,.2f}".format(subtotal.val + vat_total.val)} \VAR{comp.data.get('currency', 'SEK')}} \\
\end{tabular}
\end{flushright}

\vspace{2em}

\BLOCK{elif comp.type == 'payment_info'}
\noindent\textbf{Betalningsinformation}\\[0.3em]
\begin{tabular}{@{}ll}
\BLOCK{if comp.data.get('bankgiro')}
\textbf{Bankgiro:} & \VAR{comp.data.bankgiro} \\
\BLOCK{endif}
\BLOCK{if comp.data.get('plusgiro')}
\textbf{Plusgiro:} & \VAR{comp.data.plusgiro} \\
\BLOCK{endif}
\BLOCK{if comp.data.get('iban')}
\textbf{IBAN:} & \VAR{comp.data.iban} \\
\BLOCK{endif}
\BLOCK{if comp.data.get('bic')}
\textbf{BIC:} & \VAR{comp.data.bic} \\
\BLOCK{endif}
\end{tabular}

\BLOCK{elif comp.type == 'invoice_note'}
\BLOCK{if comp.data.get('note')}
\vspace{1em}
\noindent\textit{\VAR{comp.data.note}}
\BLOCK{endif}

\BLOCK{endif}
\BLOCK{endfor}

\end{document}
