@extends('base.layout')

@section('content')
 <div class="wrapper">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="card-box ribbon-box">
							<div class="ribbon ribbon-primary">Doctor List</div>
							<p class="m-b-0"></p>
                            <button type="button" class="btn btn-custom waves-light waves-effect" data-toggle="modal" data-target="#addModal">Add New Data</button>
                            <br><br>
							@if(Session::has('message'))
								<div class="alert {{ Session::get('alert-class') }} alert-dismissable">
									<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
									<h4><i class="icon fa fa-check"></i> {{ Session::get('status') }}</h4>
										{!! Session::get('message') !!}
								</div>
							@endif
                            <table id="datatable" class="table table-bordered display">
                                <thead>
                                    <tr>
                                        <th>Name</th>
                                        <th>Gender</th>
                                        <th>Birth Date</th>
                                        <th>City</th>
                                        <th>Address</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($dokters as $dokter)
                                        <tr>
                                            <td>{{ $dokter->nama }}</td>
                                            <td>{{ $dokter->jk }}</td>
                                            <td>{{ $dokter->tgl_lahir }}</td>
                                            <td>{{ $dokter->kota }}</td>
                                            <td>{{ $dokter->alamat }}</td>
                                            <td>
                                                <div class="btn-group">
                                                    <button type="button" class="btn btn-light dropdown-toggle waves-effect" data-toggle="dropdown" aria-expanded="false"> <i class="icon-settings"></i> </button>
                                                    <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
                                                        <a class="dropdown-item update" href="#" dokter-id="{{ $dokter->id }}" data-toggle="modal" data-target="#updateModal">Update</a>
                                                        <a class="dropdown-item delete" href="#" dokter-id="{{ $dokter->id }}">Delete</a>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                            <form id="formDelete" action="{{ route('deleteDokter') }}" method="POST">
                              {{ csrf_field() }}
                              <input type="hidden" name="dokter_id" id="delete_dokter">
                            </form>
                        </div>
                    </div>
                </div>
            </div> <!-- end container -->
        </div>

        <!-- Add Modal -->
        <div id="addModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel">Form Add New Doctor</h4>
                    </div>
                    <div class="modal-body">
                        <p>Please Add From User Management</p>
                        <div class="form-group account-btn text-center m-t-10">
                            <div class="col-12">
                                <a href="/user">Go To User Management Pages</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Update Modal Poli -->
        <div id="updateModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel">Update Data</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" action="{{ url('dokter/update') }}" method="POST">
                        {{ csrf_field() }}
                            <div class="form-group m-b-25">
                                <div class="col-12">
                                    <label>Name</label>
                                    <input class="form-control" type="text" id="nama" name="nama" required="">
                                </div>
                            </div>
                            <div class="form-group m-b-25">
                                <div class="col-12">
                                    <label>Gender</label>
                                    <div class="radio">
                                        <input type="radio" name="jk" id="radio3" value="L">
                                        <label for="radio3">Laki-laki</label>&ensp;
                                        <input type="radio" name="jk" id="radio4" value="P">
                                        <label for="radio4">Perempuan</label>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group m-b-25">
                                <div class="col-12">
                                    <label>Birth Date</label>
                                    <div class="input-group">
                                        <input type="text" class="form-control" name="tgl_lahir" placeholder="yyyy-mm-dd" id="tgl">
                                            <div class="input-group-append">
                                                <span class="input-group-text"><i class="mdi mdi-calendar"></i></span>
                                            </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group m-b-25">
                                <div class="col-12">
                                    <label>City</label>
                                    <input class="form-control" type="text" id="kota" name="kota" required="">
                                </div>
                            </div>
                            <div class="form-group m-b-25">
                                <div class="col-12">
                                    <label>Address</label>
                                    <textarea class="form-control" id="alamat" name="alamat" rows="5"></textarea>
                                </div>
                            </div>
                            <input type="hidden" name="id_dokter" id="id_dokter">
                            <div class="form-group account-btn text-center m-t-10">
                                <div class="col-12">
                                    <button class="btn w-lg btn-custom waves-effect waves-light" type="submit">Update</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
@endsection

@push('script')
<script type="text/javascript">
    $(document).ready(function() {
		$("#tgl_lahir").datepicker({format: 'yyyy-mm-dd'});
		$("#tgl").datepicker({format: 'yyyy-mm-dd'});
        $('form').parsley();
    });

    $('.delete').click(function () {
        var dokter_id     =   $(this).attr("dokter-id");
        $("#delete_dokter").val(dokter_id);
        swal({
            title: 'Are you sure?',
            text: "Do you realy want to delete this data!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonClass: 'btn btn-confirm mt-2',
            cancelButtonClass: 'btn btn-cancel ml-2 mt-2',
            confirmButtonText: 'Yes, delete it!'
        }).then(function () {
            swal({
                title: 'Deleted !',
                text: "Data has been deleted",
                type: 'success',
                confirmButtonClass: 'btn btn-confirm mt-2'
            })
            $("#formDelete").submit();
        })
    });

    $(document).on('click', '.update', function(e) {
        var id  =   $(this).attr('dokter-id');
        console.log(id);
        $.ajax({
            type : 'POST', 
            url  : '{{ route("getDokter") }}',
            data : {'id' : id, _token : "{{ csrf_token() }}"},
            success : function(response) {
                var data  =   JSON.parse(response);
                $("#nama").val(data['nama']);
                var jk  =   data['jk'];
                if (jk == 'L') {
                    document.getElementById("radio3").checked = true;
                } else {
                    document.getElementById("radio4").checked = true;
                }
                $(".tgl").val(data['tgl_lahir']);
                $("#kota").val(data['kota']);
                $("#alamat").val(data['alamat']);
                $("#id_dokter").val(data['id']);
            }
        });
    });
</script>
@endpush