<?php

namespace App\DataTables;

use App\Models\StoreType;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;
use App\Traits\DataTableTrait;

class StoreTypeDataTable extends DataTable
{
    use DataTableTrait;
    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->editColumn('status', function($data) {
                $delete_at = null;
                $action_type = 'status';
                return view('storetype.action',compact('data','action_type'))->render();
             })
            ->editColumn('created_at', function ($query) {
                return dateAgoFormate($query->created_at, true);
            })
            ->addColumn('storetype_image', function($row){
                return '<a href="'.getSingleMedia($row , 'storetype_image').'" class="image-popup-vertical-fit"><img src="'.getSingleMedia($row , 'storetype_image').'" width="40" height="40" ></a>';
            })
            ->order(function ($query) {
                if (request()->has('order')) {
                    $order = request()->order[0];
                    $column_index = $order['column'];

                    $column_name = 'id';
                    $direction = 'desc';
                    if ($column_index != 0) {
                        $column_name = request()->columns[$column_index]['data'];
                        $direction = $order['dir'];
                    }

                    $query->orderBy($column_name, $direction);
                }
            })

            ->addColumn('action', function ($row) {
                $id = $row->id;
                $action_type = 'action';
                return view('storetype.action', compact('id','action_type'))->render();
            })
            ->addIndexColumn()
            ->rawColumns(['checkbox', 'action', 'status','storetype_image']);
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\StoreType $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(StoreType $model)
    {
        $model = StoreType::query();
        return $model;
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('DT_RowIndex')
                ->searchable(false)
                ->title(__('message.srno'))
                ->orderable(false),
            ['data' => 'id', 'name' => 'id', 'title' => __('message.id')],
            ['data' => 'name', 'name' => 'name', 'title' => __('message.name'),'class' => 'text-capitalize'],
            ['data' => 'storetype_image', 'name' => 'storetype_image', 'title' => __('message.image'),'orderable' => false],
            ['data' => 'status', 'name' => 'status', 'title' => __('message.status')],
            ['data' => 'created_at', 'name' => 'created_at', 'title' => __('message.created_at')],

            Column::computed('action')
                ->exportable(false)
                ->printable(false)
                ->title(__('message.action'))
                ->width(60)
                ->addClass('text-center hide-search'),
        ];
    }

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