<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <template id="hr_timesheet.timesheet_table">
        <t t-set='is_uom_day' t-value='lines._is_timesheet_encode_uom_day()'/>
        <div class="row mt8">
            <div class="col-12">
                <table class="table table-sm">
                    <thead style="display: table-row-group">
                        <tr>
                            <t t-set="timesheet_record_label" t-value="False"/>
                            <t t-if="show_task">
                                <t t-set="timesheet_record_label">Task</t>
                            </t>
                            <t t-elif="show_project">
                                <t t-set="timesheet_record_label">Project</t>
                            </t>
                            <th class="text-start align-middle"><span>Date</span></th>
                            <th class="text-start align-middle"><span>Employee</span></th>
                            <th class="text-start align-middle" t-if="timesheet_record_label"><span t-out="timesheet_record_label"/></th>
                            <th class="text-start align-middle"><span>Description</span></th>
                            <th class="text-end">
                                <span>Time Spent</span>
                            </th>
                        </tr>
                   </thead>
                   <tbody>
                        <tr t-foreach="lines" t-as="line" t-att-style="'background-color: #F1F1F1;' if line_index % 2 == 0 else ''">
                            <td class="align-middle">
                               <span t-field="line.date">2021-09-01</span>
                            </td>
                            <td class="align-middle">
                               <span t-if="line.user_id.partner_id.name" t-field="line.user_id.partner_id.name">Audrey Peterson</span>
                               <span t-else="" t-field="line.employee_id">Audrey Peterson</span>
                            </td>
                            <t t-set="timesheet_record_info" t-value="False"/>
                            <t t-if="show_project">
                                <t t-set="timesheet_record_info" t-value="line.project_id.sudo().name"/>
                                <t t-if="show_task and line.task_id" t-set="timesheet_record_info" t-value="'%s / %s' % (timesheet_record_info, line.task_id.sudo().name)"/>
                            </t>
                            <t t-elif="show_task" t-set="timesheet_record_info" t-value="line.task_id.sudo().name"/>
                            <td t-if="show_task or show_project" class="align-middle">
                                <span t-if="timesheet_record_info" t-out="timesheet_record_info">Research and Development/New Portal System</span>
                            </td>
                            <td class="align-middle">
                                <span t-field="line.name" t-options="{'widget': 'text'}">Call client and discuss project</span>
                            </td>
                            <td class="text-end align-middle">
                                <span t-if="not is_uom_day" t-field="line.unit_amount" t-options="{'widget': 'duration', 'digital': True, 'unit': 'hour', 'round': 'minute'}">2 hours</span>
                                <span t-else="" t-esc="line._get_timesheet_time_day()" t-options="{'widget': 'timesheet_uom'}">1 day</span>
                            </td>
                        </tr>
                        <tr>
                            <td class="text-end" colspan="100">
                                <strong t-if="not is_uom_day">
                                    <span style="margin-right: 15px;">Total (Hours)</span>
                                    <t t-esc="sum(lines.mapped('unit_amount'))" t-options="{'widget': 'duration', 'digital': True, 'unit': 'hour', 'round': 'minute'}">2 hours</t>
                                </strong>
                                <strong t-else="">
                                    <span style="margin-right: 15px;">Total (Days)</span>
                                    <t t-esc="lines._convert_hours_to_days(sum(lines.mapped('unit_amount')))" t-options="{'widget': 'timesheet_uom'}">1 day</t>
                                </strong>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </template>

    <template id="hr_timesheet.timesheet_project_task_page">
        <t t-set="show_record" t-value="len(docs.ids) == 1"/>
        <t t-set="title" t-value="docs._description"/>
        <t t-set="company" t-value="docs.company_id if len(docs) == 1 else docs.env.company"/>
        <t t-set="show_task" t-value="True"/>
        <t t-if="not from_project">
            <t t-set="show_task" t-value="False"/>
            <t t-set="timesheet_report_data" t-value="docs._get_timesheet_report_data()"/>
            <t t-set="timesheets_per_task" t-value="timesheet_report_data['timesheets_per_task']"/>
            <t t-set="subtask_ids_per_task_id" t-value="timesheet_report_data['subtask_ids_per_task_id']"/>
        </t>
        <t t-call="web.html_container">
            <t t-call="web.internal_layout">
                <div class="page">
                    <t t-foreach="docs" t-as="doc">
                        <div class="oe_structure"/>
                        <div class="mt8">
                            <t t-set="lines" t-value="doc.timesheet_ids if from_project else timesheets_per_task.get(doc, doc.env['account.analytic.line'])"/>
                            <h1 t-if="lines">
                                <t t-out="title"/>: <t t-out="doc.name"/>
                            </h1>
                            <t t-if="lines" t-call="hr_timesheet.timesheet_table"/>
                            <t t-if="not from_project">
                                <t t-set="task_id" t-value="doc.id"/>
                                <t t-call="hr_timesheet.timesheet_report_subtask"/>
                            </t>
                        </div>
                    </t>
                </div>
            </t>
        </t>
    </template>

    <template id="timesheet_report_subtask">
        <t t-set="subtasks" t-value="doc.browse(subtask_ids_per_task_id.get(task_id, []))"/>
        <t t-foreach="subtasks.sorted('sequence')" t-as="subtask">
            <t t-if="subtask in timesheets_per_task">
                <h2 class="my-4">
                    Sub-Task of '<t t-out="doc.name"/>': <t t-out="subtask.name"/>
                </h2>
                <t t-set="lines" t-value="timesheets_per_task[subtask]"/>
                <t t-call="hr_timesheet.timesheet_table"/>
            </t>
            <t t-set="task_id" t-value="subtask.id"/>
            <t t-call="hr_timesheet.timesheet_report_subtask"/>
        </t>
    </template>

    <template id="report_timesheet">
        <t t-call="web.html_container">
            <t t-call="web.internal_layout">
                <t t-set="company" t-value="docs.project_id.company_id if len(docs.project_id) == 1 else docs.env.company"/>
                <t t-set="show_task" t-value="bool(docs.task_id)"/>
                <t t-set="show_project" t-value="len(docs.project_id) > 1"/>
                <div class="page">
                    <div class="oe_structure"/>
                    <div class="row mt8">
                        <div class="col-lg-12">
                            <h2>
                                <span>Timesheets
                                    <t t-if="len(docs.project_id) == 1">
                                        for the <t t-out="docs.project_id.name">Research and Development</t> Project
                                    </t>
                                </span>
                            </h2>
                        </div>
                    </div>
                    <t t-set='lines' t-value='docs'/>
                    <t t-call="hr_timesheet.timesheet_table"/>
                    <div class="oe_structure"/>
                </div>
            </t>
        </t>
    </template>

    <!-- Project Task Timesheet Report for given timesheets -->
    <template id="report_timesheet_task">
        <t t-call="web.html_container">
            <t t-call="web.internal_layout">
                <t t-set="company" t-value="docs.project_id.company_id if len(docs.project_id) == 1 else docs.env.company"/>
                <t t-set="show_task" t-value="len(docs.task_id) > 1"/>
                <t t-set="show_project" t-value="False"/>
                <div class="page">
                    <div class="oe_structure"/>
                    <div class="row mt8">
                        <div class="col-12">
                            <h2>
                                <span>Timesheets
                                    <t t-if="len(docs.task_id) == 1">
                                        for <t t-out="docs.task_id.name"/>
                                    </t>
                                </span>
                            </h2>
                        </div>
                    </div>
                    <t t-set='lines' t-value='docs'/>
                    <t t-call="hr_timesheet.timesheet_table"/>
                    <div class="oe_structure"/>
                </div>
            </t>
        </t>
    </template>

    <record id="timesheet_report" model="ir.actions.report">
        <field name="name">Timesheets</field>
        <field name="model">account.analytic.line</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">hr_timesheet.report_timesheet</field>
        <field name="report_file">report_timesheet</field>
        <field name="binding_model_id" ref="model_account_analytic_line"/>
        <field name="binding_type">report</field>
    </record>

    <!-- Project Task Timesheet Report -->
    <template id="report_project_task_timesheet">
        <t t-call="hr_timesheet.timesheet_project_task_page"/>
    </template>

    <record id="timesheet_report_task" model="ir.actions.report">
        <field name="name">Timesheets</field>
        <field name="model">project.task</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">hr_timesheet.report_project_task_timesheet</field>
        <field name="report_file">report_timesheet_task</field>
        <field name="binding_model_id" ref="model_project_task"/>
        <field name="binding_type">report</field>
    </record>

    <!-- Project Timesheet Report -->
    <template id="report_timesheet_project">
        <t t-set="from_project" t-value="True"/>
        <t t-call="hr_timesheet.timesheet_project_task_page"/>
    </template>

    <record id="timesheet_report_project" model="ir.actions.report">
        <field name="name">Timesheets</field>
        <field name="model">project.project</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">hr_timesheet.report_timesheet_project</field>
        <field name="report_file">report_timesheet_project</field>
        <field name="binding_model_id" ref="model_project_project"/>
    </record>

    <record id="timesheet_report_task_timesheets" model="ir.actions.report">
        <field name="name">Timesheets</field>
        <field name="model">account.analytic.line</field>
        <field name="report_type">qweb-pdf</field>
        <field name="report_name">hr_timesheet.report_timesheet_task</field>
        <field name="report_file">report_timesheet</field>
        <field name="binding_type">report</field>
    </record>
</odoo>