<?xml version="1.0" encoding="UTF-8" ?>
<Module>
    <ModulePrefs title="My Certificates">
        <Require feature="pubsub-2"/>
    </ModulePrefs>
    <Content type="html" view="default">
        <![CDATA[
        <div class='icon-file icon-lock-dashboard icon-marketing-styles'></div>
        <p>Manage your certificates and Certificate Signing Requests</p>
        <p><a class='btn btn-default max_view' href=''>View details</a></p>

        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/main.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap-theme.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap-missing.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap-theme.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/dialog.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/smoothness/jquery-ui-1.12.1.custom.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/smoothness/jqueryui-themeroller.css">
          <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/dataTables.bootstrap.css">
        <script src="/portal/gadgets/certificate_authority/js/jquery.min.js" type="text/javascript"></script>
        <script>
            $(function () {
                $('.max_view').click(function () {
                    gadgets.Hub.publish('org.wso2.is.dashboard', {
                        msg:'A message from CA',
                        id:"certificate_authority .expand-widget"
                    });
                });
            });
        </script>
        ]]>
    </Content>

    <Content type="html" view="home">
        <![CDATA[
        <script type="text/javascript" src="/portal/csrf.js"></script>
        <script src="/portal/gadgets/certificate_authority/js/jquery.min.js" type="text/javascript"></script>
        <script src="/portal/gadgets/certificate_authority/js/main.js" type="text/javascript"></script>
        <script src="/portal/gadgets/certificate_authority/js/modal.js" type="text/javascript"></script>
        <script src="/portal/gadgets/certificate_authority/js/gadget.js" type="text/javascript"></script>

        <script src="/portal/gadgets/certificate_authority/js/jquery.dataTables.js" type="text/javascript"></script>
        <script src="/portal/gadgets/certificate_authority/js/dataTables.bootstrap.js" type="text/javascript"></script>
        <script src="/portal/gadgets/certificate_authority/js/pagination.js" type="text/javascript"></script>

        <script src="/portal/gadgets/wso2-dev/js/jquery.numberformatter-1.2.3.js" type="text/javascript"></script>
        <script src="/portal/gadgets/wso2-dev/js/jquery.dependClass-0.1.js" type="text/javascript"></script>
        <script src="/portal/gadgets/wso2-dev/js/jquery.slider.js" type="text/javascript"></script>
        <script src="/portal/themes/portal/js/UESContainer.js"></script>


        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/main.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap-theme.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap-missing.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/bootstrap-theme.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/dialog.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/smoothness/jquery-ui-1.12.1.custom.min.css">
        <link rel="stylesheet" href="/portal/gadgets/certificate_authority/js/ui/css/smoothness/jqueryui-themeroller.css">


        <script type="text/javascript">
            var cookie = null;
            var json = null;
            var userName = null;
            $(function WindowLoad(event) {


                url = 'wss://localhost:9443/dashboard/session_manager.jag';
                ws = new WebSocket(url);

                ws.onopen = function () {
                    console.log("web Socket onopen. ");
                    ws.send("First Message open");
                };
                ws.onmessage = function (event) {
                    var obj = $.parseJSON(event.data);
                    cookie = obj.cookie;
                    userName = obj.user;
                    if (cookie != null) {
                              drawOptionPage();
                    }
                };
                ws.onclose = function () {
                    console.log("web Socket onclose. ");
                };
            });

            function sendCSRToAdmin(cn,dept, org,city,state,country,keySize) {

                var str = "/portal/gadgets/certificate_authority/controllers/my-profile/generate-csr.jag";
                $.ajax({
                    url:str,
                    type:"POST",
                    data:"commonName="+cn+"&department="+dept+"&organization="+org+"&city="+city+"&state="
                    +state+"&country="+country+"&keySize="+keySize+"&cookie=" + cookie + "&user=" + userName
                })
                        .done(function (data) {
                          var csrDetails = jQuery.parseJSON(data);
                          message({
                          content:'Certificate Signing Request has been successfully sent (Serial Number : '+csrDetails.serialNo
                          +') Click ok to download your private key',
                          type:'ok',
                          cbk:function(){
                            download(csrDetails.serialNo,'.key',csrDetails.pk);
                            drawOptionPage();
                            }
                           });

                        })
                        .fail(function () {
                        message({content:'Error while Sending Certificate Signing Request',type:'error', cbk:function(){} });

                        })
                        .always(function () {
                            console.log('completed');
                        });

            }

              function sendUploadedCSRToAdmin(csr) {

               if (!csr.replace(/\s/g, '').length) {
                    message({content:'Pleas upload or paste a Certificate Signing Request before sending!', type:'warning', cbk:function () {
                    uploadCSR();
                     } });
                 }else{

                var str = "/portal/gadgets/certificate_authority/controllers/my-profile/upload-csr.jag";

                var uploadingCSR = encodeURIComponent(csr);
                $.ajax({
                    url:str,
                    type:"POST",
                    data:"csr="+uploadingCSR+"&cookie=" + cookie + "&user=" + userName
                })
                        .done(function (data) {
                        var csrDetails = jQuery.parseJSON(data);

                   message({
                          content:'Certificate Signing Request has been successfully sent (Serial Number : '+csrDetails.return+')',
                          type:'ok',
                          cbk:function(){
                           drawOptionPage();
                            }
                           });
                        })
                        .fail(function () {
                        message({content:'Error while Sending Certificate Signing Request',type:'error', cbk:function(){} });

                        })
                        .always(function () {
                            console.log('completed');
                        });
                }

            }

               function getList() {

                var str = "/portal/gadgets/certificate_authority/controllers/my-profile/get-csr-list.jag";


                $.ajax({
                    url:str,
                    type:"POST",
                    data:"cookie=" + cookie + "&user=" + userName
                })
                        .done(function (data) {

                        var csrList = jQuery.parseJSON(data);

                        drawCSRSListPage(csrList);

                        $('#csr_details').dataTable();


                        })
                        .fail(function () {
                        message({content:'Error while Sending Certificate Signing Request',type:'error', cbk:function(){} });

                        })
                        .always(function () {
                            console.log('completed');
                        });

            }


    function getCSRFromSerialNo(serialNo) {

                var str = "/portal/gadgets/certificate_authority/controllers/my-profile/get-csr.jag";


                $.ajax({
                    url:str,
                    type:"POST",
                    data:"cookie=" + cookie + "&user=" + userName+"&serialNo="+serialNo
                })
                        .done(function (data) {
                        var csrDetails = jQuery.parseJSON(data);
                        displayCSR(csrDetails);

                        })
                        .fail(function () {
                        message({content:'Error while Sending Certificate Signing Request',type:'error', cbk:function(){} });

                        })
                        .always(function () {
                            console.log('completed');
                        });

            }


    function getCert(serialNo,certOnly) {

                var str = "/portal/gadgets/certificate_authority/controllers/my-profile/get-certificate.jag";

                $.ajax({
                    url:str,
                    type:"POST",
                    data:"cookie=" + cookie + "&user=" + userName+"&serialNo="+serialNo
                })
                        .done(function (data) {
                          var certDetails = jQuery.parseJSON(data);

                          if(certOnly=='true'){
                          download(serialNo,'.key',certDetails.publicCertificate);

                          }else{
                              displayCertificate(certDetails);
                          }

                        })
                        .fail(function () {
                        message({content:'Error while Sending Certificate Signing Request',type:'error', cbk:function(){} });

                        })
                        .always(function () {
                            console.log('completed');
                        });

            }

        function download(filenamePrefix,extension,content){
            var filename = filenamePrefix+extension;
            cert.href="data:text/plain,"+encodeURIComponent(content);
            cert.download=filename;
            cert.click();
         }




        </script>


        <form id="gadgetForm" class="form-horizontal">
            <div id="gadgetBody"></div>
	    <div id="message"></div>
        </form>

        ]]>
    </Content>
</Module>