@extends('layouts.app')

@section('content')
    <section class="content">
        <div class="card card-primary card-outline card-outline-tabs">
            <div class="card-header p-0 border-bottom-0">
                <ul class="nav nav-tabs" id="custom-tabs-four-tab" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link {{ isset($list) ? 'active' : '' }}" href="{{ route('users.index') . qString() }}">
                            <i class="bi bi-list-ol"></i> User List
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link {{ isset($create) ? 'active' : '' }}" href="{{ route('users.create') . qString() }}">
                            <i class="bi bi-plus-circle"></i> Add User
                        </a>
                    </li>
                    @if (isset($edit))
                        <li class="nav-item">
                            <a class="nav-link active" href="#">
                                <i class="bi bi-pencil-square"></i> Edit User
                            </a>
                        </li>
                    @endif
                    @if (isset($show))
                        <li class="nav-item">
                            <a class="nav-link active" href="#" role="tab">
                                <i class="bi bi-eye-fill"></i> User Details
                            </a>
                        </li>
                    @endif
                </ul>
            </div>

            <div class="card-body">
                <div class="tab-content" id="custom-tabs-four-tabContent">
                    @if (isset($list))
                        <div class="tab-pane fade show active" role="tabpanel">
                            <form method="GET" action="{{ route('users.index') }}" class="filter">
                                <div class="row justify-content-end mr-1">
                                    <div class="col-sm-12 col-md-4 col-lg-3">
                                        <div class="form-group">
                                            <select name="role" class="form-control select2">
                                                <option value="">Any Role</option>
                                                @foreach (['Admin', 'Staff', 'User'] as $item)
                                                    <option value="{{ $item }}" {{ Request::get('role') == $item ? 'selected' : '' }}>{{ $item }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-12 col-md-4 col-lg-3">
                                        <div class="form-group">
                                            <select name="status" class="form-control select2">
                                                <option value="">Any Status</option>
                                                @foreach (['Active', 'Inactive'] as $item)
                                                    <option value="{{ $item }}" {{ Request::get('status') == $item ? 'selected' : '' }}>{{ $item }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-sm-12 col-md-4 col-lg-3">
                                        <div class="form-group input-group-sm">
                                            <input type="text" class="form-control" name="search" value="{{ Request::get('search') }}" placeholder="Write your search text...">
                                        </div>
                                    </div>
                                    <div class="col-sm-12 col-md-4 col-lg-1">
                                        <div class="btn-group" role="group">
                                            <button type="submit" class="btn text-success py-1">
                                                <i class="bi bi-search"></i>
                                            </button>

                                            <a class="btn text-danger py-1" href="{{ route('users.index') }}" data-toggle="tooltip" data-placement="top" title="Refresh">
                                                <i class="bi bi-arrow-repeat"></i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </form>

                            <div class="card">
                                <div class="card-body p-0">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-sm mb-0">
                                            <thead>
                                                <tr>
                                                    <th style="width: 10px">SN</th>
                                                    <th style="width: 60px">Image</th>
                                                    <th>Name</th>
                                                    <th>Email</th>
                                                    <th>Phone</th>
                                                    <th style="width: 100px">Role</th>
                                                    <th style="width: 85px">Status</th>
                                                    <th style="width: 110px">Action</th>
                                                </tr>
                                            </thead>

                                            <tbody>
                                                @foreach ($records as $record)
                                                    <tr>
                                                        <td class="text-center">{{ $loop->iteration }}</td>
                                                        <td class="text-center">
                                                            <a href="{{ asset('storage/user/' . $record->image) }}" data-fancybox="gallery" data-caption="{{ $record->title }}" data-download-src="{{ asset('storage/user/' . $record->image) }}">
                                                                <img src="{{ asset('storage/user/' . $record->image) }}" class="img-fluid" style="height: 20px; width: 40px;" />
                                                            </a>
                                                        </td>
                                                        <td>{{ $record->name }}</td>
                                                        <td>{{ $record->email }}</td>
                                                        <td>{{ $record->phone }}</td>
                                                        <td>{{ $record->role }}</td>
                                                        <td class="text-center status px-0">
                                                            <span class="badge rounded-pill {{ $record->status == 'Active' ? 'bg-success' : 'bg-danger' }}">
                                                                {{ $record->status }}
                                                            </span>
                                                        </td>
                                                        <td class="text-center action-button px-0">
                                                            <div class="btn-group" role="group">
                                                                <a href="{{ route('users.show', $record->id) . qString() }}" class="btn" data-toggle="tooltip" data-placement="top" title="Details">
                                                                    <i class="bi bi-eye-fill"></i>
                                                                </a>

                                                                <a href="{{ route('users.edit', $record->id).qString() }}" class="btn" data-toggle="tooltip" data-placement="top" title="Edit">
                                                                    <i class="bi bi-pencil-square"></i>
                                                                </a>

                                                                <form id="delete-form-{{ $record->id }}" action="{{ route('users.destroy', $record->id) }}" method="POST" enctype="multipart/form-data">
                                                                    @csrf
                                                                    @method('DELETE')
                                                                    <button type="button" class="btn delete-button rounded-0" data-record-id="{{ $record->id }}" data-toggle="tooltip" data-placement="top" title="Delete">
                                                                        <i class="bi bi-trash-fill"></i>
                                                                    </button>
                                                                </form>

                                                                <form action="{{ route('users.update', $record->id) }}{{ qString() }}" method="POST" enctype="multipart/form-data">
                                                                    @csrf
                                                                    @method('PUT')
                                                                    <input name="status" value="{{ $record->status == 'Active' ? 'Inactive' : 'Active'}}" hidden>
                                                                    <button type="submit" class="btn rounded-0" data-toggle="tooltip" data-placement="top" title="{{ $record->status == 'Active' ? 'Inactive' : 'Active' }}">
                                                                        <i class="bi {{ $record->status == 'Active' ? 'bi-check2-square' : 'bi-hand-thumbs-down' }}"></i>
                                                                    </button>
                                                                </form>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                @endforeach
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                            <div class="d-flex justify-content-center mt-4">{{ $records->links() }}</div>
                        </div>
                    @elseif(isset($show))
                        <div class="tab-pane active" role="tabpanel">
                            <div class="card">
                                <div class="card-body p-0">
                                    <div class="table-responsive">
                                        <table class="table table-bordered table-sm mb-0">
                                            <thead>
                                                <tr>
                                                    <th style="width: 200px">Fields</th>
                                                    <th style="width: 20px">:</th>
                                                    <th>Values</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr>
                                                    <th>Image</th>
                                                    <th>:</th>
                                                    <td>
                                                        <img src="{{ asset('storage/user/' . $record->image) }}" class="image-fluid" style="max-height: 300px;">
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <th>Name</th>
                                                    <th>:</th>
                                                    <td>{{ $record->name }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Email</th>
                                                    <th>:</th>
                                                    <td>{{ $record->email }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Phone</th>
                                                    <th>:</th>
                                                    <td>{{ $record->phone }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Role</th>
                                                    <th>:</th>
                                                    <td>{{ $record->role }}</td>
                                                </tr>
                                                <tr>
                                                    <th>Status</th>
                                                    <th>:</th>
                                                    <td>{{ $record->status }}</td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @elseif(isset($edit) || isset($create))
                        <div class="tab-pane active">
                            <div class="card">
                                <div class="card-body">
                                    <form method="POST"
                                        action="{{ isset($edit) ? route('users.update', $edit) : route('users.store') }}{{ qString() }}"
                                        id="are_you_sure" class="form-horizontal" enctype="multipart/form-data">
                                        @csrf

                                        @if (isset($edit))
                                            @method('PUT')
                                        @endif

                                        <div class="row">
                                            <div class="col-sm-9">
                                                <div class="form-group row {{ $errors->has('image') ? ' has-error' : '' }}">
                                                    <label class="col-sm-3 col-form-label">Image :</label>
                                                    <div class="col-sm-9">
                                                        <input type="file" class="form-control" name="image" onchange="readURL(this)">
                                                        <small class="form-text text-muted">
                                                            @if ($errors->has('image'))
                                                                {{ $errors->first('image') }}
                                                            @else
                                                                Width-Auto X Height-Auto
                                                                @if (isset($edit))
                                                                    / <a href="{{ asset('storage/users/' . $record->image) }}" target="_blank">{{ $record->image }}</a>
                                                                @endif
                                                            @endif
                                                        </small>
                                                    </div>
                                                </div>

                                                <div class="form-group row {{ $errors->has('name') ? ' has-error' : '' }}">
                                                    <label class="col-sm-3 col-form-label required">Name :</label>
                                                    <div class="col-sm-9">
                                                        <input type="text" class="form-control" name="name" value="{{ old('name', isset($record) ? $record->name : '') }}" required>

                                                        @if ($errors->has('name'))
                                                            <span class="help-block">{{ $errors->first('name') }}</span>
                                                        @endif
                                                    </div>
                                                </div>

                                                <div class="form-group row {{ $errors->has('email') ? ' has-error' : '' }}">
                                                    <label class="col-sm-3 col-form-label required">Email :</label>
                                                    <div class="col-sm-9">
                                                        <input type="email" class="form-control" name="email" value="{{ old('email', isset($record) ? $record->email : '') }}" required>

                                                        @if ($errors->has('email'))
                                                            <span class="help-block">{{ $errors->first('email') }}</span>
                                                        @endif
                                                    </div>
                                                </div>

                                                <div class="form-group row {{ $errors->has('phone') ? ' has-error' : '' }}">
                                                    <label class="col-sm-3 col-form-label required">Phone :</label>
                                                    <div class="col-sm-9">
                                                        <input type="number" class="form-control" name="phone" value="{{ old('phone', isset($record) ? $record->phone : '') }}" required>

                                                        @if ($errors->has('phone'))
                                                            <span class="help-block">{{ $errors->first('phone') }}</span>
                                                        @endif
                                                    </div>
                                                </div>

                                                <div class="form-group row {{ $errors->has('password') ? ' has-error' : '' }}" style="{{ isset($edit) ? ' display: none' : ''}}">
                                                    <label class="col-sm-3 col-form-label required">Password :</label>
                                                    <div class="col-sm-9">
                                                        <input type="password" class="form-control" name="password" value="{{ old('password', isset($record) ? $record->password : '') }}" required>

                                                        @if ($errors->has('password'))
                                                            <span class="help-block">{{ $errors->first('password') }}</span>
                                                        @endif
                                                    </div>
                                                </div>

                                                <div class="form-group row {{ $errors->has('password_confirmation') ? ' has-error' : '' }}" style="{{ isset($edit) ? ' display: none' : ''}}">
                                                    <label class="col-sm-3 col-form-label required">Confirm Password :</label>
                                                    <div class="col-sm-9">
                                                        <input type="password" class="form-control" name="password_confirmation" required>

                                                        @if ($errors->has('password_confirmation'))
                                                            <span class="help-block">{{ $errors->first('password_confirmation') }}</span>
                                                        @endif
                                                    </div>
                                                </div>

                                                <div class="form-group row {{ $errors->has('role') ? ' has-error' : '' }}">
                                                    <label class="col-sm-3 col-form-label required">Role :</label>
                                                    <div class="col-sm-9">
                                                        <select name="role" class="form-control select2" required>
                                                            @php($role = old('role', isset($record) ? $record->role : ''))
                                                            @foreach (['User', 'Admin', 'Staff'] as $item)
                                                                <option value="{{ $item }}" {{ $role == $item ? 'selected' : '' }}>{{ $item }}</option>
                                                            @endforeach
                                                        </select>

                                                        @if ($errors->has('role'))
                                                            <span class="help-block"><strong>{{ $errors->first('role') }}</strong></span>
                                                        @endif
                                                    </div>
                                                </div>

                                                <div class="form-group row {{ $errors->has('status') ? ' has-error' : '' }}">
                                                    <label class="col-sm-3 col-form-label required">User Status :</label>
                                                    <div class="col-sm-9">
                                                        <select name="status" class="form-control select2" required>
                                                            @php($status = old('status', isset($record) ? $record->status : ''))
                                                            @foreach (['Active', 'Inactive'] as $item)
                                                                <option value="{{ $item }}" {{ $status == $item ? 'selected' : '' }}>{{ $item }}</option>
                                                            @endforeach
                                                        </select>

                                                        @if ($errors->has('status'))
                                                            <span class="help-block"><strong>{{ $errors->first('status') }}</strong></span>
                                                        @endif
                                                    </div>
                                                </div>

                                                <div class="form-group text-center">
                                                    <button type="submit" class="btn btn-success" style="width: 100px;">
                                                        <i class="bi bi-plus-circle"></i> {{ isset($edit) ? 'Update' : 'Create' }}
                                                    </button>

                                                    <button type="reset" class="btn btn-info" style="width: 100px;">
                                                        <i class="bi bi-arrow-repeat"></i> Reset
                                                    </button>
                                                </div>
                                            </div>

                                            <div class="col-sm-3 text-center">
                                                @php($imagePath = isset($record) && $record->image != null ? asset('storage/users/' . $record->image) : asset('images/default_image.png'))
                                                <figure class="figure">
                                                    <img src="{{ $imagePath }}" id="img_preview" class="rounded img-fluid">
                                                    <figcaption class="figure-caption text-left mt-1">Image Preview.</figcaption>
                                                </figure>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </section>
@endsection
