{extends "reports/reports_base.tpl.html"}
{block "title"}{t}Custom Fields{/t}{/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 Report{/t}
</h3>
<form name="custom_fields_report" method="get" onSubmit="return validateForm(this);">
<input type="hidden" name="cat" value="generate">
      <table class="bordered filters">
        <tr>
          <th width="30%" align="center">
            <b>{t}Field to Graph{/t}</b>
          </th>
          <th width="30%" align="center">
            <b>{t}Options to Graph{/t}</b>
          </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" rowspan="5">
            <select name="custom_options[]" size="8" multiple>
            </select>
          </td>
        </tr>
        <tr>
          <td width="30%" align="center" height="10">
            {if $core.has_crm}
              <b>{t}Group By{/t}</b>
            {/if}
          </td>
        </tr>
        <tr>
          <td align="left" valign="top">
            {if $core.has_crm}
            &nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="group_by" value="issue" {if $group_by != 'customer'}CHECKED{/if}>
                <a id="link" href="javascript:void(null)"
                            onClick="checkRadio('custom_fields_report', 'group_by', 0);">{t}Issue{/t}</a><br />
            &nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="group_by" value="customer" {if $group_by == 'customer'}CHECKED{/if}>
                <a id="link" href="javascript:void(null)"
                            onClick="checkRadio('custom_fields_report', 'group_by', 1);">{t}Customer{/t}</a><br />
            {else}
              <input type="hidden" name="group_by" value="issue">
            {/if}
          </td>
        </tr>
    <tr>
      <th align="center" height="10">
          <b>{t}Interval{/t}</b>
      </th>
    </tr>
    <tr>
      <td align="center" valign="top">
        <select name="interval">
            <option value=""></option>
            <option value="day"{if $smarty.request.interval|default:'' == 'day'} selected{/if}>{t}Day{/t}</option>
            <option value="week"{if $smarty.request.interval|default:'' == 'week'} selected{/if}>{t}Week{/t}</option>
            <option value="month"{if $smarty.request.interval|default:'' == 'month'} selected{/if}>{t}Month{/t}</option>
            <option value="year"{if $smarty.request.interval|default:'' == 'year'} selected{/if}>{t}Year{/t}</option>
        </select>
      </td>
    </tr>
    <tr>
      <th colspan="2">
        <b>{t}Issues Created Between{/t}</b>
      </th>
    </tr>
    <tr id="start_row">
      <td width="120" align="center">
        <b>{t}Start{/t}</b>
      </td>
      <td width="200">
        {html_select_date time=$start_date prefix="" field_array="start" start_year="-2" end_year="+1" field_order="YMD" month_format="%b" year_empty='YYYY' month_empty='MM' day_empty='DD' all_extra="class='default'"}
      </td>
    </tr>
    <tr id="end_row">
      <td width="120" align="center">
        <b>{t}End{/t}</b>
      </td>
      <td width="200">
        {html_select_date time=$end_date prefix="" field_array="end" start_year="-2" end_year="+1" field_order="YMD" month_format="%b" year_empty='YYYY' month_empty='MM' day_empty='DD' all_extra="class='default'"}
      </td>
    </tr>
    <tr>
      <th colspan="2">
        <b>{t}Assignee{/t}</b>
      </th>
    </tr>
    <tr>
      <td colspan="2" align="center">
        <select name="assignee">
                <option value="-1"></option>
                {html_options options=$assignees selected=$assignee}
            </select>
      </td>
    </tr>
        <tr>
          <td colspan="2" align="center">
            <input type="submit" name="cat" value="{t}Generate{/t}" class="shortcut">
          </td>
        </tr>
      </table>
    </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|escape:javascript}', '{$cfo_id|escape:javascript}');
    {/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;
        }
    }
}


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}') {
          options_field.options[i].selected = true;
        }
      }

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

{if $custom_options|@count > 0}
<div align="center">
    <img src="graph.php?graph=custom_fields&{$smarty.server.QUERY_STRING}"><br /><br />
    <img src="graph.php?graph=custom_fields&{$smarty.server.QUERY_STRING}&type=pie"><br />
    {t}Percentages may not add up to exactly 100% due to rounding.{/t}<br /><br />
</div>
{/if}

{if $data|@count > 0}
{assign var="colspan" value=3}
  <br />
  <table class="bordered grid medium">
    <tr class="title">
      <th colspan="{$colspan}">
        {t}Issues/Customers matching criteria{/t}
      </th>
    </tr>
    <tr>
      {if $core.has_crm}
      <th>{t}Customer{/t}</th>
      {/if}
      {if $smarty.request.group_by == 'customer'}
      <th>{t}Issue Count{/t}</th>
      {else}
      <th>{t}Issue ID{/t}</th>
      <th>{t}Summary{/t}</th>
      <th>{$field_info.fld_title}</th>
      {/if}
    </tr>
    {foreach from=$data item=row}
    <tr class="{cycle values='odd,even'}">
      {if $core.has_crm}
      <td>{$row.customer_title}</td>
      {/if}
      {if $smarty.request.group_by == 'customer'}
      <td align="center">{$row.row_count}</td>
      {else}
      <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>{$row.field_value}</td>
      {/if}
    </tr>
    {foreachelse}
    <tr>
        <td colspan="8" align="center"><i>{t}No data found{/t}</i></td>
    </tr>
    {/foreach}
  </table>
{/if}
{/block}
