<?php

namespace App\DataTables\Customer;

use App\Models\Customer;
use Yajra\DataTables\Services\DataTable;

class CustomerInvoicesDataTable extends DataTable
{
    public function dataTable($query)
    {
        return datatables($query)
            ->addIndexColumn()
            ->editColumn('products',function($row){
                $return = '';
               foreach( $row->saleitems as $saleitem ) {
                   $return .= sprintf(
                       "<p>%s (%d) </p>",
                       $saleitem->product->product_name,
                       $saleitem->quantity
                   );
               }
               
               return $return;
            })
            ->editColumn('total',function($row){
                return number_format( $row->grand_total,2,',' );
            })
            ->editColumn('due',function($row){
                return number_format( $row->due,2,',' );
            })
            ->editColumn('checkbox',function($row){
                if ( $row->due <= 0 ) {
                    return;
                }
                return '<input type="checkbox" name="invoices[]" class="bulk-select" value="'.$row->id.'"/>';
            })
            ->rawColumns(['products','checkbox'])
            ->setRowClass('gradeX');
    }

    public function query(Customer $model)
    {
        return $this->id->invoices();
    }

   
    protected function getActionColumn($data): string
    {
        return '';
    }

    public function html()
    {
        return $this->builder()
                    ->addIndex()
                    ->columns($this->getColumns())
                    ->minifiedAjax()
                    ->paging(true)
                    ->lengthMenu([[50, 100,500, -1], [50, 100,500, 'All']])
                    ->parameters($this->getBuilderParameters())
                    ->scrollX(true);
    }

    protected function getColumns(): array
    {
        return [
            [
                'data'           => 'DT_RowIndex',
                'name'           => 'DT_RowIndex',
                'title'          => 'S. No',
                'orderable'      => false,
                'searchable'     => false,
                'exportable'     => false,
                'printable'      => true,
                'width'          => '8%',

            ],
            [
                'data'          => 'products',
                'name'          => 'products',
                'title'         => 'Products',
                'searchable'    => true,
                'visible'       => true,
                'orderable'     => true
            ],
            [
                'data'          => 'total',
                'name'          => 'total',
                'title'         => 'Grand Total',
                'searchable'    => true,
                'visible'       => true,
                'orderable'     => true
            ],
            [
                'data'          => 'due',
                'name'          => 'due',
                'title'         => 'Due Amount',
                'searchable'    => true,
                'visible'       => true,
                'orderable'     => true
            ],
            [
                'title'          => '',
                'data'           => 'checkbox',
                'name'           => 'checkbox',
                'orderable'      => false,
                'searchable'     => false,
                'exportable'     => false,
                'printable'      => true,
                'width'          => '10px',
            ],
        ];
    }

    protected function filename()
    {
        return 'Customer_' . date('YmdHis');
    }
}
