<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\FolderDocumentProyek;
use App\Models\DokumenProject;

class FolderDocumentProyekController extends Controller
{ 
    public function add(Request $request){
        $this->validate($request, [
            'name_folder'   => 'required',
            'proyek_id'     => 'required'
        ]);
        $data = $request->all(); 
        $data['created_by'] = $this->currentName;

        $result = FolderDocumentProyek::create($data); 
        if($result){
            return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfull created','code'=>200]);
        }else{
            return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed created','code'=>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 = FolderDocumentProyek::find($id);
        if($data){
            $result = $data->update($request->all());
        }else{
            return response()->json(['status'=>'failed','message'=>'Data Folder Document Proyek not found!','code'=>400], 400);
            die();
        }
        
        if($result){
            return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfully updated!','code'=>200], 200);
        }else{
            return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed updated!','code'=>400], 400);
        }
    }

    public function delete($id)
    {
        $data = FolderDocumentProyek::find($id);

        if($data){
            $delete = $data->delete();
        }else{
            return response()->json(['status'=>'failed','message'=>'data Folder Document Proyek not found!','code'=>400], 400);
            die();
        }
        

        if($delete){
            return response()->json(['status'=>'success','message'=>'Folder Document Proyek successfully deleted!','code'=>200], 200);
        }else{
            return response()->json(['status'=>'failed','message'=>'Folder Document Proyek failed deleted!','code'=>400], 400);
        }
    }

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

    public function list()
    {
        $data = FolderDocumentProyek::all();
        $countData = $data->count();

        if($data){
            return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
        }else{
            return response()->json(['status'=>'failed','message'=>'failed get list tools request, please try again later!','code'=>400], 400);
        }
    }
    public function getTree($id)
    {
        $data = FolderDocumentProyek::where('proyek_id', $id)->whereNull('parent_id')->orderByRaw('id ASC')->get();
        $dataFile = DokumenProject::where('type_dokumen', 'project-document-out-folder')->where('ref_id', $id)->orderByRaw('id ASC')->get();
        $finalData = [];
        foreach($data as $objRow){
            $objRow->children = $this->getChildren($id, $objRow->id);
            $objRow->key = rand(1, 1000);
            $objRow->isDir = true;
            $finalData[] = $objRow;
        }

        foreach($dataFile as $objRowFile) {
            $objRowFile->key = rand(1, 1000);
            $objRowFile->isDir = false;
            $objRowFile->proyek_id = $id;
            $finalData[] = $objRowFile;
        }

        return response()->json(['status'=>'success','data'=>$finalData,'code'=>200], 200);
    }

    private function getChildren($proyek_id, $parent_id)
    {
        $finalData = [];
        $data = FolderDocumentProyek::where('proyek_id', $proyek_id)->where('parent_id', $parent_id)->orderByRaw('id ASC')->get();
        $dataFile = DokumenProject::where('type_dokumen', 'project-document-in-folder')->where('ref_id', $parent_id)->orderByRaw('id ASC')->get();
        foreach($data as $objRow){
            $objRow->key = rand(1, 1000);
            $objRow->children = $this->getChildren($proyek_id, $objRow->id);
            $objRow->isDir = true;
            $finalData[] = $objRow;
        }
        
        foreach($dataFile as $objRowFile) {
            $objRowFile->key = rand(1, 1000);
            $objRowFile->isDir = false;
            $objRowFile->proyek_id = $proyek_id;
            $finalData[] = $objRowFile;
        }

        return $finalData;
    }
}