<?php $__env->startSection('title', 'Manage Inventory'); ?>


<?php $__env->startSection('internal-js'); ?>
    <script>
        $(document).ready(function () {
            $('#example').DataTable( {
                dom: 'lBfrtip',
                buttons: [
                    { extend: 'excel', className: 'download-buttons' },
                    { extend: 'pdf', className: 'download-buttons' }
                ]
            } );
        });

        
        function addNewInventoryItems(){
            var route_link = '/add-new-inventory-items';

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="<?php echo e(asset('images/loads/loader.gif')); ?>" border=0></div>')
                .load(encodeURI(route_link))
                .dialog({
                    modal: true,
                    width: 750,
                    title: "Add New Inventory Items into the System",
                    buttons: [
                        {
                            text: "Save Inventory List",
                            icon: "ui-icon-check",
                            click: function(e) {
                                if(confirm('Are you sure to Save Entered Details?')){
                                    e.preventDefault();
                                    var datas = new FormData($('#add-new-inventory-item')[0]);

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        enctype: 'multipart/form-data',
                                        contentType: false,
                                        processData: false,
                                        success: function (res){
                                            if(res == 'item exists'){
                                                alert('One or More of the Entered Items Names already Exists');
                                                location.reload();
                                            }else if(res == 'success'){
                                                alert('Inventory List Successfully Uploaded');
                                                location.reload();
                                            }else if(res == 'error'){
                                                alert('An Error occurred and System is unable to perform the requested action. Try again');
                                                location.reload();
                                            }else{
                                                alert('System Encountered an Error. Please Contact System Admin');
                                                location.reload();
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function() {
                        $(this).parent().css({'top': 100});
                    }
                });
        }

        
        function editItem(id){
            var route_link = '/edit-item-details/'+id;

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="<?php echo e(asset('images/loads/loader.gif')); ?>" border=0></div>')
                .load(encodeURI(route_link))
                .dialog({
                    modal: true,
                    width: 750,
                    title: "Edit Items details",

                    buttons: [
                        {
                            text: "Save Details",
                            icon: "ui-icon-check",
                            click: function(e) {
                                if(confirm('Are you sure to Save Entered Details?')){
                                    e.preventDefault();
                                    var datas = $('#edit-item-details').serializeArray();

                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res){
                                            if(res == 'exists'){
                                                alert('Another item is using a similar name and the entered name above. Please check your inputs');
                                            }else if(res == 'success'){
                                                alert('Changes have been Saved Successfully');
                                                $('#edit_dialog_content').dialog('close');
                                            }else if(res == 'not authorized'){
                                                alert('You are not authorized to make changes on Items. Please contact System Admin for Help');
                                                location.reload();
                                            }else if(res == 'error'){
                                                alert('An Error occurred and System is unable to perform the requested action. Try again');
                                                location.reload();
                                            }else{
                                                alert('System Encountered an Error. Please Contact System Admin');
                                                location.reload();
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function() {
                        $(this).parent().css({'top': 100});
                    }
                });
        }

        
        function updateInventoryStatus(){
            var route_link = '/update-inventory-stock-balances';

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="<?php echo e(asset('images/loads/loader.gif')); ?>" border=0></div>')
                .load(encodeURI(route_link))
                .dialog({
                    modal: true,
                    width: 900,
                    title: "Update Inventory Stock Balances",
                    buttons: [
                        {
                            text: "Save Inventory Balances Details",
                            icon: "ui-icon-check",
                            click: function(e) {
                                if(confirm('Are you sure to Save Entered Details?')){
                                    e.preventDefault();
                                    var datas = $('#update-inventory-stock-balances').serializeArray();
                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res){
                                            if(res == 'success'){
                                                alert('Inventory Stock Balances List Successfully Uploaded');
                                                location.reload();
                                            }else if(res == 'error'){
                                                alert('An Error occurred and System is unable to perform the requested action. Try again');
                                                location.reload();
                                            }else{
                                                alert('System Encountered an Error. Please Contact System Admin');
                                                location.reload();
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function() {
                        $(this).parent().css({'top': 100});
                    }
                });
        }

        
        function updateItemStock(id){
            var route_link = '/update-item-inventory-stock-balance/'+id;

            $('#edit_dialog_content').html('<div style="text-align: center; margin: 20px 0 20px 0;"><img src="<?php echo e(asset('images/loads/loader.gif')); ?>" border=0></div>')
                .load(encodeURI(route_link))
                .dialog({
                    modal: true,
                    width: 900,
                    title: "Update Inventory Stock Balance",
                    buttons: [
                        {
                            text: "Save Inventory Balance Details",
                            icon: "ui-icon-check",
                            id: "save-stock-balance",
                            click: function(e) {
                                if(confirm('Are you sure to Save Entered Details?')){
                                    e.preventDefault();
                                    $('#save-stock-balance').attr('disabled', true).text('Please Wait....');
                                    var datas = $('#update-item-inventory-stock-balance').serializeArray();
                                    $.ajax({
                                        method: "POST",
                                        url: route_link,
                                        data: datas,
                                        success: function (res){
                                            if(res == 'success'){
                                                alert('Inventory Stock Balances List Successfully Uploaded');
                                                $('#edit_dialog_content').dialog('close');
                                            }else if(res == 'error'){
                                                alert('An Error occurred and System is unable to perform the requested action. Try again');
                                            }else{
                                                alert('System Encountered an Error. Please Contact System Admin');
                                                location.reload();
                                            }
                                        }
                                    });
                                }
                                return false;
                            }
                        }
                    ],
                    open: function() {
                        $(this).parent().css({'top': 100});
                    }
                });
        }

        
        function updateReminderLevel(id){
            alert('This Functionality is Coming Soon');
        }
    </script>
<?php $__env->stopSection(); ?>


<?php $__env->startSection('internal-css'); ?>
    <style>
        #example th, td {
            text-align: left;
            padding: 5px;
        }
        #example tr:hover {background-color: #a3aef6;}
    </style>
<?php $__env->stopSection(); ?>


<?php $__env->startSection('headers'); ?>
    <?php echo $__env->make('headers.inventory-header', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php $__env->stopSection(); ?>


<?php $__env->startSection('heading', 'Manage Inventory'); ?>
<?php $__env->startSection('searches'); ?>
    <?php if(auth()->user()->admin == 1): ?>
        
    <?php endif; ?>

    <button onclick="addNewInventoryItems()" type="button" class="navButtons" style="margin-bottom: 0;background-color: #42f174; float: right; color: black"><i
            class="fa fa-cart-plus"></i> &nbsp;Add New Inventory Item(s)
    </button>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
    <div class="tab">
        <button class="tablinks" onclick="openCity(event, 'active')" id="defaultOpen">Manage Company Inventory</button>
    </div>

    <div class="tabcontent" id="active">
        <span onclick="this.parentElement.style.display='none'" class="topright" style="margin-left: 20px">&times</span>
        <h3 class="error">
            Available Inventory Items
        </h3>
        <table id="example" class="display" style="width:100%; border-collapse: collapse">
            <thead>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Item Name</th>
                <th>Buying Price</th>
                <th>Minimum Selling Price</th>
                <th>Maximum Selling Price</th>
                <th>Date Created</th>
                <th>Created By</th>
                <th>Current Stock</th>
                <th>Stock Reminder Level</th>
                <th></th>
                <?php if(auth()->user()->admin == 1): ?>
                    <th></th>
                <?php endif; ?>

            </tr>
            </thead>
            <tbody>
            <?php $__currentLoopData = $list; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $row): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <tr>
                    <td class="ar"></td>
                    <td><a href="<?php echo e(url('inventory/'.$row->id)); ?>" target="_blank" class="inner" style="color: red; font-weight: bold"><?php echo e($row->name); ?></a></td>
                    <td><?php echo number_format($row->buying_price, 2); ?></td>
                    <td><b class="error"><?php echo number_format($row->min_selling_price, 2); ?></b></td>
                    <td><b class="success"><?php echo number_format($row->max_selling_price, 2); ?></b></td>
                    <td><?php echo date('d/m/Y H:i:s a', strtotime($row->created_at)); ?></td>
                    <td>
                        <?php if($row->user): ?>
                            <?php echo $row->user->firstName." ".$row->user->lastName; ?>

                        <?php else: ?>
                            -
                        <?php endif; ?>
                    </td>
                    <td><b><?php echo $row->itemInventory ? $row->itemInventory->sum('quantity') : 0; ?></b></td>
                    <td><?php echo Form::number('reminder-level', '', ['class'=>'input-text-height', 'onchange'=>'updateReminderLevel('.$row->id.')', 'style'=>'width: 80px; border:1px solid green']); ?></td>
                    <td>
                        <i onclick="editItem('<?php echo e($row->id); ?>')" class="fa fa-pencil error" style="cursor: pointer; font-size: 14px" title="Edit Item Details"></i>
                    </td>
                    <?php if(auth()->user()->admin == 1): ?>
                        <td>
                            <i onclick="updateItemStock('<?php echo e($row->id); ?>')" class="fa fa-pencil success" style="cursor: pointer; font-size: 14px" title="Edit Item Details"></i>
                        </td>
                    <?php endif; ?>
                </tr>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </tbody>
            <tfoot>
            <tr style="background-color: #a1b5d3">
                <th>No</th>
                <th>Item Name</th>
                <th>Buying Price</th>
                <th>Minimum Selling Price</th>
                <th>Maximum Selling Price</th>
                <th>Date Created</th>
                <th>Created By</th>
                <th>Current Stock</th>
                <th>Stock Reminder Level</th>
                <th></th>
                <?php if(auth()->user()->admin == 1): ?>
                    <th></th>
                <?php endif; ?>
            </tr>
            </tfoot>
        </table>
    </div>
<?php $__env->stopSection(); ?>



<?php $__env->startSection('modal_heading'); ?>
    <h1>Manage Company Inventory</h1>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('modal_title'); ?>
    <h3><u>Dashboard Functionalities</u></h3>
    <b>
        <ol>
            <li>View all Available Stock and Balances</li>
            <li>Add New Stock Items to the Company</li>
            <li>Export Inventory Listing</li>
        </ol>
    </b>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/zedarroot/public_html/edwulp/resources/views/inventory/manage-inventory.blade.php ENDPATH**/ ?>