<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\WorkOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WorkOrdersController 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 create()
    {
        //
    }

    /**
     * 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\WorkOrder  $workOrder
     * @return \Illuminate\Http\Response
     */
    public function show(WorkOrder $workOrder)
    {
        return response()->json($workOrder);
    }

    public function asset(WorkOrder $workOrder) {
        return response()->json($workOrder->asset);
    }

    public function notes(WorkOrder $workOrder) {
        return response()->json($workOrder->notes->values(), 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\WorkOrder  $workOrder
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, WorkOrder $workOrder)
    {
        $validationRules = [
            'probdesc' => 'required',
            'suggested' => 'required',
        ];

        $messages = [
            'probdesc.required' => 'The Problem Description is required!',
            'suggested.required' => 'The Suggested Solution is required!',
        ];

        $validator = Validator::make($request->all(), $validationRules, $messages)->validate();

        $workOrder->probdesc = $request->input('probdesc');
        $workOrder->suggested = $request->input('suggested');
        $workOrder->storeid = $request->input('storeid');
        $workOrder->save();
        event(new \App\Events\WorkOrderUpdated($workOrder));
        return response()->json($workOrder, 200);
    }

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