﻿<?xml version="1.0"?>
<AlvaoApplication xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ModelVersion="1">
  <Applications>
    <Application id="42">
      <Name>TicketShowRequesterAddress</Name>
      <Description>If the request is in the service and status defined in the described class, a tab with the requester's address (according to the organization) will be displayed in the map in the request detail.</Description>
      <Scripts>
        <Script id="113">
          <Name>Settings</Name>
          <Code>public static class Settings 
{
    public const string TabName = "Show requester's address";
    public const int ServiceId = 1; // The ID of the service for which we want the view to appear
    public const int StateId = 1; // The status ID in which the request must be in order for the view to be displayed.
}</Code>
          <IsLibCode>true</IsLibCode>
        </Script>
        <Script id="114">
          <Name>ShowRequesterAddress</Name>
          <Code>using System;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Xml;
using System.Text;
using Alvao.API.Common.Model.CustomApps;
using Alvao.Apps.API;
using Alvao.Context;
using Dapper;

public class ShowRequesterAddress : IEntityTab
{
    public string Id {get; set;}
    public Entity Entity {get; set;}

    public ShowRequesterAddress() 
    {
        Id = Settings.TabName.GetHashCode().ToString();
        Entity = Entity.Request;
    }

    public EntityTabShowResult Show(int entityId, int personId)
    {        
        bool show = false;
        string name = Settings.TabName;
        string url = string.Empty;

        using (var scope = AlvaoContext.GetConnectionScope())
        {
            //Retrieving the required request data from the database.
            var ticketModel = scope.Connection.QueryFirstOrDefault&lt;RequesterAddressTicketModel&gt;(@"
                SELECT
                    ZAA.Street + ' ' + ZAA.City RequesterAddress,	
                    liHdTicketHdSectionId ServiceId,
                    TicketStateId StateId
                FROM 
                    tHdTicket HDT 
                    JOIN tPerson Z on Z.iPersonId = HDT.liHdTicketUserPersonId 
                    JOIN tAccount ZA ON Z.liAccountId = ZA.iAccountId 
                    JOIN tAddress ZAA ON ZAA.id = ZA.AddressId
                WHERE
                    HDT.iHdTicketId = @iHdTicketId", new { iHdTicketId = entityId }
            );

            if (ticketModel == null){
                return new EntityTabShowResult (show, name, url);
            }

            // Check if the request is in the required service and status.
            if (ticketModel.ServiceId == Settings.ServiceId &amp;&amp; ticketModel.StateId == Settings.StateId)
            {
                // The returned url displays the address of the requester on the map in its own tab.
                var SAPI = new Uri(HttpUtility.UrlPathEncode("http://api.mapy.cz/geocode?query=" + ticketModel.RequesterAddress));
                using var httpClient = new HttpClient();
                string XMLresponse = httpClient.GetStringAsync(SAPI).GetAwaiter().GetResult();
                XmlDocument APIresponse = new XmlDocument();
                APIresponse.LoadXml(XMLresponse);
                var APIitems = APIresponse.GetElementsByTagName("item");
                if (APIitems.Count &gt;= 1)
                {
                    var Atributes = APIitems.Item(0).Attributes;
                    var Query = new StringBuilder("q=");
                    Query.Append(ticketModel.RequesterAddress);
                    
                    foreach (XmlAttribute Atribute in Atributes)
                    {
                        Query.Append('&amp;');
                        Query.Append(Atribute.Name);
                        Query.Append('=');
                        Query.Append(Atribute.Value);
                    }

                    url = HttpUtility.UrlPathEncode("http://frame.mapy.cz/zakladni?" + Query.ToString());
                    show = true;
                }
            }
        } 

        return new EntityTabShowResult (show, name, url);
    }
}

public class RequesterAddressTicketModel {
    public int ServiceId {get; set;}
    public int StateId {get; set;}
    public string RequesterAddress {get; set;}
}</Code>
          <IsLibCode>false</IsLibCode>
        </Script>
      </Scripts>
    </Application>
  </Applications>
</AlvaoApplication>