<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\ProjectComment;

class ProjectCommentController extends Controller
{
    public function add(Request $request)
    {
        $data = $request->all();

        $data['created_by'] = $this->currentName;

        $result = ProjectComment::create($data);

        if($result){
            return response()->json(['status'=>'success','message'=>'add comment successfully!','code'=>200], 200);
        }else{
            return response()->json(['status'=>'failed','message'=>'add comment failed!','code'=>400], 400);
        }
    }

    public function update(Request $request, $id)
    {
        if(!$id || (int) $id < 0 || $id==""){
            return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
        }

        $data = ProjectComment::find($id);

        if($data){
            $result = $data->update($request->all());
        }else{
            return response()->json(['status'=>'failed','message'=>'data project  not found!','code'=>400], 400);
            die();
        }
        
        if($result){
            return response()->json(['status'=>'success','message'=>'data project  successfully updated!','code'=>200], 200);
        }else{
            return response()->json(['status'=>'failed','message'=>'data project  failed updated!','code'=>400], 400);
        }
    }

    public function search(Request $request)
    {
        $payload = $request->all();
        $dataBuilder = $this->setUpPayload($payload, 'm_project_comment');
        $builder = $dataBuilder['builder'];
        $countBuilder = $dataBuilder['count'];
        $dataGet = $builder->get();
        $totalRecord = $countBuilder->count();
        return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
    }
}