<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <facilitor>
            <xsl:copy>
                <xsl:copy-of select="facilitor/header"/>
            </xsl:copy>
            <melding>
                <mld_melding_key>
                    <xsl:value-of select="facilitor/opdracht/melding/key"/>
                </mld_melding_key>
                <opdracht>
                    <mld_opdr_key>
                        <xsl:value-of select="facilitor/opdracht/key"/>
                    </mld_opdr_key>
                </opdracht>
            </melding>
            <topdesk>
                <!-- De vaste waarden voor toegang tot Topdesk: -->
                <xsl:variable name="topdesk_user"></xsl:variable>
                <xsl:variable name="topdesk_key"></xsl:variable>
                <xsl:variable name="topdesk_url"></xsl:variable>

                <!-- ******** -->
                <xsl:variable name="v_methode">
                    <xsl:call-template name="get_methode">
                        <xsl:with-param name="opdracht" select="facilitor/opdracht"/>
                    </xsl:call-template>
                </xsl:variable>
                <xsl:variable name="v_actie">
                    <xsl:call-template name="get_actie">
                        <xsl:with-param name="opdracht" select="facilitor/opdracht"/>
                    </xsl:call-template>
                </xsl:variable>
                <info>
                    <api_user>
                        <xsl:value-of select="$topdesk_user"/>
                    </api_user>
                    <api_key>
                        <xsl:value-of select="$topdesk_key"/>
                    </api_key>
                    <base_url>
                        <xsl:value-of select="$topdesk_url"/>
                    </base_url>
                    <methode>
                        <xsl:value-of select="$v_methode"/>
                    </methode>
                    <actie>
                        <xsl:value-of select="$v_actie"/>
                    </actie>
                    <xsl:choose>
                        <xsl:when test="$v_actie = 'incident'">
                            <xsl:call-template name="getInfoIncident"/>
                        </xsl:when>
                        <xsl:when test="$v_actie = 'change'">
                            <xsl:call-template name="getInfoChange"/>
                        </xsl:when>
                    </xsl:choose>
                </info>
                <data>
                    <xsl:choose>
                        <xsl:when test="$v_actie = 'incident'">
                            <xsl:call-template name="getDataIncident">
                                <xsl:with-param name="topdesk_user" select="$topdesk_user"/>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:when test="$v_actie = 'change'">
                            <xsl:call-template name="getDataChange">
                                <xsl:with-param name="topdesk_user" select="$topdesk_user"/>
                            </xsl:call-template>
                        </xsl:when>
                    </xsl:choose>
                </data>
            </topdesk>
        </facilitor>
    </xsl:template>

    <xsl:template name="get_methode">
    <!-- Zoek de meest recente actie van de opdracht -->
        <xsl:param name="p_opdracht"/>
        <xsl:for-each select="facilitor/opdracht/tracking/track[code != 'ORDMLE']">
            <xsl:sort select="datum/timestamp" order="descending" data-type="number"/>
            <xsl:if test="position() = 1">
                <xsl:choose>
                    <xsl:when test="code = 'ORDNEW'">POST</xsl:when>
                    <xsl:otherwise>GET</xsl:otherwise>
                </xsl:choose>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="get_actie">
        <!-- Welke API-opdracht moet worden uitgevoerd: altijd in kleine letters -->
        <xsl:param name="p_opdracht"/>

        <xsl:variable name="v_opdracht" select="facilitor/opdracht/opdrachttype/omschrijving"/>
        <xsl:value-of select="translate($v_opdracht, 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞŸŽŠŒ', 'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿžšœ')" />
    </xsl:template>

    <xsl:template name="get_template_id">
        <xsl:variable name="stdmelding_oms" select="facilitor/opdracht/melding/stdmelding/omschrijving"/>

        <xsl:choose>
            <xsl:when test="$stdmelding_oms = 'Non standard Change IT IM org'">XX NNNNN</xsl:when>
            <!-- 85253d50-fc91-421a-ac23-b8791f220ce2  ????????  SMCSSR001 - CMDB aanpassing  -->
            <xsl:when test="$stdmelding_oms = 'Non standard Change IT IM'">CT 36327</xsl:when>
            <!-- "id": "b3cbe6d7-648a-41bb-adbc-322612833e1c","number": "CT 36327", "briefDescription": "Test - CAT2 - Category 2 change" -->
        </xsl:choose>
    </xsl:template>

    <!--
    **** Incident Management data
    -->
    <xsl:template name="getInfoIncident">
        <lookup_list>
        </lookup_list>
    </xsl:template>

    <xsl:template name="getDataIncident">
        <xsl:param name="topdesk_user"/>

        <!-- de basisgegevens om een melding aan te kunnen maken in Topdesk: -->
        <xsl:variable name="opdrachtomschrijving"></xsl:variable>
        <xsl:variable name="soortmelding_id"></xsl:variable>
        <xsl:variable name="categorie_id"></xsl:variable>
        <xsl:variable name="subcategorie_id"></xsl:variable>

        <xsl:variable name="prefix" select="facilitor/opdracht/melding/discipline/srtdiscipline/prefix"/>
        <xsl:variable name="mldkey" select="facilitor/opdracht/melding/key"/>
        <xsl:variable name="volgnr" select="facilitor/opdracht/bedrijfopdr_volgnr"/>

        <briefDescription>
            <xsl:value-of select="facilitor/opdracht/melding/onderwerp"/>
        </briefDescription>
        <request>
             <xsl:value-of select="facilitor/opdracht/omschrijving"/>
        </request>
        <caller>
            <dynamicName><xsl:value-of select="$topdesk_user"/></dynamicName>
        </caller>
        <externalNumber>
            <xsl:value-of select="concat($prefix,$mldkey,'/',$volgnr)"/>
        </externalNumber>
        <callType>
            <id>3b4e1c80-be3b-5b35-b6f2-5330a9acdb64</id>
        </callType>
        <category>
            <id>4ae0eeee-88ce-4ae4-bd3e-e0cd8db4b3a8</id>
        </category>
        <subcategory>
            <id>cda26297-78e2-4e58-a71e-9a519878900a</id>
        </subcategory>
    </xsl:template>

    <!--
    **** Change Management data, obv mailadres moet de aanvrager bepaald worden
    -->
    <xsl:template name="getInfoChange">
        <lookup_list>
            <lookup>
                <actie>person</actie>
                <qparams>
                    <xsl:value-of select="concat('?email=', facilitor/opdracht/melding/melder/email)"/>
                </qparams>
                <src>data[0].id</src>
                <dest>data.requester.id</dest>
            </lookup>
        </lookup_list>
    </xsl:template>

    <xsl:template name="getDataChange">
        <xsl:param name="topdesk_user"/>

        <xsl:variable name="prefix" select="facilitor/opdracht/melding/discipline/srtdiscipline/prefix"/>
        <xsl:variable name="mldkey" select="facilitor/opdracht/melding/key"/>
        <xsl:variable name="volgnr" select="facilitor/opdracht/bedrijfopdr_volgnr"/>

        <template><number><xsl:call-template name="get_template_id"/></number></template>
        <briefDescription><xsl:value-of select="facilitor/opdracht/melding/stdmelding/omschrijving"/></briefDescription>
        <request><xsl:value-of select="facilitor/opdracht/omschrijving"/></request>
        <requester><id/></requester>
        <externalNumber><xsl:value-of select="concat($prefix,$mldkey,'/',$volgnr)"/></externalNumber>
    </xsl:template>

</xsl:stylesheet>
