<?php $__env->startSection('title','Production Costing'); ?>

<?php $__env->startSection('content'); ?>
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>Production Costing
        <small>Manage Recipe Costing</small>
    </h1>
</section>

<!-- Main content -->
<section class="content">
<div class="row">
    <div class="col-md-12">
    <?php $__env->startComponent('components.filters', ['title' => __('report.filters')]); ?> 
        
        <div class="col-md-6"> 
            <div class="form-group">
                <?php echo Form::label('location_id',  'Business Location:'); ?>

                <?php echo Form::select('location_id', $business_locations, null, ['class' => 'form-control select2', 'style' => 'width:100%', 'id' => 'location_id', 'placeholder' => __('lang_v1.all')]);; ?>

            </div>
        </div>  
        <div class="col-md-6">
            <div class="form-group">
                <?php echo Form::label('expense_date_range', __('report.date_range') . ':'); ?>

                <?php echo Form::text('expense_date_range', null, ['placeholder' => __('lang_v1.select_a_date_range'), 'class' => 'form-control', 'id' => 'expense_date_range', 'readonly']);; ?>

            </div>
        </div> 
        
    <?php echo $__env->renderComponent(); ?>
    </div>
</div>
    <?php $__env->startComponent('components.widget', ['class' => 'box-primary', 'title' => 'Existing Productions']); ?>
            <?php $__env->slot('tool'); ?>
                <div class="box-tools">
                     <button type="button" class="btn btn-primary btn-modal pull-right" 
                         data-href="<?php echo e(action('ProductionCostingController@createProduction'), false); ?>"
                         data-container="#account_type_modal">
                         <i class="fa fa-plus"></i> <?php echo app('translator')->getFromJson( 'messages.add' ); ?></button>
                </div>
            <?php $__env->endSlot(); ?> 

            <div class="table-responsive">
                <table class="table table-bordered table-striped" id="production_table">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Location</th>
                            <th>Production Details</th>
                            <th>Added By</th>
                            <th><?php echo app('translator')->getFromJson('lang_v1.added_on'); ?></th> 
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <td></td> 
                            <td></td> 
                            <td></td>
                            <td></td>
                            <td></td> 
                        </tr>
                    </tfoot>
                </table>
            </div> 
    <?php echo $__env->renderComponent(); ?>

    <div class="modal fade account_type_modal" tabindex="-1" role="dialog" 
        aria-labelledby="gridSystemModalLabel" id="account_type_modal">
    </div>
</section>
<!-- /.content -->

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

<?php $__env->startSection('javascript'); ?>
<script>
    $(document).ready(function(){			
	var columnproduction = [
		{ data: 'production_date', name: 'production_date' },
		{ data: 'location_name', name: 'location_name',searchable:false,orderable:false, },
		{ data: 'finished_product', name: 'finished_product',searchable:false,orderable:false, },
		{ data: 'username', name: 'username',searchable:false,orderable:false, },
		{ data: 'created_at', name: 'created_at' , searchable: false}, 
	]; 
		
    var production_table = $('#production_table').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: '/recipe-costing/production',
            data: function(d) {
                d.location_id = $('select#location_id').val();
                d.start_date = $('input#expense_date_range')
                    .data('daterangepicker')
                    .startDate.format('YYYY-MM-DD');
                d.end_date = $('input#expense_date_range')
                    .data('daterangepicker')
                    .endDate.format('YYYY-MM-DD');
            },
        },
        columns: columnproduction,
        fnDrawCallback: function(oSettings) { 
        },
    });
	
    $('#expense_date_range, #location_id').change(function() {  
		production_table.ajax.reload();
    });
		production_table.ajax.reload();
		
    $(document).on('change', 'select#main_category', function() {
		var main_cat=$(this).val();
		var url="<?php echo e(action('ProductionCostingController@getProductListProduction'), false); ?>";
                $.ajax({
                    type: "GET",
                    url: url,
                    data: {'main_cat': main_cat},
                    dataType: "json",
                    beforeSend: function () {
						$('select#finished_product').html("");
                    },
                    success: function (data) {
						var div_data = '<option value="">Select Product</option>';
                        $.each(data, function (i, obj)
                        {
                            div_data += "<option value=" + obj.id + ">" + obj.name + "</option>";
                        });
                        $('select#finished_product').append(div_data);
                    }
                });
		
    }); 
    $(document).on('change', 'select#finished_product', function() {
		//get specific details
		var product=$(this).val();
		var url="<?php echo e(action('ProductionCostingController@getProductSetupDetails'), false); ?>";
                $.ajax({
                    type: "GET",
                    url: url,
                    data: {'product': product},
                    dataType: "json",
                    beforeSend: function () {
						$('input#production_mst_id').val('');
						$('input#production_qty').val(0);
						$('input#expected_production').val(0);
                    },
                    success: function (data) { 
							$('input#production_mst_id').val(data.id);
							$('input#production_qty').val(data.production_qty);
							$('input#expected_production').val(data.production_qty);
                    }
                });
		
    }); 
	
    $(document).on('submit', 'form#decompose_setup', function(e){
        e.preventDefault();
        var data = $(this).serialize();
        $.ajax({
          method: "POST",
          url: $(this).attr("action"),
          dataType: "json",
          data: data,
          success: function(result){
            if(result.success == 1){
              $('div.account_type_modal').modal('hide');
              toastr.success(result.msg);
			  location.reload();
            } else {
              toastr.error(result.msg);
            }
          }
        });
    }); 

        $(document).on('click', 'button.close_account', function(){
            swal({
                title: LANG.sure,
                icon: "warning",
                buttons: true,
                dangerMode: true,
            }).then((willDelete)=>{
                if(willDelete){
                     var url = $(this).data('url');

                     $.ajax({
                         method: "get",
                         url: url,
                         dataType: "json",
                         success: function(result){
                             if(result.success == true){
                                toastr.success(result.msg);
                                capital_account_table.ajax.reload();
                                other_account_table.ajax.reload();
                             }else{
                                toastr.error(result.msg);
                            }

                        }
                    });
                }
            });
        });

        $(document).on('submit', 'form#edit_payment_account_form', function(e){
            e.preventDefault();
            var data = $(this).serialize();
            $.ajax({
                method: "POST",
                url: $(this).attr("action"),
                dataType: "json",
                data: data,
                success:function(result){
                    if(result.success == true){
                        $('div.account_model').modal('hide');
                        toastr.success(result.msg);
                        capital_account_table.ajax.reload();
                        other_account_table.ajax.reload();
                    }else{
                        toastr.error(result.msg);
                    }
                }
            });
        });

        $(document).on('submit', 'form#payment_account_form', function(e){
            e.preventDefault();
            var data = $(this).serialize();
            $.ajax({
                method: "post",
                url: $(this).attr("action"),
                dataType: "json",
                data: data,
                success:function(result){
                    if(result.success == true){
                        $('div.account_model').modal('hide');
                        toastr.success(result.msg);
                        capital_account_table.ajax.reload();
                        other_account_table.ajax.reload();
                    }else{
                        toastr.error(result.msg);
                    }
                }
            });
        });


    $('#account_status').change( function(){
        other_account_table.ajax.reload();
    });

    $(document).on('submit', 'form#deposit_form', function(e){
        e.preventDefault();
        var data = $(this).serialize();

        $.ajax({
          method: "POST",
          url: $(this).attr("action"),
          dataType: "json",
          data: data,
          success: function(result){
            if(result.success == true){
              $('div.view_modal').modal('hide');
              toastr.success(result.msg);
              capital_account_table.ajax.reload();
              other_account_table.ajax.reload();
            } else {
              toastr.error(result.msg);
            }
          }
        });
    });

    $('.account_model ').on('shown.bs.modal', function(e) {
        $('.account_model .select2').select2({ dropdownParent: $(this) })
    });

    $('.account_type_modal ').on('shown.bs.modal', function(e) {
        $('.account_type_modal .select2').select2({ dropdownParent: $(this) })
    });

    //Add products
	$(document).on('focus','#search_product',function(){
    if ($('#search_product').length > 0) {
        $('#search_product')
            .autocomplete({
                source: function(request, response) {
                    $.getJSON(
                        '/recipe-costing/get_products',
                        {term: request.term },
                        response
                    );
                },
                minLength: 2,
                response: function(event, ui) {
                    if (ui.content.length == 1) {
                        ui.item = ui.content[0];
                        $(this)
                            .data('ui-autocomplete')
                            ._trigger('select', 'autocompleteselect', ui);
                        $(this).autocomplete('close');
                    } else if (ui.content.length == 0) {
                        var term = $(this).data('ui-autocomplete').term;
                        swal({
                            title: LANG.no_products_found,
                            text: 'Search Afresh...Product Missing',
                            buttons: [LANG.cancel],
                        });
                    }
                },
                select: function(event, ui) {
                    $(this).val(null);
                    get_purchase_entry_row(ui.item.product_id, ui.item.variation_id);
                },
            })
            .autocomplete('instance')._renderItem = function(ul, item) {
            return $('<li>')
                .append('<div>' + item.text + '</div>')
                .appendTo(ul);
        };
    }
    });
   
   $(document).on('click', 'button.delete_account_type', function(){
        swal({
            title: LANG.sure,
            icon: "warning",
            buttons: true,
            dangerMode: true,
        }).then((willDelete)=>{
            if(willDelete){
                $(this).closest('form').submit();
            }
        });
    })

function get_purchase_entry_row(product_id, variation_id) {
    if (product_id) {
        var row_count = $('#product_row_count').val(); 
        $.ajax({
            method: 'POST',
            url: '/recipe-costing/get_purchase_entry_row',
            dataType: 'html',
            data: { 
                product_id: product_id, 
                row_count: row_count, 
                variation_id: variation_id 
            },
            success: function(result) {
                $(result)
                    .find('.purchase_quantity')
                    .each(function() {
                        row = $(this).closest('tr');
							$('#purchase_entry_table tbody').append(
								update_purchase_entry_row_values(row)
							);
                    });
                if ($(result).find('.purchase_quantity').length) {
                    $('#product_row_count').val(
                        $(result).find('.purchase_quantity').length + parseInt(row_count)
                    );
                }
            },
        });
    }
}


function update_purchase_entry_row_values(row) {
    if (typeof row != 'undefined') {
        return row;
    }
}
 
    $(document).on('click', '.remove_purchase_entry_row', function() {
        swal({
            title: LANG.sure,
            icon: 'warning',
            buttons: true,
            dangerMode: true,
        }).then(value => {
            if (value) {
                $(this)
                    .closest('tr')
                    .remove();
            }
        });
    });
  
$(document).on('change', '#finished_product', function() {
    if ($('#finished_product').val()) {
        $('#search_product').removeAttr('disabled');
        $('#search_product').focus();
    } else {
        $('#search_product').attr('disabled', true);
    }
});
   });
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\System\htdocs\bethosonlinee\resources\views/production_costing/index_production.blade.php ENDPATH**/ ?>