<?php

namespace App\Http\Controllers\Admin;

use App\Models\Leave;
use Carbon\Carbon;
use Illuminate\Http\Request;

class LeaveController extends Controller
{
    protected $redirect_index = '/admin/Leave/index';

    protected $view_path = 'admin.leaves.';

    protected $pre_uri = '/admin/Leave/';

    protected $model_name = '请假';

    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new Leave();
    }

    public function index(Request $request)
    {
        $list = $this->model->where('id', '>', 0)->orderBy('created_at', 'desc')->get();

        if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
            $keyword = trim($request->input('keyword'));

            $list = $list->filter(function ($value) use($keyword) {
                if(!empty($value->student) && !(strpos($value->student->name, $keyword) === false)) {
                    return true;
                }
                if(!empty($value->course) && !(strpos($value->course->name, $keyword) === false)) {
                    return true;
                }
                return false;
            });
        }

        $list = $this->paginate($list)->withPath($this->getPaginateUrl());

        list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
        return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
    }
}
