<?php

namespace App\Http\Controllers\Resources;

use App\Http\Requests\SaleDetailRequest;
use App\Http\Controllers\Controller;

use App\Sale;
use App\SaleDetail;

class SaleDetailController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index($idSale)
    {
      $sale=Sale::find($idSale);

      if (!$sale) {
        return response()->json(['errors'=>array(['code'=>404,'message'=>__('sales.not_find')])],404);
      }

      return response()->json(['status'=>'ok','data'=>$sale->details()->get()],200);
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      $detail=SaleDetail::find($id);

  		if (!$detail) {
  			return response()->json(['errors'=>array(['code'=>404,'message'=>__('sales.not_find_detail')])],404);
  		}

  		return response()->json(['status'=>'ok','data'=>$detail],200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  App\Http\Requests\SaleDetailRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SaleDetailRequest $request, $id)
    {
        $detail=SaleDetail::find($id);

    		if (!$detail) {
    			return response()->json(['errors'=>array(['code'=>404,'message'=>__('sales.not_find_detail')])],404);
    		}

        return tap($detail)->update($request->all());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
      $detail=SaleDetail::find($id);

      if (!$detail) {
        return response()->json(['errors'=>array(['code'=>404,'message'=>__('sales.not_find_detail')])],404);
      }

      $detail->delete();

      return response()->json(['code'=>204,'message'=>__('strings.deleted_successfully')],204);
    }
}
