<?xml version="1.0" encoding="UTF-8"?>
<scenario begin="LoadInfo">
    
    <!-- Load Info Page
    ID: 91 / 1091 - Enel
    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>
    
    <!-- Cod plata
    - Sa continta doar cifre;
    - Sa permita continuarea doar la indeplinirea celor 9 cifre;
    - Deschid pana la 11, mai exista doua cifre libere.
    -->
    <screen id="StepOne" type="numeric" title="Introduceti codul de plata">
        <fields>
            <text-field id="id1" keyboard="Digital" max-len="11" title="Cod plata">
                <validator type="regex">
                    <rules>
                        <rule regex="^\d{9,11}$"/>
                    </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="Cod plata" regex="^\d{29}(\d{9})\d{2}$" is-bind="true"/>
                    <rule key="id2" title="ID factura" regex="^(\d{11})\d{29}$" is-bind="true"/>
                    <rule key="date" title="Data emitere" regex="^\d{23}([0-9]{6})" is-bind="true"/>
                    <rule key="sum" title="Total factura" regex="(0|[1-9][0-9]{0,6})\d{17}$" is-bind="true"/>
                    <rule key="provider" title="Furnizor" regex="^\d{29}(\d{1})\d{10}$" 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>
                      <switch value="provider">
                          <case value="1">
                              <set key="provider" key-title="Furnizor" value="1" value-title="Enel Energie Muntenia SA"/>
                          </case>
                          <case value="2">
                              <set key="provider" key-title="Furnizor" value="2" value-title="Enel Energie SA"/>
                          </case>
                          <case value="3">
                              <set key="provider" key-title="Furnizor" value="3" value-title="Enel Energie Muntenia SA"/>
                          </case>
                          <case value="4">
                              <set key="provider" key-title="Furnizor" value="4" value-title="Enel Energie Muntenia SA"/>
                          </case>
                          <case value="5">
                              <set key="provider" key-title="Furnizor" value="5" value-title="Enel Energie SA"/>
                          </case>
                          <default>
                              <set key="provider" key-title="Furnizor" value="0" value-title="Enel Energie Muntenia SA"/>
                          </default>
                      </switch>
                      <modify src-key="date" t-value-title="^(\d{2})(\d{2})(\d{2})$" v-value-title="$1.$2.20$3"/>
                      <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="LoadInfo"/>
            </action>
            <action type="Exit">
                <goto target="exit"/>
            </action>
        </actions>
    </screen>
    
    <!-- ID factura
    - Sa continta doar cifre;
    - Sa permita continuarea doar la indeplinirea celor 11 cifre;
    -->
    <screen id="StepTwo" type="numeric" title="Introduceti identificatorul de factura">
        <fields>
            <text-field id="id2" keyboard="Digital" max-len="11" title="ID factura">
                <validator type="regex">
                    <rules>
                        <rule regex="^(\d{8,11})$"/>
                    </rules>
                </validator>
                <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="EnelSmith"/>
                <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="provider,id1,id2,id3,date,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>
