<?php

namespace App\Admin\Controllers;

use App\Models\Leave;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class LeaveController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Leave(), function (Grid $grid) {
           
            $grid->column('student_id')->using(config('map.students'));
            $grid->column('leave_type')->using(config('map.leave_type'));
            $grid->column('reason');
            $grid->column('start_time');
            $grid->column('end_time');
            $grid->disableEditButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->date('created_at')->datetime()->width(4);
                $filter->equal('student_id','输入/选择学生')->select(config('map.students'))->width(4);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Leave(), function (Show $show) {
            $show->row(function (Show\Row $show) {
              $show->field('content','请假详情')
                  ->width(8)
                  ->unescape()
                  ->as(function () {
                  $html = '';
                  $html .= '<div style="text-align:left">';
                  $html .= '<div style="margin-top:5px">请假人：' . config('map.students')[$this->student_id] . '</div>';
                  $html .= '<div style="margin-top:5px">请假原因：' . $this->reason . '</div>';
                  $html .= '<div style="margin-top:5px">请假类型：' .  config('map.leave_type')[$this->leave_type] . '</div>';
                  $html .= '<div style="margin-top:5px">开始时间：' . $this->start_time . '</div>';
                  $html .= '<div style="margin-top:5px">结束时间：' . $this->end_time . '</div>';
                  $html .= '</div>';
                  return $html;
              });
          });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Leave(), function (Form $form) {
            $form->display('id');
            $form->text('student_id');
            $form->text('reason');
            $form->text('start_time');
            $form->text('end_time');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
