<?php

namespace App\Http\Controllers;

use App\query;
use App\query_reply;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class QueryController extends Controller
{
    public function index($id)
    {
        $query = DB::table('query')
            ->join('query_type', 'query.query_type', '=', 'query_type.id')
            ->select('query.*', 'query_type.type as query_name')
            //->selectRaw("DATE_FORMAT('query.query_date', '%d/%l/%Y') as date")
            ->where('query.submitted_by', '=', $id)
            ->orderBy('query.id', 'desc')
            ->get();
        if ($query) {
            $data['query'] = $query;
            $data['status'] = 1;
            $data['message'] = "Queries retrieved successfully";
            return response($data, 200);
        } else {
            $data['status'] = 0;
            $data['message'] = "Query retrieval failed";
            return response($data, 400);
        }
    }

    public function show()
    {
        $query = DB::table('query')
            ->join('query_type', 'query.query_type', '=', 'query_type.id')
            ->join('users', 'query.submitted_by', '=', 'users.id')
            ->select('query.*', 'query_type.type as query_type', 'users.fullname')
            ->orderBy('query.id', 'desc')
            ->get();
        return view("queries_list", compact('query'));

    }

    public function getReplies(Request $request)
    {
        $id = $request->id;
        $reply = DB::table('query_reply as qr')
            ->leftjoin('users as us', 'qr.replied_by', '=', 'us.id')
            ->leftjoin('user_type as ut', 'us.user_type_id', '=', 'ut.id')
            ->select('qr.*', 'us.fullname')
            ->where('qr.query_id', '=', $id)
            ->orderby('qr.id', 'desc')
            ->get();
//        $query = DB::table('query')
//            ->join('query_type', 'query.query_type', '=', 'query_type.id')
//            ->join('users', 'query.submitted_by', '=', 'users.id')
//            ->select('query.*','query_type.type as query_type','users.fullname')
//            ->orderBy('query.id','desc')
//            ->get();
//
        return view("query_replies", compact('reply','id'));
//        $data['reply']=$reply;
//        return response($data, 200);
    }

    public function store(Request $request)
    {
        $input = $request->all();
        $validator = \Validator::make($input, [
            'query_type' => 'required|integer',
            'submitted_by' => 'required|integer',
            'query_desc' => 'required|string',
        ]);
        if ($validator->passes()) {
            $query = new query();
            $query->query_type = $request->input('query_type');
            $query->submitted_by = $request->input('submitted_by');
            $query->query_desc = $request->input('query_desc');
            $query->query_date = Carbon::now();
            $query->save();
            $data['status'] = 1;
            $data['message'] = 'Query submitted successfully';
            $data['id'] = $query->id;
            return response($data, 200);
        } else {
            $data['message'] = $validator->errors(0);
            $data['status'] = 0;
            return response($data, 400);
        }
    }

    public function submitReply(Request $request)
    {
        $input = $request->all();
        $reply = new query_reply();
        $reply->query_id = $input['query_id'];
        $reply->query_reply = $input['query_reply'];
        $reply->replied_by = $input['replied_by'];
        $reply->save();
        $data['status'] = 1;
        $data['message'] = 'Reply submitted successfully';
        $data['id'] = $reply->id;
        return response($data, 200);

    }
}
