<?php

namespace App\Http\Controllers;

error_reporting(9);

use App\Models\subTicket;
use Illuminate\Http\Request;
use App\Models\ticket;
use App\Models\Admin;
use App\Models\User;
use App\Models\project;

class ticketController extends Controller
{

    public function index()
    {

        //نمایش تسک یا نوت بر اثاس مجوز و ادمین
        if ($Admin = User::where('name', $_COOKIE['admin'])->first()){
            $tasks = ticket::all();
        }
        return view('content.ticket.index',compact('tasks','Admin'));
    }

    public function addTicket()
    {
        if ($Admin = User::where('name', $_COOKIE['admin'])->first()){
            $tasks = ticket::all();
            $Projects = project::where('user_id', $Admin->id)->get();

        }

        return view('content.ticket.addTicket',compact('tasks','Admin','Projects'));
    }

    public function saveNewTicket(Request $request)
    {


        $ticket = new ticket();
        if ($request->project_id){
            $project = explode('|-|',$request->project_id);
            $ticket->project_id   = $project[0]  ;
            $ticket->project   = $project[1]  ;
        }
        if ($request->admin_id){
            $admin = explode('|-|',$request->admin_id);
            $ticket->admin_id   = $admin[0]  ;
            $ticket->admin   = $admin[1]  ;
        }
        $ticket->status  = $request->user_status ;
        $ticket->title   = $request->title  ;
        $ticket->user_id  = $request->user_id ;
        $ticket->user  = $request->user ;
        $ticket->department  = $request->department ;
        $ticket->save();



        $user = User::where('name',$_COOKIE['admin'])->first();

        return view('content.ticket.addSubTicket', compact('ticket','user'));

    }

    public function subTicket(Request $request,$id)
    {

        $user = User::where('name',$_COOKIE['admin'])->first();
        $ticket = ticket::where('id',$id)->first();
        return view('content.ticket.addSubTicket', compact('ticket','user'));
    }

    public function saveSubTicket(Request $request)
    {

        $ticket = new subTicket();
        $ticket->text  = $request->text ;
        $ticket->file  = $request->file ;
        $ticket->maker  = $request->maker ;
        $ticket->ticket_id  = $request->ticket_id ;
        $ticket->save();
        $ticket = ticket::where('id', $request->ticket_id)->first();
        $user = User::where('name',$_COOKIE['admin'])->first();
        return view('content.ticket.addSubTicket', compact('ticket','user'));

    }

    public function saveSubTicketFile(Request $request)
    {
        dd($request);
        $ticket = new subTicket();
        $ticket->file  = $request->file ;
        $ticket->maker  = $request->maker ;
        $ticket->ticket_id  = $request->ticket_id ;
        $ticket->save();
        $ticket = ticket::where('id', $request->ticket_id)->first();
        $user = User::where('name',$_COOKIE['admin'])->first();
        return view('content.ticket.addSubTicket', compact('ticket','user'));

        
        File::makeDirectory('../../public_html/accounts/'.$request->name);
        $ImageName = $request->imagename;
        request()->validate(['image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:200048',]);
        $imageName = $ImageName.'.JPG';
        request()->image->move(public_path('../../public_html/accounts/'.$request->imagename), $imageName);
//        request()->image->move(public_path('accounts/'.$id), $imageName);
        header("Location: https://admin.borjeweb.com/");
//
//        return view("templates/bw/admin",compact("dbvalue"));
    }
}
