/**
  * reply
  * @param type $value 
  * @return type view
  */
 public function reply($id, Request $request)
 {
     $comment = $request->input('comment');
     if ($comment != null) {
         $tickets = Tickets::where('id', '=', $id)->first();
         $tickets->closed_at = null;
         $tickets->closed = 0;
         $tickets->reopened_at = date('Y-m-d H:i:s');
         $tickets->reopened = 1;
         $threads = new Ticket_Thread();
         $threads->user_id = Auth::user()->id;
         $threads->ticket_id = $tickets->id;
         $threads->poster = "client";
         $threads->body = $comment;
         try {
             $threads->save();
             $tickets->save();
             return \Redirect::back()->with('success1', 'Successfully replied');
         } catch (Exception $e) {
             return \Redirect::back()->with('fails1', $e->errorInfo[2]);
         }
     } else {
         return \Redirect::back()->with('fails1', 'Please fill some data!');
     }
 }
 /**
  * reply
  * @param type $value 
  * @return type view
  */
 public function reply($id, Request $request)
 {
     $comment = $request->input('comment');
     if ($comment != null) {
         $tickets = Tickets::where('id', '=', $id)->first();
         $threads = new Ticket_Thread();
         $threads->user_id = Auth::user()->id;
         $threads->ticket_id = $tickets->id;
         $threads->poster = "client";
         $threads->body = $comment;
         $threads->save();
         return \Redirect::back()->with('success1', 'Successfully replied');
     } else {
         return \Redirect::back()->with('fails1', 'Please fill some data!');
     }
 }
 /**
  * Function to surrender a ticket
  * @param type $id
  * @return type bool
  */
 public function surrender($id)
 {
     $ticket = Tickets::where('id', '=', $id)->first();
     // if($ticket->assigned_to == Auth::user()->id)
     // {
     $InternalContent = Auth::user()->first_name . " " . Auth::user()->last_name . " has Surrendered the assigned Ticket";
     $thread = Ticket_Thread::where('ticket_id', '=', $id)->first();
     $NewThread = new Ticket_Thread();
     $NewThread->ticket_id = $thread->ticket_id;
     $NewThread->user_id = Auth::user()->id;
     $NewThread->is_internal = 1;
     $NewThread->poster = Auth::user()->role;
     $NewThread->title = $thread->title;
     $NewThread->body = $InternalContent;
     $NewThread->save();
     // }
     $ticket->assigned_to = null;
     $ticket->save();
     return 1;
 }
 /**
  * Ticekt reply
  * @param type Ticket_Thread $thread
  * @param type TicketRequest $request
  * @return type Response
  */
 public function reply(Ticket_Thread $thread, TicketRequest $request)
 {
     $thread->ticket_id = $request->input('ticket_ID');
     $thread->title = $request->input('To');
     $thread->user_id = Auth::user()->id;
     $thread->body = $request->input('ReplyContent');
     $thread->poster = 'user';
     $thread->save();
     $ticket_id = $request->input('ticket_ID');
     $tickets = Tickets::where('id', '=', $ticket_id)->first();
     $thread = Ticket_Thread::where('ticket_id', '=', $ticket_id)->first();
     return Redirect("thread/" . $ticket_id);
 }
 /**
  * Function to delete ticket
  * @param type $id
  * @param type Tickets $ticket
  * @return type string
  */
 public function delete($ids, $ticket)
 {
     try {
         foreach ($ids as $id) {
             $ticket_delete = $ticket->where('id', '=', $id)->first();
             if ($ticket_delete) {
                 if ($ticket_delete->status == 5) {
                     $ticket_delete->delete();
                     $ticket_threads = Ticket_Thread::where('ticket_id', '=', $id)->get();
                     if ($ticket_threads) {
                         foreach ($ticket_threads as $ticket_thread) {
                             if ($ticket_thread) {
                                 $ticket_thread->delete();
                             }
                         }
                     }
                     $ticket_attachments = Ticket_attachments::where('thread_id', '=', $id)->get();
                     if ($ticket_attachments) {
                         foreach ($ticket_attachments as $ticket_attachment) {
                             if ($ticket_attachment) {
                                 $ticket_attachment->delete();
                             }
                         }
                     }
                 } else {
                     $ticket_delete->is_deleted = 0;
                     $ticket_delete->status = 5;
                     $ticket_delete->save();
                     $ticket_status_message = Ticket_Status::where('id', '=', $ticket_delete->status)->first();
                     $thread = new Ticket_Thread();
                     $thread->ticket_id = $ticket_delete->id;
                     $thread->user_id = Auth::user()->id;
                     $thread->is_internal = 1;
                     $thread->body = $ticket_status_message->message . " " . Auth::user()->first_name . " " . Auth::user()->last_name;
                     $thread->save();
                 }
             } else {
                 return "ticket not found";
             }
         }
         return "your tickets has been deleted";
     } catch (\Exception $e) {
         return $e->getMessage();
     }
 }