<?php

namespace App\DataTables;

use App\Models\BondMaterialRecipe;
use Yajra\DataTables\Services\DataTable;
use Yajra\DataTables\EloquentDataTable;

class BondMaterialRecipeDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        $dataTable = new EloquentDataTable($query);

        $dataTable->addColumn('bond_product', function (BondMaterialRecipe $album) {
            return $album->bondProduct->name;
        });
        $dataTable->addColumn('bond_process', function (BondMaterialRecipe $album) {
            return $album->bondProcessParam->name;
        });

        $dataTable->addColumn('weight', function (BondMaterialRecipe $album) {
            return $album->weight/1000 .' kg';
        });

        $dataTable->addColumn('action', 'bond_material_recipes.datatables_actions');
        
        return $dataTable;
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\BondMaterialRecipe $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(BondMaterialRecipe $model)
    {
        return $model->newQuery();
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->addAction(['width' => '120px', 'printable' => false])
            ->parameters([
                'dom'       => 'Bfrtip',
                'stateSave' => true,
                'order'     => [[0, 'desc']],
                'buttons'   => [
                    ['extend' => 'create', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'export', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'print', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'reset', 'className' => 'btn btn-default btn-sm no-corner',],
                    ['extend' => 'reload', 'className' => 'btn btn-default btn-sm no-corner',],
                ],
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'created_at',
            'material_id',
            'bond_product',
            'bond_process',
            'weight',
            'powder_cost',
            'total_piece_packing',
            'priority',
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'bond_material_recipesdatatable_' . time();
    }
}
