<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <record id="view_device_approval_wizard_form" model="ir.ui.view">
        <field name="name">fusion_claims.device.approval.wizard.form</field>
        <field name="model">fusion_claims.device.approval.wizard</field>
        <field name="arch" type="xml">
            <form string="Device Approval Verification">
                <field name="has_unapproved" invisible="1"/>
                <field name="has_deductions" invisible="1"/>
                <field name="has_invoices" invisible="1"/>
                <field name="has_submission_data" invisible="1"/>
                <field name="is_mark_approved_mode" invisible="1"/>
                
                <!-- Instructions -->
                <div class="alert alert-info" role="alert">
                    <strong>Stage 2: Device Approval Verification</strong>
                    <p class="mb-0">
                        1. Uncheck devices that were NOT approved by ADP.<br/>
                        2. If approved with deduction, set the type and value.<br/>
                        <small class="text-muted">
                            PCT = ADP covers X% of normal portion. AMT = Fixed $ deducted from ADP.
                        </small>
                    </p>
                </div>
                
                <!-- Stage 1 Reference -->
                <div class="alert alert-secondary" role="alert" invisible="not has_submission_data">
                    <strong>Stage 1 Reference:</strong> 
                    <field name="submitted_device_types" nolabel="1" class="d-inline"/>
                    <br/>
                    <small class="text-muted">Compare with ADP approval letter.</small>
                </div>
                
                <!-- Invoices Warning -->
                <div class="alert alert-warning" role="alert" invisible="not has_invoices">
                    <i class="fa fa-exclamation-triangle"/>
                    <strong>Invoices exist.</strong> Changes will automatically update existing invoices.
                </div>
                
                <!-- Claim Number and Approval Date - Required for Mark as Approved -->
                <group invisible="not is_mark_approved_mode">
                    <group>
                        <field name="claim_number" required="is_mark_approved_mode"
                               placeholder="Enter ADP Claim Number from approval letter"/>
                    </group>
                    <group>
                        <field name="approval_date" required="is_mark_approved_mode"/>
                    </group>
                </group>
                
                <!-- Header: Order and All Approved -->
                <group>
                    <group string="Order">
                        <field name="sale_order_id" readonly="1"/>
                    </group>
                    <group string="Quick Actions">
                        <field name="all_approved"/>
                    </group>
                </group>
                
                <!-- Device Table -->
                <group string="Devices for Approval">
                    <field name="line_ids" nolabel="1" colspan="2">
                        <list editable="bottom" create="0" delete="0"
                              decoration-danger="not approved"
                              decoration-success="approved and (not deduction_type or deduction_type == 'none')"
                              decoration-warning="approved and deduction_type and deduction_type != 'none'">
                            <field name="approved" string="Approved"/>
                            <field name="device_type" string="Device Type"/>
                            <field name="product_name" string="Product"/>
                            <field name="device_code" string="ADP Code"/>
                            <field name="serial_number" string="S/N"/>
                            <field name="quantity" string="Qty"/>
                            <field name="unit_price" string="Unit $"/>
                            <field name="deduction_type" string="Deduction"/>
                            <field name="deduction_value" string="Ded. $"/>
                            <field name="estimated_adp" string="Est. ADP" decoration-danger="estimated_adp == 0"/>
                            <field name="estimated_client" string="Est. Client"/>
                            <field name="sale_line_id" column_invisible="1"/>
                        </list>
                    </field>
                </group>
                
                <!-- Unapproved Warning -->
                <div class="alert alert-danger" role="alert" invisible="not has_unapproved">
                    <strong><i class="fa fa-times-circle"/> Unapproved Items</strong>
                    <p class="mb-0">
                        Devices marked as NOT approved will be billed to client at 100% and removed from ADP invoice.
                    </p>
                </div>
                
                <!-- Deductions Info -->
                <div class="alert alert-info" role="alert" invisible="not has_deductions">
                    <strong><i class="fa fa-calculator"/> Deductions Applied</strong>
                    <p class="mb-0">
                        One or more devices have deductions. The client will be responsible for the difference.
                    </p>
                </div>
                
                <!-- Approval Documents Section - Only visible in Mark as Approved mode -->
                <group string="Approval Documents" invisible="not is_mark_approved_mode">
                    <div class="alert alert-info mb-3" role="alert" colspan="2">
                        <strong><i class="fa fa-upload"/> Upload Approval Documents</strong>
                        <p class="mb-0">Upload the ADP approval letter and any screenshots from the approval.</p>
                    </div>
                    <group string="Approval Letter (PDF)">
                        <field name="approval_letter" filename="approval_letter_filename"
                               widget="binary"/>
                        <field name="approval_letter_filename" invisible="1"/>
                    </group>
                    <group string="Approval Screenshots (Drag &amp; Drop Multiple)">
                        <field name="approval_photo_ids" widget="many2many_binary" 
                               string="Approval Screenshots"
                               options="{'accepted_file_extensions': 'image/*,.pdf'}"/>
                    </group>
                </group>
                
                <footer>
                    <button name="action_confirm_approval" type="object" 
                            string="Confirm &amp; Apply" class="btn-primary"/>
                    <button name="action_approve_all" type="object" 
                            string="Approve All" class="btn-success"
                            invisible="all_approved"/>
                    <button string="Cancel" class="btn-secondary" special="cancel"/>
                </footer>
            </form>
        </field>
    </record>

    <record id="action_device_approval_wizard" model="ir.actions.act_window">
        <field name="name">Device Approval Verification</field>
        <field name="res_model">fusion_claims.device.approval.wizard</field>
        <field name="view_mode">form</field>
        <field name="target">new</field>
        <field name="binding_model_id" ref="sale.model_sale_order"/>
        <field name="binding_view_types">form</field>
        <field name="context">{'active_id': active_id}</field>
    </record>
</odoo>
