<?php

namespace App\Http\Controllers\Kasir;

use App\Http\Controllers\Controller;
use App\Models\DetailPesanan;
use App\Models\Pesanan;
use Illuminate\Http\Request;

class History extends Controller
{
  public function index(){
    return view('pages.history.index');
  }

  public function getDataHistory(){
    $data = Pesanan::with(['detailPesanan', 'user'])->get();
    $nomor = 1;

    return $datatables = datatables()
      ->of($data)
      ->addColumn('nomor', function($data) use (&$nomor){
        return $nomor++;
    })
    ->addColumn('nama_user', function($data) {
      return $data->user->name;
    })
    ->addColumn('created_at', function($data) {
      return \Carbon\Carbon::parse($data->created_at)->format('d F Y H:i:s');
    })
    ->addColumn('grand_total', function($data) {
      return 'Rp ' . number_format($data->grand_total, 0, ',', '.');
    })
    ->addColumn('ubah', function($data) {
      return '<div class="btn-group">
      <a href="javascript:void(0)" onclick="print(\''.$data->id.'\')"><span class="btn btn-xs btn-success"><i class="fas fa-print"></i></span></a></div>';
    })
    ->rawColumns(['nomor', 'nama_user', 'created_at', 'grand_total', 'ubah'])
    ->make(true);
  }

  public function print($id){
    $pesanan = \App\Models\Pesanan::with('detailPesanan', 'user')->find($id);

    return view('pages.kasir.print_dapur', compact('pesanan'));
  }
}
