<?php $__env->startSection('workspace'); ?>

    <!-- Breadcrumb -->
    <section class="breadcrumb lg:flex items-start">
        <div>
            <h1>EzyPOS License</h1>
            <ul>
                <li><a href="#no-link">Pages</a></li>
                <li class="divider la la-arrow-right"></li>
                <li><a href="#no-link">License</a></li>
                <li class="divider la la-arrow-right"></li>
                <li>List</li>
            </ul>
        </div>

        <div class="flex flex-wrap gap-2 items-center ltr:ml-auto rtl:mr-auto mt-5 lg:mt-0">

            <!-- Search -->
            <form class="flex flex-auto">
                <label class="form-control-addon-within rounded-full">
                    <input class="form-control border-none" placeholder="Search">
                    <button
                        class="text-gray-300 dark:text-gray-700 text-xl leading-none la la-search ltr:mr-4 rtl:ml-4"></button>
                </label>
            </form>

            <div class="flex gap-x-2">

                <!-- Sort By -->
                <div class="dropdown">
                    <button class="btn btn_outlined btn_secondary uppercase" data-toggle="dropdown-menu">
                        Sort By
                        <span class="ltr:ml-3 rtl:mr-3 la la-caret-down text-xl leading-none"></span>
                    </button>
                    <div class="dropdown-menu">
                        <a href="#no-link">Ascending</a>
                        <a href="#no-link">Descending</a>
                    </div>
                </div>



            </div>
        </div>
    </section>

    <!-- List -->
    <div class="card p-5">
        <div class="overflow-x-auto">
            <table class="table table-auto table_hoverable w-full">
                <thead>
                <tr>

                    <th class="ltr:text-left rtl:text-right uppercase">License Key</th>
                    <th class="text-center uppercase">App Id</th>
                    <th class="text-center uppercase">License Type</th>
                    <th class="text-center uppercase">Change License Count</th>
                    <th class="text-center uppercase">Register Date</th>
                    <th class="text-center uppercase">Status</th>
                    <th class="uppercase">Action</th>
                </tr>
                </thead>
                <tbody>

                <?php if($licenses->count() > 0): ?>
                    <?php $__currentLoopData = $licenses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $license): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>
                            <td><?php echo e($license->license_key); ?></td>
                            <td class="text-center"><?php echo e($license->app_id); ?></td>
                            <td class="text-center"><?php echo e(ucfirst($license->license_type )); ?></td>
                            <td class="text-center"><?php echo e($license->change_license_count); ?></td>
                            <td class="text-center"><?php echo e($license->registered_date); ?></td>
                            <td class="text-center">

                                <?php if($license->status == 'active'): ?>

                                    <div class="badge badge_outlined badge_success uppercase"><?php echo e($license->status); ?></div>
                                <?php else: ?>
                                    <div class="badge badge_outlined badge_danger uppercase"><?php echo e($license->status); ?></div>

                                <?php endif; ?>

                            </td>
                            <td class="ltr:text-right rtl:text-left whitespace-nowrap">
                                <div class="inline-flex ltr:ml-auto rtl:mr-auto">

                                    <?php if($license->app_id == ''): ?>
                                        
                                        <button class="btn btn-icon btn_outlined btn_primary" data-toggle="modal"
                                                data-target="#exampleModalCentered"
                                                onclick="openModal('<?php echo e($license->license_key); ?>')"><span
                                                class="la la-file-contract"></span></button>

                                    <?php else: ?>
                                        
                                        <a class="btn btn-icon btn_outlined btn_success"
                                           href="<?php echo e(route('sub-dealer.customers.info', $license->app->company->company_id)); ?>"><span
                                                class="la la-info-circle"></span></a>

                                        
                                        <button class="btn btn-icon btn_outlined btn_info ltr:ml-2 rtl:mr-2"
                                                data-toggle="modal"
                                                data-target="#changeLicenseTypeModal"
                                                onclick="openModal('<?php echo e($license->license_key); ?>')"><span
                                                class="la la-file-contract"></span></button>

                                        <?php if($license->status == 'active'): ?>
                                            
                                            <button class="btn btn-icon btn_outlined btn_danger ltr:ml-2 rtl:mr-2"
                                                    data-toggle="modal"
                                                    data-target="#disabledLicenseModal"
                                                    onclick="openDisableLicenseModal('<?php echo e($license->license_key); ?>', 'off')"><span
                                                    class="la la-ban"></span></button>
                                        <?php else: ?>
                                            
                                            <button class="btn btn-icon btn_outlined btn_success ltr:ml-2 rtl:mr-2"
                                                    data-toggle="modal"
                                                    data-target="#disabledLicenseModal"
                                                    onclick="openDisableLicenseModal('<?php echo e($license->license_key); ?>','on')"><span
                                                    class="la la-check-circle"></span></button>

                                        <?php endif; ?>
                                        
                                        <button class="btn btn-icon btn_outlined btn_secondary ltr:ml-2 rtl:mr-2"
                                                data-toggle="modal"
                                                data-target="#transferLicenseModal"
                                                onclick="openModal('<?php echo e($license->license_key); ?>')"><span
                                                class="la la-exchange"></span></button>

                                    <?php endif; ?>




                                    
                                    
                                    
                                </div>
                            </td>
                        </tr>

                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <?php else: ?>
                    <tr>
                        <td colspan="5" class="text-center">No License Found</td>
                    </tr>
                <?php endif; ?>

                </tbody>
            </table>
        </div>
    </div>

    <div id="changeLicenseTypeModal" class="modal" data-animations="fadeInDown, fadeOutUp" data-static-backdrop>
        <div class="modal-dialog modal-dialog_centered max-w-2xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title">Change License Type</h2>
                    <button class="close la la-times" data-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="custom-select">
                        <select id="change_license_type" class="form-control">
                            <option>Select</option>
                            <option value="standalone">Standalone</option>
                            
                            <option value="local">Local</option>
                        </select>
                        <div class="custom-select-icon la la-caret-down"></div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="flex ltr:ml-auto rtl:mr-auto">
                        <button class="btn btn_secondary uppercase" data-dismiss="modal">Close</button>
                        <button class="btn btn_primary ltr:ml-2 rtl:mr-2 uppercase" onclick="submitTransferType()">
                            Confirm
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div id="transferLicenseModal" class="modal" data-animations="fadeInDown, fadeOutUp" data-static-backdrop>
        <div class="modal-dialog modal-dialog_centered max-w-2xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title">Transfer EzyPOS License</h2>
                    <button class="close la la-times" data-dismiss="modal" onclick="closeModal()"></button>
                </div>
                <div class="modal-body">
                    <div class="relative">
                        <label
                            class="label absolute block bg-input top-0 ltr:ml-3 rtl:mr-3 px-1 rounded font-heading uppercase"
                            for="ezypos_appid_new">New EzyPOS AppId</label>
                        <input id="ezypos_appid_new" class="form-control mt-2 pt-5" placeholder="Enter new appid here">
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="flex ltr:ml-auto rtl:mr-auto">
                        <button class="btn btn_secondary uppercase" data-dismiss="modal" onclick="closeModal()">Close
                        </button>
                        <button class="btn btn_primary ltr:ml-2 rtl:mr-2 uppercase" onclick="submitTransfer()">Confirm
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <div id="exampleModalCentered" class="modal" data-animations="fadeInDown, fadeOutUp" data-static-backdrop>
        <div class="modal-dialog modal-dialog_centered max-w-2xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title">Assign EzyPOS License</h2>
                    <button class="close la la-times" data-dismiss="modal" onclick="closeModal()"></button>
                </div>
                <div class="modal-body">
                    <div class="relative">
                        <label
                            class="label absolute block bg-input top-0 ltr:ml-3 rtl:mr-3 px-1 rounded font-heading uppercase"
                            for="ezypos_appid">EzyPOS AppId</label>
                        <input id="ezypos_appid" class="form-control mt-2 pt-5" placeholder="Enter appid here">
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="flex ltr:ml-auto rtl:mr-auto">
                        <button class="btn btn_secondary uppercase" data-dismiss="modal" onclick="closeModal()">Close
                        </button>
                        <button class="btn btn_primary ltr:ml-2 rtl:mr-2 uppercase" onclick="submitFunction()">Confirm
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div id="disabledLicenseModal" class="modal" data-animations="fadeInDown, fadeOutUp" data-static-backdrop>
        <div class="modal-dialog modal-dialog_centered max-w-2xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h2 class="modal-title" id="disabledTitle">License Deactivate</h2>
                    <button class="close la la-times" data-dismiss="modal"
                            onclick="closeDisableLicenseModal()"></button>
                </div>
                <div class="modal-body">
                    <p id="disabledContent">Are you sure want to deactivate this license?</p>
                </div>
                <div class="modal-footer">
                    <div class="flex ltr:ml-auto rtl:mr-auto">
                        <button class="btn btn_secondary uppercase" data-dismiss="modal"
                                onclick="closeDisableLicenseModal()">Close
                        </button>
                        <button class="btn btn_primary ltr:ml-2 rtl:mr-2 uppercase" onclick="confirmDisableFunction()">
                            Confirm
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>

    
    
    

    <!-- Footer Bar -->
    
    
    
    
    
    
    
    
    
    
    
    

<?php $__env->stopSection(); ?>



<?php $__env->startSection('scripts'); ?>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script>

        let selectedLicenseKey = '';

        function closeModal() {
            document.getElementById('ezypos_appid').value = '';
            document.getElementById('ezypos_appid_new').value = '';
            selectedLicenseKey = '';
        }

        function openModal(licenseKey) {
            selectedLicenseKey = licenseKey;
        }

        function submitTransferType() {
            let transferType = document.getElementById('change_license_type').value;
            if (transferType === 'Select') {
                alert('Please select transfer type');
                return;
            } else {
                console.log(transferType);
                console.log(selectedLicenseKey);


                axios.post('<?php echo e(route('sub-dealer.licenses.transfer-type')); ?>', {
                    _token: '<?php echo e(csrf_token()); ?>',
                    license_key: selectedLicenseKey,
                    license_type: transferType,
                })
                    .then(function (response) {
                        console.log(response);
                        if (response.status === 200) {
                            alert(response.data.message);
                            window.location.reload();
                        } else {
                            alert(response.data.message);
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            }

        }

        function submitTransfer() {
            let ezypos_appid = document.getElementById('ezypos_appid_new').value;
            if (ezypos_appid === '') {
                alert('Please enter EzyPOS AppId');
                return;
            }
            if (ezypos_appid.length !== 22) {
                alert('Please enter valid EzyPOS AppId');
                return;
            }

            axios.post('<?php echo e(route('sub-dealer.licenses.transfer')); ?>', {
                _token: '<?php echo e(csrf_token()); ?>',
                license_key: selectedLicenseKey,
                ezypos_appid: ezypos_appid,
            })
                .then(function (response) {
                    console.log(response);
                    if (response.status === 200) {
                        alert(response.data.message);
                        window.location.reload();
                    } else {
                        alert(response.data.message);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        }


        function submitFunction() {

            //ezypos appid
            let ezypos_appid = document.getElementById('ezypos_appid').value;
            if (ezypos_appid === '') {
                alert('Please enter EzyPOS AppId');
                return;
            }
            if (ezypos_appid.length !== 22) {
                alert('Please enter valid EzyPOS AppId');
                return;
            }

            console.log(selectedLicenseKey);

            axios.post('<?php echo e(route('sub-dealer.licenses.assign')); ?>', {
                _token: '<?php echo e(csrf_token()); ?>',
                license_key: selectedLicenseKey,
                ezypos_appid: ezypos_appid,
            })
                .then(function (response) {
                    console.log(response);
                    if (response.status === 200) {
                        alert(response.data.message);
                        window.location.reload();
                    } else {
                        alert(response.data.message);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        }

        // 获取输入字段或文本框的引用
        let inputField = document.getElementById('ezypos_appid');
        let inputFieldNew = document.getElementById('ezypos_appid_new');

        // 添加事件监听器
        inputField.addEventListener('input', addHyphen);
        inputFieldNew.addEventListener('input', addHyphen);
        inputField.addEventListener('paste', addHyphen);
        inputFieldNew.addEventListener('paste', addHyphen);


        // 处理函数：在输入或粘贴时自动添加连字符
        function addHyphen(event) {

            console.log('addHyphen');

            // 延迟执行以便等待输入或粘贴完成
            setTimeout(function () {
                // 获取输入字段的值
                let value = event.target.value.toUpperCase();

                value = value.replace(/-/g, "");

                //make sure length is 20
                if (value.length > 20) {
                    value = value.substring(0, 20);
                }


                // 使用正则表达式添加连字符
                let formattedValue = value.replace(/(\w{6})(\w{8})(\w{6})/, '$1-$2-$3');

                // 将格式化的值设置回输入字段
                event.target.value = formattedValue;
            }, 0);
        }


        function closeDisableLicenseModal() {
            selectedLicenseKey = '';
        }

        function openDisableLicenseModal(licenseKey, status) {
            selectedLicenseKey = licenseKey;

            if (status === 'on') {
                let disabledTitle = document.getElementById('disabledTitle');
                disabledTitle.innerHTML = 'License Activate';
                let disabledContent = document.getElementById('disabledContent');
                disabledContent.innerHTML = 'Are you sure want to activate this license?';
            }


        }

        function confirmDisableFunction() {
            axios.post('<?php echo e(route('sub-dealer.licenses.status')); ?>', {
                _token: '<?php echo e(csrf_token()); ?>',
                license_key: selectedLicenseKey,
            })
                .then(function (response) {
                    console.log(response);
                    if (response.status === 200) {
                        alert(response.data.message);
                        window.location.reload();
                    } else {
                        alert(response.data.message);
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        }

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

<?php echo $__env->make('layouts.master', ['title' => 'List - License - Pages'], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/ezyorder/public_html/ezypos.ezyorder.com.my/ezypos-backend/resources/views/pages/sub-dealer/license-list.blade.php ENDPATH**/ ?>