{extends "base.tpl.html"}
{block "title"}
{if $details|default:"" == ""}
    {t 1=$issue_id}#%1 - Add Time Tracking{/t}
{else}
    {t 1=$issue_id}#%1 - Edit Time Tracking{/t}
{/if}
{/block}

{block "content"}

{if $time_add_result|default:'' != '' or $time_update_result|default:'' != ''}
    <br />
    <center>
    <span><b>
    {if $time_add_result|default:'' == -1}
        {t}An error occurred while trying to run your query{/t}
    {elseif $time_add_result|default:'' == 1}
        {t}Thank you, the time tracking entry was added successfully.{/t}
    {elseif $time_update_result|default:'' == -1}
        {t}An error occurred while trying to run your query{/t}
    {elseif $time_update_result|default:'' == 1}
        {t}Thank you, the time tracking entry was updated successfully.{/t}
    {/if}
    </b></span>
    </center>
    <script type="text/javascript">
    <!--
    var noparent = {$smarty.get.noparent|default:0};
    {if $core.current_user_prefs.close_popup_windows}
        setTimeout('Eventum.close_and_refresh(noparent)', 2000);
    {/if}
    //-->
    </script>
    <br />
    {if not $core.current_user_prefs.close_popup_windows}
    <center>
        <span><a href="javascript:void(null);" onClick="Eventum.close_and_refresh(noparent);">{t}Continue{/t}</a></span>
    </center>
    {/if}
{else}

<script type="text/javascript">
<!--

function validateTimeForm()
{
    if (Validation.isFieldWhitespace('summary')) {
        Validation.selectField('summary');
        alert('{t escape=js}Please enter the summary for this new time tracking entry.{/t}');
        return false;
    }
    if (Eventum.getField('category').val() == '') {
        Validation.selectField('category');
        alert('{t escape=js}Please choose the time tracking category for this new entry.{/t}');
        return false;
    }
    if ((Validation.isFieldWhitespace('time_spent') || (!Validation.isNumberOnly(Eventum.getField('time_spent').val())))) {
        Validation.selectField('time_spent');
        alert('{t escape=js}Please enter integers (or floating point numbers) on the time spent field.{/t}');
        return false;
    }
    if (!Validation.isValidDate('date')) {
        alert('{t escape=js}Please select a valid date of work.{/t}');
        return false;
    }

    var now = new Date();
    var d1 = Eventum.makeDate('date');
    if (d1 > now) {
        alert('{t}Start time in the future.{/t}');
        return false;
    }
    var d2 = Eventum.makeDate('date2');
    if (d2 > now) {
        alert('{t}End time in the future.{/t}');
        return false;
    }
    return true;
}

$().ready(function() {
  var $form = $('#add_time_form');
  $form.garlic({
    getPath: function ($elem) {
      var form = $elem.parents('form');
      var field_keys = [form.find('input[name=issue_id]').val(), $elem.attr('name')];
      return field_keys.join('_');
    }
  });

{if $details|default:'' == ''}
  Eventum.updateTimeFields($form, 'date[Year]', 'date[Month]', 'date[Day]', 'date[Hour]', 'date[Minute]');
  Eventum.updateTimeFields($form, 'date2[Year]', 'date2[Month]', 'date2[Day]', 'date2[Hour]', 'date2[Minute]');
{/if}
  $form.submit(validateTimeForm);
});
//-->
</script>

    <form id="add_time_form" method="post">
        {if $details|default:'' != ''}
        <input type="hidden" name="cat" value="update_time">
        <input type="hidden" name="ttr_id" value="{$details.ttr_id}">
        {else}
        <input type="hidden" name="cat" value="add_time">
        <input type="hidden" name="issue_id" value="{$issue_id}">
        {/if}
        <table class="bordered">
            {if $core.current_role > $core.roles.viewer}
            <tr class="title">
                <th colspan="2">
                    {if $details|default:"" == ""}
                        {t}Add Time Worked{/t}
                    {else}
                        {t}Edit Time Worked{/t}
                    {/if}
                </th>
            </tr>
            <tr>
                <th width="190" nowrap>{t}Summary{/t}</th>
                <td><input type="text" name="summary" size="45" value="{$details.ttr_summary|default:''|escape}">{include file="error_icon.tpl.html" field="summary"}</td>
            </tr>
            <tr>
                <th nowrap>{t}Category{/t}</th>
                <td>
                    <select name="category">
                        <option value="">{t}Please choose a category{/t}</option>
                        {html_options options=$time_categories selected=$details.ttc_id|default:''}
                    </select>
                    {include file="error_icon.tpl.html" field="category"}
                </td>
            </tr>
            <tr>
                <th nowrap>{t}Time Spent{/t}</th>
                <td><input type="text" size="5" name="time_spent" onChange="Eventum.calcDateDiff(this.form, 0)"
                         value="{$details.ttr_time_spent|default:''|escape}"> <span>({t}in minutes{/t})</span>{include file="error_icon.tpl.html" field="time_spent"}</td>
            </tr>
            <tr>
            <th nowrap>{t}Work started{/t}:</th>
                <td>
                    {html_select_date start_year='-1' day_value_format='%02d' field_array='date' prefix='' all_extra=' onChange="Eventum.calcDateDiff(this.form, 1, this.name)"'
                        time=$start_date|default:''}&nbsp;
                    {html_select_time minute_interval=5 field_array='date' prefix='' all_extra=' onChange="Eventum.calcDateDiff(this.form, 1, this.name)"' display_seconds=false
                        time=$start_date|default:''}
                    <a href="javascript:void(null);" onClick="Eventum.updateTimeFields($('form#add_time_form'), 'date[Year]', 'date[Month]', 'date[Day]', 'date[Hour]', 'date[Minute]');Eventum.calcDateDiff($('form#add_time_form'), 11)">
                      <i class="fa fa-refresh" aria-hidden="true"></i>
                    </a>
                </td>
            </tr>
            <tr>
                <th nowrap>{t}Work ended{/t}:</th>
                <td>
                    {html_select_date start_year='-1' day_value_format='%02d' field_array='date2' prefix='' all_extra=' class="default" onChange="Eventum.calcDateDiff(this.form, 2, this.name)"'
                        time=$start_date|default:''}&nbsp;
                    {html_select_time minute_interval=5 field_array='date2' prefix='' all_extra=' class="default" onChange="Eventum.calcDateDiff(this.form, 2, this.name)"' display_seconds=false
                        time=$end_date|default:''}
                    <a href="javascript:void(null);" onClick="Eventum.updateTimeFields($('form#add_time_form'), 'date2[Year]', 'date2[Month]', 'date2[Day]', 'date2[Hour]', 'date2[Minute]'); Eventum.calcDateDiff($('form#add_time_form'), 12)">
                      <i class="fa fa-refresh" aria-hidden="true"></i>
                    </a>
                </td>
            </tr>
            <tr class="buttons">
                <td colspan="2">
                    <input type="submit" value="{if $details|default:"" == ""}{t}Add Time Entry{/t}{else}{t}Edit Time Entry{/t}{/if}">
                    <input name="cancel" type="reset" value="{t}Cancel{/t}" onClick="window.close();">
                </td>
            </tr>
            {/if}
        </table>
    </form>
{/if}
{/block}
