<?php 

use Jackiedo\DotenvEditor\Facades\DotenvEditor;

$editor = DotenvEditor::load(storage_path('app/settings.env'));

?>
<form id="new_material">
    <input type="hidden" name="data[_material_at_tickets]" value="{{$model->getAttribute('_material_at_tickets')}}">
    
    <div class="input-group mb-3">
        <div class="input-group-prepend">
            <span class="input-group-text" style="width:80px;">Material</span>
        </div>
        <textarea class="form-control" name="data[descriptor]" autocomplete="off"></textarea>
        <div class="input-group-append">
            <button class="btn btn-info" type="button" id="open_materialmaster_button">aus Materialstamm</button>
        </div>
    </div>
    
    <div class="form-group" align="right">
        <a class="btn btn-outline-info btn-sm" type="button" id="extend_materialmaster">.. zu Materialstamm hinzufügen</a>
    </div>
  
    <div class="input-group mb-3">
        <div class="input-group-prepend">
            <span class="input-group-text" style="width:80px;">VPE</span>
        </div>
        <input type="text" class="form-control" name="data[unit]" autocomplete="off" readonly>
        <div class="input-group-append">
            <button class="btn btn-info" type="button" id="open_unitselection_button">Auswahl</button>
        </div>
    </div>
    
    <div class="input-group mb-3">
        <div class="input-group-prepend">
            <span class="input-group-text" style="width:80px;">Menge</span>
        </div>
        <input type="text" class="form-control" name="data[amount]" value="0" autocomplete="off" oninput="this.value = this.value.replace(/[^\d,-]+/g, '')">
    </div>
  

    <hr class="mt-3 mb-3">

    <div class="input-group">

    <select class="custom-select" id="vat_rate" name="data[vat]" style="width:10%;">
        @if(!$editor->getValue('COMPANY_SBLAW'))
        @foreach(\Runtime::make('hwplus_vat_rates')->get() AS $rate)
        <option value="{{$rate->rate}}" @if($rate->flag_default) selected @endif>{{$rate->descriptor}}</option>
        @endforeach
        @else
        <option value="0">0%</option>
        @endif
    </select>

    <input type="text" class="form-control" name="data[netprice]" value="0" placeholder="Einzelpreis" autocomplete="off" oninput="this.value = this.value.replace(/[^\d,-]+/g, '')">
    <div class="input-group-append">
        <span class="input-group-text">€</span>
    </div>
    </div>

</form>

 <div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 0; bottom: 0;">
  <div id="liveToast" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="2000">
    <div class="toast-header">
      <img src="..." class="rounded mr-2" alt="...">
      <strong class="mr-auto">System</strong>
      <small>Gerade eben ..</small>
      <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="toast-body">
      Material erfolgreich zu Materialstamm hinzugefügt.
    </div>
  </div>
</div>

<script>

    $('input[name="data[descriptor]"]').focus();

    $('#extend_materialmaster').on('click', function() {
    
            if(!$('#new_material input[name="data[descriptor]"]').val()) {
            
                BootstrapDialog.alert({title: "Fehler", message:'Materialbezeichnung fehlt'});
                return;
            
            }
    
            BootstrapDialog.show({
                message: $('<div></div>').load('../../16d4ac0d96f52f29795b6696c6c52af5036b7ca3/5a7dfc613be971ac442fff63b73ecd1b2ebd9594'),
                title:'Zu Materialstamm hinzufügen',
                type:BootstrapDialog.TYPE_LIGHT,
                buttons: [{
                    label: 'Speichern',
                    cssClass:'btn-success',
                    action: function(dialogItself) {
                    
                        let postData = $('#new_materialmaster').serialize();
                                       
                        
                        $.post("../../../api/{{$model->getConfig()->_service->url}}/5a7dfc613be971ac442fff63b73ecd1b2ebd9594/96f9fdfcc3a5c85884c6fb1e16b99f9952fdd499", postData)
                        
                        .done(function(data) { 
                            dialogItself.close();
                            $('#liveToast').toast('show');
                            let unit = $('#new_materialmaster select[name="data[_unit]"] option:selected').text();
                            $('#new_material input[name="data[unit]"]').val(unit);
                        })
                        
                        .fail(function(xhr, status, error) {
                             if(422 == xhr.status) {
                                $('#new_materialmaster').find('.is-invalid').removeClass('is-invalid');
                                $.each(xhr.responseJSON, function(index, item) {
                                    let array = index.split('.');
                                    $('#new_materialmaster').find('[name="data['+array[1]+']"]').addClass('is-invalid'); 
                                });
                            
                            
                            }

                            else alertException(xhr);
                        
                        });
                    
                    }
                }, {
                    label: 'Abbrechen',
                    action: function(dialogItself){
                        dialogItself.close();
                    }
                }]
            });
    
    
    })

    $('input[name="data[descriptor]"').focus();
    $('input[name="data[amount]"]').on('click', function(el) { $(this).select(); })
    $('input[name="data[netprice]"]').on('click', function(el) { $(this).select(); })

</script>


