@extends('layouts.app')
@section('content')

<div class="row">
    <div class="col-sm-3">
        @include('side_menu')
    </div>
    <div class="col-sm-9">
        <div class="row">
            <div class="col-sm-12 add-container">
                <form action="/institution/save" method="post" class="add-form">
                    <div class="container">
                        <div class="row">
                            @csrf
                            @if ($errors->any())
                            <div class="alert alert-danger" role="alert">
                                Please fix the following errors
                            </div>
                            @endif
                            @if($selectedInstitution != null)
                            <input type="hidden" id="selected_institution" name="selected_institution" value="{{$selectedInstitution->institution_id}}" />
                            @endif
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="institution_name">Institution Name</label>
                                    <input type="text" class="form-control @error('institution_name') is-invalid @enderror" id="institution_name" name="institution_name" placeholder="Name" value="@if($selectedInstitution != null){{$selectedInstitution->institution_name}}@else{{ old('institution_name') }}@endif">
                                    @error('institution_name')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="subcategory">Sub Category</label>
                                    <select class="form-control @error('subcategory') is-invalid @enderror" id="sub_category_id" name="sub_category_id">
                                        <option value="">Select Sub Category</option>
                                        @foreach($subcategories as $subcategory)
                                        @if($selectedInstitution != null)
                                            <option @if($selectedInstitution->sub_category_id == $subcategory->sub_category_id) selected @endif value="{{$subcategory->sub_category_id}}">{{$subcategory->sub_category_name}}</option>
                                        @else
                                            <option value="{{$subcategory->sub_category_id}}">{{$subcategory->sub_category_name}}</option>
                                        @endif
                                        @endforeach
                                    </select>
                                    @error('title')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="description">Description</label>
                                    <textarea name="description" id="description" class="form-control @error('description') is-invalid @enderror" placeholder="Description">@if($selectedInstitution != null){{$selectedInstitution->description}}@else{{ old('description') }}@endif</textarea>
                                    @error('description')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-primary bt-margin">Save</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="container">
                <div class="row search-mg">
                    <div class="col-md-12 search-col">
                        <button class="btn float-right search-bt" onclick="searchInstitutions()">Search</button>
                        <input type="text" class="form-control float-right search-txt" id="search-institution" name="search-institution" placeholder="Search" />
                    </div>
                </div>
                <div class="row t-header">
                    <div class="col-5">Institution Name</div>
                    <div class="col-5">Sub Category</div>
                    <div class="col-2"></div>
                </div>
                <div id="institutions-data">
                    @foreach ($institutions as $institution)
                    <div class="row t-content">
                        <div class="col-5">
                            {{ $institution->institution_name }}
                        </div>
                        <div class="col-5">
                            {{ $institution->subcategory->sub_category_name }}
                        </div>
                        <div class="col-2 edit-butts">
                            <span>
                                <a href="/institution/edit/{{$institution->institution_id}}">
                                    <img src="/images/edit.png" width="15px" alt="Edit" />
                                </a>
                                <button rel="{{$institution->institution_id}}" class="btn delinstitution" data-toggle="modal" data-target="#institutionsModal">
                                    <img src="/images/delete.png" width="15px" alt="Delete" />
                                </button>
                            </span>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="institutionsModal" tabindex="-1" role="dialog" aria-labelledby="institutionsModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="institutionsModalLabel">Confirm Delete</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    Are you sure you would like to delete the selected institution?
                </div>
                <div class="modal-footer">
                    <div class="row" style="width:100%">
                        <div class="col-8">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        </div>
                        <div class="col-4">
                            <form action="/institution/del" method="post">
                                @csrf
                                <input type="hidden" id="del-institution" name="deleteinstitution" />
                                <button type="submit" class="btn btn-primary">Confirm</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endsection