<?php

namespace App\Http\Controllers;
use App\Models\UserCertificate;
use App\Models\TraceDocument;
use Illuminate\Http\Request;
use Validator;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Storage;
class TraceDocumentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

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


    public function listrace(Request $request)
    {
        $rules = [
            'id_document' => 'required',   
            'subject'=>'required',  
            'sign'=>'required',
            'usersign'=>'required'
        ];
    
        $input     = $request->only('id_document','subject','sign','usersign');
        $validator = Validator::make($input, $rules);
        if ($validator->fails()) {
            return response()->json(['success' => false, 'error' => $validator->messages()]);
        }
       // $date = Carbon::now()->format('Y-m-d H:i:s');
        $tracedocument = TraceDocument::select('trace_documents.*',DB::raw("CONCAT(usuarios.name,' ',usuarios.lasname) as full_name"),'trace_types.description as typet','trace_types.color')
                                        ->join('usuarios','usuarios.id_user','=','trace_documents.fk_user')
                                        ->join('trace_types','trace_types.id_tracetype','=','trace_documents.fk_tracetype')
                                        ->where('fk_document',$request->id_document)
                                        ->orderBy('trace_documents.date_create', 'desc')
                                        ->get();

          $datesign= '';                             
         if($request->sign){
            foreach ($tracedocument as $trace) {
                 if ($trace->fk_user==$request->usersign) {
                    $datesign=$trace->date_create;
                }
            }
            $url = Storage::disk('public')->path('/documentsign/'.$request->subject.'.pdf');
            $certificateuser=UserCertificate::select('user_certificates.*')           
           ->where('fk_iduser',$request->usersign)
           ->get(); 

        foreach ($certificateuser as $email) {
            $email->datesign =  $datesign;
         }
 
         }else{
            $url = Storage::disk('public')->path('/documents/'.$request->subject.'.pdf');
            $certificateuser=[];
         }
                                         
         return response()->json(['success' => true,'urlpdf'=>$url,'certificateuser'=> $certificateuser, 'datatrace' => $tracedocument]);
    }

    public function create(Request $request)
    {
        $rules = [
            'fk_document' => 'required',
            'fk_tracetype'=>'required',
            'fk_user' => 'required',
             
             
        ];
    
        $input     = $request->only('fk_document','fk_tracetype','fk_user');
        $validator = Validator::make($input, $rules);
        if ($validator->fails()) {
            return response()->json(['success' => false, 'error' => $validator->messages()]);
        }
        $date = Carbon::now()->format('Y-m-d H:i:s');
        $tracedocument = TraceDocument::create(['fk_tracetype' =>$request->fk_tracetype,
        'fk_document'=>$request->fk_document,'fk_user'=>$request->fk_user,'date_create'=>$date]);
        return response()->json(['success' => true, 'data' => array("id_tracedoc"=>$tracedocument->id_tracedocument)]);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\TraceDocument  $traceDocument
     * @return \Illuminate\Http\Response
     */
    public function show(TraceDocument $traceDocument)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\TraceDocument  $traceDocument
     * @return \Illuminate\Http\Response
     */
    public function edit(TraceDocument $traceDocument)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\TraceDocument  $traceDocument
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, TraceDocument $traceDocument)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\TraceDocument  $traceDocument
     * @return \Illuminate\Http\Response
     */
    public function destroy(TraceDocument $traceDocument)
    {
        //
    }
}
