<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Tipe <span class="required">*</span></label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        {!! html()->select('tipe_regulasi', \App\Models\TipeRegulasi::pluck('nama', 'id'), old('tipe_regulasi', isset($regulasi) ? $regulasi->tipe_regulasi : null))->class('form-control')->id('tipe')->required() !!}
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Judul <span class="required">*</span></label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        {{ html()->text('judul')->class('form-control')->placeholder('Judul Regulasi')->required()->value(old('judul', isset($regulasi) ? $regulasi->judul : null)) }}
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Deskripsi <span class="required">*</span></label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        {{ html()->textarea('deskripsi')->class('form-control')->placeholder('Deskripsi')->required()->value(old('deskripsi', isset($regulasi) ? $regulasi->deskripsi : null)) }}
    </div>
</div>
<div class="form-group">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">File Regulasi <span class="required">*</span></label>
    <div class="col-md-6 col-sm-6 col-xs-12">
        <input accept="image/*, application/pdf" type="file" id="file_regulasi" name="file_regulasi" class="form-control">
        <br>

        @if (isset($regulasi->file_regulasi) && $regulasi->mime_type != 'pdf')
            <img class="" src="@if (isset($regulasi->file_regulasi)) {{ asset($regulasi->file_regulasi) }} @else {{ 'http://placehold.co/1000x600' }} @endif" id="showgambar" style="max-width:400px;max-height:250px;float:left;" />
        @endif

        @if (isset($regulasi->file_regulasi) && $regulasi->mime_type == 'pdf')
            <object data="@if (isset($regulasi->file_regulasi)) {{ asset($regulasi->file_regulasi . '#toolbar=1') }} @endif" type="application/pdf" class="showpdf" id="showpdf"> </object>
        @endif

        <img class="hide" src="@if (isset($regulasi->file_regulasi)) {{ asset($regulasi->file_regulasi) }} @else {{ 'http://placehold.co/1000x600' }} @endif" id="showgambar" style="max-width:400px;max-height:250px;float:left;" />

        <object data="@if (isset($regulasi->file_regulasi)) {{ asset($regulasi->file_regulasi . '#toolbar=1') }} @endif" type="application/pdf" class="showpdf hide" id="showpdf"> </object>
    </div>
</div>

<div class="ln_solid"></div>

@include('partials.asset_jqueryvalidation')

@push('scripts')
    {!! JsValidator::formRequest('App\Http\Requests\RegulasiUpdateRequest', '#form-regulasi') !!}

    <script>
        $(function() {

            var fileTypes = ['jpg', 'jpeg', 'png', 'bmp', 'pdf']; //acceptable file types

            function readURL(input) {
                if (input.files && input.files[0]) {
                    var extension = input.files[0].name.split('.').pop().toLowerCase(), //file extension from input file
                        isSuccess = fileTypes.indexOf(extension) > -1; //is extension in acceptable types

                    if (isSuccess) { //yes
                        var reader = new FileReader();
                        reader.onload = function(e) {

                            if (extension != 'pdf') {
                                $('#showgambar').attr('src', e.target.result);
                                $('#showgambar').removeClass('hide');
                                $('#showpdf').addClass('hide');
                            } else {
                                $('#showpdf').attr('data', e.target.result + '#toolbar=1');
                                $('#showpdf').removeClass('hide');
                                $('#showgambar').addClass('hide');
                            }

                        }

                        reader.readAsDataURL(input.files[0]);
                    } else { //no
                        //warning
                        $("#file_regulasi").val('');
                        openAlert('File tersebut tidak diperbolehkan.', 'Peringatan', 'warning');
                    }
                }
            }

            $("#file_regulasi").change(function() {
                readURL(this);
            });
        });
    </script>
@endpush
