<?xml version="1.0" encoding="UTF-8"?>
<scenario begin="LoadInfo">
    
    <!-- Load Info Page
    ID: 95 / 1095 - Engie
    Smith & Smith
    -->
    <screen type="Void" id="LoadInfo">
        <actions>
            <action type="Next">
                <set key="#file" value="file://smith.html" />
                <goto target="InfoPage"/>
            </action>
        </actions>
    </screen>
  
    <!-- Info -->
    <screen id="InfoPage" type="info" title="Informare">
        <field key="#file" />
        <actions>
            <action type="Next">
                <goto target="StepOne"/>
            </action>
            <action type="Prev">
                <goto target="previous"/>
            </action>
            <action type="Exit">
                <goto target="exit"/>
            </action>
        </actions>
    </screen>
    
    <!-- Codul de client
    - Sa contina doar cifre;
    - Sa permita continuarea doar la indeplinirea celor 12 cifre;
    -->
    <screen id="StepOne" type="numeric" title="Introduceti codul de client">
        <fields>
            <text-field id="id1" keyboard="Digital" max-len="12" title="Codul de client">
                <validator type="regex">
                    <rules>
                        <rule regex="^\d{12}$"/>
                    </rules>
                </validator>
                <help>
                    <![CDATA[<html>Optiuni disponibile:<br/>1. Introducere manuala a datelor.<br/>2. Scanare a codului de bare.]]>
                </help> 
            </text-field>
        </fields>
        <!-- Parse BarCode -->
        <barcode-scanner show-error="true" on-success="next">
            <barcode-parser type="regex">
                <rules validate="^.*$">
                    <rule key="id1" title="Codul de client" regex="^(\d{12})\d{21}$" is-bind="true"/>
                    <rule key="id2" title="Numarul de factura" regex="^\d{12}(?:0(\d{11})|(\d{12}))\d{9}$" is-bind="true"/>
                    <rule key="sum" title="Total factura" regex="(0|[1-9][0-9]{0,6})\d{1}$" is-bind="true"/>
                    <rule key="barcode" title="Barcode" regex="^.*$" is-bind="true"/>
                </rules>
            </barcode-parser> 
        </barcode-scanner>
        <actions>
            <action type="Next">
            <!-- Make Barcode variables readable -->
            <if condition="is-not-null id2">
                <then>
                    <modify src-key="id2" t-value="^(.*)$" t-value-title="^(.*)$" v-value="$1" v-value-title="$1"/>
                    <modify src-key="sum" t-value="^(\d{1,4}?)(\d{2})$" t-value-title="^(\d{1,4}?)(\d{2})$" v-value="$1.$2" v-value-title="$1.$2 LEI"/>
                    <goto target="PhoneNumber"/>
                </then>
                <else>
                    <goto target="StepTwo"/>
                </else>
            </if>
            </action>
            <action type="Prev">
                <goto target="InfoPage"/>
            </action>
            <action type="Exit">
                <goto target="exit"/>
            </action>
        </actions>
    </screen>
    
    <!-- Numarul de factura
    - Sa contina prefixul ENG;
    - Sa fie lungimea maxima 15 impreuna cu prefixul;
    - Sa fie numarul de factura pana in 11 cifre, Engie mai are un zero libere;
    -->
    <screen id="StepTwo" type="numeric" title="Introduceti numarul de factura">
        <fields>
            <text-field id="id2" keyboard="Digital" prefix="" max-len="15" title="Numarul de factura">
                <validator type="regex">
                    <rules>
                        <rule regex="^(\d{11,12})$"/>
                    </rules>
                </validator>
                <formatter type="regex">
                    <rules default="************"/>
                </formatter>
                <help>
                    <![CDATA[<html>Prin continuarea tranzactiei va dati acordul pentru plata unui comision de <b>3 RON</b> pentru procesarea acestei plati;]]>
                </help> 
            </text-field>
        </fields>
        <actions>
            <action type="Next">
                <goto target="PhoneNumber"/>
            </action>
            <action type="Prev">
                <goto target="StepOne"/>
            </action>
            <action type="Exit">
                <goto target="exit"/>
            </action>
        </actions>
    </screen>
    
    <!-- Numarul de telefon
    - Sa contina prefixul 07, doar retea mobila;
    - Sa fie lungimea maxima 10 impreuna cu prefixul;
    - Sa permita continuarea doar la indeplinirea celor 10 cifre;
    -->
    <screen id="PhoneNumber" type="numeric" title="Introduceti numarul de telefon">
        <fields>
            <text-field id="id3" keyboard="Digital" prefix="07" max-len="10" title="Numarul de telefon">
                <validator type="regex">
                    <rules>
                        <rule regex="^07(\d{8})$"/>
                    </rules>
                </validator>
                <formatter type="regex">
                    <rules default="**** *** ***"/>
                </formatter>
                <help>
                    <![CDATA[<html>Prin continuarea tranzactiei va dati acordul pentru plata unui comision de <b>3 RON</b> pentru procesarea acestei plati;]]>
                </help> 
            </text-field>
        </fields>
        <actions>
            <action type="Next" title="Inainte">
                <set key="service_name" value="EngieSmith"/>
                <online-request type="default" function="check_request" params="id1,id2,id3,service_name">
                    <actions>
                        <action type="success">
                            <if condition="code == 0">
                                <then>
                                    <goto target="ConfirmPage"/>
                                </then>
                                <else>
                                    <dialog type="Info" title="Eroare" message="Eroare: {0}" timeout="10" default="okay" mess-params="exception">
                                        <actions>
                                            <action type="okay" title="OK">
                                                <cancel/>
                                            </action>
                                        </actions>
                                    </dialog>
                                </else>
                            </if>
                        </action>
                        <action type="error">
                            <dialog type="Info" title="Eroare" message="A intervenit o eroare, va rugam sa incercati din nou" timeout="10" default="okay">
                                <actions>
                                    <action type="okay" title="OK">
                                        <cancel/>
                                    </action>
                                </actions>
                            </dialog>
                        </action>
                    </actions>
                </online-request>
            </action>
            <action type="Prev">
                <goto target="StepTwo"/>
            </action>
            <action type="Exit">
                <goto target="exit"/>
            </action>
        </actions>
    </screen>
    
    <!-- Confirm -->
    <screen id="ConfirmPage" type="confirm" title="Verificati corectitudinea datelor introduse">
        <fields list="id1,id2,id3,sum"/>
        <actions>
            <action type="Next">
                <clear keys="#file"/>
                <goto target="pay"/>
            </action>
            <action type="Prev">
                <goto target="StepOne"/>
            </action>
            <action type="Exit">
                <goto target="exit"/>
            </action>
        </actions>
    </screen>
</scenario>
