{% extends 'base.html' %}

{% load pathology %}

{% block head %}
	<script type="text/javascript"> 	

		function showUrlInDialogx(url, options){
		  options = options || {};
		  var tag = $("<div></div>"); //This tag will the hold the dialog content.
		  $.ajax({
		    url: url,
		    type: (options.type || 'GET'),
		    beforeSend: options.beforeSend,
		    error: options.error,
		    complete: options.complete,
		    success: function(data, textStatus, jqXHR) {
		      if(typeof data == "object" && data.html) { //response is assumed to be JSON
		        tag.html(data.html).dialog({modal: options.modal, title: data.title}).dialog('open');
		      } else { //response is assumed to be HTML
		        tag.html(data).dialog({modal: options.modal, title: options.title}).dialog('open');
		      }
		      $.isFunction(options.success) && (options.success)(data, textStatus, jqXHR);
		    }
		  });
		}	 	
	 	
		function showUrlInDialog(url){
		  var tag = $("<div></div>");
		  $.ajax({
		    url: url,
		    success: function(data) {
		      tag.html(data).dialog({
				dialogClass: "alert",
				modal: true,
				show: "slide",
				width: 640,
			    open: function(){
			        $("body").css("overflow", "hidden");
			    },
			    close: function(){
			        $("body").css("overflow", "auto");
			    },				
			}).dialog('open');
		    }
		  });
		}	 	

    document.addEventListener('DOMContentLoaded', function() {
        // Your code here will execute once the DOM is ready
        // document.getElementById('id_models').style.display = 'none';
        document.getElementById("id_models").innerHTML = "<label>Pathology reports</label>";
    });        

	</script>
	
 	
{% endblock %}

{% block logo %}
	<h1><a href="#">{{specialty}} 病理報告搜尋</a></h1>
{% endblock %}

{% block content %}
    <h2>Search</h2>

    <div id="accordion">
        <div class="card">
            <div class="card-header" id="headingTwo">
                <h5 class="mb-0">
                    <button class="btn btn-primary" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                    Query tips!
                    </button>
                    <a href="https://whoosh.readthedocs.io/en/latest/querylang.html" target="_blank">query language</a>
                </h5>
            </div>
            <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordion">
                <div class="card-body">
                    <table class="table table-sm">
                        <tr><td>單一關鍵字(非100%精確)</td><td>e.g. meningioma, metastatic, schwannoma</td><td>"malignant meningioma"</td></tr>
                        <tr><td>含任一關鍵字</td><td>metastases OR metastasis OR metastatic</td><td>SSTR2a OR EMA</td></tr>
                        <tr><td>含所有關鍵字</td><td>meningioma malignancy</td><td>meningioma AND malignancy</td></tr>
                        <tr><td>排除關鍵字(慎用)</td><td>meningioma NOT atypical NOT malignancy</td><td>meningioma NOT (atypical OR malignancy)</td></tr>
                        <tr><td>萬用字元(慢)</td><td>metasta*</td><td>metastas?s</td></tr>
                    </table>
                </div>
            </div>
        </div>
    </div>


    <form method="get" action=".">
        <table>
            {{ form.as_table }}
            <tr>
                <td>&nbsp;</td>
                <td>
                    <!-- <input type="submit" value="Search"> -->
					<input class="btn btn-primary" type="submit" id="search_submit"   name="search_submit"   value="Search" />
					<input class="btn btn-success" type="submit" id="search_xls"      name="search_xls"      value="匯出 Excel 2007-2013 XML 工作簿" />
                </td>
            </tr>
        </table>

        {% if query %}
            <h3>Results - {{page_obj.paginator.count}} Page: {{page_obj.number}}/{{page_obj.paginator.num_pages}}</h3>

            {% with page=page_obj %}

            {% if page.has_previous or page.has_next %}
            <nav aria-label="Page navigation example">
            <ul class="pagination">
                {% if page.has_previous %}
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page=1">First</a></li>
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page={{ page.previous_page_number }}">Previous</a></li>
                {% endif %}
                <li class="page-item"><a class="page-link">{{page_obj.number}}</a></li>
                {% if page.has_next %}
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page={{ page.next_page_number }}">Next</a></li>
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page={{ page_obj.paginator.num_pages }}">Last({{ page_obj.paginator.num_pages }})</a></li>
                {% endif %}
            </ul>
            </nav>
            {% endif %}

            <table class="table">
                <thead>
                    <tr>
                        <th>病歷號</th>
                        <!-- <th>ReportKey</th> -->
                        <!-- <th>ReportCode</th> -->
                        <!-- <th>PersonID</th> -->
                        <th>生日</th>
                        <th>病理號</th>
                        <th>收件日</th>
                        <th>報告日</th>
                        <th>html</th>
                    </tr>
                </thead>
                <tbody>
                {% for result in page.object_list %}
                    <tr>
                        <td>{{ result.object.ChartNo }}</td>
                        <!-- <td>{{ result.object.ReportKey }}</td> -->
                        <!-- <td>{{ result.object.ReportCode }}</td> -->
                        <!-- <td>{{ r.object.PersonID }}</td> -->
                        <td>{{ result.object.html|DOB }}</td>
                        <td>{{ result.object.PathCode }}</td>
                        <td>{{ result.object.SpecimenGetDate|date:"Y-m-d" }}</td>
                        <td>{{ result.object.ReportDate|date:"Y-m-d" }}</td>
                        <!-- <td>{{ result.object.url }}</td> -->
                        <td>
                            <a href="#" title='{{result.object.html|patho_long}}' onclick="showUrlInDialog('/registry/patho_report/{{result.object.PathCode}}/'); return false;">{{result.object.html|patho_short|truncatewords:20}}</a>
                        </td>
                    </tr>
                {% empty %}
                    <p>No results found.</p>
                {% endfor %}
                </tbody>
            </table>

            {% if page.has_previous or page.has_next %}
            <nav aria-label="Page navigation example">
            <ul class="pagination">
                {% if page.has_previous %}
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page=1">First</a></li>
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page={{ page.previous_page_number }}">Previous</a></li>
                {% endif %}
                <li class="page-item"><a class="page-link">{{page_obj.number}}</a></li>
                {% if page.has_next %}
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page={{ page.next_page_number }}">Next</a></li>
                <li class="page-item"><a class="page-link" href="?q={{ query }}&amp;page={{ page_obj.paginator.num_pages }}">Last({{ page_obj.paginator.num_pages }})</a></li>
                {% endif %}
            </ul>
            </nav>
            {% endif %}

            {% endwith %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}
    </form>
{% endblock %}
