{% extends "base.html" %}

{% block head %}
<script type="text/javascript" src="/site_media/js/date.js"></script>
<script type="text/javascript" src="/site_media/js/date.format.js"></script>
<script type="text/javascript" src="/site_media/js/jquery.cookie.js"></script>
<!-- <script type="text/javascript" src="/site_media/datetimepicker_css.js"></script> -->

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.css"/ >
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js"></script>

<script type="text/javascript">

function onModeStage() {
    var tomorrow = new Date ();
    tomorrow.setDate (tomorrow.getDate() + 1);
    date = tomorrow.format("isoDate")+' ';

    switch (document.getElementById("id_mode").value)
    {
        case '110': //Fiducial
        case '200': //固定器
            document.getElementById("id_DTSTART").value=date + "10:00";
            document.getElementById("id_DURATION").value="0:30";
            break;
        case '210': //CT
            document.getElementById("id_DTSTART").value=date + "13:00";
            document.getElementById("id_DURATION").value="0:30";
            break;
        case '220': //MRI
            document.getElementById("id_DTSTART").value=date + "11:00";
            document.getElementById("id_DURATION").value="0:30";
            break;
        case '300': //預排
        case '310': //治療
            // document.getElementById("id_DTSTART").value=date + "9:00";
            document.getElementById("id_DURATION").value="1:00";
            break;
    }
}

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

	if (!$.isNumeric($( "#id_treatment" ).val()))
		$( "#id_treatment" ).val($.cookie("treatment"));

    if (document.getElementById("id_DURATION").value=="")
        document.getElementById("id_DURATION").value = '1:00';

    document.getElementById("id_mode").onchange = onModeStage;

    jQuery('#id_DTSTART').datetimepicker({
        format:'Y-m-d H:i',
        step: 30,
        });

}

</script>
{% endblock %}

{% block title %}
Event Form
{% endblock %}

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

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

{{object.treatment.patient.name}}
<form action="." class="cssform" method="POST" name="eventForm">{% 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><label for="id_mode">Mode</label>
       {{ form.mode }}
       {% if form.mode.errors %}{{ form.mode.errors }}{% endif %}
       {{ form.mode_remark }}
       {% if form.mode_remark.errors %}{{ form.mode_remark.errors }}{% endif %}
    </p>

    <p><label for="id_DTSTART">Start:</label>
        {{ form.DTSTART }}
        <!-- <a href="javascript: NewCssCal('id_DTSTART','yyyymmdd','dropdown',true)">   -->
        <!-- <img src="/images/cal.gif" width="16" height="16" alt="Pick a date" /></a> -->
       {% if form.DTSTART.errors %}{{ form.DTSTART.errors }}{% endif %}</p>

    <p><label for="id_DURATION">Duration</label>
       {{ form.DURATION }}
       {% if form.DURATION.errors %}{{ form.DURATION.errors }}{% endif %}</p>
    <p><label for="id_DESCRIPTION">Description</label>
       {{ form.DESCRIPTION }}
       {% if form.DESCRIPTION.errors %}{{ form.DESCRIPTION.errors }}{% endif %}</p>
    <p><label for="id_price">批價相關資訊</label>
       {{ form.price }}
       {% if form.price.errors %}{{ form.price.errors }}{% endif %}</p>
    <p>治療時間相關資訊</p>
    <p><label for="id_DTEND">Tx end</label>
       {{ form.DTEND }}
       {% if form.DTEND.errors %}{{ form.DTEND.errors }}{% endif %}</p>
    <p><label for="id_break_frequency">休息次數</label>
       {{ form.break_frequency }}
       {% if form.break_frequency.errors %}{{ form.break_frequency.errors }}{% endif %}</p>
    <p><label for="id_system_err">System Err次數</label>
       {{ form.system_err }}
       {% if form.system_err.errors %}{{ form.system_err.errors }}{% endif %}</p>
    <p><label for="id_shift">Pat移位置次數</label>
       {{ form.shift }}
       {% if form.shift.errors %}{{ form.shift.errors }}{% endif %}</p>
    <p><label for="id_cone">cone</label>
       {{ form.cone }}
       {% if form.cone.errors %}{{ form.cone.errors }}{% endif %}</p>
    <p><label for="id_path">path</label>
       {{ form.path }}
       {% if form.path.errors %}{{ form.path.errors }}{% endif %}</p>
    <div style="margin-left: 150px;">
        <input type="submit" /> <input type="reset" />
    </div>
    <a href='/treatment/detail/{{object.treatment.id}}/'>Return to treatment</a>
    <a href='/patient/detail/{{object.treatment.patient.id}}/'>Return to patient</a>
</form>

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

{% endblock %}
