@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="/subcategory/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($selectedSubcategory != null)
                            <input type="hidden" id="selected_subcategory" name="selected_subcategory" value="{{$selectedSubcategory->sub_category_id}}" />
                            @endif
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="sub_category_name">Sub-category Name</label>
                                    <input type="text" class="form-control @error('sub_category_name') is-invalid @enderror" id="sub_category_name" name="sub_category_name" placeholder="Name" value="@if($selectedSubcategory != null){{$selectedSubcategory->sub_category_name}}@else{{ old('sub_category_name') }}@endif">
                                    @error('sub_category_name')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="category">Category</label>
                                    <select class="form-control @error('category') is-invalid @enderror" id="category_id" name="category_id">
                                        <option value="">Select Category</option>
                                        @foreach($categories as $category)
                                        @if($selectedSubcategory != null)
                                            <option @if($selectedSubcategory->category_id == $category->category_id) selected @endif value="{{$category->category_id}}">{{$category->category_name}}</option>
                                        @else
                                            <option value="{{$category->category_id}}">{{$category->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($selectedSubcategory != null){{$selectedSubcategory->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="searchSubcategories()">Search</button>
                        <input type="text" class="form-control float-right search-txt" id="search-subcategory" name="search-subcategory" placeholder="Search" />
                    </div>
                </div>
                <div class="row t-header">
                    <div class="col-4">Sub-category Name</div>
                    <div class="col-4">Category</div>
                    <div class="col-4"></div>
                </div>
                <div id="subcategories-data">
                    @foreach ($subcategories as $subcategory)
                    <div class="row t-content">
                        <div class="col-4">
                            {{ $subcategory->sub_category_name }}
                        </div>
                        <div class="col-4">
                            {{ $subcategory->category->category_name }}
                        </div>
                        <div class="col-4 edit-butts">
                            <span>
                                <a href="/subcategory/edit/{{$subcategory->sub_category_id}}">
                                    <img src="/images/edit.png" width="15px" alt="Edit" />
                                </a>
                                <button rel="{{$subcategory->sub_category_id}}" class="btn delsubcategory" data-toggle="modal" data-target="#subcategoriesModal">
                                    <img src="/images/delete.png" width="15px" alt="Delete" />
                                </button>
                            </span>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="subcategoriesModal" tabindex="-1" role="dialog" aria-labelledby="subcategoriesModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="subcategoriesModalLabel">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 sub-category?
                </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="/subcategory/del" method="post">
                                @csrf
                                <input type="hidden" id="del-subcategory" name="deletesubcategory" />
                                <button type="submit" class="btn btn-primary">Confirm</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endsection