{% macro render_field(df, doc) -%}
	{%- if df.fieldtype=="Table" -%}
		{{ render_table(df, doc) }}
	{%- elif df.fieldtype=="HTML" and df.options -%}
		<div>{{ frappe.render_template(df.options, {"doc": doc}) or "" }}</div>
	{%- elif df.fieldtype in ("Text", "Text Editor", "Code") -%}
		{{ render_text_field(df, doc) }}
	{%- elif df.fieldtype in ("Image", "Attach Image", "Attach")
		and (guess_mimetype(doc[df.fieldname])[0] or "").startswith("image/")  -%}
		{{ render_image(df, doc) }}
	{%- elif df.fieldtype=="Geolocation" -%}
		{{ render_geolocation(df, doc) }}
	{%- elif df.fieldtype=="Signature" -%}
		{{ render_signature(df, doc) }}
	{%- elif df.fieldtype=="Currency" -%}
		{%- if doc.print_templates and
				doc.print_templates.get(df.fieldname) -%}
			{% include doc.print_templates[df.fieldname] %}
		{%- else -%}
			{{ render_field_with_label(df, doc) }}
		{%- endif -%}
	{%- 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 %}
						{% if (data and not data[0].flags.compact_item_print) or tdf.fieldname in doc.get(df.fieldname)[0].flags.compact_item_fields %}
							<th style="width: {{ get_width(tdf) }};" class="{{ get_align_class(tdf) }}" {{ fieldmeta(df) }}>
								{{ _(tdf.label) }}</th>
						{% endif %}
						{% endfor %}
					</tr>
				</thead>
				<tbody>
					{% for d in data %}
					<tr>
						<td class="table-sr">{{ d.idx }}</td>
						{% for tdf in visible_columns %}
						{% if not d.flags.compact_item_print or tdf.fieldname in doc.get(df.fieldname)[0].flags.compact_item_fields %}
							<td class="{{ get_align_class(tdf) }}" {{ fieldmeta(df) }}>
                                <div class="value">{{ print_value(tdf, d, doc, visible_columns) }}</div></td>
						{% endif %}
						{% 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-{{ "9" if df.fieldtype=="Check" else "5" }}
				{%- if doc.align_labels_right %} text-right{%- endif -%}">

				{% if df.fieldtype not in ("Image", "HTML", "Check") and
					doc.get(df.fieldname) != None %}
				<label>{{ _(df.label) }}</label>
				{% endif %}
				{% if df.fieldtype == "Check" and
				doc.get(df.fieldname) != 0 %}
				<label>{{ _(df.label) }}</label>
				{% endif %}
			</div>
			<div class="col-xs-{{ "3" if df.fieldtype=="Check" else "7" }}
			{{ get_align_class(df) }} 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 -%}
		{%- if df.fieldtype=="Text Editor" -%}<div class='text-editor-print'>{%- endif -%}
		{{ doc.get_formatted(df.fieldname, parent_doc or doc, translated=df.translatable) }}
		{%- if df.fieldtype=="Text Editor" -%}</div>{%- endif -%}
  {% endif -%}
</div>
{%- endif -%}
{%- endmacro -%}

{%- macro render_image(df, doc) -%}
	{{ print_value(df, doc) }}
{% endmacro %}

{%- macro render_signature(df, doc) -%}
	{{ print_value(df, doc) }}
{% endmacro %}

{%- macro render_geolocation(df, doc) -%}
	{{ "" }}
{%- endmacro -%}

{%- macro print_value(df, doc, parent_doc=None, visible_columns=None) -%}
    {% if doc.print_templates and
			doc.print_templates.get(df.fieldname) %}
        {% include doc.print_templates[df.fieldname] %}
	{% elif df.fieldtype=="Check" %}
		<i class="{{ 'fa fa-check' if doc[df.fieldname] }}"></i>
	{% elif df.fieldtype=="Image" %}
		<img src="{{ doc[doc.meta.get_field(df.fieldname).options] }}"
			class="img-responsive"
			{%- if df.print_width %} style="width: {{ get_width(df) }};"{% endif %}>
	{% elif df.fieldtype=="Signature" %}
		<img src="{{ doc[df.fieldname] }}" class="signature-img img-responsive"
			{%- if df.print_width %} style="width: {{ get_width(df) }};"{% endif %}>
	{% elif df.fieldtype in ("Attach", "Attach Image") and doc[df.fieldname]
		and frappe.utils.is_image(doc[df.fieldname]) %}
		<img src="{{ doc[df.fieldname] }}" class="img-responsive"
			{%- if df.print_width %} style="width: {{ get_width(df) }};"{% endif %}>
    {% elif df.fieldtype=="HTML" %}
        {{ frappe.render_template(df.options, {"doc":doc}) }}
	{% else %}
		{{ doc.get_formatted(df.fieldname, parent_doc or doc, translated=df.translatable) }}
	{% endif %}
{%- endmacro %}

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

{% macro get_align_class(df) %}
	{%- if df.align -%}{{ "text-" + df.align }}
	{%- elif df.fieldtype in ("Int", "Float", "Currency", "Check", "Percent") -%}{{ "text-right" }}
	{%- else -%}{{ "" }}
	{%- endif -%}
{% endmacro %}

{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead, footer, print_settings=None) -%}
	{% 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 and (print_settings==None or print_settings.add_draft_heading) -%}
	<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 -%}
