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

use Illuminate\Support\Facades\Session;

Fun::lang() ?>
<?php $total = 0 ?>
<div class="page">
    <div class="ajax-alert">
        <?php if($message = Session::get('success')): ?>
        <div class="alert-message alert-success">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <i class="fa fa-check"></i>
            <h3>Success</h3>
            <p><?php echo e($message); ?></p>
        </div>
        <?php endif; ?>
        <?php if($message = Session::get('danger')): ?>
        <div class="alert-message alert-danger">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <i class="fa fa-exclamation-triangle"></i>
            <h3>Errors</h3>
            <p><?php echo e($message); ?></p>
        </div>
        <?php endif; ?>
        <?php if($message = Session::get('warning')): ?>
        <div class="alert-message alert-warning">
            <button type="button" class="close" data-dismiss="alert">×</button>
            <i class="fa fa-exclamation-triangle"></i>
            <h3>Warning</h3>
            <p><?php echo e($message); ?></p>
        </div>
        <?php endif; ?>
    </div>
    <div class="top-content">
        <div class="row pos">
            <div class="col-sm-12 col-xs-12">
                <div class="pull-left">
                    <h3>VIEW - EDIT - APPROVE POS</h3>
                </div>
                <div class="pull-right">
                    <div class="button-pos">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="box-body">
        <div class="pos">
            <div class="row">
                <div class="col-sm-8">
                    <div class="pos-box">
                        <div class="row" id="search-item">
                            <div class="col-sm-12">
                                <div class="col-sm-6">
                                    <div class="col-sm-4">
                                        <label for="">Item Name</label>
                                    </div>
                                    <div class="col-sm-8">
                                        <input type="text" class="form-control" name="item" id="item_filter">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <hr>
                        <div id="tag_container">
                            <?php echo $__env->make('administrator.external.deposit.pre-sult', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
                        </div>
                        <div id="form-pay" style="display: none;">
                            <?php echo Form::model($saleRequest, ['method' => 'PATCH','route' => ['deposit.approve_update_info',$saleRequest->id],'enctype'=>'multipart/form-data']); ?>

                            <div class="total">
                                <strong class="green" style="display:none" id="total_amount"></strong>
                            </div>
                            <div class="form-group">
                                <div class="new-customer-button">
                                    <button class="pull-right" type="button" id="add" class="btn btn-sm btn btn-warning" style="margin-bottom:5px">
                                        <span> New Customer </span>
                                        <i class="fa fa-plus"></i>
                                    </button>
                                </div>
                                <label class="required">Select Customer</label>
                                <?php echo Form::select('app_client_id', $clients, null, ['class' => 'form-control select2','id'=>'client_id']); ?>

                            </div>
                            <div class="form-group">
                                <label for="required">Sale By</label>
                                <?php echo Form::select('sale_type_by', ['1'=>'Employee','2'=>'Agency'], null, ['class' => 'form-control select2','id'=>'sale-type-by']); ?>

                            </div>
                            <?php if($sale->sale_type_by==1): ?>

                            <div id="append-sale">
                                <div class="form-group">
                                    <label for="required">Select Employee(Sale)</label>
                                    <?php echo Form::select('employees[]', $employees, $employees_sale, ['class' => 'form-control select2','required']); ?>

                                </div>
                            </div>
                            <?php else: ?>

                            <div id="append-sale">
                                <div class="form-group">
                                    <label for="required">Select Agency</label>
                                    <?php echo Form::select('app_agency_id', $agencies, null, ['class' => 'form-control select2','id'=>'agency_id']); ?>

                                </div>
                                <div class="form-group">
                                    <label for="required">Close Sale</label>
                                    <?php echo Form::select('close_sale_id', $closesale, null, ['class' => 'form-control select2','id'=>'close_sale_id']); ?>

                                </div>
                            </div>

                            <?php endif; ?>
                            
                            <div class="form-group">
                                <label class="required">Date Contract</label>
                                <input type="text" name="date_contract" class="form-control number-only date_picker" placeholder="Date Contract *" value="<?php echo e(date('d-m-Y',strtotime($saleRequest->app_date_contract))); ?>" required />
                            </div>
                            <div class="form-group">
                                <label class="required">Discount Amount</label>
                                <input type="text" name="sale_discount_amount" class="form-control number-only" placeholder="Discount Amount ($)*" value="<?php echo e($saleRequest->sale_discount); ?>" />
                            </div>
                            <div class="form-group">
                                <label class="required">Deposit Amount</label>
                                <input type="text" name="sale_receive" class="form-control number-only sale_receive" placeholder="Deposit Amount ($)*" value="<?php echo e($saleRequest->app_booking_amount); ?>" readonly />
                                <input type="hidden" name="sale_id" value="<?php echo e($sale->id); ?>">
                                <input type="hidden" name="app_id" value="<?php echo e($saleRequest->id); ?>">
                            </div>
                            <div class="form-group">
                                <label for="required">Promotion</label>
                                <?php echo Form::select('promotion[]', $promotion, !empty($promotion_sale)?$promotion_sale->pluck('promotion_id')->toArray():null, ['class' => 'form-control select2','multiple']); ?>

                            </div>
                            <div class="form-group">
                                <label for="required">Payment Policy</label>
                                <?php echo Form::select('payment_policy[]', $payment_policy, !empty($payment_policy_sale)?$payment_policy_sale->pluck('payment_policy_id')->toArray():null, ['class' => 'form-control select2','multiple']); ?>

                            </div>
                            <div class="form-group">
                                <label class="">Remark</label>
                                <textarea name="deposit_remark" id="" cols="30" rows="5" class="form-control"><?php echo e($saleRequest->deposit_remark); ?></textarea>
                            </div>
                            <div style="display: none;">
                                <?php if(session('cart_edit')): ?> <?php $__currentLoopData = session('cart_edit'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $item_cart): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <span>
                                    <input type="hidden" name="saledetail_itemid[]" value="<?php echo e($item_cart['item_id']); ?>"><br>
                                    <input type="hidden" name="sale_item_code[]" value="<?php echo e($item_cart['item_name']); ?>"><br>
                                    <input type="hidden" name="saledetail_price[]" value="<?php echo e($item_cart['item_price']); ?>"><br>
                                    <input type="hidden" name="saledetail_cost[]" value="<?php echo e($item_cart['item_cost']); ?>"><br>
                                    <input type="hidden" name="project_type[]" value="1"><br>
                                    <input type="hidden" name="total_amount" class="form-control">
                                </span>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?> <?php endif; ?>
                            </div>
                            <div class="form-group">
                                <div class="button-pos">
                                    <button type="submit" class="btn button_success pull-right">
                                        <i class="fa fa-credit-card-alt" aria-hidden="true"></i> Approve</button>
                                    <button type="button" onClick="javascript:history.go(-1)" class="button_primary pull-right">
                                        <span class=" fa fa-arrow-circle-left"></span> Back
                                    </button>
                                </div>
                            </div>
                            <?php echo Form::close(); ?>

                        </div>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="pos-box" id="session-table">
                        <table id="cart" class="table table-hover table-condensed">
                            <thead>
                                <tr>
                                    <th width="10%">#</th>
                                    <th width="50%">Item Name</th>
                                    <th width="5%">Quantity</th>
                                    <th width="30%">Item Pirce</th>
                                    <th width="5%">Action</th>
                                </tr>
                            </thead>
                            <tbody id="tbody">
                                <?php $i = 0; ?>
                                <?php if(session('cart_edit')): ?> <?php $__currentLoopData = session('cart_edit'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $id => $item_cart): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php $i++ ?>
                                <?php $total += $item_cart['item_price'] * $item_cart['quantity'] ?>
                                <tr>
                                    <td data-th="#">
                                        <?php echo e($i); ?>

                                    </td>
                                    <td data-th="Product">
                                        <div class="row">
                                            <div class="col-sm-12">
                                                <?php echo e($item_cart['item_name']); ?>

                                            </div>
                                        </div>
                                    </td>
                                    <td data-th="Quantity">
                                        <?php echo e($item_cart['quantity']); ?>

                                    </td>
                                    <td data-th="Price" class="text-right">$ <?php echo e(number_format($item_cart['item_price'],2)); ?></td>
                                    <td class="actions" data-th="">
                                        <button class="btn btn-danger btn-xs remove-from-cart" data-id="<?php echo e($id); ?>"><i class="fa fa-trash-o"></i></button>
                                    </td>
                                </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?> <?php endif; ?>
                            </tbody>
                            <tfoot id="pos-footer">
                                <tr>
                                    <td colspan="3" class="text-right">Total :</td>
                                    <td id="totalPrice" class=" text-right">$ <?php echo e(number_format($total,2)); ?></td>
                                    <td></td>
                                </tr>
                            </tfoot>
                        </table>
                        <!-- <div class="sum-footer">
                            <strong class="green pull-right">$ <?php echo e($total); ?></strong>
                        </div> -->
                        <input type="hidden" value="<?php echo e($total); ?>" name="total" class="total">

                        <div class="col-sm-12 padding-0">
                            <div class="button-pos">
                                <button onclick="fun_pay()" class="btn button_primary"><i class="fa fa-share icon-flipped"></i> Back</button>
                                <button onclick="fun_back()" class="btn button_success"><i class="fa fa-shopping-cart"></i> Add more</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

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

<div class="modal fade bs-modal-lg" id="addnew" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h3 class="modal-title">New Client</h3>
            </div>
            <br />
            <form action="<?php echo e(route('add-new-client')); ?>" id="addForm">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-6 col-sm-6 col-md-6">
                            <div class="form-group">
                                <strong>First Name</strong>
                                <input type="text" name="first_name" class="form-control" placeholder="First Name" required="">
                            </div>
                            <div class="form-group">
                                <strong>Last Name</strong>
                                <input type="text" name="last_name" class="form-control" placeholder="First Name" required="">
                            </div>
                            <div class="form-group">
                                <strong>Gender</strong>
                                <select name="client_gender" id="" class="form-control select2">
                                    <option value="1">Male</option>
                                    <option value="2">Female</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <strong>Phone Number</strong>
                                <input type="text" name="client_phone1" class="form-control" placeholder="Phone Number" required="">
                            </div>
                            <div class="form-group">
                                <strong>Client Address</strong>
                                <input type="text" name="client_address" class="form-control" placeholder="Client Address" required="">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" data-dismiss="modal" class="btn btn-danger"><i class="fa fa-close"></i> No</button>
                    <button type="submit" class="btn btn-success" data-toggle="modal"><i class="fa fa-save"></i> OK</button>
                </div>
            </form>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<div class="overlay">
    <div id="loading-img"></div>
</div>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('js'); ?>
<script type="text/javascript">
    $(document).on("change","#sale-type-by",function()
    {
        $("#append-sale").empty();
        if($(this).val()==1)
        {
            $("#append-sale").append(`<div class="form-group">
                                    <label for="required">Select Employee(Sale)</label>
                                    <?php echo Form::select('employees[]', $employees, Auth::user()->id, ['class' => 'form-control select2','required']); ?>

                                </div>`);
             $(".select2").select2();
        }
        else{
            $("#append-sale").append(`
                            <div class="form-group">
                                <label for="required">Select Agency</label>
                                <?php echo Form::select('app_agency_id', $agencies, null, ['class' => 'form-control select2','id'=>'agency_id']); ?>

                            </div>
                            <div class="form-group">
                                <label for="required">Close Sale</label>
                                <?php echo Form::select('close_sale_id', $closesale, null, ['class' => 'form-control select2','id'=>'close_sale_id']); ?>

                            </div>`);
            $(".select2").select2();
        }
    });
    $(document).ready(function() {
        $("#search-item").hide();
        $("#tag_container").hide();
        $("#form-pay").show();
    });
    $(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            } else {
                getData(page);
            }
        }
    });
    $(window).on('hashchange', function() {
        if (window.location.hash) {
            var page = window.location.hash.replace('#', '');
            if (page == Number.NaN || page <= 0) {
                return false;
            } else {
                getData(page);
            }
        }
    });
    $(document).ready(function() {
        $(document).on('click', '.pagination a', function(event) {
            event.preventDefault();
            $('li').removeClass('active');
            $(this).parent('li').addClass('active');
            var myurl = $(this).attr('href');
            var page = $(this).attr('href').split('page=')[1];
            getData(page);
        });
    });

    function getData(page) {
        $.ajax({
            url: '?page=' + page,
            type: "get",
            datatype: "html"
        }).done(function(data) {
            $("#tag_container").empty().html(data);
            location.hash = page;
        }).fail(function(jqXHR, ajaxOptions, thrownError) {
            alert('No response from server');
        });
    }

    $(document).ready(function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        $('#add').click(function() {
            $('#addnew').modal('show');
            $('#addForm')[0].reset();
        });
        $('#addForm').on('submit', function(e) {
            e.preventDefault();
            var form = $(this).serialize();
            var url = $(this).attr('action');
            $.ajax({
                type: 'POST',
                url: url,
                data: form,
                dataType: 'json',
                success: function() {
                    $('#addnew').modal('hide');
                    get_new_client();
                }
            });
        });
    });

    function get_new_client() {
        $('#client_id').empty();
        $.ajax({
            type: 'GET',
            url: "<?php echo e(route('pos-get-new-client')); ?>",
            dataType: 'json',
            success: function(data) {
                $.each(data, function(index, element) {
                    $("#client_id").append('<option value="' + element.id + '">' + element.client_firstname+' '+element.client_lastname + '</option>');
                });
            }
        });
    }

    function fun_pay() {
        $("#tag_container").hide();
        $("#search-item").hide();
        $("#form-pay").show();

        var total = $("input[name=total]").val();
        $("input[name=total_amount]").val(total);
        document.getElementById('total_amount').innerHTML = total;
    }

    function fun_back() {
        $("#tag_container").show();
        $("#search-item").show();
        $("#form-pay").hide();
    }
    $(document).on('click', '.remove-from-cart', function(e) {
        e.preventDefault();
        var ele = $(this);
        var id = $(this).data('id');
        var route = "<?php echo e(route('remove-from-cart-edit',':id')); ?>";
        route = route.replace(':id', id);
        $.confirm({
            title: 'Confirm!',
            content: 'This Item will be removed!',
            buttons: {
                cancel: function() {

                },
                confirm: function() {
                    $('.overlay').show();
                    $.ajax({
                        url: route,
                        method: "GET",
                        data: {
                            _token: '<?php echo e(csrf_token()); ?>',
                            id: ele.attr("data-id")
                        },
                        success: function(response) {
                            var page = window.location.hash.replace('#', '');
                            if (page == Number.NaN || page <= 0) {
                                page = 1;
                            }
                            getData(page);
                            get_cart();
                            $('.overlay').hide();
                        }
                    });
                }
            }
        });
    });
    $(document).on('change', '#project_id', function() {
        var project_id = $(this).find(":selected").val();
        var route = "<?php echo e(route('pos-filter')); ?>";
        var div = "";
        $('#item_list').empty();
        $.ajax({
            type: "POST",
            url: route,
            dataType: 'text',
            data: {
                project_id: project_id,
                _token: '<?php echo e(csrf_token()); ?>',
            },
            success: function(response) {
                $('#item_list').html(response);
            },
            error: function() {
                alert('Error');
            }
        });
    });
    $(document).on('click', '.btn-add-to-cart', function() {
        var id = $(this).data('id');
        var route = "<?php echo e(route('add-to-cart-edit',':id')); ?>";
        route = route.replace(":id", id);
        $('.overlay').show();
        $.ajax({
            type: 'GET',
            url: route,
            dataType: 'json',
            data: {
                'id': id
            },
            success: function(data) {
                if (data.success == false) {
                    $.alert("Item was Already Added!");
                } else if (data.success == true) {
                    get_cart();
                    var page = window.location.hash.replace('#', '');
                    if (page == Number.NaN || page <= 0) {
                        page = 1;
                    }
                    getData(page);
                } else {
                    $.alert('Not found');
                }
                $('.overlay').hide();
            }
        });
    });
    $(document).on('keyup', '#item_filter', function() {
        var item_name = $(this).val();
        var project_id = $('#project_id').find(":selected").val();
        var route = "<?php echo e(route('pos-filter-by-name')); ?>";
        if (item_name != "") {
            $.ajax({
                type: "POST",
                url: route,
                dataType: 'text',
                data: {
                    item_name: item_name,
                    project_id: project_id,
                    _token: '<?php echo e(csrf_token()); ?>',
                },
                success: function(response) {
                    $('#tag_container').html(response);
                },
                error: function() {
                    alert('Error');
                }
            });
        }
    });

    function get_cart() {
        var route = "<?php echo e(route('get-session-cart-edit')); ?>";
        var table = "";
        var i = 1;
        var totalPrice = 0;
        $.ajax({
            type: 'GET',
            url: route,
            dataType: 'json',
            data: {
                _token: '<?php echo e(csrf_token()); ?>',
            },
            success: function(response) {
                var data = response.data;
                $('#tbody').empty();
                $.each(data, function(index, val) {
                    var amount = val.quantity*val.item_price;
                    totalPrice+=amount;
                    table += `<tr>
                                <td>${(i++)}</td>
                                <td>${val.item_name}</td>
                                <td>${val.quantity}</td>
                                <td class="text-right">$ ${parseFloat(val.item_price).toFixed(2)}</td>
                                <td><span data-id="${val.item_id}" class="btn btn-xs btn-danger fa fa-trash remove-from-cart"></span></td>
                            </tr>`;
                });
                $('#totalPrice').text('$ '+parseFloat(totalPrice).toFixed(2));
                $('#tbody').append(table);
            }
        });
    }
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>