@extends('layouts.master')
@section('title', 'Addiction Datas')
@section('styles')
    <!-- DataTables -->
    <link href="{{ asset('assets/vendors/datatables.net-bs5/css/dataTables.bootstrap5.min.css') }}" rel="stylesheet" type="text/css" />

    <!-- Responsive datatable examples -->
    <link href="{{ asset('assets/vendors/datatables.net-responsive-bs5/css/responsive.bootstrap5.min.css') }}" rel="stylesheet" type="text/css" />
@endsection
@section('content')
    <div class="page-content">
        <nav class="page-breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="#">Master</a></li>
                <li class="breadcrumb-item active" aria-current="page">Addiction</li>
            </ol>
        </nav>
        <div class="row">
            <div class="col-md-12 grid-margin stretch-card">
                <div class="card">
                    <div class="card-body">
                        <h4 class="card-title">Addiction Data</h4>
                        <a href="{{ route('admin.addictions.create') }}" class="btn btn-primary mb-3">Create Addiction</a>
                        <div class="table-responsive">
                            <table id="datatable" class="table align-middle text-wrap w-100">
                                <thead>
                                    <tr>
                                        <th width="10px" class="text-center">No</th>
                                        <th>Name</th>
                                        <th>Percentage</th>
                                        <th>Description</th>
                                        <th>Solution</th>
                                        <th class="text-center">Status</th>
                                        <th width="10px" class="text-center">Detail</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach ($addictions as $key => $addiction)
                                        <tr>
                                            <td class="text-center">{{ $loop->iteration }}</td>
                                            <td>
                                                {{ $addiction->name }} <br> <p class="text-muted m-0"> Code: {{ $addiction->code }}</p>
                                            </td>
                                            <td>{{ $addiction->min_percentage }} % - {{ $addiction->max_percentage }} %</td>
                                            <td class="text-wrap">{{ $addiction->description }}</td>
                                            <td class="text-wrap">{{ $addiction->solution }}</td>
                                            <td class="text-center">
                                                <span class="badge rounded-pill bg-{{ $addiction->status == 'active' ? 'success' : 'danger' }}">{{ ucwords($addiction->status) }}</span>
                                            </td>
                                            <td class="text-center">
                                                <div class="dropdown d-inline-block">
                                                    <button class="btn btn-inverse-secondary btn-xs dropdown py-0 px-1" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                                                        <i data-feather="more-horizontal"></i>
                                                    </button>
                                                    <ul class="dropdown-menu dropdown-menu-end">
                                                        <li><a class="dropdown-item" href="{{ route('admin.addictions.edit', $addiction->id) }}"><i class="mdi mdi-square-edit-outline align-bottom me-2 text-muted"></i> Edit</a></li>
                                                        <li><a class="dropdown-item" href="{{ route('admin.addictions.show', $addiction->id) }}"><i class="mdi mdi-eye-outline align-bottom me-2 text-muted"></i> Detail</a></li>
                                                        <li>
                                                            <a href="javascript:void(0);" class="dropdown-item delete" data-id="{{ $addiction->id }}">
                                                                <i class="mdi mdi-delete-outline align-bottom me-2 text-muted"></i> Delete
                                                            </a>
                                                        </li>
                                                    </ul>
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div> <!-- end col -->
        </div>
    </div>
@endsection
@section('scripts')
    <!-- Required datatable js -->
    <script src="{{ asset('assets/vendors/datatables.net/js/jquery.dataTables.min.js') }}"></script>
    <script src="{{ asset('assets/vendors/datatables.net-bs5/js/dataTables.bootstrap5.min.js') }}"></script>

    <!-- Responsive examples -->
    <script src="{{ asset('assets/vendors/datatables.net-responsive/js/dataTables.responsive.min.js') }}"></script>
    <script src="{{ asset('assets/vendors/datatables.net-responsive-bs5/js/responsive.bootstrap5.min.js') }}"></script>
    <script>
        $(function () {
            $('#datatable').DataTable();
            $('.delete').click(function (e) {
                var id = $(this).data('id');
                e.preventDefault();
                n.fire({
                    title:"Are you sure?",
                    text:"You won't be able to revert this!",
                    icon:"warning",
                    showCancelButton:!0,
                    confirmButtonColor:"#3085d6",
                    cancelButtonColor:"#d33",
                    confirmButtonText:"Yes, delete it!"
                }).then((result) => {
                    if (result.isConfirmed) {
                        var data = {
                            _token: "{{ csrf_token() }}",
                            _method:'delete'
                        };

                        $.ajax({
                            url: `{{ url('admin/addictions')}}/${id}`,
                            dataType: 'json',
                            data: data,
                            type: 'POST',
                        }).done(function(response) {
                            if (response.status) {
                                n.fire({
                                    title: "Success!",
                                    text: response.message,
                                    icon: "success",
                                    confirmButtonClass: "btn btn-primary w-xs mt-2",
                                    buttonsStyling: !1
                                })
                                setTimeout(
                                    function() {
                                        window.location.reload(true);
                                    }, 1000
                                );
                            } else {
                                n.fire({
                                    title: "Error!",
                                    text: response.message,
                                    icon: "error",
                                    confirmButtonClass: "btn btn-primary w-xs mt-2",
                                    buttonsStyling: !1
                                })
                            }
                        }).fail(function(response) {
                            var response = response.responseJSON;
                            n.fire({
                                title: "Error!",
                                text: response.message,
                                icon: "error",
                                confirmButtonClass: "btn btn-primary w-xs mt-2",
                                buttonsStyling: !1
                            })
                        })
                    }
                });
            });
        });
    </script>
@endsection
