{extends "reports/reports_base.tpl.html"}
{block "title"}Custom Fields Weekly{/block}

{block "report_content"}

<script type="text/javascript">
    <!--
    function validateForm(f)
    {
        if (getSelectedOption(f, 'custom_field') == -1) {
            selectField(f, 'custom_field');
            alert('{t escape=js}Please select the custom field that you would like to generate a report against.{/t}');
            return false;
        }
        return true;
    }
    //-->
</script>

<h3 align="center">
    {t}Custom Fields Weekly Report{/t}
</h3>
<form name="custom_fields_report" method="post" onSubmit="return validateForm(this);">
    <input type="hidden" name="cat" value="generate">
    <table class="bordered filters">
        <tr>
            <th width="120">
                {t}Report Type{/t}:
            </th>
            <td width="200" NOWRAP>
                <label><input type="radio" name="report_type" value="weekly" {if $report_type!='range'}checked{/if} onClick="changeType('weekly');">
                    {t}Weekly{/t}</label>&nbsp;
                <label><input type="radio" name="report_type" value="range" {if $report_type=='range'}CHECKED{/if} onClick="changeType('range');">
                    {t}Date Range{/t}</label>&nbsp;
            </td>
        </tr>
        <tr id="week_row">
            <th width="120">
                {t}Week{/t}:
            </th>
            <td width="200">
                <select name="week">
                    {html_options options=$weeks selected=$week}
                </select>
            </td>
        </tr>
        <tr id="start_row">
            <th width="120">
                {t}Start{/t}:
            </th>
            <td width="200">
                {html_select_date time=$start_date|default:'' prefix="" field_array="start" start_year="-2" end_year="+1" field_order="YMD" month_format="%b" day_value_format="%02d" all_extra="class='default'"}
            </td>
        </tr>
        <tr id="end_row">
            <th width="120">
                {t}End{/t}:
            </th>
            <td width="200">
                {html_select_date time=$end_date|default:'' prefix="" field_array="end" start_year="-2" end_year="+1" field_order="YMD" month_format="%b" day_value_format="%02d" all_extra="class='default'"}
            </td>
        </tr>
        <tr>
            <th width="30%" align="center">
                {t}Custom Field{/t}
            </th>
            <th width="30%" align="center">
                {t}Options{/t}
            </th>
        </tr>
        <tr>
            <td align="center" valign="top" height="10">
                <select name="custom_field" onChange="setOptions(this.options[this.selectedIndex].value, true)">
                    <option value="-1"></option>
                    {html_options options=$custom_fields selected=$custom_field}
                </select>
                {include file="error_icon.tpl.html" field="custom_field"}
            </td>
            <td align="center" valign="top">
                <select name="custom_options[]" size="8" multiple>
                </select>

            </td>
        </tr>
        <tr>
            <th width="120" valign="top">
                {t}Options{/t}:
            </th>
            <td width="200" NOWRAP>
                <label><input type="checkbox" name="time_in_min" value="1" {if $smarty.post.time_in_min|default:'' == 1}checked{/if}>
                    {t}Time spent as minutes{/t}</label>&nbsp;
                <br />
                <label>
                    <input type="checkbox" name="time_per_user" value="1" {if $smarty.post.time_per_user|default:'' == 1}checked{/if}>
                    {t}Time spent per user{/t}</label>&nbsp;
                <br />
            </td>
        </tr>

        <tr class="buttons">
            <td colspan="2">
                <input type="submit" name="cat" value="{t}Generate{/t}" class="shortcut">
            </td>
        </tr>
    </table>
</form>
<script type="text/javascript">
    var options = [];
    var option_to_fld_id = [];
    {foreach from=$options key=fld_id item=option_list name=option_list}
    option_to_fld_id[{$smarty.foreach.option_list.iteration-1}] = {$fld_id};
    options[{$smarty.foreach.option_list.iteration-1}] = [];
    {foreach from=$option_list key=cfo_id item=cfo_value name=option}
    options[{$smarty.foreach.option_list.iteration-1}][{$smarty.foreach.option.iteration-1}] = new Option({$cfo_value|json_encode}, {$cfo_id|json_encode});
    {/foreach}
        {/foreach}


            var options_field = document.forms['custom_fields_report'].elements['custom_options[]'];
            function setOptions(fld_id, auto_select)
            {
                var fld_id_index = '';
                for (var i = 0; i < option_to_fld_id.length; i++) {
                    if (option_to_fld_id[i] == fld_id) {
                        fld_id_index = i;
                    }
                }

                options_field.length = 0;
                if (options[fld_id_index]) {
                    options_field.length = options[fld_id_index].length;
                    for (var i = 0; i < options[fld_id_index].length; i++) {
                        options_field.options[i] = options[fld_id_index][i];
                        options_field.options[i].selected = auto_select;
                    }
                }
            }
            function changeType(type) {
                if (type == 'range') {
                    $('#week_row').hide();
                    $('#start_row').show();
                    $('#end_row').show();
                } else {
                    $('#week_row').show();
                    $('#start_row').hide();
                    $('#end_row').hide();
                }
            }

                changeType('{$report_type}');

                setOptions('{$custom_field}', false);
                {if $custom_options|@count > 0}
                {foreach from=$custom_options key=option_index item=option}

                for (var i = 0; i < options_field.options.length; i++) {
                    if (options_field.options[i].value == {$option|json_encode}) {
                    options_field.options[i].selected = true;
                }
            }

                {/foreach}
                    {/if}
</script>

{if $data|default:'' != ''}
{assign var="colspan" value=5}
<br />
<table class="bordered grid">
    <tr class="title">
        <th colspan="{$colspan}">
            {t}Issues/Customers matching criteria{/t}
        </th>
    </tr>
    <tr>
        <th>{t}Issue ID{/t}</th>
        <th>{t}Summary{/t}</th>
        <th>{t}Time Spent{/t}</th>
        <th>{$field_info.fld_title|escape:"html"}</th>
        {if $per_user}
        <th>{t}User{/t}</th>
        {/if}
    </tr>
    {foreach from=$data item=row}
    <tr class="{cycle values='odd,even'}">
        <td align="right">
            <a href="{$core.base_url}view.php?id={$row.iss_id}">{$row.iss_id}</a>
        </td>
        <td>{$row.iss_summary}</td>
        <td>{if $smarty.post.time_in_min|default:'' == 1}{$row.ttr_time_spent_sum}{else}{$row.ttr_time_spent_sum_formatted}{/if}</td>
        <td>{$row.field_value}</td>
        {if $per_user}
        <td>{$row.usr_full_name|escape:html}</td>
        {/if}
    </tr>
    {foreachelse}
    <tr>
        <td colspan="8" align="center"><i>{t}No data found{/t}</i></td>
    </tr>
    {/foreach}
</table>
{/if}
{/block}
