<?php

namespace App\Admin\Controllers;

use App\Models\Attendance;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
//use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Controllers\AdminController;

class AttendanceController extends AdminController
{

    public function index($parent, Content $content)
    {
        $title = '考勤记录';
        return $content
            ->title($title)
            ->description('列表')
            // 添加面包屑导航
            ->breadcrumb([
                    'text' => '返回列表',
                    'url' => admin_url('/attendance'),
                    'icon' => 'fa fa-step-backward'
                ],['text' => $title]
            )
            ->body($this->grid($parent));
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Attendance(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type');
            $grid->column('arrive_school_time');
            $grid->column('off_school_time');
            $grid->column('leave_type');
            $grid->column('leave_start_time');
            $grid->column('leave_end_time');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Attendance(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('arrive_school_time');
            $show->field('off_school_time');
            $show->field('leave_type');
            $show->field('leave_start_time');
            $show->field('leave_end_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id)
    {
        return Form::make(new Attendance(), function (Form $form) use($id) {

            $form->row(function (Form\Row $row) {
              $row->width(6)->select('type')->options(config('map.sex'));
              $row->width(6)->datetime('arrive_school_time');
            });

            $form->row(function (Form\Row $row) {
              $row->width(6)->datetime('off_school_time');
              $row->width(6)->select('leave_type')->options(config('map.leave_type'));
              
            });

            $form->row(function (Form\Row $row) {
              $row->width(6)->datetime('leave_start_time');
              $row->width(6)->datetime('leave_end_time');
            });

            $form->disableListButton();
            $form->saved(function (Form $form) {
              return $form
                  ->response()
                  ->success('保存成功')
                  ->redirect('/student');
            });
          });
    }
}
