/**
  * Replying a ticket
  * @param type Ticket_Thread $thread
  * @param type TicketRequest $request
  * @return type bool
  */
 public function reply(Ticket_Thread $thread, TicketRequest $request, Ticket_attachments $ta)
 {
     $attachments = $request->file('attachment');
     $check_attachment = null;
     // Event fire
     $eventthread = $thread->where('ticket_id', $request->input('ticket_ID'))->first();
     $eventuserid = $eventthread->user_id;
     $emailadd = User::where('id', $eventuserid)->first()->email;
     $source = $eventthread->source;
     $form_data = $request->except('ReplyContent', 'ticket_ID', 'attachment');
     \Event::fire(new \App\Events\ClientTicketFormPost($form_data, $emailadd, $source));
     // dd($attachments);
     // }
     //return $attachments;
     $reply_content = $request->input('ReplyContent');
     $thread->ticket_id = $request->input('ticket_ID');
     $thread->poster = 'support';
     $thread->body = $request->input('ReplyContent');
     $thread->user_id = Auth::user()->id;
     $ticket_id = $request->input('ticket_ID');
     //dd($ticket_id);
     $tickets = Tickets::where('id', '=', $ticket_id)->first();
     $tickets->isanswered = '1';
     $tickets->save();
     $ticket_user = User::where('id', '=', $tickets->user_id)->first();
     if ($tickets->assigned_to == 0) {
         $tickets->assigned_to = Auth::user()->id;
         $tickets->save();
         $thread2 = new Ticket_Thread();
         $thread2->ticket_id = $thread->ticket_id;
         $thread2->user_id = Auth::user()->id;
         $thread2->is_internal = 1;
         $thread2->body = "This Ticket have been assigned to " . Auth::user()->first_name . " " . Auth::user()->last_name;
         $thread2->save();
     }
     if ($tickets->status > 1) {
         $tickets->status = '1';
         $tickets->closed_at = '0';
         $tickets->closed = null;
         $tickets->reopened_at = date('Y-m-d H:i:s');
         $tickets->reopened = 1;
         $tickets->isanswered = '1';
         $tickets->save();
     }
     $thread->save();
     //$atachPath = '';
     foreach ($attachments as $attachment) {
         if ($attachment != null) {
             $name = $attachment->getClientOriginalName();
             //dd(dirname($attachment));
             $type = $attachment->getClientOriginalExtension();
             $size = $attachment->getSize();
             $data = file_get_contents($attachment->getRealPath());
             // $tem_path = $attachment->getRealPath();
             // $tem = basename($tem_path).PHP_EOL;
             // //dd($tem);
             $attachPath = $attachment->getRealPath();
             //dd($attachPath);
             $ta->create(['thread_id' => $thread->id, 'name' => $name, 'size' => $size, 'type' => $type, 'file' => $data, 'poster' => 'ATTACHMENT']);
             $check_attachment = 1;
         } else {
             $check_attachment = null;
         }
     }
     $thread = Ticket_Thread::where('ticket_id', '=', $ticket_id)->first();
     $ticket_subject = $thread->title;
     $user_id = $tickets->user_id;
     $user = User::where('id', '=', $user_id)->first();
     $email = $user->email;
     $user_name = $user->user_name;
     $ticket_number = $tickets->ticket_number;
     $company = $this->company();
     $username = $ticket_user->user_name;
     if (!empty(Auth::user()->agent_sign)) {
         $agentsign = Auth::user()->agent_sign;
     } else {
         $agentsign = null;
     }
     // foreach($attachments as $attachment){
     // $pathToFile = $attachment->getRealPath();
     // $name = $attachment->name;
     // $data = $attachment->file;
     // $display = $attachment->file;
     // $mime = $attachment->type;
     // }
     //dd(sizeOf($attachments));
     //$size = sizeOf($attachments);
     //dd($thread->id);\
     // mail to main user
     //$path = 'C:\wamp\tmp\php5D3A.tmp';
     // Event
     \Event::fire(new \App\Events\FaveoAfterReply($reply_content, $user->phone_number, $request, $tickets));
     Mail::send(array('html' => 'emails.ticket_re-reply'), ['content' => $reply_content, 'ticket_number' => $ticket_number, 'From' => $company, 'name' => $username, 'Agent_Signature' => $agentsign], function ($message) use($email, $user_name, $ticket_number, $ticket_subject, $attachments, $check_attachment) {
         $message->to($email, $user_name)->subject($ticket_subject . '[#' . $ticket_number . ']');
         // if(isset($attachments)){
         if ($check_attachment == 1) {
             $size = sizeOf($attachments);
             for ($i = 0; $i < $size; $i++) {
                 $message->attach($attachments[$i]->getRealPath(), ['as' => $attachments[$i]->getClientOriginalName(), 'mime' => $attachments[$i]->getClientOriginalExtension()]);
             }
         }
     }, true);
     $collaborators = Ticket_Collaborator::where('ticket_id', '=', $ticket_id)->get();
     foreach ($collaborators as $collaborator) {
         //mail to collaborators
         $collab_user_id = $collaborator->user_id;
         $user_id_collab = User::where('id', '=', $collab_user_id)->first();
         $collab_email = $user_id_collab->email;
         if ($user_id_collab->role == "user") {
             $collab_user_name = $user_id_collab->user_name;
         } else {
             $collab_user_name = $user_id_collab->first_name . " " . $user_id_collab->last_name;
         }
         Mail::send('emails.ticket_re-reply', ['content' => $reply_content, 'ticket_number' => $ticket_number, 'From' => $company, 'name' => $collab_user_name, 'Agent_Signature' => $agentsign], function ($message) use($collab_email, $collab_user_name, $ticket_number, $ticket_subject, $attachments, $check_attachment) {
             $message->to($collab_email, $collab_user_name)->subject($ticket_subject . '[#' . $ticket_number . ']');
             if ($check_attachment == 1) {
                 $size = sizeOf($attachments);
                 for ($i = 0; $i < $size; $i++) {
                     $message->attach($attachments[$i]->getRealPath(), ['as' => $attachments[$i]->getClientOriginalName(), 'mime' => $attachments[$i]->getClientOriginalExtension()]);
                 }
             }
         }, true);
     }
     return 1;
 }
 /**
  * Create Attachment
  * @param type $thread
  * @param type $attach
  * @return int
  */
 public function attach($thread, $attach)
 {
     try {
         $ta = new Ticket_attachments();
         foreach ($attach as $file) {
             $ta->create(['thread_id' => $thread, 'name' => $file['name'], 'size' => $file['size'], 'type' => $file['type'], 'file' => $file['file'], 'poster' => 'ATTACHMENT']);
         }
         $ta->create(['thread_id' => $thread, 'name' => $name, 'size' => $size, 'type' => $type, 'file' => $file, 'poster' => 'ATTACHMENT']);
         return 1;
     } catch (\Exception $e) {
         return $e->getMessage();
     }
 }