 <?php $__env->startSection('content'); ?>
<style>
    .table th {
            vertical-align: middle !important;
    }
    .table > thead > tr > th {
        border: 1px solid white;
        text-align: center;
    }
    .jconfirm .jconfirm-box div.jconfirm-content-pane .jconfirm-content div {
        color: rgb(0, 49, 79);
        font-weight: 500;
        padding-left: 5px;
        text-align: left;
    }
</style>
    <?php Fun::lang() ?>
    <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="#">Return To Stock</a></li>
                <li><a href="#">Create</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>Set To Ajust Stock</h3>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12 col-xs-12"> 

                <div class="form-wrapper">
                    <?php echo Form::open(array('route' => 'construction.return_stock_store','method'=>'POST', 'enctype'=>'multipart/form-data')); ?>

                    <?php echo csrf_field(); ?>
                    <div class="row">
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="required">Contractor</label>
                            <?php echo Form::select('contractor_id',$contractors, Auth::user()->id, array('class' => 'form-control select2','id'=>'contractor-id')); ?>

                        </div>
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="required">Project</label>
                            <?php echo Form::select('project_id',$projects, null, array('class' => 'form-control select2','id'=>'project-id')); ?>

                        </div>
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="required">Warehouse</label>
                            <?php echo Form::select('warehouse_id',[], null, array('class' => 'form-control select2','id'=>'warehouse-id')); ?>

                        </div>
                        <div class="form-group col-sm-6 col-xs-12">
                            <label class="">Date</label>
                            <?php echo Form::text('date', date('d-m-Y'), array('placeholder' => 'Date','class' => 'form-control date_picker','autocomplete'=>'off')); ?>

                        </div>

                        <div class="form-group col-sm-12 col-xs-12">
                            <table class="table table-striped table-bordered" width="100%">
                                <thead>
                                    <tr>
                                        <th rowspan="2">No</th>
                                        <th rowspan="2">
                                            <label class="required">
                                                Product Name
                                            </label>
                                        </th>
                                        <th colspan="2" style="text-align: center;">
                                            <label class="required">
                                               Place to use 
                                            </label>
                                        </th>
                                        <th rowspan="2">
                                            <label class="required">
                                                Qty
                                            </label>
                                        </th>
                                        <th rowspan="2" width="160px">
                                            <label class="required">
                                                Unit
                                            </label>
                                        </th>

                                        <th rowspan="2">
                                            <label>
                                               Other
                                            </label>
                                        </th>
                                        <th rowspan="2">
                                            <span class="pull-right button_primary btn-sm plus_ens fa fa-plus" onclick="add_row_product(this)"></span>
                                        </th>
                                    </tr>
                                    <tr>
                                        <th>
                                            Zone
                                        </th>
                                        <th>
                                            House Number
                                        </th>
                                    </tr>
                                </thead>
                                <tbody class="resultbody1">
                                    <tr>
                                        <td width="4%">
                                            <span>1</span>
                                        </td>
                                        <td width="12%">
                                            <select name="product_id[]" class="form-control select2" id="first-select-product" onchange="selected_product(this)">
                                            </select>
                                        </td>
                                        <td style="width: 200px !important;">
                                            <select name="place_to_use_zone[]" class="form-control select2" >
                                                <?php echo $zone; ?>

                                            </select>
                                        </td>
                                        <td style="width: 200px !important;">
                                            <select name="place_to_use_house_number[]" class="form-control select2" >
                                                <?php echo $number_house; ?>

                                            </select>
                                        </td>
                                        <td width="10%">
                                            <input type="text" class="form-control" name="qty[]" onchange="check_qty(this)">
                                        </td>
                                        <td width="160px">
                                            <select class="form-control select2" name="unit[]" onchange="select_unit(this)">

                                            </select>
                                            <input type="hidden" name = "unit_type[]">
                                        </td>
                                        <td>
                                            <?php echo Form::textarea('description[]', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')); ?>

                                        </td>
                                        <td><button class="btn btn-sm btn-danger" disabled>
                                            <span class="fa fa-trash"></span></button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>

                        <div class="col-sm-12 col-xs-12">
                            <div class="form-group">
                                <button type="submit" id="button-submit" style="display: none;" class="button_sm button_success pull-right" data-type="order"><span
                                    class="fa fa-chevron-circle-right"></span>
                                </button>
                                <button type="button" class="button_sm button_success pull-right" data-type="order" onclick="submit_button()"><span
                                            class="fa fa-chevron-circle-right"></span> save
                                </button>
                                <button type="button" onClick="javascript:history.go(-1)"
                                        class="button_sm button_warning pull-right">
                                    <span class=" fa fa-arrow-circle-left"></span> Back
                                </button>
                            </div>
                        </div>
                    </div>

                    <?php echo Form::close(); ?>

                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('js'); ?>
<script>
    var product_option = ``;var zone_option = "<?php echo $zone; ?>";var number_house_option = "<?php echo $number_house; ?>";
    function select_unit(obj){
        var type = $(obj).find("option:selected").data('type');
        $(obj).closest('td').find('input').val(type);
    }
    function check_qty_product()
    {
        var status_error = 0;
        return status_error;
    }
    function check_selection(){
        var row = $(".resultbody1").find('tr');
        var status_submit = 0;
        $(row).each(function(index,value){
            $(row).each(function(dex,val){
                if(index==dex){

                }else{
                    if($(value).find("td:eq(1)").find('select').val()==$(val).find("td:eq(1)").find('select').val()){
                        if($(value).find("td:eq(2)").find('select').val()==$(val).find("td:eq(2)").find('select').val()){
                            if($(value).find("td:eq(3)").find('select').val()==$(val).find("td:eq(3)").find('select').val()){
                                status_submit=1;
                            }
                        }
                    }
                }
                    
            });
        });
        return status_submit;
        
    }
    function submit_button(){
        if(check_selection()==1){
            $.alert({
                title: '',
                type: 'red ',
                content :"Your requesst is failed.Please it again before submit !",
                useBootstrap: false,
                boxWidth: '20%',
                theme: 'modern',
                buttons: {
                    close: {
                        text: 'close',
                        btnClass: 'button_warning',
                    }
                }
            });
        }else if(check_qty_product()==1){
            $.alert({
                title: '',
                type: 'red ',
                content :"Your requesst is failed (Qty < 0).Please it again before submit !",
                useBootstrap: false,
                boxWidth: '20%',
                theme: 'modern',
                buttons: {
                    close: {
                        text: 'close',
                        btnClass: 'button_warning',
                    }
                }
            });
        }
        else{
            $("#button-submit").click();
        }
    }
    function check_qty(obj)
    {
        
    }
    function check_selected_product(select_product,obj)
    {
        var row = $('tbody.resultbody1').find('tr');
        var check = 0;
        $(row).each(function  (index,value) {
            var product_check = $(this).find("td:eq(1)").find('select').val();
            
            if(select_product==product_check){
                check++;
            }
            if(check>=2){
                $.alert({
                    title: '',
                    type: 'red ',
                    content :'Product have been choosing. Please change selection !',
                    useBootstrap: false,
                    boxWidth: '20%',
                    theme: 'modern',
                    buttons: {
                        close: {
                            text: 'close',
                            btnClass: 'button_warning',
                        }
                    }
                });
                $(obj).html(product_option);
            }
        });
    }

    $(document).on("change","#warehouse-id",function(){
        var route = "<?php echo e(route('construction.get_product_stock_out',':id')); ?>";
        route = route.replace(':id',$(this).val());
        $("#main-spinner").show();
        $.ajax({
            url:route,
            type:"GET",
            dataType:"JSON",
            success:function(data)
            {
                product_option = `<option value=''>Please Select</option>`;
                $.each(data,function(index,value)
                {
                    product_option +=`<option value='${value.product_id}' data-unit-name = '${value.unit_name}'>${value.product_name}</option>`;
                });
                $("#first-select-product").html(product_option);
            },
            complete:function()
            {
                $("#main-spinner").hide();
            }
        });
    });
    $(document).on("change","#project-id",function(){
        var route = "<?php echo e(route('construction.select_warehouse',':id')); ?>";
        route = route.replace(":id",$(this).val());
        $("#main-spinner").show();
        $.ajax({
            url:route,
            type:"GET",
            dataType:"JSON",
            success:function(data)
            {
                var opt = '<option value = "">Please Select</option>';
                $.each(data,function(index,value){
                    opt+=`<option value = "${value.id}">${value.name}</option>`;
                });
                $("#warehouse-id").html(opt);
                
            },
            complete:function(){
                $("#main-spinner").hide();
            }
        });
    });
    function add_row_product(obj)
    {
        var tr_class =  $(obj).closest("table").find('tbody').attr('class');
        var tr = `<tr class="tr-table-${tr_class}">
                    <td width="4%">
                        <span>${ $(obj).closest("table").find("tbody").find("tr").length+1}</span>
                    </td>
                    <td width="12%">
                        <select name="product_id[]" class="form-control select2" onchange="selected_product(this)">
                            ${product_option}
                        </select>
                    </td>
                    <td style="width: 200px !important;">
                        <select name="place_to_use_zone[]" class="form-control select2" >
                            ${zone_option}
                        </select>
                    </td>
                    <td style="width: 200px !important;">
                        <select name="place_to_use_house_number[]" class="form-control select2" >
                            ${number_house_option}
                        </select>
                    </td>
                    <td width="10%">
                        <input type="text" class="form-control" name="qty[]" onchange="check_qty(this)">
                    </td>
                    <td width="6%">
                        <select class="form-control select2" name="unit[]" onchange="select_unit(this)">

                        </select>
                        <input type="hidden" name = "unit_type[]">
                    </td>
                    <td>
                        <?php echo Form::textarea('description[]', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')); ?>

                    </td>
                    <td><button type="button" class="btn btn-sm btn-danger">
                        <span class="fa fa-trash" onclick="remove(this)"></span></button>
                    </td>
                </tr>`;

        var table = $(obj).closest("table");
            
        $(table).find("tbody").append(tr);
        $(".select2").select2();
    }
    function remove(obj)
    {
        var index = 1;
        var tr_class =  $(obj).closest("table").find('tbody').attr('class');
        $(obj).closest('tr').remove();
        $("tr.tr-table-"+tr_class).each(function(){
            $(this).find('td:eq(0)').html(`<span>${++index}</span>`);
        });
    }
    function selected_product(obj){
        var id = $(obj).val();
        if(id==""){

        }else{
            $("#main-spinner").show();
        }
        
        var route = "<?php echo e(route('construction.get_unit_product',':id')); ?>";
        route = route.replace(":id",id);
        $.ajax({
            url:route,
            dataType:"JSON",
            type:"GET",
            success:function(data){
                $("#main-spinner").hide();
                var data_opt = `<option data-type="1" value="${data['product'].unit_id}" selected>${data['product'].unit_name}</option>`;
                $.each(data['sub_units'],function(index,value){
                    data_opt += `<option value = "${value.id}" data-type="2">${value.name}</option>`;
                });
                $(obj).closest('tr').find("td:eq(5)").find("select").html(data_opt);
                $(obj).closest('tr').find('td:eq(5)').find('input').val(1);
            },
            error:function(){
                $(obj).closest('tr').find("td:eq(5)").find("select").html(`<option data-type="" value="">Please Select</option>`);
            }
        });
    }
    $(document).on("focus","#date_picker",function(){
        $(this).datepicker({
            dateFormat: 'dd-mm-yy',
            changeMonth: true
        });
    });
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>