<?php

namespace App\DataTables;

use App\Models\Student;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
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 StudentsDataTable 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('Action', 'admin.students.action')
            ->rawColumns([
                'Action',
            ])
            ->setRowId(function ($record) {
                return 'row_' . $record->id;
            })
            ->editColumn('created_at', function ($record) {
                return $record->created_at ? with(new Carbon($record->created_at))->format('Y-m-d H:i') : '';
            })
            ->filterColumn('mother_name', function ($query, $keyword) {
                $keywords = trim($keyword);
                $query->whereRaw("CONCAT(mother.first_name, ' ',mother.last_name) like ?", ["%{$keywords}%"]);
            })
            ->filterColumn('father_name', function ($query, $keyword) {
                $keywords = trim($keyword);
                $query->whereRaw("CONCAT(father.first_name, ' ',father.last_name) like ?", ["%{$keywords}%"]);
            })->addIndexColumn();
    }

    /**
     * Get query source of dataTable.
     *
     * @param \App\Models\Student $model
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query(Student $model)
    {
        $allowedDivisions = [];
        $userPermission = Auth::guard('admin')->user()->getUserPermissionByCode('manage-students');
        if ($userPermission) {
            $allowedDivisions = json_decode($userPermission['details'], true)['divisions'];
        }

        $query = Student::select(
                'students.id',
                'students.first_name',
                'students.last_name',
                'students.gender',
                'cities.title as city',
                'students.area',
                'students.telephone',
                'divisions.title as division',
                DB::raw("CONCAT(mother.first_name, ' ',mother.last_name) AS mother_name"),
                DB::raw("CONCAT(father.first_name, ' ',father.last_name) AS father_name"),
            )
                ->leftJoin('parents as mother', 'students.mother_id', '=', 'mother.id')
                ->leftJoin('parents as father', 'students.father_id', '=', 'father.id')
                ->leftJoin('divisions', 'students.division_id', '=', 'divisions.id')
                ->leftJoin('cities', 'students.city_id', '=', 'cities.id')
                ->orderBy('id', 'desc');



        if ($this->division_id != null) {
            $query = $query->where('students.division_id', $this->division_id);
        }else {
            if (!in_array(-1, $allowedDivisions)) {
                $query = $query->whereIn('students.division_id', $allowedDivisions);
            }
        }

        return $query;
    }


    public function lang()
    {
        $langJson = [
            "sEmptyTable"     =>  trans('general.datatable.sEmptyTable'),
            "sInfo"           =>  trans('general.datatable.sInfo'),
            "sInfoEmpty"      =>  trans('general.datatable.sInfoEmpty'),
            "sInfoFiltered"   =>  trans('general.datatable.sInfoFiltered'),
            "sInfoPostFix"    =>  trans('general.datatable.sInfoPostFix'),
            "sInfoThousands"  =>  trans('general.datatable.sInfoThousands'),
            "sLengthMenu"     =>  trans('general.datatable.sLengthMenu'),
            "sLoadingRecords" =>  trans('general.datatable.sLoadingRecords'),
            "sProcessing"     =>  trans('general.datatable.sProcessing'),
            "sSearch"         =>  trans('general.datatable.sSearch'),
            "sZeroRecords"    =>  trans('general.datatable.sZeroRecords'),
            "sFirst"          =>  trans('general.datatable.sFirst'),
            "sLast"           =>  trans('general.datatable.sLast'),
            "sNext"           =>  trans('general.datatable.sNext'),
            "sPrevious"       =>  trans('general.datatable.sPrevious'),
            "sSortAscending"  =>  trans('general.datatable.sSortAscending'),
            "sSortDescending" =>  trans('general.datatable.sSortDescending'),
            "paginate"=> [
                "first"=> trans('general.datatable.sFirst'),
                "previous"=> trans('general.datatable.sPrevious'),
                "next"=> trans('general.datatable.sNext'),
                "last"=> trans('general.datatable.sLast')
            ],
        ];
        return $langJson;
    }


    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\DataTables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
            ->setTableId('students-table')
            ->columns($this->getColumns())
            ->ajaxWithForm('',  '#division-select-form')
            ->dom('<"d-flex justify-content-between align-items-center mx-0 row"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6"f>>t<"d-flex justify-content-between mx-0 row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>')
            ->orderBy(0)
            ->buttons(
                Button::make('reset')->text(' <i class="fas fa-redo"></i> ' . trans('app.datatable.reset')),
                Button::make('reload')->text(' <i class="fas fa-sync-alt"></i> ' . trans('app.datatable.reload')),
            )->parameters([
                'responsive' => true,
                'autoWidth' => false,
                'lengthMenu' => [[50, 100, 200, 500], ['50', '100', '200', '500']],
                'language' => $this->lang(),
            ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::computed('DT_RowIndex')->title('#')
                ->addClass('text-center'),
            Column::make('first_name')->title(trans('general.First Name')),
            Column::make('last_name')->title(trans('general.Last Name')),
            Column::make('gender')->title(trans('general.gender')),
            Column::make('city')->name('cities.title')->title(trans('general.city')),
            Column::make('area')->title(trans('general.area')),
            Column::make('telephone')->title(trans('general.telephone')),
            Column::make('mother_name')->title(trans('general.mother')),
            Column::make('father_name')->title(trans('general.father')),
            Column::make('division')->name('divisions.title')->title(trans('general.division')),
            Column::computed('Action')->title(trans('general.options'))
                ->exportable(false)
                ->printable(false)
                ->addClass('text-center')
                ->searchable(false),
        ];
    }

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