<?php

namespace App\Http\Controllers\Api\cms;

use Illuminate\Support\Facades\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\cms\Feedback\Feedback;
use App\Models\cms\Ticket\Ticket;

use App\Models\User\UserHistory;
use App\Models\User;
use Illuminate\Support\Facades\Redirect;

class TicketController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function feedbackView(Request $request)
    {
        $datas = Feedback::orderBy('id', 'DESC')->get();
        $users = User::all();
        return view('manage_tickets.feedbacks', compact('datas', 'users'));
    }

    public function ticketView(Request $request)
    {
        $datas = Ticket::orderBy('id', 'DESC')->get();
        $users = User::all();
        return view('manage_tickets.tickets', compact('datas', 'users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

     public function chat()
     {
         return view('ticket.chat');
     }

    public function createTicket()
    {
        return view('ticket.create');
    }

    public function createFeedback()
    {
        return view('feedback.create');
    }

    public function ticket_details($id)
    {
        $ticket = Ticket::find($id);
        return view('manage_tickets.ticket_details', compact('ticket'));
    }

    public function feedback_details($id)
    {
        $feedback = Feedback::find($id);
        return view('manage_tickets.feedback_details', compact('feedback'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function storeFeedback(Request $request)
    {
        $user = Auth::user();

        $data = new Feedback;
        $data->user_id = $user->id;
        $data->issue_type = $request->input("issue_type");
        $data->module = $request->input("module");
        $data->page_url = $request->input("page_url");
        $data->audio_video_url = $request->input("audio_video_url");
        $data->priority = $request->input("priority");
        if ($request->hasFile('screenshots')) {
            $screenshots = $request->file('screenshots');
            $screenshotsData = [];

            foreach ($screenshots as $screenshot) {
                $filename = time() . '_' . $screenshot->getClientOriginalName();
                $path = public_path('uploads');
                $screenshot->move($path, $filename);

                $screenshotsData[] = $filename;
            }

            $data->screenshots = json_encode($screenshotsData);
        }
        $data->comments = $request->input("comments");
        $data->save();

        session()->flash('success', 'Feedback created successfully.');
        return Redirect::back();
    }

    public function storeTicket(Request $request)
    {
        $user = Auth::user();

        $data = new Ticket;
        $data->user_id = $user->id;
        $data->issue_type = $request->input("issue_type");
        $data->module = $request->input("module");
        $data->page_url = $request->input("page_url");
        $data->audio_video_url = $request->input("audio_video_url");
        $data->priority = $request->input("priority");
        if ($request->hasFile('screenshots')) {
            $screenshots = $request->file('screenshots');
            $screenshotsData = [];

            foreach ($screenshots as $screenshot) {
                $filename = time() . '_' . $screenshot->getClientOriginalName();
                $path = public_path('uploads');
                $screenshot->move($path, $filename);

                $screenshotsData[] = $filename;
            }

            $data->screenshots = json_encode($screenshotsData);
        }
        $data->comments = $request->input("comments");
        $data->save();

        session()->flash('success', 'Ticket created successfully.');
        return Redirect::back();
    }

    public function ticket_status(Request $request)
    {
        $id = $request->input("ticket_id");
        $data = Ticket::find($id);
        $data->status = $request->input("status");
        $data->save();

        session()->flash('success', 'Status updated successfully.');

        return Redirect::back();
    }

    public function feedback_status(Request $request)
    {
        $id = $request->input("feedback_id");
        $data = Feedback::find($id);
        $data->status = $request->input("status");
        $data->save();

        session()->flash('success', 'Status updated successfully.');

        // return redirect()->route('feedback.view');

        return Redirect::back();
    }



    public function destroy($id)
    {
        //
        $cat = DailyDeals::find($id);
        $cat->delete();
        session()->flash('success', 'You\'ve Deleted Expense Account successfully.');

        return Redirect::back();
    }

}
