<?php

namespace App\DataTables;

use App\Models\Transfer;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Html\Editor\Editor;
use Yajra\DataTables\Html\Editor\Fields;
use Yajra\DataTables\Services\DataTable;

class TransferDataTable extends DataTable
{
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('account_number', function ($v) {
                return $v->transaction?$v->transaction->account->number:'--';
            })
            ->addColumn('office_name', function ($v) {
                return $v->transaction?$v->transaction->account->office->name:'--';
            })
            ->addColumn('amount', function ($v) {
                return $v->transaction?formatPrice($v->transaction->amount):'--';
            })
            ->addColumn('status', function ($v) {
                return $v->transaction?view('components.badge',['status'=> $v->transaction->status]):'--';
            })
            ->addColumn('receipts_number', function ($v) {
                return $v->number;
            })
            ->addColumn('created_at', function ($account) {
                return $account->created_at->format('Y-m-d h:i');
            })
            ->addColumn('action', function ($transfer) {
       
                $acrtion = view('components.add_transfer', ['id' => $transfer->id ,'status'=>$transfer->transaction->status]);
                
          
                return $acrtion;
            });
    }

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

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->setTableId('transfer-table')
            ->columns($this->getColumns())
            ->minifiedAjax()
            ->dom('Bfrtip')
            ->orderBy(1)
            ->buttons(
                Button::make('export'),
                Button::make('print'),
                Button::make('reset'),
                Button::make('reload')
            );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('account_number')->title(__('Account Number')),
            Column::make('office_name')->title(__('Name')),
            Column::make('amount')->title(__('Amount')),
            Column::make('receipts_number')->title(__('Receipts Number')),
            Column::make('status')->title(__('Status')),
            Column::make('created_at')->title(__('Created At')),
            Column::computed('action')->title(__('Action'))
                ->extransferable(false)
                ->printable(false)
                ->width(150)
                ->addClass('text-center'),
        ];
    }

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