@extends('layouts.app')
@push('third_party_stylesheets')
    @include('layouts.datatables_css')
@endpush

@section('title')
    Profil
@endsection

@section('content')

    <div class="content px-3">

        @include('flash::message')

        <div class="clearfix"></div>

        {!! Form::open(['route' => 'user.update', 'enctype'=>"multipart/form-data"]) !!}
        <div class="card mb-4">
            <div class="card-header">
                <h5>Edit Profil</h5>
            </div>
            <div class="card-body px-4">
                <div class="row">
                    <div class="form-group col-sm-6">
                        {!! Form::label('name', 'Nama:') !!}
                        {!! Form::text('name', isset($user) ? $user->name : null, ['class'=>'form-control']) !!}
                    </div>
                    <div class="form-group col-sm-6">
                        {!! Form::label('email', 'Email:') !!}
                        {!! Form::text('email', isset($user) ? $user->email : null, ['class'=>'form-control']) !!}
                    </div>
                    <div class="form-group col-sm-6">
                        {!! Form::label('photo', 'Upload Gambar:') !!}
                        <input class="form-control mb-4" type="file" name="photo" id="product-image-1" />

                        @if (isset($user) && isset($user->photo))
                            <img src="{{ asset('/media/' . $user->photo) }}" alt="" id="image-1" width="200px" height="200px" style="object-fit: contain" />
                        @else
                            <img src="{{ asset('images/blank.png') }}" alt="" id="image-1" width="200px" height="200px" style="object-fit: contain" />
                        @endif
                    </div>
                </div>
            </div>
            <div class="card-footer">
                {!! Form::submit('Simpan', ['class' => 'btn btn-primary']) !!}
            </div>
        </div>
        {!! Form::close() !!}
    </div>

@endsection

@push('third_party_scripts')
@include('layouts.datatables_js')
<script>
    $('#product-image-1').change(function() {
        const file = this.files[0];
        if (file) {
            let reader = new FileReader();
            reader.onload = function(event) {
                console.log(event.target.result);
                $('#image-1').attr('src', event.target.result);
            }
            reader.readAsDataURL(file);
        }
    });
</script>

<script>
    function deleteImage(id) {
        var url = "{{ route('objekGallery.destroy', ':id') }}";
        url = url.replace(':id', id);

        Swal.fire({
            title: "Apa Anda Yakin?",
            text: "Menghapus data ini!",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Ya, Hapus!",
            cancelButtonText: "Tidak ",
        }).then(function (data) {
            if (data.isConfirmed) {
                $.ajax({
                    url: url,
                    method: 'DELETE',
                    data : { '_method':'delete' },
                    contentType: 'application/json',
                    headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                    success: function(result) {
                        Swal.fire({
                            title: 'Informasi',
                            text: 'Data berhasil dihapus.',
                            icon: 'success',
                            didClose: function () {
                                window.location.reload(); // Memuat ulang halaman saat dialog ditutup
                            }
                        });
                    },
                    error: function(request,msg,error) {
                        // handle failure
                    }
                });
            };
        });
    }
</script>
@endpush