<!-- BEGIN {$smarty.template} -->
<script type="text/javascript" src="js/tasks.js"></script>
<script type="text/javascript" src="js/table.js"></script>

{if count($operations) > 0}
<script type="text/javascript" >
    {literal}

    $(document).ready(function() {

        var curryear = $('#curryear').val();
        var currmonth = $('#currmonth').val();

        /* View task details */

        var $viewTaskDialog = $('<div><\/div>').dialog({ 
            autoOpen: false, 
            title: '{/literal}{t}Task information{/t}{literal}', 
            width: 'auto', 
            height: 'auto' 
            });

        $('.tview').live('click', function() {
                var id = $(this).attr('id');
                var task_id = id.replace(/^tv/g, "");
                $viewTaskDialog.load('service.php?saction='+{/literal}{$smarty.const.ACTION_SERVICE_OPDETAILS}{literal}
                    +'&id=' + task_id);
                $viewTaskDialog.dialog('open');
                return false;
        });

        /* Add a task to a project */

        var $addTaskDialog = $('<div><\/div>').dialog({ 
            autoOpen: false, 
            title: '{/literal}{t}Add task{/t}{literal}', 
            modal: true, 
            width: 'auto', 
            height: 'auto',
            buttons: { 
                "Cancel": function() { $(this).dialog("close"); },
                "OK": function() { submitTaskFormDyn($(this), $viewTaskDialog); return false; } 
                }
            });

        $('.tadd').live('click', function() {
            var id = $(this).attr('id');
            var prj_id = id.replace(/^ta/g, "");
            $addTaskDialog.load('service.php?saction='+{/literal}{$smarty.const.ACTION_SERVICE_GETADDTASK_HTML}{literal}
                +'&id=' + prj_id);
            $addTaskDialog.dialog('open');
            return false;
        });

        /* Navigate through years and months */

        $('#mhr_prev').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            year--;
            $('#year').html(year);
            set_navbuttons();
            replot_mhrs(year, month, true);
            return false;
        });

        $('#mhr_next').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            year++;
            if ((year==curryear) && (month>currmonth)) {
                month=currmonth;
                $('#month').html(month);
            }
            $('#year').html(year);
            set_navbuttons();
            replot_mhrs(year, month, true);
            return false;
        });

        $('#mhr_prevm').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            var replot_year = false;
            if (month == 1) {
                year--;
                month = 12;
                replot_year = true;
            } else
                month--;

            $('#year').html(year);
            $('#month').html(month);

            set_navbuttons();
            
            replot_mhrs(year, month, replot_year);
            return false;
        });

        $('#mhr_nextm').click(function() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());
            var replot_year = false;
            if (month == 12) {
                year++;
                month = 1;
                replot_year = true;
            } else
                month++;

            $('#year').html(year);
            $('#month').html(month);

            set_navbuttons();
            
            replot_mhrs(year, month, replot_year);
            return false;
        });

        function set_navbuttons() {
            var year = $('#year').html();
            var month = parseInt($('#month').html());

            if ((year < curryear) || ((year == curryear) && (month < currmonth)))
                $('#mhr_nextm').show();

            if ((year == curryear) && (month >= currmonth))
                $('#mhr_nextm').hide();

            if (year < curryear)
                $('#mhr_next').show();
            if (year >= curryear)
                $('#mhr_next').hide();
        }

        /* Manhours summary */

        function getPersonManhoursStat() {

            $.getJSON("data_service.php?req=mhrsstatpe&pe_id={/literal}{$PE_ID}{literal}",
                    function(data) { 
                        $('#mh_fy').html(data.fy);
                        $('#mh_curr_fy').html(data.curr_fy);
                        $('#mh_curr_month').html(data.curr_month);
                        $('#mh_curr_week').html(data.curr_week);
                        $('#mh_spark').sparkline(data.spark_month, { height: 30 });
                        $('#mh_oprange').html(data.op.begin+' - '+data.op.end);
                    });
        }

        /* Charts */
        
        function replot_mhrs(year, month, replot_year) {

            if (replot_year == true) {
                $('#mhchartyear').empty();
                $.getJSON("data_service.php?req=mhrs&pe_id={/literal}{$PE_ID}"+
                        "&type={$smarty.const.PHPAGA_MANHOURS_DISPLAY_YEAR}{literal}&date="+
                        year+"-01-01",  
                        function(data) { mhrsplot = get_mhrsplot_year('mhchartyear', data); });
            }

            $('#mhchartmonth').empty();
            $.getJSON("data_service.php?req=mhrs&pe_id={/literal}{$PE_ID}"+
                    "&type={$smarty.const.PHPAGA_MANHOURS_DISPLAY_MONTH}{literal}&date="+
                    year+"-"+month+"-01", 
                    function(data) { mhrsplot = get_mhrsplot_month('mhchartmonth', data); });
        }

        replot_mhrs($('#curryear').val(), $('#currmonth').val(), true);

        set_navbuttons();

        getPersonManhoursStat();

        /* Delete tasks */

        var delOperationDialog = $('<div><span id="opinfo"><\/span>{/literal}Delete this task?{literal}<\/div>').dialog({ 
            autoOpen: false, 
            title: '{/literal}Delete this task?{literal}', 
            width: 'auto', 
            height: 'auto',
            modal: true
            });

        $('.opdel').live('click', function() {
                    op_id = $(this).attr('id').replace(/^op/g, "");
                    prj_id = $('#oprow'+op_id+' .opprj').attr('title').replace(/^opprjid/g, "");

                    $.get('service.php?saction={/literal}{$smarty.const.ACTION_SERVICE_OPDETAILS}{literal}&id=' + op_id, function(data) { $('#opinfo').html(data+'<br \/>'); });

                    delOperationDialog.dialog("option", {
                            buttons: {
                                '{/literal}Delete{literal}': function() {
                                    $.ajax({
                                            url: 'service.php',
                                            type: 'POST',
                                            data: 'saction={/literal}{$smarty.const.ACTION_SERVICE_DELETE_OP}{literal}&op_id='+op_id,
                                            dataType: 'json',
                                            cache: false,
                                            success: function(result) {
                                                if (result.status != 0) {
                                                    alert(result.message);
                                                } else {
                                                    delOperationDialog.dialog('close');
                                                    $('#oprow'+op_id).fadeOut();

                                                    $.getJSON('service.php?saction={/literal}{$smarty.const.ACTION_SERVICE_PRJHOURS}{literal}&id=' + prj_id, function(pdata) { if (pdata.status==0) $('#dur'+prj_id).html(pdata.data); });
                                                }
                                            }
                                        });

                                },
                                Cancel: function() { $(this).dialog('close'); }
                            }
                        });

                    delOperationDialog.dialog('open');
                    return false;
            });
    });
        
{/literal}
</script>

<input type="hidden" name="curryear" id="curryear" value="{$curryear}">
<input type="hidden" name="currmonth" id="currmonth" value="{$currmonth}">
<input type="hidden" name="currperson" id="currperson" value="{$PE_ID}">

<div id="allcntnr">
    <div class="flleft" id="chartcntnr">
        <div class="dcaption year"><em>{t}My manhours{/t}
                <a href="#" id="mhr_prev"><img src="img/phpaga/resultset_previous.png" width="16" height="16"></a>
                <span id="year">{$curryear}</span> 
                <a href="#" id="mhr_next"><img src="img/phpaga/resultset_next.png" width="16" height="16"></a> | 
                <a href="#" id="mhr_prevm"><img src="img/phpaga/resultset_previous.png" width="16" height="16"></a>
                <span id="month">{$currmonth}</span> 
                <a href="#" id="mhr_nextm"><img src="img/phpaga/resultset_next.png" width="16" height="16"></a>
        </em>&nbsp;</div>

        <div class="floatcontainer">
            <div id="mhchartyear" class="mhchart"></div>
            <div id="mhchartmonth" class="mhchart"></div>
        </div>
    </div>

    <div class="flleft mhrsum" id="tabmhrsum">
        <div>
            <div class="dcaption"><em>{t}My monthly billable manhours{/t}</em>&nbsp;</div>
            <p id="mh_oprange">&nbsp;</p>
            <div id="mh_spark"></div>
        </div>
        <div>
            <div class="dcaption"><em>{t}My billable manhours{/t} <strong id="mh_fy"></strong></em>&nbsp;</div>
            <table cellspacing="0">
                <tbody>
                    <tr>
                        <th>{t}FY{/t}</th>
                        <td id="mh_curr_fy"></td>
                    </tr>
                    <tr>
                        <th>{t}Month{/t}</th>
                        <td id="mh_curr_month"></td>
                    </tr>
                    <tr>
                        <th>{t}Week{/t}</th>
                        <td id="mh_curr_week"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="spacer"></div>
{/if}

<form action="{$smarty.const.PHPAGA_SELF}" method="get">
    <table class="table-autosort table-autostripe table-stripeclass:even" cellspacing="0">
        <caption><em>{t}My projects and projects I'm a member of{/t}</em>
            {t}Status{/t}:
            <select name="prj_status" onchange="this.form.submit();">
                {html_options options=$prjstat selected=$prjstat_sel}
            </select>
        </caption>

        <thead>
            <tr>
                <th class="table-sortable:default"> {t}Title{/t} </th>
                <th class="table-sortable:default"> {t}Status{/t} </th>
                <th class="table-sortable:default"> {t}Customer{/t} </th>
                <th class="table-sortable:default"> {t}Prio.{/t} </th>
                <th class="table-sortable:date"> {t}Start date{/t} </th>
                <th class="table-sortable:date"> {t}Deadline{/t} </th>
                <th class="table-sortable:default"> {t}Category{/t} </th>
                <th class="table-sortable:numeric_colon"> {t}Hours{/t} </th>
                <th> {t}Actions{/t} </th>
            </tr>
        </thead>

        {if (count($projects) > 0)}
        <tbody>
            {section name=pdetails loop=$projects}
            <tr>
                <td{if ((strlen($projects[pdetails].prj_title) > $smarty.const.PHPAGA_SHOWPRJTITLELENGHT))} class="tTip" title="{$projects[pdetails].prj_title|phpaga_popup_sanitizestr}"{/if}>
                    <a href="project.php?id={$projects[pdetails].prj_id}">{$projects[pdetails].prj_title|wash|trnc:$smarty.const.PHPAGA_SHOWPRJTITLELENGHT:"...":true}</a>
                </td>
                <td> {$projects[pdetails].prjstat_title|wash} </td>
                <td> <a href="company.php?id={$projects[pdetails].cpn_id}">{$projects[pdetails].cpn_name|wash}</a> </td>
                <td class="time"> {$projects[pdetails].prj_priority|wash|default:"&nbsp;"} </td>
                <td class="time"> {$projects[pdetails].prj_startdate|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                <td{if $projects[pdetails].prj_overdue} class="overdue"{/if}> {if strlen($projects[pdetails].prj_deadline)}{$projects[pdetails].prj_deadline|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT}{else}&nbsp;{/if} </td>
                <td> {$projects[pdetails].prjcat_title|wash} </td>
                <td class="time" id="dur{$projects[pdetails].prj_id}"> {if (strlen($projects[pdetails].prj_duration) && ($projects[pdetails].prj_duration > 0))}{$projects[pdetails].prj_duration|phpaga_minutes2duration}{else}&nbsp;{/if} </td>
                <td class="actions"> <a href="add_operation.php?prj_id={$projects[pdetails].prj_id}" class="ptr tadd" id="ta{$projects[pdetails].prj_id}"><img src="img/phpaga/task_add.png" height="16" width="16" border="0" alt="{t}Add task{/t}" title="{t}Add task{/t}"></a> </td>
            </tr>
            {/section}
        </tbody>
        {else}
        <tr>
            <td colspan="9">
                {if strlen($prjstat_sel_name)}
                {t}You have not created any projects and you are not a member of any existing project with the selected status.{/t}
                {else}
                {t}You have not created any projects and you are not a member of any existing project.{/t}
                {/if}
            </td>
        </tr>
        {/if}
    </table>
</form>

{if count($operations)}
<table class="table-autosort table-autostripe table-stripeclass:even" cellspacing="0" id="lop">
    <caption><em>{t 1=$num_lastoperations}My last %1 tasks{/t}</em></caption>

    <thead>
        <tr>
            <th class="table-sortable:default"> {t}Project{/t} </th>
            <th class="table-sortable:default"> {t}Task{/t} </th>
            <th class="time table-sortable:date"> {t}Date{/t} </th>
            <th class="table-sortable:default"> {t}Category{/t} </th>
            <th class="table-sortable:numeric_colon"> {t}Hours{/t} </th>
            <th> {t}Actions{/t} </th>
        </tr>
    </thead>
    <tbody>
        {section name=details loop=$operations}
        <tr id="oprow{$operations[details].op_id}">
            <td class="opprj" title="opprjid{$operations[details].prj_id}"{if ((strlen($operations[details].prj_title) > $smarty.const.PHPAGA_SHOWPRJTITLELENGHT))} class="tTip" title="{$operations[details].prj_title|phpaga_popup_sanitizestr}"{/if}>
                <a href="project.php?id={$operations[details].prj_id}">{$operations[details].prj_title|wash|trnc:$smarty.const.PHPAGA_SHOWPRJTITLELENGHT:"..."}</a>
            </td>
            <td{if ((strlen($operations[details].op_desc) > $smarty.const.PHPAGA_SHOWOPDESCLENGHT))} class="tTip" title="{$operations[details].op_desc|wash|phpaga_popup_sanitizestr}"{/if}>
                {$operations[details].op_desc|wash|trnc:$smarty.const.PHPAGA_SHOWOPDESCLENGHT:"..."}
            </td>
            <td class="time"> {$operations[details].op_startdate|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
            <td> {$operations[details].opcat_title|wash} </td>
            <td class="time"> {$operations[details].op_duration_hrs} </td>
            <td class="actions"> {strip}

                <a class="tview" id="tv{$operations[details].op_id}" href="#"><img src="img/phpaga/view.png" height="16" width="16" border="0" alt="{t}View{/t}" title="{t}View{/t}"></a>

                {if ($operations[details].op_canedit)}
                <a href="operation.php?id={$operations[details].op_id}&amp;ref=main.php"><img src="img/phpaga/edit.png" height="16" width="16" border="0" alt="{t}Edit{/t}" title="{t}Edit{/t}"></a>
                <a id="op{$operations[details].op_id}" class="opdel" href="#"><img src="img/phpaga/delete.png" height="16" width="16" border="0" alt="{t}Delete{/t}" title="{t}Delete{/t}"></a>
                {elseif $perm_view_invoices}
                <a href="bill.php?id={$operations[details].bill_id}"><img src="img/phpaga/bill.png" height="16" width="16" border="0" alt="{t}View invoice{/t}" title="{t}View invoice{/t}"></a>
                {/if}
                {/strip}
            </td>
        </tr>
        {/section}
    </tbody>
</table>
{/if}

<!-- END {$smarty.template} -->
