@extends("layouts.dashboard")

@section("title", "Users - Dashboard | TechnoBlog")

@section("dashboard_content")

	<div class="my-3 my-md-5">

		<div class="container">

			<div class="page-header">
				<h1 class="page-title">Users</h1>
			</div>

			<div class="row">

				<div class="col-sm-12">
					<div class="card">

						<div class="card-header">
							<h3 class="card-title">List of users</h3>
							<a href="{{ route("dashboard.users.create") }}" style='margin-left: auto; order: 2;'>
								<button type="button" class="btn btn-primary">Add new</button>
							</a>
						</div>

						<div class="table-responsive pt-5 pb-5">

							<table id="dttable" class="table table-hover table-outline table-vcenter text-nowrap card-table">
								<thead>
									<tr>
										<th class="text-center w-1"><i class="icon-people"></i></th>
										<th>User</th>
										<th>Posts</th>
										<th>Technoblog E-mail</th>
										<th>Registration Date</th>
										<th>Status</th>
										<th class="text-center">Actions</th>
									</tr>
								</thead>
								<tbody>
									@foreach ($users as $user)
										<tr>
											<td class="text-center">
												<div class="avatar d-block" style="background-image: url(demo/faces/female/26.jpg)"></div>
											</td>
											<td>
												<div>{{ $user->name .' '. $user->surname }}</div>
												<div class="small text-muted">
													{{ ucfirst($user->role->name) }}
													<br>
													{{ $user->email }}
													<br>
													{{ $user->mobile_number }}
												</div>
											</td>
											<td>
												{{ $user->getPostsCount($user->id) }}
											</td>
											<td>{{ $user->userProfile->technoblog_email }}</td>
											<td>{{ date('d.m.Y', strtotime($user->created_at)) }}</td>
											<td>
												@if($user->is_confirmed)
													@if ($user->is_active)
														Active
													@else
														Blocked
													@endif
												@else
													New user
												@endif
											</td>
											<td class="text-center">
												@if ($user->is_confirmed)
													<div class="item-action dropdown">
														<a href="javascript:void(0)" data-toggle="dropdown" class="icon" aria-expanded="false"><i class="fe fe-more-vertical"></i></a>
														<div class="dropdown-menu dropdown-menu-right" x-placement="bottom-end" style="position: absolute; transform: translate3d(15px, 20px, 0px); top: 0px; left: 0px; will-change: transform;">

															<a href="{{ route("blog.user-profile", ["profileLink" => $user->userProfile->profile_link]) }}" target="_blank" class="dropdown-item"><i class="dropdown-icon fe fe-user"></i> View profile </a>

															@if (auth()->user()->id != $user->id)

																@if ($user->is_active)
																	<a href="javascript:void(0)" class="actionLink dropdown-item" style="cursor: pointer;" data-action="{{ route("dashboard.users.block", ["id" => $user->id]) }}" data-method="patch"><i class="dropdown-icon fe fe-lock"></i> Block </a>
																@else
																	<a href="javascript:void(0)" class="actionLink dropdown-item" style="cursor: pointer;" data-action="{{ route("dashboard.users.unblock", ["id" => $user->id]) }}" data-method="patch"><i class="dropdown-icon fe fe-unlock"></i> Unblock </a>
																@endif

																<div class="dropdown-divider"></div>

																@if ($user->post->count() > 0)
																	<a href="javascript:void(0)" data-from="{{ $user->id }}" class="dropdown-item transferPostsAndDelete"style="cursor: pointer;"><i class="dropdown-icon fe fe-trash-2 text-danger"></i> Delete </a>
																@else
																	<a href="javascript:void(0)" class="actionLink dropdown-item" style="cursor: pointer;" data-action="{{ route("dashboard.users.destroy", ["id" => $user->id]) }}" data-method="delete"><i class="dropdown-icon fe fe-trash-2 text-danger"></i> Delete </a>
																@endif

															@endif

														</div>
													</div>
												@else
													<a href="javascript:void(0)" class="actionLink dropdown-item" style="cursor: pointer;" data-action="{{ route("dashboard.users.destroy", ["id" => $user->id]) }}" data-method="delete"><i class="dropdown-icon fe fe-trash-2 text-danger"></i> Delete </a>
												@endif

											</td>

										</tr>

									@endforeach

								</tbody>

							</table>

							<div class="modal fade deleteUser" id="transferPosts" tabindex="-1" role="dialog" aria-hidden="true">

								<div class="modal-dialog modal-dialog-centered" role="document">

									<div class="modal-content">

										<div class="modal-header">
											<h5 class="modal-title" id="exampleModalLabel">Delete User</h5>
											<button type="button" class="close" data-dismiss="modal" aria-label="Close"></button>
										</div>

										<div class="modal-body">

											<form id="transferPosts" action="/dashboard/settings/transfer" method="post">

												@csrf

												<div class="form-group">
													<label class="form-label">Transfer posts to</label>
													<select class="selectData" id="selectWithContent" name="to">
														@foreach ($users as $user)
															<option value="{{ $user->id }}" {{ (old("to") == $user->id) ? "selected" : "" }} data-data='{"image": "/uploads/users/{{ $user->userProfile->profile_photo_link }}"}'>{{ $user->getFullName() }}</option>
														@endforeach
													</select>
												</div>

												<input type="hidden" name="from" value="">

												<div class="modal-footer">
													<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
													<input type="submit" class="btn btn-primary" value="Transfer">
												</div>

											</form>

										</div>

									</div>

								</div>

							</div>

						</div>

					</div>

				</div>

			</div>

		</div>

	</div>

@endsection
