{% macro render_field(df, doc) -%}
	{%- if df.fieldtype=="Table" -%}
		{{ render_table(df, doc) }}
	{%- elif df.fieldtype=="HTML" -%}
		<div>{{ frappe.render_template(df.options, {"doc": doc}) or "" }}</div>
	{%- elif df.fieldtype in ("Text", "Text Editor", "Code") -%}
		{{ render_text_field(df, doc) }}
	{%- else -%}
		{{ render_field_with_label(df, doc) }}
	{%- endif -%}
{%- endmacro -%}

{%- macro render_table(df, doc) -%}
	{%- set table_meta = frappe.get_meta(df.options) -%}
	{%- set data = doc.get(df.fieldname)[df.start:df.end] -%}
	{%- if doc.print_templates and
			doc.print_templates.get(df.fieldname) -%}
		{% include doc.print_templates[df.fieldname] %}
	{%- else -%}
		{%- if data -%}
		{%- set visible_columns = get_visible_columns(doc.get(df.fieldname),
			table_meta, df) -%}
		<div {{ fieldmeta(df) }}>
			<table class="table table-bordered table-condensed">
				<thead>
					<tr>
						<th style="width: 40px" class="table-sr">Sr</th>
						{% for tdf in visible_columns %}
							<th style="width: {{ get_width(tdf) }}px;" class="{{ get_align_class(tdf.fieldtype) }}" {{ fieldmeta(df) }}>
								{{ _(tdf.label) }}</th>
						{% endfor %}
					</tr>
				</thead>
				<tbody>
					{% for d in data %}
					<tr>
						<td class="table-sr">{{ d.idx }}</td>
						{% for tdf in visible_columns %}
							<td class="{{ get_align_class(tdf.fieldtype) }}" {{ fieldmeta(df) }}>
                                <div class="value">{{ print_value(tdf, d, table_meta, doc) }}</div></td>
						{% endfor %}
					</tr>
					{% endfor %}
				</tbody>
			</table>
		</div>
		{%- endif -%}
	{%- endif -%}
{%- endmacro -%}

{% macro fieldmeta(df) -%}
data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}"
{%- endmacro %}

{%- macro render_field_with_label(df, doc) -%}
		<div class="row {% if df.bold %}important{% endif %} data-field" {{ fieldmeta(df) }}>
			<div class="col-xs-5 text-right">
				{% if df.fieldtype not in ("Image","HTML") and
					doc.get(df.fieldname) != None %}
				<label>{{ _(df.label) }}</label>
				{% endif %}
			</div>
			<div class="col-xs-7 {{ get_align_class(df.fieldtype) }} value">
				{% if doc.get(df.fieldname) != None -%}
					{{ print_value(df, doc) }}{% endif %}
			</div>
		</div>
{%- endmacro -%}

{%- macro render_text_field(df, doc) -%}
{%- if doc.get(df.fieldname) != None -%}
<div style="padding: 10px 0px" {{ fieldmeta(df) }}>
	{%- if df.fieldtype in ("Text", "Code") %}<label>{{ _(df.label) }}</label>{%- endif %}
	{%- if df.fieldtype=="Code" %}
		<pre class="value">{{ doc.get(df.fieldname) }}</pre>
	{% else -%}
		{{ doc.get_formatted(df.fieldname, parent_doc or doc) }}
	{% endif -%}
</div>
{%- endif -%}
{%- endmacro -%}

{%- macro print_value(df, doc, meta, parent_doc=None) -%}
    {% if doc.print_templates and
			doc.print_templates.get(df.fieldname) %}
        {% include doc.print_templates[df.fieldname] %}
	{% elif df.fieldtype=="Check" %}
		<i class="{{ 'icon-check' if doc[df.fieldname] else 'icon-check-empty' }}"></i>
	{% elif df.fieldtype=="Image" %}
		<img src="{{ doc[doc.meta.get_field(df.fieldname).options] }}" class="img-responsive">
	{% elif df.fieldtype in ("Attach", "Attach Image") and doc[df.fieldname]
		and (guess_mimetype(doc[df.fieldname])[0] or "").startswith("image/") %}
		<img src="{{ doc[df.fieldname] }}" class="img-responsive">
    {% elif df.fieldtype=="HTML" %}
        {{ frappe.render_template(df.options, {"doc":doc}) }}
	{% else %}
		{{ doc.get_formatted(df.fieldname, parent_doc or doc, translated=True) }}
	{% endif %}
{%- endmacro %}

{% macro get_width(df) -%}
	{%- if df.print_width -%}{{ (df.print_width|str).replace("px", "") }}
	{%- elif df.fieldtype in ("Int", "Check", "Float", "Currency") -%}{{ 80 }}
	{%- else -%}{{ 150 }}{% endif -%}
{%- endmacro %}

{% macro get_align_class(fieldtype) %}
	{%- if fieldtype in ("Int", "Float", "Currency") -%}{{ "text-right" }}
	{%- else -%}{{ "" }}
	{%- endif -%}
{% endmacro %}

{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead, footer) -%}
	{% if letter_head and not no_letterhead %}
		<div class="letter-head">{{ letter_head }}</div>
	{% endif %}
    {% if doc.print_heading_template %}
        {{ frappe.render_template(doc.print_heading_template, {"doc":doc}) }}
    {% else %}
    <div class="print-heading">
		<h2>{{ doc.select_print_heading or (doc.print_heading if doc.print_heading != None
			else _(doc.doctype)) }}<br>
			<small>{{ doc.sub_heading if doc.sub_heading != None
				else doc.name }}</small>
        </h2>
    </div>
    {% endif %}
	{%- if doc.meta.is_submittable and doc.docstatus==0-%}
	<div class="text-center" document-status="draft">
		<h4 style="margin: 0px;">{{ _("DRAFT") }}</h4>
    </div>
	{%- endif -%}
	{%- if doc.meta.is_submittable and doc.docstatus==2-%}
	<div class="text-center" document-status="cancelled">
		<h4 style="margin: 0px;">{{ _("CANCELLED") }}</h4>
    </div>
	{%- endif -%}
{%- endmacro -%}
