<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="portal_layout" name="Portal layout: timesheet menu entry" inherit_id="portal.portal_breadcrumbs" priority="35">
        <xpath expr="//ol[hasclass('o_portal_submenu')]" position="inside">
            <li t-if="page_name == 'timesheet' or timesheet" t-attf-class="breadcrumb-item #{'active' if not timesheet else ''}">
                <t>Timesheets</t>
            </li>
        </xpath>
    </template>

    <template id="portal_my_timesheets" name="My Timesheets">
        <t t-call="portal.portal_layout" breadcrumbs_searchbar="True">

            <t t-call="portal.portal_searchbar" title.translate="Timesheets"/>
            <t t-if="not grouped_timesheets">
                <div class="alert alert-warning" role="alert">
                    There are no timesheets.
                </div>
            </t>
            <t t-if="grouped_timesheets">
                <t t-call="portal.portal_table">
                    <thead>
                        <tr>
                            <th t-if="not groupby == 'date'">Date</th>
                            <th t-if="not groupby == 'employee_id'">Employee</th>
                            <th t-if="not groupby == 'project_id'">Project</th>
                            <th t-if="not groupby == 'task_id'">Task</th>
                            <th>Description</th>
                            <th class="text-end" t-att-colspan="2 if groupby != 'none' else 0" name="t_label">Time Spent</th>
                        </tr>
                    </thead>
                    <t t-foreach="grouped_timesheets" t-as="timesheets_with_hours">
                        <t t-set="timesheets" t-value="timesheets_with_hours[0]"/>
                        <t t-set="hours_spent" t-value="timesheets_with_hours[1]"/>
                        <tbody style="font-size: 0.8rem">
                            <tr t-if="not groupby =='none'" class="table-light">
                                <t t-if="groupby == 'project_id'">
                                    <th colspan="5">
                                        <span t-field="timesheets[0].project_id.name"/>
                                    </th>
                                </t>
                                <t t-elif="groupby == 'task_id'">
                                    <th colspan="5">
                                        <span t-if="timesheets[0].task_id" t-field="timesheets[0].task_id.name"/>
                                        <span t-else="">No Task</span>
                                    </th>
                                </t>
                                <t t-elif="groupby == 'date'">
                                    <th colspan="5">
                                        <span t-field="timesheets[0].date"/>
                                    </th>
                                </t>
                                <t t-elif="groupby == 'employee_id'">
                                    <th colspan="5">
                                        <span t-field="timesheets[0].employee_id.name"/>
                                    </th>
                                </t>
                                <t t-elif="groupby == 'parent_task_id'">
                                    <th colspan="6">
                                        <span t-if="timesheets[0].parent_task_id" t-field="timesheets[0].parent_task_id.name"/>
                                        <span t-else="">No Parent Task</span>
                                    </th>
                                </t>
                                <th colspan="1" class="text-end text-muted">
                                    <t t-if="is_uom_day">
                                        Total: <span t-out="timesheets._convert_hours_to_days(hours_spent)" t-options='{"widget": "timesheet_uom"}'/>
                                    </t>
                                    <t t-else="">
                                        Total: <span t-out="hours_spent" t-options='{"widget": "float_time"}'/>
                                    </t>
                                </th>
                            </tr>
                        </tbody>
                        <tbody style="font-size: 0.8rem">
                            <t t-foreach="timesheets" t-as="timesheet">
                                <tr>
                                    <td t-if="not groupby == 'date'"><span t-field="timesheet.date" t-options='{"widget": "date"}'/></td>
                                    <td t-if="not groupby == 'employee_id'"><span t-field="timesheet.employee_id" t-att-title="timesheet.employee_id.display_name" /></td>
                                    <td t-if="not groupby == 'project_id'"><span t-field="timesheet.project_id" t-att-title="timesheet.project_id.display_name"/></td>
                                    <td t-if="not groupby == 'task_id'"><span t-field="timesheet.task_id" t-att-title="timesheet.task_id.display_name"/></td>
                                    <td><span t-out="timesheet.name" t-att-title="timesheet.name"/></td>
                                    <td class="text-end" t-att-colspan="2 if groupby != 'none' else 0">
                                        <span t-if="is_uom_day" t-out="timesheet._get_timesheet_time_day()" t-options='{"widget": "timesheet_uom"}'/>
                                        <span t-else="" t-field="timesheet.unit_amount" t-options='{"widget": "float_time"}'/>
                                    </td>
                                </tr>
                            </t>
                        </tbody>
                    </t>
                </t>
            </t>
        </t>
    </template>

    <template id="portal_timesheet_table" name="Portal Timesheet Table">
        <table class="o_portal_my_doc_table table table-sm table-striped">
            <thead>
              <tr>
                <th>Date</th>
                <th>Employee</th>
                <th>Description</th>
                <th class="text-end" name="t_label">Time Spent</th>
              </tr>
            </thead>
            <tr t-foreach="timesheets" t-as="timesheet" valign="middle">
                <td><t t-out="timesheet.date" t-options='{"widget": "date"}'/></td>
                <td t-attf-title="#{timesheet.employee_id.name}"><t t-out="timesheet.employee_id.name"/></td>
                <td><t t-out="timesheet.name"/></td>
                <td class="text-end">
                    <span t-if="is_uom_day" t-out="timesheet._get_timesheet_time_day()" t-options='{"widget": "timesheet_uom"}'/>
                    <span t-else="" t-field="timesheet.unit_amount" t-options='{"widget": "float_time"}'/>
                </td>
            </tr>
        </table>
    </template>

    <template id="portal_timesheet_table_with_total" name="Portal Timesheet Table">
        <t t-call="hr_timesheet.portal_timesheet_table"/>
        <div class="container_subtotal">
            <div class="row justify-content-end">
                <div t-attf-class="{{'col-auto' if report_type != 'html' else 'col-sm-2'}}">
                    <table class="table table-sm">
                        <tr>
                            <td><strong>Total Time Spent: </strong></td>
                            <td class="text-end">
                                <span t-if="is_uom_day" t-out="timesheets._convert_hours_to_days(task.effective_hours)" t-options="{'widget': 'timesheet_uom'}"/>
                                <span t-else="" t-out="task.effective_hours" t-options='{"widget": "float_time"}'/>
                            </td>
                        </tr>
                        <tr>
                            <div t-if="task.subtask_effective_hours">
                                <td><strong>Time recorded on sub-tasks: </strong></td>
                                <td class="text-end">
                                    <span t-if="is_uom_day" t-out="timesheets._convert_hours_to_days(task.subtask_effective_hours)" t-options='{"widget": "timesheet_uom"}'/>
                                    <span t-else="" t-out="task.subtask_effective_hours" t-options='{"widget": "float_time"}'/>
                                </td>
                            </div>
                        </tr>
                        <tr>
                            <div t-if="task.total_hours_spent and task.subtask_effective_hours" name="total_time">
                                <t t-if="is_uom_day">
                                    <td><strong>Total Days: </strong></td>
                                    <td class="text-end">
                                        <span t-out="timesheets._convert_hours_to_days(task.total_hours_spent)" t-options='{"widget": "timesheet_uom"}'/>
                                    </td>
                                </t>
                                <t t-else="">
                                    <td><strong>Total Hours: </strong></td>
                                    <td class="text-end">
                                        <span t-out="task.total_hours_spent" t-options='{"widget": "float_time"}'/>
                                    </td>
                                </t>
                            </div>
                        </tr>
                        <tr t-attf-class="{{task.remaining_hours &lt; 0 and 'text-danger' or ''}}">
                            <div t-if="task.allocated_hours > 0" name="allocated_time">
                                <td><strong>Time Remaining: </strong></td>
                                <td class="text-end">
                                    <span t-if="is_uom_day" t-out="timesheets._convert_hours_to_days(task.remaining_hours)" t-options='{"widget": "timesheet_uom"}'/>
                                    <span t-else="" t-out="task.remaining_hours" t-options='{"widget": "float_time"}'/>
                                </td>
                            </div>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </template>
</odoo>
