﻿
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML Strict//EN">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<html>
<head>
    <style>
        body, input, select, textarea, table.ms-crm-Loading, div.ms-crm-Grid-Title
        {
            font-family: Segoe UI,Tahoma,Arial;
            font-size: 11px;
        }

        button
        {
            font-size: 11px;
        }

        .ms-crm-RefreshDialog-Button, .ms-crm-RefreshDialog-Header-Desc, .ms-crm-RefreshDialog-Main, .ms-crm-RefreshDialog-Main-HeaderLess
        {
            font-size: 12px;
        }

        html, body
        {
            height: 100%;
        }

        body
        {
            margin-top: 0px;
            margin-right: 0px;
            margin-bottom: 0px;
            margin-left: 0px;
            border-top-color: currentColor;
            border-right-color: currentColor;
            border-bottom-color: currentColor;
            border-left-color: currentColor;
            border-top-width: 0px;
            border-right-width: 0px;
            border-bottom-width: 0px;
            border-left-width: 0px;
            border-top-style: none;
            border-right-style: none;
            border-bottom-style: none;
            border-left-style: none;
            cursor: default;
            direction: ltr;
        }

        button
        {
            width: 84px;
            height: 20px;
            text-align: center;
            line-height: 16px;
            padding-right: 5px;
            padding-left: 5px;
            border-top-width: 1px;
            border-right-width: 1px;
            border-bottom-width: 1px;
            border-left-width: 1px;
            border-top-style: solid;
            border-right-style: solid;
            border-bottom-style: solid;
            border-left-style: solid;
            cursor: pointer;
            background-repeat: repeat-x;
        }

        body.ms-crm-Setting-Context-top, body
        {
            scrollbar-base-color: #cdcdcd;
            scrollbar-face-color: #cdcdcd;
            scrollbar-3dlight-color: #f0f0f0;
            scrollbar-shadow-color: #cdcdcd;
            scrollbar-highlight-color: #cdcdcd;
            scrollbar-darkshadow-color: #f0f0f0;
            scrollbar-arrow-color: #1e395b;
            scrollbar-track-color: #f0f0f0;
            background-color: transparent;
        }

        button
        {
            border-top-color: #666666;
            border-right-color: #666666;
            border-bottom-color: #666666;
            border-left-color: #666666;
            background-image: url("/_imgs/theme/Outlook15White/ButtonNormalGradient.png?ver=-152044938");
        }

        .ms-crm-RefreshDialog-Button
        {
            color: #444444;
            background-color: rgb(255, 255, 255);
        }

        body
        {
            left: 0px;
            top: 0px;
            right: 0px;
            bottom: 0px;
            overflow: hidden;
            position: absolute;
        }

        .ms-crm-RefreshDialog-Button
        {
            width: auto;
            height: 24px;
            font-family: Segoe UI,Tahoma,Arial;
            border-top-color: #c6c6c6;
            border-right-color: #c6c6c6;
            border-bottom-color: #c6c6c6;
            border-left-color: #c6c6c6;
            border-top-width: 1px;
            border-right-width: 1px;
            border-bottom-width: 1px;
            border-left-width: 1px;
            border-top-style: solid;
            border-right-style: solid;
            border-bottom-style: solid;
            border-left-style: solid;
            white-space: nowrap;
            min-width: 80px;
            background-image: none;
        }

        :enabled.ms-crm-RefreshDialog-Button:hover
        {
            background-color: rgb(177, 214, 240);
        }
    </style>
    <script type="text/javascript" src="ClientGlobalContext.js.aspx"></script>
    <script type="text/javascript" src="iotap_scripts/iotap_utility.js"></script>
    <script type="text/javascript" src="iotap_scripts/iotap_jQuery.js"></script>
    <script type="text/javascript" src="iotap_scripts/iotap_ReportDownloader.js"></script>

    <script type="text/javascript">

        function btnDynamicButton_onClick()
        {
            var _actionId = getDataParam(1).replace(/ /g, ""); Log("_actionId: " + _actionId);
            
            if ( !ValidateLicense() ) {
                parent.Xrm.Utility.alertDialog( "OneClickPdf : Invalid License" );
                window.close();
            }

            var httpReq = new XMLHttpRequest();
            httpReq.open( "GET", _getUrl( parent.Xrm.Page.context ) + "/api/data/v8.0/" + "iotap_1clickdocs?$select=iotap_report,iotap_action,iotap_fileformat,iotap_filename&$filter=iotap_actionid eq '" + _actionId + "'", false );
            httpReq.setRequestHeader( "Accept", "application/json" );
            httpReq.setRequestHeader( "Content-Type", "application/json; charset=utf-8" );
            httpReq.setRequestHeader( "OData-MaxVersion", "4.0" );
            httpReq.setRequestHeader( "OData-Version", "4.0" );
            httpReq.send( null );

            if ( httpReq.status === 200 ) {
                var configColl = JSON.parse( httpReq.responseText ).value;

                if(configColl.length == 0){
                    var features = "location=no,menubar=no,status=no,toolbar=no,width=400,height=500,top=200,left=500";
                    window.open( _getUrl( parent.Xrm.Page.context ) +
                            "/webresources/iotap_DownloadOptions.htm?Data=" +
                            encodeURIComponent( parent.Xrm.Page.data.entity.getEntityName() ) + "|" +
                            encodeURIComponent( parent.Xrm.Page.data.entity.getId() ), "_blank", features, false );
                }
                else {
                    ReportDownloader.ReportId = configColl[0].iotap_report;
                    ReportDownloader.Action = String(configColl[0].iotap_action); //false = SAVE & true = open
                    ReportDownloader.Fileformat = String(configColl[0].iotap_fileformat);
                    ReportDownloader.FileName = configColl[0].iotap_filename;
                    DownloadReport();
                }
            }            
        }


        function ValidateLicense()
        {
            var isValid = false;

            $.ajax( {
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: getSUrl() + "/api/data/v8.0/" + "sdkmessageprocessingsteps?$filter=startswith(name,'IOTAP.AddOn.OneClickPdf.ValidateLicense.LicenseValidator:') and statecode eq 0",
                async: false,
                data: null,
                beforeSend: function ( XMLHttpRequest )
                {
                    XMLHttpRequest.setRequestHeader( "Accept", "application/json" );
                    XMLHttpRequest.setRequestHeader( "OData-MaxVersion", "4.0" );
                    XMLHttpRequest.setRequestHeader( "OData-Version", "4.0" );
                },
                success: function ( data, textStatus, XmlHttpRequest )
                {
                    var results = data.value;
                    if ( results.length == 2 ) {
                        isValid = true;
                    }
                },
                error: function ( XmlHttpRequest, textStatus, errorThrown )
                {
                    parent.Xrm.Utility.alertDialog( "Error " + textStatus + errorThrown );

                }
            } );

            return isValid;
        }

        document.onreadystatechange = function ()
        {
            if (document.readyState == "complete")
            {
                if (!IsRequiredPluginPresent()) { alert(Constant.TrialFailError); return; }
                document.getElementById("btnDynamicButton").innerHTML = getDataParam(0);
            }
        }
    </script>

</head>
<body>
    <div style="width: 100%; height: 100%; overflow: auto; text-align: right;">
        <button class="ms-crm-RefreshDialog-Button" id="btnDynamicButton" onclick="btnDynamicButton_onClick();" type="button"></button>
    </div>
    <div id="trEditFilter">
        <table id="tblReport" style="width: 100%;" cellpading="3" cellspacing="0">
            <tbody>
                <tr>
                    <td>                        
                        <div id="divLoadingPanel" style="display: none; background-color: #898AA0; border-color: black; width: 200px; height: 50px; position: absolute; left: 48%; top: 48%px; z-index: 1; text-align: center; vertical-align: middle; font-size: large;">Please wait.....</div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</body>
</html>