@extends('layouts.app')


@section('title')
    Select Project
@stop

@section('breadcrumb')
    <ol class="breadcrumb pull-right">
        <li><a href="{{ route('home') }}"><i class="fa fa-dashboard"></i> Home</a></li>
     
        <li class="active">Project before Inventory</li>
    </ol>
@stop

@section('content')


<div class="container-fluid">

  
       
 
    <!--.row-->
    <div id='updatecontent' style='margin-top:50px'>

        <div id="toaster"></div>
        
        @can('isSuperAdmin')
     
        <div class="row">
        
            
            <div class="col-md-12">
                @if ($errors->any())
                    <div class="white-box">
                        <h2>Errors</h2>
                        <ul>
                            {!! implode('', $errors->all('<li><span class="help-block"><strong>:message</strong></span></li>')) !!}
                        </ul>
                    </div>
                @endif
                <div class="white-box">

                    <h3 class="box-title m-b-0"><i class="fa fa-dashboard"></i> Select Any Project</h3>
                    <p class="text-muted m-b-30 font-13">Select a project before add a new inventory </p>
                    <form class="form-horizontal" action="{{ route('inventory.add') }}" method="GET">
                        {{ csrf_field() }}
                        
                        
                       

                       

                        <div class="form-group">
                            <label for="address" class="col-sm-2 control-label" id="pro">Select Project</label>
                            <div class="col-sm-4">
                                <div class="input-group">
                                    <div class="input-group-addon"><i class="fa fa-circle-o"></i></div>
                                    <select id="multipro" class="form-control chosen-select" name="multipro_id">
                                        <option value=""><b>None</b></option>
                                        @foreach ($multi_projects as $pro)
                                        @php
                                       $p_id= encrypt($pro->id); 
                                        @endphp
                                            @if (old('multipro_id') == $p_id)
                                                <option selected="" value="{{  $p_id }}">{{ $pro->name }}
                                                </option>
                                            @else
                                                <option value="{{  $p_id}}">{{ $pro->name }} </option>
                                            @endif
                                        @endforeach
                        
                                    </select>
                                    @if ($errors->has('multipro_id'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('multipro_id') }}</strong>
                                        </span>
                                    @endif
                                </div>
                            </div>


                        </div>


                      



                      


                     

                        <div class="form-group">
                            <div class="col-sm-2">&nbsp;</div>
                            <div class=" col-sm-4">
                                <button type="submit" class="btn btn-info waves-effect waves-light "
                                    >Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        @endcan

    </div>
    <!--./row-->
</div>
    <!-- /.container-fluid -->

@stop


@section('js')

    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/jquery.inputmask.bundle.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>



    <script>
          function check_post(self) {

 
if ($(self).val() == 1) { //unallocated portion
    $('#parent_div').hide()
    $('#category_portion').hide();
    $('#inventory_type').html('Inventory Id')

    var data = {
        'type': $(self).val()
    };
    $('#unit_floor_id').html('<option selected value=""></option>');


    // $.get('/getNillFloor', data, function(data) {

    // $('#unit_floor_id').html('<option selected value="' + data.output.id + '">' + data.output.name +
    // '</option>');

    // // $(".chosen-select").val(data.output.id).trigger("chosen:updated");
    // }, 'json');



} else {

    $('#parent_div').show()
    $('#category_portion').show();
    $('#inventory_type').html('Unit No')

    var data = {
        'type': $(self).val()
    };

    $.get('/getNillFloor', data, function(data) {

        $option = '<option selected value="0">none</option>';

        $.each(data.output, function(index, val) {
            $option += '<option value="' + val.id + '">' + val.name + '</option>';
        });
        $('#unit_floor_id').html($option);
        $(".chosen-select").val(0).trigger("chosen:updated");
    }, 'json');
}
}

        

        @can('isSuperAdmin')
        async function updateStatus(value) {
            $('.preloader').fadeIn();

            if (value === 'search') {
                $('.preloader').fadeOut();
                return false;
            }

            if (value == "") {
                $('.preloader').fadeOut();
                return false;
            }

            var data = {
                'multipro_id': value,
            };

          

            try {
                const response = await new Promise((resolve, reject) => {
                    $.get('{{ route('inventory.add') }}', data, function(data) {
                        resolve(data);
                    }).fail(function(xhr, status, error) {
                        reject(error);
                    });
                });

                $('.preloader').fadeOut();

                await new Promise(resolve => {
                    document.getElementById('updatecontent').innerHTML = response;
                    $('.chosen-select').trigger('chosen:updated');
                    $('.chosen-select').chosen({
                        // configuration options for the chosen select box
                    });
                    resolve();
                });

                await callItBack();
                $('#set_multipro_id').val(value);
                var successAlert = $('<div class="alert alert-success alert-dismissible">' +
                                '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>' +
                                '<strong>Success!</strong> Form updated successfully!.' +
                            '</div>');
        $('#toaster').append(successAlert);
               


            } catch (error) {
                // Handle error if the request fails
                console.error(error);
            }

            $('.preloader').fadeOut();
        }
        @endcan
    </script>


@stop
