@extends('layouts.master')
@section('content')
<div class="row justify-content-center">
    <div class="col-md-10 p-3">
        <div class="card card-outline card-primary">
            <div class="card-header text-center">
                TPHA Privacy Policy
            </div>

            <div class="card-body">

                @if($policies->count() != 0)

                @foreach ($policies as $item)

                <form id="updateForm">
                    @csrf

                    <input type="hidden" name="policy" value="{{$item->id}}">

                    <div class="form-group">
                        <label for="">Policy Title:</label>
                        <input type="text" name="title" id="title" class="form-control" value="{{$item->title}}" placeholder="Policy Title">
                    </div>

                    <div class="form-group">
                        <label for="">Policy Description:</label>
                        <textarea class="summernote" name="description">{!!$item->description!!}</textarea>
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn bg-gradient-primary">Click Here to Submit <i
                                class="fas fa-fw fa-check-circle"></i></button>
                    </div>

                </form>

                @endforeach

                @else

                <form id="createForm">
                    @csrf

                    <div class="form-group">
                        <label for="">Policy Title:</label>
                        <input type="text" name="title" id="title" class="form-control" placeholder="Policy Title">
                    </div>

                    <div class="form-group">
                        <label for="">Policy Description:</label>
                        <textarea class="summernote" name="description"></textarea>
                    </div>

                    <div class="form-group">
                        <button type="submit" class="btn bg-gradient-primary">Click Here to Submit <i
                                class="fas fa-fw fa-check-circle"></i></button>
                    </div>

                </form>

                @endif


            </div>
        </div>
    </div>
</div>

<script>
    $("document").ready(function(){

        $('.summernote').summernote({
            height: 200,
        });

        //updating
        $('#updateForm').on('submit',function(event){
            event.preventDefault();

            var data_url = "{{route('privacy_policy.update')}}";

            $.ajax({
                url: data_url,
                method: "POST",
                data: $(this).serialize(),
                dataType: "JSON",

                success:function(data){
                    if(data.success){
                        var result = data.success;

                        Swal.fire({
                            title: "Notification!",
                            text: result,
                            icon: "success",
                            showConfirmButton: false,
                            showCancelButton: false,
                            timer: 3000,
                        });

                        setTimeout(function(){
                            window.location = "{{url('privacy_policy')}}";
                        },2000);
                    }

                    //errors
                    if(data.errors){
                        var result = data.result;

                        Swal.fire({
                            title: "Info!",
                            text: result,
                            icon: "warning",
                            showConfirmButton: false,
                            showCancelButton: false,
                            timer: 3000,
                        });
                    }
                }
            })

            
        })

        $('#createForm').on('submit',function(event){
            event.preventDefault();

            var data_url = "{{route('privacy_policy.store')}}";

            $.ajax({
                url: data_url,
                method: "POST",
                data: $(this).serialize(),
                dataType: "JSON",

                success:function(data){
                    if(data.success){
                        var result = data.success;

                        Swal.fire({
                            title: "Notification!",
                            text: result,
                            icon: "success",
                            showConfirmButton: false,
                            showCancelButton: false,
                            timer: 3000,
                        });

                        setTimeout(function(){
                            window.location = "{{url('privacy_policy')}}";
                        },2000);
                    }

                    //errors
                    if(data.errors){
                        var result = data.result;
                        Swal.fire({
                            title: "Info!",
                            text: result,
                            icon: "warning",
                            showConfirmButton: false,
                            showCancelButton: false,
                            timer: 3000,
                        });
                    }
                }
            })
        
        })
    })
</script>

@endsection