<?php

namespace App\DataTables;

use App\Models\Order;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
use App\Traits\DataTableTrait;


class CompleteOrderDataTable extends DataTable
{
    use DataTableTrait;
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->editColumn('status', function ($query) {
                $status = 'danger';
                $status_name = 'completed';
                switch ($query->status) {

                    case 'completed':
                        $status = 'success';
                        $status_name = __('message.completed');
                        break;
                }
                return '<span class="text-capitalize badge bg-' . $status . '">' . $status_name . '</span>';
            })
            ->editColumn('id', function ($row) {
                $user = $row->id;
                return $user ? '<a href="' . route('order.show', $user) . '">' . $user . '</a>' : '-' ;
            })
            ->editColumn('client_id', function ($row) {
                $user = $row->client;
                return $user ? '<a href="' . route('users.show', $user->id) . '">' . $user->name . '</a>' : '-' ;
            })

            ->editColumn('delivery_datetime', function ($row) {
                return dateAgoFormate($row->delivery_datetime,true) ?? '-';
            })
            ->editColumn('pickup_datetime', function ($row) {
                return dateAgoFormate($row->pickup_datetime,true) ?? '-';
            })

            ->editColumn('delivery_man_id', function ($query) {
                $deliveryMan = $query->delivery_man;
                return $deliveryMan ? '<a href="' . route('deliveryman.show', $deliveryMan->id) . '">' . $deliveryMan->name . '</a>' : '-' ;
            })
            ->editColumn('pickup_point', function ($model) {
                $address = isset($model->pickup_point['address']) ? $model->pickup_point['address'] : '';
                return strlen($address)  ? '<span data-toggle="tooltip" data-html="true" data-placement="left" title="'.$address.'">'. substr($address, 0, 35) . '...' . '</span>' : '-';

            })

            ->editColumn('delivery_point', function ($model) {
                $address = isset($model->delivery_point['address']) ? $model->delivery_point['address'] : '';
                return strlen($address) ? '<span data-toggle="tooltip" data-html="true" data-placement="left" title="'.$address.'">'. substr($address, 0, 35) . '...' . '</span>' : '-';
            })
            ->addColumn('invoice', function ($query) {
                return $query->status == 'completed' ? '<a href="' . route('order-invoice', $query->id) . '"><i class="fa fa-download"></i></a>' : 'N/A';
            })

            ->editColumn('created_at', function ($row) {
                return dateAgoFormate($row->created_at, true);
            })
            ->addColumn('action', function ($order) {
                $id = $order->id;
                $delete_at = $order->deleted_at;
                return view('order.otheraction', compact('id', 'delete_at', 'order'))->render();
            })
            ->addIndexColumn()
            ->rawColumns(['action','id','status','pickup_point', 'delivery_point', 'invoice', 'delivery_man_id', 'client_id']);
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Order $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Order $model)
    {
        $model = Order::where('status','completed')->orderBy('created_at','desc');
        return $model;
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
                Column::make('DT_RowIndex')
                ->searchable(false)
                ->title(__('message.srno'))
                ->orderable(false)
                ->width(60),
                ['data' => 'id', 'name' => 'id', 'title' => __('message.order_id')],
                ['data' => 'client_id', 'name' => 'client_id', 'title' => __('message.customer_name')],
                ['data' => 'pickup_point', 'name' => 'pickup_point', 'title' => __('message.pickup_address')],
                ['data' => 'delivery_point', 'name' => 'delivery_point', 'title' => __('message.delivery_address')],
                ['data' => 'delivery_man_id', 'name' => 'delivery_man_id', 'title' => __('message.delivery_man')],
                ['data' => 'pickup_datetime', 'name' => 'pickup_datetime', 'title' => __('message.pickup_date')],
                ['data' => 'delivery_datetime', 'name' => 'delivery_datetime', 'title' => __('message.delivery_date')],
                ['data' => 'invoice', 'name' => 'invoice', 'title' => __('message.invoice'),'orderable' => false],
                ['data' => 'created_at', 'name' => 'created_at', 'title' => __('message.created_at')],
                ['data' => 'status', 'name' => 'status', 'title' => __('message.status')],
                Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->width(60)
                ->addClass('text-center hide-search'),
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'Order_' . date('YmdHis');
    }
}
