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

    <record id="poynt_payment_wizard_form" model="ir.ui.view">
        <field name="name">poynt.payment.wizard.form</field>
        <field name="model">poynt.payment.wizard</field>
        <field name="arch" type="xml">
            <form string="Collect Poynt Payment">
                <field name="state" invisible="1"/>
                <field name="poynt_transaction_ref" invisible="1"/>
                <field name="provider_id" invisible="1"/>
                <field name="surcharge_enabled" invisible="1"/>
                <field name="surcharge_applied" invisible="1"/>
                <field name="original_amount" invisible="1"/>

                <!-- Status banner for waiting / done / error -->
                <div class="alert alert-info" role="alert"
                     invisible="state != 'waiting'">
                    <strong>Waiting for terminal...</strong>
                    <field name="status_message" nolabel="1"/>
                </div>
                <div class="alert alert-success" role="alert"
                     invisible="state != 'done'">
                    <strong>Payment Collected</strong>
                    <br/>
                    <field name="status_message" nolabel="1"/>
                </div>
                <div class="alert alert-danger" role="alert"
                     invisible="state != 'error'">
                    <strong>Error</strong>
                    <br/>
                    <field name="status_message" nolabel="1"/>
                </div>

                <group invisible="state == 'done'">
                    <group string="Payment Details">
                        <field name="invoice_id"/>
                        <field name="partner_id"/>
                        <field name="amount"/>
                        <field name="currency_id"/>
                        <field name="provider_name"/>
                    </group>
                    <group string="Payment Mode"
                           invisible="state not in ('draft', 'error')">
                        <field name="payment_mode" widget="radio"
                               readonly="state not in ('draft', 'error')"/>
                    </group>
                </group>

                <!-- Card Type & Surcharge section -->
                <group string="Card Type &amp; Surcharge"
                       invisible="state == 'done' or not surcharge_enabled">
                    <group>
                        <field name="card_type"
                               widget="radio"
                               required="surcharge_enabled and state in ('draft', 'error')"
                               readonly="state not in ('draft', 'error')"/>
                    </group>
                    <group invisible="not card_type">
                        <field name="surcharge_rate" string="Rate (%)"/>
                        <field name="surcharge_amount"/>
                        <div class="text-muted" colspan="2"
                             invisible="surcharge_amount == 0">
                            A surcharge line will be added to the invoice before payment.
                        </div>
                    </group>
                </group>

                <!-- Terminal section -->
                <group string="Terminal"
                       invisible="payment_mode != 'terminal' or state == 'done'">
                    <field name="terminal_id"
                           required="payment_mode == 'terminal' and state in ('draft', 'error')"
                           readonly="state == 'waiting'"/>
                </group>

                <!-- Card entry section -->
                <group string="Card Details"
                       invisible="payment_mode != 'card' or state == 'done'">
                    <group>
                        <field name="card_number"
                               placeholder="4111 1111 1111 1111"
                               required="payment_mode == 'card' and state in ('draft', 'error')"
                               password="True"/>
                        <field name="cardholder_name"
                               placeholder="Name on card"/>
                    </group>
                    <group>
                        <field name="exp_month"
                               placeholder="MM"
                               required="payment_mode == 'card' and state in ('draft', 'error')"/>
                        <field name="exp_year"
                               placeholder="YYYY"
                               required="payment_mode == 'card' and state in ('draft', 'error')"/>
                        <field name="cvv"
                               placeholder="123"
                               required="payment_mode == 'card' and state in ('draft', 'error')"
                               password="True"/>
                    </group>
                </group>

                <footer>
                    <!-- Draft / Error state: show action buttons -->
                    <button string="Send to Terminal"
                            name="action_collect_payment"
                            type="object"
                            class="btn-primary"
                            invisible="payment_mode != 'terminal' or state not in ('draft', 'error')"
                            data-hotkey="q"/>
                    <button string="Collect Payment"
                            name="action_collect_payment"
                            type="object"
                            class="btn-primary"
                            invisible="payment_mode != 'card' or state not in ('draft', 'error')"
                            data-hotkey="q"/>

                    <!-- Waiting state: check status + cancel -->
                    <button string="Check Status"
                            name="action_check_status"
                            type="object"
                            class="btn-primary"
                            invisible="state != 'waiting'"
                            data-hotkey="q"/>
                    <button string="Cancel Payment"
                            name="action_cancel_payment"
                            type="object"
                            class="btn-secondary"
                            invisible="state not in ('waiting', 'error')"
                            data-hotkey="x"/>

                    <!-- Done state: send receipt + close -->
                    <button string="Send Receipt"
                            name="action_send_receipt"
                            type="object"
                            class="btn-primary"
                            icon="fa-envelope"
                            invisible="state != 'done'"
                            data-hotkey="s"/>
                    <button string="Close"
                            class="btn-secondary"
                            special="cancel"
                            invisible="state != 'done'"
                            data-hotkey="x"/>

                    <!-- Draft state: cancel cleans up -->
                    <button string="Cancel"
                            name="action_cancel_payment"
                            type="object"
                            class="btn-secondary"
                            invisible="state != 'draft'"
                            data-hotkey="x"/>
                </footer>
            </form>
        </field>
    </record>

</odoo>
