 <?php $__env->startSection('content'); ?>
<style>
    .formName{
        text-align: left !important;
    }
    .select2-dropdown.select2-dropdown--below{
        z-index: 99999999;
    }
</style>
    <?php Fun::lang()?>
    <div class="modal fade bs-modal-lg" id="show-form-add-product-origin-stock" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-dm modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                    <h3>Add product to stock</h3>
                </div>
                <br />
                
            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
    <div class="title-page">
        <div class="page-bar">
            <ul class="page-breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
                <li><a href="#">Construction</a></li>
                <li><a href="#">Manage Construction Stock</a></li>
                <li><a href="#">Assign To Warehouse</a></li>
            </ul>
        </div>
    </div>
    <div class="page">
        <div class="top-content">
            <div class="row">
                <div class="col-sm-12 col-xs-12">
                    <div class="title-page">
                        <div class="pull-left">
                            <h3>Assign To Warehouse</h3>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
        <div class="box-body table-responsive">

            <table id="datatable_add_product" class="table table-striped table-bordered table-hover nowrap">
                <thead>
                <tr>
                    <th>NO</th>
                    <th>Date</th>
                    <th>Request By</th>
                    <th>Project</th>
                    <th>Supplier</th>
                    <th>Phone</th>
                    <th>Stock Position</th>
                    <th>Status</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>

                </tbody>
            </table>

        </div>
    </div>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('js'); ?>
    <script type="text/javascript">
            var warehouses = <?php echo json_encode($warehouses); ?>;
            let table = $('#datatable_add_product').DataTable({
                ordering: false,
                "serverSide": true,
                "scrollY":"600px",
                "scrollCollapse": true,
                "scrollX": true,
                "lengthMenu": [
                    [20, 25, 50, 100, 500],
                    [20, 25, 50, 100, 500]
                ],
                ajax: "<?php echo e(route('construction-purchase.verified_product_list')); ?>",
                columns: [
                    {data: 'DT_RowIndex'},
                    {data: 'date'},
                    {data: 'request_by'},
                    {data: 'project_name'},
                    {data: 'supplier_id'},
                    {data: 'phone_number'},
                    {data: 'stock_position'},
                    {data: 'status'},
                    {data: 'action'},
                ],
                "initComplete": function (settings, json) {
                    $('#main-spinner').hide();
                }
            });

            $.fn.dataTable.ext.errMode = 'throw';


       /* function display_form_add_product(purchase_id)
        {
            $("#show-form-add-product-origin-stock").modal('show');
            var date = "<?php echo e(date('d-m-Y')); ?>";
            var user = "<?php echo e(Auth::user()->user_firstname.' '.Auth::user()->user_lastname); ?>";
            var opt = `<option value=''>Please Select</option>`;
            $.each(warehouses,function(index,value){
                opt += `<option value = "${value.id}">${value.name}</option>`;
            });

            $("#warehouse-id").html(opt);
            $("#purchase-id").val(purchase_id);
        }
        */
        function display_form_add_product(purchase_id)
        {
            var date = "<?php echo e(date('d-m-Y')); ?>";
            var user = "<?php echo e(Auth::user()->user_firstname.' '.Auth::user()->user_lastname); ?>";
            var opt = `<option value=''>Please Select</option>`;
            $.each(warehouses,function(index,value){
                opt += `<option value = "${value.id}">${value.name}</option>`;
            });

            $.confirm({
                closeIcon: true,
                boxWidth: '1000px',
                boxHeight: '1500px',
                useBootstrap: false,
                type: 'green    ',
                title: 'Add product to warehouse',
                content: '',
                buttons: {
                    cancel: {
                        text: 'cancel',
                        btnClass: 'btn-red',
                        action: function () {
                        
                        }
                    }, 
                    submit: {
                        text: 'submit',
                        btnClass: 'btn-blue',
                        action: function () {
                            var status_submit = 1;
                            var warehouse = parseFloat($("#warehouse-id").val());
                            if(warehouse == null || warehouse == "" || isNaN(warehouse)){
                                $.alert("Please Select Warehouse");
                                status_submit = 0;
                                return false;
                            }
                            var row = this.$content.find('.asisgn-to-warehouse').find('tr');
                            $(row).each(function(index,value){
                                let balance_qty = parseFloat($(this).find("td:eq(3)").text());
                                let qty_add = parseFloat($(this).find("td:eq(4)").find('input').val());
                                
                                if(qty_add > balance_qty){
                                    $.alert("Qty is invalid !");
                                    status_submit = 0;
                                    return false;
                                }
                            });
                            if(status_submit == 1){ 
                                $("#transfer-to-warehouse").submit();
                            }
                        }
                    }
                },
                onContentReady: function () {
                    var self = this;
                    var url = "<?php echo e(route('get_qty_add_to_warehouse')); ?>/"+purchase_id;
                    $.ajax({
                        type: 'GET',
                        url :url,
                        async: false,
                        success: function(data){
                            self.setContentAppend(`
                            <form class="form-horizontal" id="transfer-to-warehouse" role="form" action="<?php echo e(route('construction.add_product_stock_store')); ?>" method="post">
                                <?php echo csrf_field(); ?>
                                <input type="hidden" name = "purchase_id" value="${purchase_id}">
                                <div class="fontChange">
                                    <div class="row" style="padding:15px;"> 
                                        <div class="form-group col-sm-6 col-xs-6">
                                            <div class="form-group">
                                                <label class="col-sm-2 required control-label">Date</label>
                                                <div class="col-sm-9">
                                                    <input type="text" name = "date" placeholder="Date" value="<?php echo e(date('d-m-Y')); ?>" class="date-work form-control" id="date_picker" required />
                                                </div>
                                            </div>  
                                        </div>
                                         <div class="form-group col-sm-6 col-xs-6"> 
                                            <div class="form-group">
                                                <label class="col-sm-4 required control-label">Warehouse</label>
                                                <div class="col-sm-6">
                                                    <select name = "warehouse_id" class="form-control select2" id="warehouse-id" required>
                                                        ${opt}
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row"> 
                                        <div class="form-group col-xs-12">
                                            <table class="table table-striped table-bordered" id="table_boq" width="100%">
                                                <thead>
                                                    <tr>
                                                        <th width="5%">No</th>
                                                        <th width="30%">
                                                            <label class="">
                                                                Product Name 
                                                            </label>
                                                        </th>
                                                        <th width="15%">
                                                            <label class="">
                                                                Unit
                                                            </label>
                                                        </th>
                                                        <th width="15%">
                                                            <label class="">
                                                                Qty Balance
                                                            </label>
                                                        </th>
                                                        <th width="15%">
                                                            <label class="">
                                                                Qty
                                                            </label>
                                                        </th>
                                                    </tr>
                                                </thead>
                                                <tbody class="asisgn-to-warehouse">
                                                    ${data}
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            `,);
                            self.$content.find('.asisgn-to-warehouse').append(data.boq);
                            $(".select2").select2();
                        }
                    });
                },
                columnClass: 'medium',
            });
        }
        function save_add_product()
        {
            return ;
            var warehouse_id = $("#warehouse-id").val();
            var purchase_id = $("#purchase-id").val();
            var date = $(".date-work").val();
            $("#main-spinner").show();
            $.ajax({
                url:"<?php echo e(route('construction.add_product_stock_store')); ?>",
                type:"POST",
                dataType:"JSON",
                data:{
                    "_token":"<?php echo e(csrf_token()); ?>",
                    purchase_id:purchase_id,
                    date : date,
                    warehouse_id :warehouse_id
                },
                success:function(data){
                    $("#datatable_add_product").DataTable().draw(true);
                    $("#show-form-add-product-origin-stock").modal("hide");
                    $("#main-spinner").hide();
                }
            });
        }
        $(document).on("focus","#date_picker",function(){
            $(this).datepicker({
                dateFormat: 'dd-mm-yy',
                changeMonth: true
            });
        });
        function show_add_warhouse(purchase_id){
            $.confirm({
                closeIcon: true,
                boxWidth: '1000px',
                boxHeight: '1500px',
                useBootstrap: false,
                type: 'green    ',
                title: 'Show added product to warehouse',
                content: '',
                buttons: {
                    cancel: {
                        text: 'cancel',
                        btnClass: 'btn-red',
                        action: function () {
                        
                        }
                    }, 
                },
                onContentReady: function () {
                    var self = this;
                    var url = "<?php echo e(route('get_qty_add_to_warehouse_show')); ?>/"+purchase_id;
                    $.ajax({
                        type: 'GET',
                        url :url,
                        async: false,
                        success: function(data){
                            self.setContentAppend(`
                                <div class="fontChange">
                                    <div class="row"> 
                                        <div class="form-group col-xs-12">
                                            <table class="table table-striped table-bordered" id="table_boq" width="100%">
                                                <thead>
                                                    <tr>
                                                        <th width="5%">No</th>
                                                        <th width="30%">
                                                            <label class="">
                                                                Product Name 
                                                            </label>
                                                        </th>
                                                        <th width="15%">
                                                            <label class="">
                                                                Unit
                                                            </label>
                                                        </th>
                                                        <th width="15%">
                                                            <label class="">
                                                                Qty Balance
                                                            </label>
                                                        </th>
                                                        <th width="15%">
                                                            <label class="">
                                                                Added Qty
                                                            </label>
                                                        </th>
                                                    </tr>
                                                </thead>
                                                <tbody class="asisgn-to-warehouse">
                                                    ${data}
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            `,);
                            self.$content.find('.asisgn-to-warehouse').append(data.boq);
                            $(".select2").select2();
                        }
                    });
                },
                columnClass: 'medium',
            });
        }
        
        
    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>