<script type="text/javascript">
    (function () 
    { 
        let qrImageWidth = "300";
        RED.nodes.registerType('admin',{
            category: 'whatsapp',
            color: '#25D366',
            defaults: {
                name: {value:"WA Admin"},
                whatsappLink: {value:"", type: "whatsappLink"}
            },
            outputs:1,
            inputs:1,
            icon: 'whatsappLink.svg',
            label: function() {
                return this.name||"admin";
            }
        });

        let removeQrCode = function(NodeID) {
            let qrImageElement = document.getElementById("whatsappLink-QRcode-" + NodeID);
            if(qrImageElement){
                qrImageElement.remove();
            }
        };

        var creatImageContainer = function(NodeID) {
            let img = document.getElementById("whatsappLink-QRcode-" + NodeID)
            if (!img) {
                const container = document.getElementById(NodeID)
                if (!container) { return }
                const img = document.createElementNS("http://www.w3.org/2000/svg", 'image')
                img.setAttribute('id', "whatsappLink-QRcode-" + NodeID)
                img.setAttribute('x', '0')
                img.setAttribute('y', '45')
                img.setAttribute('width', qrImageWidth)
                container.insertBefore(img, container.lastChild.nextSibling)
            }
        };

        var renderQrCode = function(id, qrCodeImage){
            creatImageContainer(id)
            let qrImage = document.getElementById("whatsappLink-QRcode-" + id);
            qrImage.setAttribute('href', qrCodeImage)
            qrImage.addEventListener("click", ()=> removeQrCode(id), {once:true})
        };


        RED.comms.subscribe("whatsappLinkQrCode", function (e, msg) {
            if (msg.image === null){
                removeQrCode(adminNodeId);
            }
            renderQrCode(msg.id, msg.image);   
        });

    })()
</script>

<script type="text/html" data-template-name="admin">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="Name">
    </div>
    <div class="form-row">
        <label for="node-input-whatsappLink"><i class="fa fa-gear"></i> Client</label>
        <input type="text" id="node-input-whatsappLink" placeholder="Name">
    </div>
</script>
  
   
<script type="text/markdown" data-help-name="admin">
Node used for Admin related tasks of whatsapp.

Node used for first time users to connect with whatsapp and other admin related tasks. </br>
Admin Node generate QR Code just below the node for easy connection with whatsapp.

| Inputs | Description           |
|--------|--------------         |
| test   | Checks the current status of whatsapp and output the same in `msg.payload`|
| destroy| Close the client and destroy the connection.|
| restart | Restart the whatsapp client |
| logout | Simply log you out and close the session. |


| Output | Description |
|--------| ------------|
|`status` | provide status on `msg.payload` for all and each input mentioned in above table. |
| Connecting..| When whatsapp attempting to connect.
| QR Code (image) | when QR code is generated. *This method can also be used to get QR Code (image) generated by whatsapp.*
| Connected | When whatapp is sucessfully connected.|
| Group Joined or Removed | `msg.paylod` : Group Name. </br> `msg.type` : joined / Removed from group.</br> `msg.notification` : Complete notification. </br> `msg.chat` : Complete Group Details.

</script>