<?php

namespace App\DataTables;

use App\Models\Account;
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 AccountsDataTable 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('owner', function($account){
                return $account->owner?$account->owner->name:'---';
             })
             ->addColumn('status', function($account){
                return  view('components.status', ['status'=>$account->status]);
             })
             ->addColumn('balance', function($account){
                return formatPrice($account->balance,$account->currency_code);
             })
             ->addColumn('currency', function($account){
                return $account->currency_code;
             })
             ->addColumn('created_at', function($account){
                return $account->created_at->format('Y-m-d h:i');
             })
             ->addColumn('action', function($account){
                 $action ='';
                if($account->status){
                    
                    $action = '<input  data-id='.$account->id.' type="checkbox" name="my-checkbox" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">'; 
                }else{
                    $action = '<input  data-id='.$account->id.' type="checkbox" name="my-checkbox"  data-bootstrap-switch data-off-color="danger" data-on-color="success">'; 
                }
                return $action;
             });
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Account $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Account $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('accounts-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('id')->title(__('Id')),
            Column::make('number')->title(__('Number')),
            Column::make('owner')->title(__('Owner')),
            Column::make('currency')->title(__('Currency')),
            Column::make('balance')->title(__('Balance')),
            Column::make('status')->title(__('Status')),
            Column::make('created_at')->title(__('Created At')),
            Column::computed('action')
            ->exportable(false)
            ->printable(false)
            ->width(150)
            ->addClass('text-center'),
        ];
    }

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