{% extends base %}
{% block head_extra %}
<style>
.label {
font-weight: bold;
}
</style>
{% endblock %}

{% block content %}
{% import "macros/action_icons_static.html" as icons with context %}
<span class="title"><h2>{% block title %}Tenders{% endblock %}</h2></span>
<div class="content-container content">
<div><button type="button" id="btn_add_tender" aria-label="Add tender">
{{ icons.add_svg(s_label="add tender") }}
Add tender
</button></div>
{% if tenders %}
<h3>Tenders</h3>
<div class="table-container">
<table class="data-table" id="tbl_tenders">
<thead>
<tr>
<th>Reference Number</th><th>Description</th><th style="font-size: smaller;">Actions</th>
</tr>
</thead>
{% for tender in tenders %}
<tr>
<td>{{ tender.v_reference_number }}</td><td>{{ tender.v_description }}</td>
<td>
<a href="#{{ tender.id }}" class="a_edit_tender">{{ icons.edit_svg() }}</a>
<a href="#{{ tender.id }}" class="a_remove_tender">{{ icons.delete_svg() }}</a>
</td>
</tr>
{% endfor %}{# end of looping through tenders #}
</table><!-- end of tbl_tenders -->
</div><!-- end of div.table-container -->
<div class="paging">
<div>Paging...</div>

{#
{% if paging.has_prev %}
<!-- prev_num --><span><a class="a_page_number" href="#{{ paging.prev_num }}"{{ "<<" }}</a></span>
{% endif %}
#}

{% for number in paging.iter_pages() %}
{% if paging.page != number %}
<!-- page_num --><span><a class="a_page_number" href="#{{ number }}">{{ number }}</a></span>&nbsp;&nbsp;
{% else %}
<!-- current_num --><span class="current-page-number">{{ number }}</span>&nbsp;&nbsp;
{% endif %}
{% endfor %}

{#
{% if paging.has_next %}
<span>
<!-- next_num --><a class="a_page_number" href="#{{ paging.next_num }}">{{ ">>" }}</a></span>
{% endif %}
#}

</div><!-- end of div.paging -->
{% endif %}{# end of checking if tenders existed #}
</div><!-- end of div.content -->
{% from "macros/dialog.html" import dlg_prep with context %}
{{ dlg_prep(["dlg_tender"]) }}
{# dlg divs below #}
<div id="dlg_tender" aria-labeledby="spn_tender">
<span id="spn_tender">Tender details</span><br>
<form action="{{ url_for("main.tenders") }}" method="post" id="frm_tender">
{{ form.csrf_token }}
{{ form.hid_tender_id }}
<ul>
<li>{{ form.txt_reference_number.label }}&nbsp;{{ form.txt_reference_number }}</li>
<li>{{ form.txt_description.label }}<br>
{{ form.txt_description }}</li>
</ul>
<input type="submit" name="btn_save_tender" value="Save">
</form>
</div><!-- end of dlg_tender -->
<script type="text/javascript">
$(document).ready( function() {

try {

var s_dlg_tender = $("#dlg_tender").html();

$("#btn_add_tender").click( function(event) {
    event.preventDefault();
    $("#dlg_tender").html(s_dlg_tender);
    $("#dlg_tender").redraw();
    $("#dlg_tender").dialog("open");
    $("#txt_reference_number").focus();
});// end of #btn_add_tender click event

$("#tbl_tenders").on("click", ".a_edit_tender", function(event) {
try {
    event.preventDefault();
    $("#dlg_tender").html(s_dlg_tender);
    $("#dlg_tender").redraw();
    var s_id = String($(this).attr("href")).replace("#", "");
    var s_url = "{{ url_for("main.tender_details", i_tender_id=99999) }}".replace("99999", s_id);
    $.get(s_url, function(o_data) {
        if (typeof(o_data)=="object") {
            var s_reference_number = String(o_data.v_reference_number);
            var s_description = String(o_data.v_description)
            $("#hid_tender_id").val(s_id);
            $("#txt_reference_number").val(s_reference_number);
            $("#txt_description").val(s_description);
            $("#dlg_tender").redraw();
            $("#dlg_tender").dialog("open");
            $("#txt_reference_number").focus();
        } else {
            do_alert(String(o_data));
        }// end of typeof check against o_data
    });// end of .get
} catch(e) {
    var s_err = String(e.name) + "\nmessage:" + String(e.message);
    s_err = (typeof(e.lineNumber)!="undefined") ? s_err + "\nline:" + String(e.lineNumber) : s_err;
    alert("Error! " + s_err);
}//end of catch
});// end of .a_edit_tender click event inside tbl_tenders

$("#tbl_tenders").on("click", ".a_remove_tender", function(event) {
try {
    event.preventDefault();
    var bl_confirm = confirm("Are you sure?");
    if (bl_confirm) {
        var s_id = String($(this).attr("href")).replace("#", "");
        $("#hid_remove_tender_id").val(s_id);
        document.getElementById("frm_remove_tender").submit();
    }// end of confirmation check
} catch(e) {
    var s_err = String(e.name) + "\nmessage:" + String(e.message);
    s_err = (typeof(e.lineNumber)!="undefined") ? s_err + "\nline:" + String(e.lineNumber) : s_err;
    alert("Error! " + s_err);
}//end of catch
});// end of .a_edit_tender click event inside tbl_tenders

function jump_page(i_page) {
    i_page = (isNaN(i_page)) ? 1 : Number(i_page);
    $("#hid_page").val(i_page);
    document.getElementById("frm_paging").submit();
}// end of jump_page function

$(".a_page_number").click( function(event) {
    event.preventDefault();
    var s_page = String($(this).attr("href")).replace("#", "");
    i_page = (isNaN(s_page)) ? 1 : Number(s_page);
    jump_page(i_page);
});// end of .a_page_number function

window.setTimeout( function() {
    var s_nada = ""; //alert("hello world");
}, 300);
var s = "nada"; // do_alert("hello world");

} catch(e) {
    var s_err = String(e.name) + "\nmessage:" + String(e.message);
    s_err = (typeof(e.lineNumber)!="undefined") ? s_err + "\nline:" + String(e.lineNumber) : s_err;
    alert("Error! " + s_err);
}//end of catch

});// end of secondary document ready
</script>
<form action="{{ url_for("main.tenders") }}" method="post" id="frm_remove_tender">
{{ remove_form.csrf_token }}
{{ remove_form.hid_remove_tender_id }}
</form>
<form action="{{ url_for("main.tenders") }}" method="post" id="frm_paging">
<input type="hidden" name="hid_page" id="hid_page" value="{{ page }}">
</form>
{% endblock %}