<?php

$user_id = Sentry::getUser()->id;
$user_group = UsersGroups::where('user_id', '=', $user_id)->get()->first();
$userDetails = UserDetails::where('user_id', '=', $user_id)->get()->first();
?>
@section('upper-dropdown')
<a data-toggle="dropdown" data-hover="dropdown" class="dropdown-toggle" data-close-others="true" href="#">
    @if($userDetails->pic == null)
    <img src="{{ URL::asset('assets/projects/images/no_img.png') }}" class="img-circle" alt="" width="30px" height="30px">
    @else
    <img src="{{ URL::asset('assets/projects/images/profilepics/'.$userDetails->pic) }}" class="img-circle" alt="" width="30px" height="30px">
    @endif
    <span class="username hidden-xs">{{ $userDetails->first_name }} {{ $userDetails->last_name }}</span> <i class="fa fa-caret-down "></i>
</a>
@stop
@section('left-user-profile')
<div class="inline-block">
    @if($userDetails->pic == null)
    <img src="{{ URL::asset('assets/projects/images/no_img.png') }}" class="img-circle" alt="" width="50px" height="50px">
    @else
    <img src="{{ URL::asset('assets/projects/images/profilepics/'.$userDetails->pic) }}" alt="" height="50px" width="50px">
    @endif
</div>
<div class="inline-block">
    <h5 class="no-margin"> Welcome </h5>
    <h4 class="no-margin"> {{ $userDetails->first_name }} {{ $userDetails->last_name }} </h4>
</div>
@stop

@section('left-menu')
 public function delete($user_id)
 {
     $tickets = Tickets::where('customer_id', $user_id)->get();
     //Delete tickets
     foreach ($tickets as $ticket) {
         TicketAttachments::where('thread_id', $ticket->id)->delete();
         MessageThread::where('id', $ticket->thread_id)->delete();
         ThreadMessages::where('thread_id', $ticket->thread_id)->delete();
     }
     Tickets::where('customer_id', $user_id)->delete();
     //Delete Chat and Conversations
     $online_users = OnlineUsers::where('user_id', $user_id)->get();
     foreach ($online_users as $online_user) {
         MessageThread::where('id', $online_user->thread_id)->delete();
         ThreadMessages::where('thread_id', $online_user->thread_id)->delete();
     }
     OnlineUsers::where('user_id', $user_id)->delete();
     $closed_conversations = ClosedConversations::where('user_id', $user_id)->get();
     foreach ($closed_conversations as $closed_conversation) {
         MessageThread::where('id', $closed_conversation->thread_id)->delete();
         ThreadMessages::where('thread_id', $closed_conversation->thread_id)->delete();
     }
     ClosedConversations::where('user_id', $user_id)->delete();
     UsersGroups::where('user_id', $user_id)->delete();
     CompanyCustomers::where("customer_id", $user_id)->delete();
     User::where("id", $user_id)->delete();
     RecentActivities::createActivity("Customer deleted by User ID:" . Auth::user()->id . " User Name:" . Auth::user()->name);
     Session::flash('success_msg', trans('msgs.customer_deleted_success'));
     return Redirect::to('/customers/all');
 }
 public function delete($company_id)
 {
     $departments = Department::where('company_id', $company_id)->get();
     if (Config::get('site-config.is_demo') && $company_id == 1) {
         Session::flash('error_msg', 'Demo : Feature is disabled');
         return Redirect::to('/dashboard');
     }
     foreach ($departments as $department) {
         if (!empty($department)) {
             $tickets = Tickets::where('department_id', $department->id)->get();
             //Delete tickets
             foreach ($tickets as $ticket) {
                 TicketAttachments::where('thread_id', $ticket->id)->delete();
                 MessageThread::where('id', $ticket->thread_id)->delete();
                 ThreadMessages::where('thread_id', $ticket->thread_id)->delete();
             }
             Tickets::where('department_id', $department->id)->delete();
             //Delete Chat and Conversations
             $online_users = OnlineUsers::where('department_id', $department->id)->get();
             foreach ($online_users as $online_user) {
                 MessageThread::where('id', $online_user->thread_id)->delete();
                 ThreadMessages::where('thread_id', $online_user->thread_id)->delete();
             }
             OnlineUsers::where('department_id', $department->id)->delete();
             $closed_conversations = ClosedConversations::where('department_id', $department->id)->get();
             foreach ($closed_conversations as $closed_conversation) {
                 MessageThread::where('id', $closed_conversation->thread_id)->delete();
                 ThreadMessages::where('thread_id', $closed_conversation->thread_id)->delete();
             }
             ClosedConversations::where('department_id', $department->id)->delete();
             $operators = OperatorsDepartment::where('department_id', $department->id)->lists('user_id');
             if (sizeof($operators) > 0) {
                 User::whereIn('id', $operators)->delete();
                 UsersGroups::whereIn('user_id', $operators)->delete();
             }
             OperatorsDepartment::where('department_id', $department->id)->delete();
             $department_admin = DepartmentAdmins::where('department_id', $department->id)->first();
             if (!empty($department_admin)) {
                 UsersGroups::where('user_id', $department_admin->user_id)->delete();
                 User::where("id", $department_admin->user_id)->delete();
                 CompanyDepartmentAdmins::where("user_id", $department_admin->user_id)->delete();
                 CannedMessages::where('operator_id', $operators)->delete();
             }
         }
         DepartmentAdmins::where('department_id', $department->id)->delete();
         Department::where('id', $department->id)->delete();
     }
     $company = Company::where('id', $company_id)->first();
     RecentActivities::createActivity("Company <a href='/companies/all'>" . $company->name . "</a> deleted by User Name " . Auth::user()->name . " User ID " . Auth::user()->id);
     Company::where('id', $company_id)->delete();
     Session::flash('success_msg', trans('msgs.company_deleted_success'));
     return Redirect::to('/companies/all');
 }
 public function all()
 {
     $user_ids = [];
     if (\KodeInfo\Utilities\Utils::isDepartmentAdmin(Auth::user()->id)) {
         $department_admin = DepartmentAdmins::where('user_id', Auth::user()->id)->first();
         $department = Department::where('id', $department_admin->department_id)->first();
         $department_ids = Department::where('company_id', $department->company_id)->lists('id');
         $user_ids = DepartmentAdmins::whereIn('department_id', $department_ids)->lists('user_id');
     } elseif (\KodeInfo\Utilities\Utils::isOperator(Auth::user()->id)) {
         $department_admin = DepartmentAdmins::where('user_id', Auth::user()->id)->first();
         $department = Department::where('id', $department_admin->department_id)->first();
         $department_ids = Department::where('company_id', $department->company_id)->lists('id');
         $user_ids = DepartmentAdmins::whereIn('department_id', $department_ids)->lists('user_id');
     } else {
         $group = Groups::where("name", "department-admin")->first();
         $user_ids = UsersGroups::where("group_id", $group->id)->lists("user_id");
     }
     if (sizeof($user_ids) > 0) {
         $this->data["admins"] = User::whereIn("id", $user_ids)->orderBy('id', 'desc')->get();
     } else {
         $this->data["admins"] = [];
     }
     foreach ($this->data["admins"] as $admin) {
         $department_admin = DepartmentAdmins::where('user_id', $admin->id)->first();
         if (!empty($department_admin)) {
             $admin->department = Department::find($department_admin->department_id);
         }
         $company_id = CompanyDepartmentAdmins::where("user_id", $admin->id)->pluck('company_id');
         $admin->company = Company::find($company_id);
     }
     return View::make('department_admins.all', $this->data);
 }
 public function all()
 {
     $group = Groups::where("name", "admin")->first();
     $user_ids = UsersGroups::where("group_id", $group->id)->lists("user_id");
     if (sizeof($user_ids) > 0) {
         $this->data["users"] = User::whereIn("id", $user_ids)->get();
     } else {
         $this->data["users"] = [];
     }
     return View::make('accounts.all', $this->data);
 }
 public function online()
 {
     if (\KodeInfo\Utilities\Utils::isDepartmentAdmin(Auth::user()->id)) {
         $department_admin = DepartmentAdmins::where('user_id', Auth::user()->id)->first();
         $department = Department::where('id', $department_admin->department_id)->first();
         $user_ids = OperatorsDepartment::where('department_id', $department->id)->lists('user_id');
     } else {
         $group = Groups::where("name", "operator")->first();
         $user_ids = UsersGroups::where("group_id", $group->id)->lists("user_id");
     }
     if (sizeof($user_ids) > 0) {
         $this->data["operators"] = User::whereIn("id", $user_ids)->where("is_online", 1)->get();
     } else {
         $this->data["operators"] = [];
     }
     foreach ($this->data["operators"] as $operator) {
         $department_id = OperatorsDepartment::where('user_id', $operator->id)->pluck("department_id");
         $department = Department::find($department_id);
         $company = Company::find($department->company_id);
         $operator->department = $department;
         $operator->company = $company;
     }
     $this->data['permissions'] = Permissions::all();
     $this->data['departments'] = Department::all();
     return View::make('operators.all', $this->data);
 }