<?php

namespace App\Http\Controllers\Resources;

use App\Http\Requests\PurchaseOrderDetailRequest;
use App\Http\Requests\ExportRequest;
use App\Http\Controllers\Controller;

use App\PurchaseOrder;
use App\PurchaseOrderDetail;

class PurchaseOrderDetailController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index($idOrder)
    {
      $order=PurchaseOrder::find($idOrder);

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

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

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      $detail=PurchaseOrderDetail::find($id);

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

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

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

    		if (!$detail) {
    			return response()->json(['errors'=>array(['code'=>404,'message'=>__('purchase_orders.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=PurchaseOrderDetail::find($id);

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

      $detail->delete();

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

    /**
     * Display report from the specified resource.
     *
     * @param  App\Http\Requests\ExportRequest $request
     * @return PDF CSV or XLS
     */
    public function export(ExportRequest $request)
    {

      return view('reports.pdf-template');

      $format = $request->input('format');
      $orderBy = $request->input('order_by');
      $betweenDates = ($request->input('between_dates')) ? explode(',', $request->input('between_dates')): ['',''];

      $queryBuilder = new QueryBuilder(new PurchaseOrder, $request);

      $meta = [ // For displaying filters description on header
          __('strings.registered_on') => $betweenDates[0].' - '.$betweenDates[1],
          __('strings.order_by') => $orderBy,
      ];

      $columns = [ // Set Column to be displayed
          __('purchase_orders.folio') => 'folio',
          __('purchase_orders.total') => 'total',
          __('purchase_orders.status') => function($status) { // You can do if statement or any action do you want inside this closure
              return ($status->status == 1) ? __('purchase_orders.pending') : __('purchase_orders.approved');
          },
      ];

      switch ($format) {
        case 'pdf':
          $report = new PdfReport();
          return $report->of(__('purchase_orders.report_title'), $meta, $queryBuilder->build()->get(), $columns)->showTotal([__('purchase_orders.total') => '$'])->download(__('purchase_orders.report_title'));
        case 'excel':
          $report = new ExcelReport();
          return $report->of(__('purchase_orders.report_title'), $meta, $queryBuilder->build()->get(), $columns)->setView('reports.list-excel-template')->download(__('purchase_orders.report_title'));
        case 'csv':
          $report = new ExcelReport();
          return $report->of(__('purchase_orders.report_title'), $meta, $queryBuilder->build()->get(), $columns)->simple()->setFormat('csv')->download(__('purchase_orders.report_title'));
        default:
          $report = new PdfReport();
          return $report->of(__('purchase_orders.report_title'), $meta, $queryBuilder->build()->get(), $columns)->showTotal([__('purchase_orders.total') => '$'])->stream();
      }
    }
}
