<?php

namespace App\Datatables\Shop;

use App\Datatables\ParentDataTable as DataTable;
use App\Models\Shop\Order;
use App\Repositories\Shop\InvoicePayments;
use App\Repositories\Shop\Orders;
use Yajra\DataTables\Html\Column;

class CustomerOrdersDataTable extends DataTable
{
    public $model_name = 'orders';

    public $sortedColumn = 1;

    public $sortedOrder = 'desc';

    public $stateSave = true;

    public function __construct()
    {
        $this->url = route('Shop.Orders.index');
    }

    public function query(Order $model)
    {
        $customerId = Auth::id();
        $model = $model->byCustomer($customerId)->with(['delivery']);

        return $this->buildQuery($model);
    }

    public function modifier($datatables)
    {
        $datatables
            ->editColumn('status', function (Order $order) {
                return Orders::getStatus($order->status);
            })
            ->editColumn('created_at', function (Order $order) {
                return $order->created_at->toDateTimeString();
            })
            ->editColumn('payment_type', function (Order $order) {
                return InvoicePayments::getPaymentType($order->payment_type);
            })
            ->rawColumns(['action']);

        return parent::modifier($datatables);
    }

    protected function getColumns()
    {
        return [
            Column::make('created_at')->title('Date'),
            Column::make('ref')->title('Ref'),
            Column::make('payment_type')->title('Règlement'),
            Column::make('total_shipped')->title('Montant')->class('text-right'),
            Column::make('status')->title('Statut'),
            $this->makeColumnButtons(),
        ];
    }
}
