<?xml version="1.0" encoding="utf-8"?>
<odoo>

    <!-- ============================================================ -->
    <!-- EXTEND PAYSLIP FORM - QuickBooks-style Canadian Payroll       -->
    <!-- ============================================================ -->
    <record id="view_hr_payslip_form_inherit_canada" model="ir.ui.view">
        <field name="name">hr.payslip.form.inherit.canada</field>
        <field name="model">hr.payslip</field>
        <field name="inherit_id" ref="hr_payroll.view_hr_payslip_form"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">
            
            <!-- Add Print Cheque button in header -->
            <xpath expr="//button[@name='action_payslip_done']" position="after">
                <button name="action_print_cheque" 
                        type="object" 
                        string="Print Cheque" 
                        class="btn-secondary"
                        icon="fa-print"
                        invisible="paid_by != 'cheque'"/>
            </xpath>
            
            <!-- Add ribbon-style cheque status using Odoo's built-in web_ribbon widget -->
            <xpath expr="//sheet" position="inside">
                <field name="cheque_state" invisible="1"/>
                <field name="cheque_id" invisible="1"/>
                <widget name="web_ribbon" title="PRINTED" bg_color="text-bg-success" 
                        invisible="paid_by != 'cheque' or cheque_state != 'printed'"/>
                <widget name="web_ribbon" title="DRAFT" bg_color="text-bg-info" 
                        invisible="paid_by != 'cheque' or cheque_state != 'draft'"/>
                <widget name="web_ribbon" title="VOIDED" bg_color="text-bg-danger" 
                        invisible="paid_by != 'cheque' or cheque_state != 'voided'"/>
                <widget name="web_ribbon" title="CASHED" bg_color="text-bg-warning" 
                        invisible="paid_by != 'cheque' or cheque_state != 'cashed'"/>
            </xpath>
            
            <!-- Reorganize header: hide the standalone avatar, move payment info to main group -->
            <xpath expr="//field[@name='employee_id']/.." position="attributes">
                <attribute name="class">oe_title d-flex align-items-center</attribute>
            </xpath>
            
            <!-- Add Payment/Cheque info in the main info group with other fields -->
            <xpath expr="//group[field[@name='payslip_run_id']]" position="before">
                <group string="Payment Information" invisible="paid_by == False">
                    <field name="paid_by" string="Payment Method"/>
                    <field name="cheque_number" string="Cheque #" invisible="paid_by != 'cheque'" readonly="1"/>
                </group>
            </xpath>

            <!-- Add Canadian Summary Tab after Salary Computation -->
            <xpath expr="//notebook" position="inside">
                <page string="Paycheque Entry" name="paycheque_entry">
                    
                    <!-- Header with Net Pay -->
                    <div class="row mb-4">
                        <div class="col-md-4">
                            <div class="card">
                                <div class="card-body">
                                    <h6 class="card-subtitle mb-2 text-muted">PAY TO</h6>
                                    <h4><field name="employee_id" readonly="1" class="fw-bold"/></h4>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card">
                                <div class="card-body">
                                    <h6 class="card-subtitle mb-2 text-muted">PAY PERIOD</h6>
                                    <div class="row">
                                        <div class="col-6">
                                            <small class="text-muted">From</small><br/>
                                            <field name="date_from" readonly="1"/>
                                        </div>
                                        <div class="col-6">
                                            <small class="text-muted">To</small><br/>
                                            <field name="date_to" readonly="1"/>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="card bg-success text-white">
                                <div class="card-body text-end">
                                    <h6 class="card-subtitle mb-2 text-white-50">NET PAY</h6>
                                    <h2 class="mb-0">
                                        <field name="net_wage" widget="monetary"/>
                                    </h2>
                                    <div invisible="paid_by != 'cheque'">
                                        <small>Cheque #: <field name="cheque_number" readonly="1"/></small>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Pay Section (Earnings) -->
                    <div class="card mb-3">
                        <div class="card-header bg-primary text-white">
                            <i class="fa fa-money me-2"/>Pay (Earnings)
                        </div>
                        <div class="card-body p-0">
                            <field name="line_ids" mode="list" readonly="state == 'done'"
                                   context="{'default_slip_id': id}"
                                   domain="[('category_id.code', 'in', ['BASIC', 'ALW', 'GROSS'])]">
                                <list string="Earnings" create="0" delete="0" 
                                      decoration-bf="category_id.code == 'GROSS'">
                                    <field name="name" string="Type"/>
                                    <field name="code" column_invisible="1"/>
                                    <field name="quantity" string="Hours"/>
                                    <field name="rate" string="Rate"/>
                                    <field name="amount" string="Current"/>
                                    <field name="total" string="Total" sum="Total Earnings"/>
                                    <field name="category_id" column_invisible="1"/>
                                </list>
                            </field>
                        </div>
                        <div class="card-footer text-end">
                            <strong>Total Earnings: </strong>
                            <field name="gross_wage" widget="monetary" class="fw-bold"/>
                            <span class="text-muted ms-3">YTD: </span>
                            <field name="ytd_gross" widget="monetary"/>
                        </div>
                    </div>

                    <!-- Employee Taxes Section -->
                    <div class="card mb-3">
                        <div class="card-header bg-warning">
                            <i class="fa fa-calculator me-2"/>Employee Taxes
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-sm mb-0">
                                    <thead>
                                        <tr>
                                            <th>Type</th>
                                            <th class="text-end">Current</th>
                                            <th class="text-end">YTD</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Income Tax</td>
                                            <td class="text-end"><field name="employee_income_tax" widget="monetary"/></td>
                                            <td class="text-end text-muted"><field name="ytd_income_tax" widget="monetary"/></td>
                                        </tr>
                                        <tr>
                                            <td>Employment Insurance (EI)</td>
                                            <td class="text-end"><field name="employee_ei" widget="monetary"/></td>
                                            <td class="text-end text-muted"><field name="ytd_ei" widget="monetary"/></td>
                                        </tr>
                                        <tr>
                                            <td>Canada Pension Plan (CPP)</td>
                                            <td class="text-end"><field name="employee_cpp" widget="monetary"/></td>
                                            <td class="text-end text-muted"><field name="ytd_cpp" widget="monetary"/></td>
                                        </tr>
                                        <tr>
                                            <td>Second Canada Pension Plan (CPP2)</td>
                                            <td class="text-end"><field name="employee_cpp2" widget="monetary"/></td>
                                            <td class="text-end text-muted"><field name="ytd_cpp2" widget="monetary"/></td>
                                        </tr>
                                    </tbody>
                                    <tfoot>
                                        <tr class="fw-bold">
                                            <td>Total</td>
                                            <td class="text-end"><field name="total_employee_deductions" widget="monetary"/></td>
                                            <td></td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- Employer Taxes Section -->
                    <div class="card mb-3">
                        <div class="card-header bg-info text-white">
                            <i class="fa fa-building me-2"/>Employer Taxes (Company Cost)
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-sm mb-0">
                                    <thead>
                                        <tr>
                                            <th>Type</th>
                                            <th class="text-end">Current</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Employment Insurance Employer (1.4x)</td>
                                            <td class="text-end"><field name="employer_ei" widget="monetary"/></td>
                                        </tr>
                                        <tr>
                                            <td>Canada Pension Plan Employer</td>
                                            <td class="text-end"><field name="employer_cpp" widget="monetary"/></td>
                                        </tr>
                                        <tr>
                                            <td>Second CPP Employer</td>
                                            <td class="text-end"><field name="employer_cpp2" widget="monetary"/></td>
                                        </tr>
                                    </tbody>
                                    <tfoot>
                                        <tr class="fw-bold">
                                            <td>Total Employer Cost</td>
                                            <td class="text-end"><field name="total_employer_cost" widget="monetary"/></td>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>

                    <!-- Summary Section -->
                    <div class="card mb-3">
                        <div class="card-header bg-dark text-white">
                            <i class="fa fa-list-alt me-2"/>Summary
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <table class="table table-sm">
                                        <tr>
                                            <td>Total Pay</td>
                                            <td class="text-end"><field name="gross_wage" widget="monetary"/></td>
                                            <td class="text-end text-muted"><field name="ytd_gross" widget="monetary"/></td>
                                        </tr>
                                        <tr>
                                            <td>Total Taxes</td>
                                            <td class="text-end text-danger">-<field name="total_employee_deductions" widget="monetary"/></td>
                                            <td></td>
                                        </tr>
                                        <tr class="fw-bold fs-5">
                                            <td>Net Pay</td>
                                            <td class="text-end text-success"><field name="net_wage" widget="monetary"/></td>
                                            <td class="text-end text-muted"><field name="ytd_net" widget="monetary"/></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Memo -->
                    <div class="card">
                        <div class="card-header">
                            <i class="fa fa-sticky-note me-2"/>Memo
                        </div>
                        <div class="card-body">
                            <field name="memo" placeholder="Add internal notes for this paycheque..."/>
                        </div>
                    </div>

                </page>
            </xpath>

        </field>
    </record>

    <!-- ============================================================ -->
    <!-- PAYSLIP LIST VIEW - Add Canadian fields                       -->
    <!-- ============================================================ -->
    <record id="view_hr_payslip_tree_inherit_canada" model="ir.ui.view">
        <field name="name">hr.payslip.tree.inherit.canada</field>
        <field name="model">hr.payslip</field>
        <field name="inherit_id" ref="hr_payroll.view_hr_payslip_tree"/>
        <field name="priority">50</field>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='employee_id']" position="after">
                <field name="paid_by" optional="hide"/>
                <field name="cheque_number" optional="hide"/>
                <field name="net_wage" string="Net Pay" widget="monetary" optional="show"/>
            </xpath>
        </field>
    </record>

</odoo>
