@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="/constituency/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($selectedConstituency != null)
                            <input type="hidden" id="selected_constituency" name="selected_constituency" value="{{$selectedConstituency->constituency_id}}" />
                            @endif
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="constituency_name">Constituency Name</label>
                                    <input type="text" class="form-control @error('constituency_name') is-invalid @enderror" id="constituency_name" name="constituency_name" placeholder="Name" value="@if($selectedConstituency != null){{$selectedConstituency->constituency_name}}@else{{ old('constituency_name') }}@endif">
                                    @error('constituency_name')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="constituency_code">Constituency Code</label>
                                    <input type="text" class="form-control @error('constituency_code') is-invalid @enderror" id="constituency_code" name="constituency_code" placeholder="Constituency Code" value="@if($selectedConstituency != null){{$selectedConstituency->constituency_code}}@else{{ old('constituency_code') }}@endif">
                                    @error('constituency_code')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                            <div class="col-sm-9">
                                <div class="form-group">
                                    <label for="county">County</label>
                                    <select class="form-control @error('county') is-invalid @enderror" id="county_id" name="county_id">
                                        <option value="">Select County</option>
                                        @foreach($counties as $county)
                                        @if($selectedConstituency != null)
                                            <option @if($selectedConstituency->county_id == $county->county_id) selected @endif value="{{$county->county_id}}">{{$county->county_name}}</option>
                                        @else
                                            <option value="{{$county->county_id}}">{{$county->county_name}}</option>
                                        @endif
                                        @endforeach
                                    </select>
                                    @error('title')
                                        <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="searchConstituencies()">Search</button>
                        <input type="text" class="form-control float-right search-txt" id="search-constituency" name="search-constituency" placeholder="Search" />
                    </div>
                </div>
                <div class="row t-header">
                    <div class="col-3">Constituency Code</div>
                    <div class="col-3">Constituency Name</div>
                    <div class="col-3">County</div>
                    <div class="col-3"></div>
                </div>
                <div id="constituencies-data">
                    @foreach ($constituencies as $constituency)
                    <div class="row t-content">
                        <div class="col-3">
                            {{ $constituency->constituency_code }}
                        </div>
                        <div class="col-3">
                            {{ $constituency->constituency_name }}
                        </div>
                        <div class="col-3">
                            {{ $constituency->county->county_name }}
                        </div>
                        <div class="col-3 edit-butts">
                            <span>
                                <a href="/constituency/edit/{{$constituency->constituency_id}}">
                                    <img src="/images/edit.png" width="15px" alt="Edit" />
                                </a>
                                <button rel="{{$constituency->constituency_id}}" class="btn delconstituency" data-toggle="modal" data-target="#constituenciesModal">
                                    <img src="/images/delete.png" width="15px" alt="Delete" />
                                </button>
                            </span>
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

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