<?php $__env->startSection('styles'); ?>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('title'); ?>
    Add/View Bot Device
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <!-- Begin Page Content -->
    <div class="container">

        <!-- Page Heading -->
        
        <div class="d-flex align-items-center p-3 my-3 text-white bg-primary rounded shadow-sm">
            <svg class="bd-placeholder-img flex-shrink-0 me-2 rounded" width="32" height="32"
                xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: 32x32"
                preserveAspectRatio="xMidYMid slice" focusable="false">
                <title>Placeholder</title>
                <rect width="100%" height="100%" fill="var(--white)"></rect><text x="50%" y="50%"
                    fill="var(--white)" dy=".3em">32x32</text>
            </svg>

            <div class="ml-2">
                <h1 class="h6 mb-0 text-white lh-1">Add/View Bot Device</h1>
                <small><a class="text-white" href="<?php echo e(url('admin/dashboard')); ?>">dashboard</a> > Add/View Bot Device</small>
            </div>
        </div>
        <!-- Content Row -->
        <div class="row mb-3">
            <div class="col-sm-9"></div>
            <div class="col-sm-3">
                <div >
                    <div class="ring-container" title="Runing">
                        <div class="ringring"></div>
                        <div class="circle"></div>
                    </div>
                    <div class="ring-container-failed" title="Failed" style="display: none">
                        <div class="ringring-failed"></div>
                        <div class="circle-failed"></div>
                    </div>
                    <span style="  margin-top: 10px;margin-left: 50px;">WhatsApp Bot Server</span>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="card mb-4 shadow">
                    <div class="card-body">
                        <div class="row">
                        <div class="col-sm-4">
                                <div class="card shadow">
                                    <div class="card-header">
                                        <div class="card-title">Add Device</div>
                                    </div>
                                    <div class="card-body">
                                        <form action="<?php echo e(url($prefix . 'store')); ?>" method="post">
                                            <?php echo csrf_field(); ?>
                                            <div class="form-group">
                                                <label class="small">Name <span class="text-danger">*</span></label>
                                                <input type="text" class="form-control <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                                    value="<?php echo e(old('name')); ?>" name="name" aria-describedby="hostnameHelp"
                                                    id="name" placeholder="Enter Session Name (Any)" required>
                                                <?php $__errorArgs = ['name'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                    <span class="text-danger small"><?php echo e($message); ?></span>
                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                            </div>
                                            <div class="form-group">
                                                <label class="small">Number <span class="text-danger">*</span></label>
                                                <input type="number" class="form-control <?php $__errorArgs = ['number'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                                    value="<?php echo e(old('number')); ?>" name="number" aria-describedby="numberHelp"
                                                    id="number" placeholder="Enter WhatsApp Number" required>
                                                <?php $__errorArgs = ['number'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                    <span class="text-danger small"><?php echo e($message); ?></span>
                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                            </div>
                                            <div class="form-group">
                                                <label class="small">Description</label>
                                                <textarea name="description" id="" cols="30" rows="3" class="form-control <?php $__errorArgs = ['description'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" placeholder="Enter Description (any)"></textarea>
                                                <?php $__errorArgs = ['description'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                                    <span class="text-danger small"><?php echo e($message); ?></span>
                                                <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                                            </div>
                                            <div class="row">
                                                <div class="col-sm-12">
                                                    <div class="text-right mt-4">
                                                        <a class="btn btn-secondary text-uppercase font-weight-bold rounded pl-4 pr-4"
                                                            href="<?php echo e(url()->previous()); ?>">Cancel</a>
                                                        <button class="btn btn-primary text-uppercase font-weight-bold rounded pl-4 pr-4"
                                                            type="submit">Add Device</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-8">
                                <div class="card table-responsive">
                                    <div class="card-header">
                                        <div class="card-title">Device List</div>
                                    </div>
                                    <div class="card-body">
                                        <div class="text-right">
                                            <form class="w-100" method="GET" action="<?php echo e(url($prefix . 'search?')); ?>" role="search">
                                                <div class="input-group">
                                                    <input type="text" class="form-control bg-light border-0 small"
                                                        placeholder="Search for..." aria-label="Search" aria-describedby="basic-addon2" name="search">
                                                    <div class="input-group-append">
                                                        <button type="submit" class="btn btn-primary" type="button">
                                                            <i class="fas fa-search fa-sm"></i>
                                                        </button>
                                                    </div>
                                                </div>
                                            </form>
                                        </div>
                                        <table class="table table-borderd text-dark text-center small">
                                        <thead class="text-uppercase small text-dark">
                                            <tr>
                                                <th>#id</th>
                                                <th>name</th>
                                                <th>number</th>
                                                <th>description</th>
                                                <th>session id</th>
                                                <th>status</th>
                                                <th>action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <tr>
                                                <td><?php echo e($item->id); ?></td>
                                                <td><?php echo e($item->name); ?></td>
                                                <td><?php echo e($item->number); ?></td>
                                                <td><?php echo e($item->description); ?></td>
                                                <td><?php echo e($item->session_id); ?></td>
                                                <td>
                                                    <?php if($item->status == 'initiate'): ?><span class="badge badge-primary"><?php echo e($item->status); ?></span><?php endif; ?>
                                                    <?php if($item->status == 'connected'): ?><span class="badge badge-success"><?php echo e($item->status); ?></span><?php endif; ?>
                                                    <?php if($item->status == 'disconnected'): ?><span class="badge badge-danger"><?php echo e($item->status); ?></span><?php endif; ?>
                                                </td>
                                                <td>
                                                <div class="nav-item dropdown no-arrow">
                                                    <a class="dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-three-dots-vertical" viewBox="0 0 16 16">
                                                            <path d="M9.5 13a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0zm0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0zm0-5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0z"/>
                                                        </svg>
                                                    </a>
                                                    
                                                    <div class="dropdown-menu dropdown-menu-right shadow animated--fade-in" aria-labelledby="navbarDropdown">
                                                        <?php if($item->status != 'connected'): ?>
                                                        <a class="dropdown-item scan" href="#" data-url="<?php echo e(url($prefix.'scan?id='.$item->id)); ?>" data-id="<?php echo e($item->id); ?>">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-qr-code-scan" viewBox="0 0 16 16">
                                                                <path d="M0 .5A.5.5 0 0 1 .5 0h3a.5.5 0 0 1 0 1H1v2.5a.5.5 0 0 1-1 0v-3Zm12 0a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0V1h-2.5a.5.5 0 0 1-.5-.5ZM.5 12a.5.5 0 0 1 .5.5V15h2.5a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5v-3a.5.5 0 0 1 .5-.5Zm15 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1 0-1H15v-2.5a.5.5 0 0 1 .5-.5ZM4 4h1v1H4V4Z"/>
                                                                <path d="M7 2H2v5h5V2ZM3 3h3v3H3V3Zm2 8H4v1h1v-1Z"/>
                                                                <path d="M7 9H2v5h5V9Zm-4 1h3v3H3v-3Zm8-6h1v1h-1V4Z"/>
                                                                <path d="M9 2h5v5H9V2Zm1 1v3h3V3h-3ZM8 8v2h1v1H8v1h2v-2h1v2h1v-1h2v-1h-3V8H8Zm2 2H9V9h1v1Zm4 2h-1v1h-2v1h3v-2Zm-4 2v-1H8v1h2Z"/>
                                                                <path d="M12 9h2V8h-2v1Z"/>
                                                              </svg>
                                                          scan
                                                        </a>
                                                        <?php else: ?>
                                                        <a class="dropdown-item bg-danger text-light" href="<?php echo e(url($prefix.'disconnect?id='.$item->id)); ?>">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-qr-code-scan" viewBox="0 0 16 16">
                                                                <path d="M0 .5A.5.5 0 0 1 .5 0h3a.5.5 0 0 1 0 1H1v2.5a.5.5 0 0 1-1 0v-3Zm12 0a.5.5 0 0 1 .5-.5h3a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0V1h-2.5a.5.5 0 0 1-.5-.5ZM.5 12a.5.5 0 0 1 .5.5V15h2.5a.5.5 0 0 1 0 1h-3a.5.5 0 0 1-.5-.5v-3a.5.5 0 0 1 .5-.5Zm15 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1 0-1H15v-2.5a.5.5 0 0 1 .5-.5ZM4 4h1v1H4V4Z"/>
                                                                <path d="M7 2H2v5h5V2ZM3 3h3v3H3V3Zm2 8H4v1h1v-1Z"/>
                                                                <path d="M7 9H2v5h5V9Zm-4 1h3v3H3v-3Zm8-6h1v1h-1V4Z"/>
                                                                <path d="M9 2h5v5H9V2Zm1 1v3h3V3h-3ZM8 8v2h1v1H8v1h2v-2h1v2h1v-1h2v-1h-3V8H8Zm2 2H9V9h1v1Zm4 2h-1v1h-2v1h3v-2Zm-4 2v-1H8v1h2Z"/>
                                                                <path d="M12 9h2V8h-2v1Z"/>
                                                              </svg>
                                                          disconnect
                                                        </a>
                                                        <?php endif; ?>
                                                        <a class="dropdown-item" href="<?php echo e(url($prefix.'edit?id='.$item->id)); ?>">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-pencil-square" viewBox="0 0 16 16">
                                                            <path d="M15.502 1.94a.5.5 0 0 1 0 .706L14.459 3.69l-2-2L13.502.646a.5.5 0 0 1 .707 0l1.293 1.293zm-1.75 2.456-2-2L4.939 9.21a.5.5 0 0 0-.121.196l-.805 2.414a.25.25 0 0 0 .316.316l2.414-.805a.5.5 0 0 0 .196-.12l6.813-6.814z"/>
                                                            <path fill-rule="evenodd" d="M1 13.5A1.5 1.5 0 0 0 2.5 15h11a1.5 1.5 0 0 0 1.5-1.5v-6a.5.5 0 0 0-1 0v6a.5.5 0 0 1-.5.5h-11a.5.5 0 0 1-.5-.5v-11a.5.5 0 0 1 .5-.5H9a.5.5 0 0 0 0-1H2.5A1.5 1.5 0 0 0 1 2.5v11z"/>
                                                          </svg>
                                                          edit
                                                        </a>
                                                        <div class="dropdown-divider"></div>
                                                        <a class="dropdown-item delete" href="#" id="<?php echo e(url($prefix.'delete?id='.$item->id)); ?>">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
                                                                <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
                                                                <path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
                                                                </svg>
                                                            delete
                                                        </a>
                                                    </div>
                                                </div>
                                                </td>
                                            </tr>
                                               
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                        </tbody>
                                    </table>
                                    <?php if(!empty($data)): ?>
                                        <?php echo e($data->links()); ?>

                                    <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <small class="d-block text-end mt-3">

        </small>
    </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
    <script>
         $(".scan").on("click", function () {
            const href = $(this).attr("data-url")
            const id = $(this).attr("data-id")
            var url = href
            $.ajax({
                type: "GET",
                url: url,
                dataType: "JSON",
            beforeSend:function (){
                $("#loader").show();
            },
            success: function (response) {
                $("#loader").hide();
                const interval = setInterval(function() {
                    qrCode(id)
                }, 5000);
                Swal.fire({
                    title: '<strong>To use WhatsApp</strong>',
                    icon: 'info',
                    html:
                    '<ul class="text-left">'+
                        '<li>1.Open WhatsApp on your phone</li>'+
                        '<li>2.Tap Menu or Settings and select Linked Devices</li>'+
                        '<li>3.Point your phone to this screen to capture the code </li>'+
                    '</ul>'+
                    '<div class="mt-3">'+
                        '<img src="'+response.qr+'" alt="" id="qr">'+
                    '</div>'+
                    '<div class="overlay" style="display:none;" id="loader_qr">'+
                        '<div class="overlay__inner">'+
                            '<div class="overlay__content"><span class="spinner"></span></div>'+
                        '</div>'+
                    '</div>' ,
                    showCloseButton: false,
                    confirmButtonText: 'Close',
                    allowOutsideClick:false,
                    onClose:()=>{
                        statusUpdate()
                    }
                }).then((result)=>{
                    if (result.isConfirmed) {
                        statusUpdate()
                    } else if (result.isDenied) {
                        statusUpdate()
                    }
                })
            },
            error:function(xhr){
                $("#loader").hide();
                Swal.fire({
                    position: 'top-end',
                    icon: "error",
                    title: xhr.responseJSON.message,
                    showConfirmButton: false,
                    timer: 7000
                })
            }
            });
            
            // $("#loader").hide();
            // $("#show_template").html(result.template)
        })
        function statusUpdate(){
            var url = "<?php echo e(url('admin/whatsapp-bot/status-update')); ?>"
            var result = ajax({'method':'POST', 'url':url, 'data':{},'type':'JSON'});
            if(result == 'true'){
                location.reload();
            }
            
        }
        function qrCode(id){
            var url = "<?php echo e(url('admin/whatsapp-bot/qr-code')); ?>"
            var result = ajax({'method':'POST', 'url':url, 'data':{id:id},'type':'JSON'});
            if(result.qr != ""){
                $("#qr").removeAttr('src')
                $("#qr").attr('src', result.qr)
                $("#loader_qr").hide();
            }else{
                $("#loader_qr").show();
                $("#qr").removeAttr('src')
            }
            if(result.status === "connected"){
                location.reload()
            }
        }
        $(document).ready(function(){
            setInterval(() => {
                ping()
            }, 5000);
        })
        function ping(){
            var url = "<?php echo e(url('admin/whatsapp-bot/server-status')); ?>"
            $.ajax({
                type: "GET",
                url: url,
                dataType: "JSON",
            success: function (response) {
                console.log(response)
                if(response === true){
                    $(".ring-container").show();
                    $(".ring-container-failed").hide();
                }else{
                    $(".ring-container").hide();
                    $(".ring-container-failed").show();
                }
            }})
        }
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('components.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/theload/kit/resources/views/admin/whatsapp-robot/wsr_add.blade.php ENDPATH**/ ?>