<?php

namespace App\DataTables\Stocks;

use App\Models\StockManagement\Adjustment;
use Illuminate\Database\Eloquent\Builder as QueryBuilder;
use Yajra\DataTables\EloquentDataTable;
use Yajra\DataTables\Html\Builder as HtmlBuilder;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;

class AdjustmentDataTable extends DataTable{

    /**
     * Build DataTable class.
     *
     * @param QueryBuilder $query Results from query() method.
     * @return EloquentDataTable
     */
    public function dataTable(QueryBuilder $query){
        return (new EloquentDataTable($query))
                ->editColumn('created_at', function ($row){
                    return format_date( $row->created_at,__("d-m-Y H:i"));
                })
                ->editColumn('created_by', function ($row){
                    return $row->createdBy->fullName ?? "";
                })
                ->editColumn('product', function ($row){
                    return $row->stock->product->fullName ?? "";
                })
                ->editColumn('reason', function ($row){
                    return $row->reason->name ?? "";
                })
                ->editColumn('operation', function ($row){
                    $operation = $row->operation;
                    return "($operation->operator) $operation->name" ;
                })
                ->addColumn('lot_id', function ($row){
                    return $row->stock->lot_id ?? "";
                })
                ->editColumn('qty_before', function ($row){
                    return round($row->qty_before ?? 0,2);
                })
                ->editColumn('qty_adjusted', function ($row){
                    return round($row->qty_adjusted ?? 0,2);
                })
                ->setRowId('id');
    }

    /**
     * Get query source of dataTable.
     *
     * @param Adjustment $adjustment
     * @return QueryBuilder
     */
    public function query(Adjustment $adjustment){
        return $adjustment->newQuery()
                          ->with("stock")
                          ->when(request()->filled("stock_id"),function ($query){
                                return $query->where("stock_id",request()->get("stock_id"));
                           })
                          ->latest("created_at");
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return HtmlBuilder
     */
    public function html(){
        return $this->builder()
                    ->setTableId('table-list-adjustments')
                    ->addTableClass('table-striped')
                    ->setTableAttribute("style", "width:100%;")
                    ->columns($this->getColumns())
                    ->minifiedAjax(route('admin.stocks.adjustments.dt-list'))
                    ->dom('Bfrtip')
                    ->orderBy(1)
                    ->selectStyleSingle()
                    ->buttons([
                        Button::make('reload'),
                        Button::make('export'),
                    ]);
    }


    public function getColumns(): array{
        return [
            Column::make('lot_id')
                ->title(__("Id Lot"))
                ->data('lot_id')
                ->searchable(false)
                ->orderable(true)
                ->footer(__("Id Lot")),

            Column::make('product')
                ->title(__("Produit"))
                ->data('product')
                ->searchable(false)
                ->orderable(true)
                ->footer(__("Produit")),

            Column::make('qty_before')
                ->title(__("Before Qty"))
                ->data('qty_before')
                ->footer(__("Before Qty")),

            Column::make('qty_adjusted')
                ->title(__("Adjusted Qty"))
                ->data('qty_adjusted')
                ->footer(__("Adjusted Qty")),

            Column::make('qty_after')
                ->title(__("After Qty"))
                ->data('qty_after')
                ->footer(__("After Qty")),

            Column::make('operation')
                ->title(__("Opération"))
                ->data('operation')
                ->footer(__("Opération")),

            Column::make('reason')
                ->title(__("Reason"))
                ->data('reason')
                ->footer(__("Reason")),

            Column::make('comment')
                ->title(__("Comment"))
                ->data('comment')
                ->footer(__("Comment")),

            Column::make('created_by')
                ->title(__("By"))
                ->data('created_by')
                ->footer(__("By")),

            Column::make('created_at')
                ->title(__("Le"))
                ->data('created_at')
                ->footer(__("Le")),
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename() : string{
        return 'Stock_file' . date('d-m-Y-H-i-s');
    }

    /**
     * @inheritDoc
     */
    public function excel(){}

    /**
     * @inheritDoc
     */
    public function csv(){}

    /**
     * @inheritDoc
     */
    public function pdf(){}
}
