<?php

namespace App\Http\Controllers\Client;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Project;
use App\Models\Group;
use App\Models\Task;
use App\Models\ActivityLog;
use App\Models\Notification;
use App\Models\CompanyModel;
use App\Models\ServiceList;
use App\Models\Ticket;
use App\Models\TicketThread;
use App\Models\AddressBookModel;
use App\User;
use Auth;
use Mail;

class TicketController extends Controller
{
    function index()
    {
        $client_id=\Auth::user()->id;


        $query=(new Ticket)->newQuery();

        $query->with(['user','created_user','user.company_data']);

        //$query->where('status','!=',3);

        if(\Auth::user()->utype==3)
        {
            $query->where('client_id',$client_id);
        }

        $query->orderBy('id','desc');

        $data['tickets']=$query->paginate(5);
        $data['title']='Support Ticket';
        return view('client.ticket.index',$data);
    }

    function create()
    {
        $data['clients']=CompanyModel::where('status',2)->orderBy('name')->get(['id','name']);
        $data['services']=ServiceList::orderBy('name','asc')->get(['id','name']);

        $data['title']='New Support Ticket';
        return view('client.ticket.create',$data);
    }

    function store(Request $req)
    {
        $this->validate($req,[
            'title'=>'required',
            'service'=>'required',
        ]);

        $client=0;
        if(\Auth::user()->utype==3)
        {
            $client=\Auth::user()->id;
        }
        else
        {
            $client=$req->get('client');
        }

        $documentUpload = $req->file('ticket_document');
        if ( !empty( $documentUpload )) {
              $documentname = $documentUpload->getClientOriginalName();
              $path = 'public/ticket_document/';
              $documentUpload->move($path,$documentname);
              $ticketDocument = $documentname;
        } else {
          $ticketDocument = '';
        }

        $ticket=new Ticket();
        $ticket->client_id          =   $client;
        $ticket->ticket_id          =   $this->generate_ticket_id();
        $ticket->service_id         =   $req->get('service');
        $ticket->title              =   $req->get('title');
        $ticket->details            =   $req->get('details');
        $ticket->priority           =   $req->get('prority');
        $ticket->ticket_document    =   $ticketDocument;
        $ticket->status             =   0;
        $ticket->created_by         =   \Auth::user()->id;

        $ticket->save();


        $client=AddressBookModel::find($ticket->client_id);
        $service=ServiceList::find($ticket->service_id);
        $created_user=User::find($ticket->created_by);
        //dd($ticket->client_id);
        $company_id = $created_user->company;
        $company = CompanyModel::find($company_id);
        $manager_id = $company->account_manager;
        if($manager_id != null){
            $manager_details = User::find($manager_id);
            $manager_email = $manager_details->email;
        } 
        $data['client']=$client;
        $data['created_by']=$created_user;
        $data['company']=$company;
        $data['service']=$service;
        $data['title']='';
        $data['ticket']=$ticket;

        if($manager_id != null){
            Mail::send('emails.ticket_created_email',$data,function($message)use($created_user,$client,$manager_email){
                $message->from('m.revinr@gmail.com', 'Revinr CMS');
                //$message->to($manager_email)->subject('New ticket created');
                if($manager_email)
                {
                    $message->to($manager_email)->subject('New ticket created');
                }
                if($client)
                {
                    $message->to($client->email1)->subject('New ticket created');
                }
                if($created_user)
                {
                    $message->to($created_user->email)->subject('New ticket created');
                }
                $message->bcc('helaldu@gmail.com','Shamim Shams');
            });
        }else{
            Mail::send('emails.ticket_created_email',$data,function($message)use($created_user,$client){
                $message->from('m.revinr@gmail.com', 'Revinr CMS');
                $message->to('helaldu@gmail.com')->subject('New ticket created');
                if($client)
                {
                    $message->to($client->email1)->subject('New ticket created');
                }
                if($created_user)
                {
                    $message->to($created_user->email)->subject('New ticket created');
                }
                $message->cc('sabuj@revinr.com', 'New ticket created');
                $message->bcc('tarikul@revinr.com','New ticket created');
            });
        }

        return redirect('ticket')->with('msg','Ticket created successfully !!');
    }

    function client_mail()
    {

    }


    function admin_mail()
    {
        
    }

    function show($id)
    {   
        $data['ticket']=Ticket::with(['user','created_user','user.company_data'])->find($id);
        // echo "<pre>";
        // print_r ($data['ticket']);
        // echo "</pre>";
        // exit;
        $data['title']='Ticket Details';
        return view('client.ticket.show',$data);
    }


    function edit($id)
    {
        $data['title']='Edit Support Ticket';
        return view('client.ticket.edit',$data);
    }

    function update(Request $req)
    {

    }

    function save_comment(Request $req)
    {
        $comment=new TicketThread();
        $comment->ticket_id=$req->get('ticket');
        $comment->comment_by=\Auth::user()->id;
        $comment->comment=$req->get('comment');
        $comment->display=1;
        $comment->save();
        return "ok";

    }

    function get_comments($tkt_id)
    {
        $data['comments']=TicketThread::where('ticket_id',$tkt_id)->get();
        return view('client.ticket.comment',$data)->render();
    }


    function change_status(Request $req)
    {
        $id=$req->get('ticket_id'); 
        $status=$req->get('status');

        $ticket=Ticket::find($id);
        $ticket->status=$status;
        $ticket->save();

        $client=AddressBookModel::find($ticket->client_id);
        $service=ServiceList::find($ticket->service_id);
        $created_user=User::find($ticket->created_by);

        $data['client']=$client;
        $data['service']=$service;
        $data['title']='';
        $data['ticket']=$ticket;

    
        Mail::send('emails.ticket_status_change_email',$data,function($message)use($created_user,$client){
            $message->from('m.revinr@gmail.com', 'Revinr CMS');
            if($client)
            {
                $message->to($client->email1)->subject('New ticket created');
            }
            if($created_user)
            {
                $message->to($created_user->email)->subject('New ticket created');
            }
            $message->to('helaldu@gmail.com')->subject('Ticket Status Change');
            $message->bcc('shamim@revinr.com','Shamim Shams');
        });

        return "ok";
    }

    function generate_ticket_id()
    {
        $ticket=Ticket::orderBy('id','desc')->first();
        if($ticket)
        {
            $last_ticket_id=$ticket->ticket_id;
            if(empty($last_ticket_id))
            {
                return 'REV-0001';
            }

            $ticket_arr=explode('-',$last_ticket_id);
            $ticket_id=(int)$ticket_arr[1];
            $ticket_id=$ticket_id+1;

            if($ticket_id<10)
            {

                return 'REV-000'.$ticket_id;
            }
            if($ticket_id<100)
            {

                return 'REV-00'.$ticket_id;
            }
            if($ticket_id<1000)
            {

                return 'REV-0'.$ticket_id;
            }
            if($ticket_id<10000)
            {

                return 'REV-'.$ticket_id;
            }
        }
        else
        {
            return 'REV-001';   
        }


    }
}