{% extends "base.html" %}

{% block title %}
Lesion Form
{% endblock %}

{% block content %}
{% if object %}
    {{object.treatment.patient}} {{object.treatment.patient.medical_records}} 
	<h2>Update lesion</h2>
{% else %}
    {{treatment.patient}} {{treatment.patient.medical_records}}
	<h2>Create lesion</h2>
{% endif %}

{{ form.errors }}

{% if form.has_errors %}
<h2>Please correct the following error{{ form.errors|pluralize }}:</h2>
{% endif %}

<p>{{object.treatment.patient.name}}</p>



<form action="." class="cssform" method="POST" name="lesionForm">{% csrf_token %}

  <input type="text" readonly="readonly" id="id_id" name="id" value={{object.id}}>
    <input type="text" readonly="readonly" id="id_patient" name="patient"
      {% if object %}
        value = {{object.treatment.patient.id}}
      {% else %}
        value = {{treatment.patient.id}}
      {% endif %}
    />
    <input type="text" readonly="readonly" id="id_treatment" name="treatment"
      {% if object %}
        value = {{object.treatment.id}}
      {% else %}
        value = {{treatment.id}}
      {% endif %}
    />

    <p>{{form.calibration_table.label_tag}}{{form.calibration_table}}{{form.calibration_table.errors}}</p>
    <p>{{form.density_correction.label_tag}}{{form.density_correction}}{{form.density_correction.errors}}</p>

    <p><label for="id_target_location">Target Location</label>
        <select id="id_target_location" class="vSelectField required" name="target_location" size="1">
            <option value="" selected="selected">---------</option>
        </select></p>

    <p>{{form.sub_location.label_tag}}{{form.sub_location}}{{form.sub_location.errors}}</p>
    <p>{{form.pathology.label_tag}}{{form.pathology}}{{form.pathology.errors}}</p>
    <!-- p><label for="id_sub_location">Anatomic Sub Location</label>
       {{ form.sub_location }}
       {% if form.sub_location.errors %}{{ form.sub_location.errors }}{% endif %}</p>
    <p><label for="id_pathology">Pathology</label>
        {{ form.pathology }}
        {% if form.pathology.errors %}{{ form.pathology.errors }}{% endif %}</p -->
    <p><label for="id_dimensions">Dimensions(mm)</label>
        {{ form.dimensions }}
        {% if form.dimensions.errors %}{{ form.dimensions.errors }}{% endif %}</p>

    <!-- <p><label for="id_volume">Target Volume(mm<sup>3</sup>)</label>
        {{ form.volume }}
        {% if form.volume.errors %}{{ form.volume.errors }}{% endif %}</p> -->
    <!-- <p><label for="id_volume_measure">Target Volume</label> -->
    <p>{{form.volume_measure.label_tag}}
        {{ form.volume_measure }}
        {% if form.volume_measure.errors %}{{ form.volume_measure.errors }}{% endif %}</p>

    <p><label for="id_plan_name">Plan name</label>
        {{ form.plan_name }}
        {% if form.plan_name.errors %}{{ form.plan_name.errors }}{% endif %}</p>

    <p>{{form.collimator_type.label_tag}}
        {{ form.collimator_type }}
        {% if form.collimator_type.errors %}{{ form.collimator_type.errors }}{% endif %}</p>
    <p><label for="id_collimator">Collimator(mm)</label>
        {{ form.collimator }}
        {% if form.collimator.errors %}{{ form.collimator.errors }}{% endif %}</p>

    <p><label for="id_path_no">Path No.</label>
        {{ form.path_no }}
        {% if form.path_no.errors %}{{ form.path_no.errors }}{% endif %}</p>
    <p><label for="id_beam_no">Beam No.</label>
        {{ form.beam_no }}
        {% if form.beam_no.errors %}{{ form.beam_no.errors }}{% endif %}</p>


    {{ form.mu_max.as_hidden }}
    {{ form.mu_min.as_hidden }}

    {% comment %}
    <p><label for="id_mu_max">MU Max.</label>
        {{ form.mu_max }}
        {% if form.mu_max.errors %}{{ form.mu_max.errors }}{% endif %}</p>
    <p><label for="id_mu_min">MU Min.</label>
        {{ form.mu_min }}
        {% if form.mu_min.errors %}{{ form.mu_min.errors }}{% endif %}</p>

        {{ form.mu_max.as_hidden }}
    {% endcomment %}

    <p><label for="id_dose">Prescription Dose(cGy)</label>
        {{ form.dose }}
        {% if form.dose.errors %}{{ form.dose.errors }}{% endif %}</p>
    <p><label for="id_fractions">Number of Fractions</label>
        {{ form.fractions }}
        {% if form.fractions.errors %}{{ form.fractions.errors }}{% endif %}</p>
    <p><label for="id_iso_dose_curve">Prescription Isodose Curve(%)</label>
        {{ form.iso_dose_curve }}
        {% if form.iso_dose_curve.errors %}{{ form.iso_dose_curve.errors }}{% endif %}</p>

    <p><label for="id_dmax">Dmax(cGy)</label>
        {{ form.dmax }}
        {% if form.dmax.errors %}{{ form.dmax.errors }}{% endif %}</p>
    <p><label for="id_dmin">Dmin(cGy)</label>
        {{ form.dmin }}
        {% if form.dmin.errors %}{{ form.dmin.errors }}{% endif %}</p>
    <p>{{form.dmean.label_tag}}
        {{ form.dmean }}
        {% if form.dmean.errors %}{{ form.dmean.errors }}{% endif %}</p>

    <p><label for="id_coverage">Coverage(%)</label>
        {{ form.coverage }}
        {% if form.coverage.errors %}{{ form.coverage.errors }}{% endif %}</p>
    <p><label for="id_ci">CI</label>
        {{ form.ci }}
        {% if form.ci.errors %}{{ form.ci.errors }}{% endif %}</p>
    <p><label for="id_nci">nCI</label>
        {{ form.nci }}
        {% if form.nci.errors %}{{ form.nci.errors }}{% endif %}</p>
    <p><label for="id_start_date">開始日期</label>
        {{ form.start_date }}
       {% if form.start_date.errors %}{{ form.start_date.errors }}{% endif %}</p>
    <p><label for="id_end_date">結束日期</label>
        {{ form.end_date }}
       {% if form.end_date.errors %}{{ form.end_date.errors }}{% endif %}</p>
    <p><label for="id_memo">Memo</label>
        {{ form.memo }}
        {% if form.memo.errors %}{{ form.memo.errors }}{% endif %}</p>
    <div style="margin-left: 150px;">
        <input type="submit" /> <input type="reset" />
    </div>
    <a href='/treatment/detail/'>Return to treatment</a>
    <a href='/patient/detail/'>Return to patient</a>
</form>


This is a lifesaver when debugging!
<p> {{ form.error_dict }} </p>


<script type="text/javascript">

	$(document).ready(function(){
	
	// if (!$.isNumeric($( "#id_patient" )))
	// 	$( "#id_patient" ).val($.cookie("patient"));

	// if (!$.isNumeric($( "#id_treatment" )))
	// 	$( "#id_treatment" ).val($.cookie("treatment"));
        
	  $("#id_start_date").datepicker();
	  $("#id_end_date").datepicker();

	    $.getJSON("/get/target_location/", function(j){
	      var options = '<option value="0">---------</option>';
	      for (var i = 0; i < j.length; i++) {
	        options += '<option value="' + j[i].pk + '">' + j[i].fields.location + '</option>';
	      }
	      $("select#id_target_location").html(options);
	    })
	    
		sid = document.getElementById('id_sub_location').value;

	    $.getJSON("/get/sublocation/",{id: sid, ajax: 'true'}, function(j){
//	        var elem = document.getElementById('id_target_location');
//    		elem.value = j[0].fields.target_location;
    		$("select#id_target_location").val(j[0].fields.target_location);	    
	    })
	    
	});

  $("select#id_target_location").change(function(){
    $.getJSON("/get/sublocation/",{target: $(this).val(), ajax: 'true'}, function(j){
      var options = '<option value="0">---------</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].pk + '">' + j[i].fields.sub_location + '</option>';
      }
      $("select#id_sub_location").html(options);
    })
  })

  $("select#id_sub_location").change(function(){
    $.getJSON("/get/pathology/",{sublocation: $(this).val(), ajax: 'true'}, function(j){
      var options = '<option value="0">---------</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].pk + '">' + j[i].fields.pathology + '</option>';
      }
      $("select#id_pathology").html(options);
    })
  })

</script>

{% endblock %}
