/**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     if (\Entrust::hasRole('student') && $request->input('type') == Category::getCategoryId('loan.type', 'interview')) {
         $judge_column = 'stu_date_began_at';
     } else {
         $judge_column = 'date_began_at';
     }
     $timezone = Timezone::whereRaw("? BETWEEN `{$judge_column}` AND `date_ended_at`", [Carbon::now()->toDateString()])->where('date_began_at', '<=', $request->input('date_began_at'))->where('date_ended_at', '>=', $request->input('date_ended_at'))->first();
     if ($timezone != null) {
         $request->merge(['timezone' => $timezone, 'long_term_token' => bindec($request->input('long_term_token'))]);
         return $next($request);
     } else {
         return abort(403);
     }
 }
    $groups = ['manager', 'lab', '104-A', '104-B', '三年A組', '銀八老師', '105-A', '105-B'];
    return ['username' => $faker->userName, 'password' => bcrypt($faker->password), 'nickname' => $faker->name, 'email' => $faker->email, 'phone' => '09' . mt_rand(10000000, 99999999), 'group' => $groups[mt_rand(0, 7)]];
});
$factory->define(\App\Affair\Property::class, function () {
    $faker = Faker\Factory::create('zh_TW');
    return ['name' => $faker->name, 'describe' => $faker->realText(16), 'category' => Category::getCategories('property')->random()->getAttribute('id'), 'status' => Category::getCategories('property.status')->random()->getAttribute('id'), 'code' => mt_rand(10000000, 99999999)];
});
$factory->define(\App\Affair\Repair::class, function () {
    $faker = Faker\Factory::create('zh_TW');
    return ['user_id' => User::all()->random()->getAttribute('id'), 'title' => mt_rand(0, 1) ? $faker->realText(mt_rand(10, 15)) : '', 'type' => Category::getCategories('repair.type')->random()->getAttribute('id'), 'remark' => $faker->realText(32), 'status' => Category::getCategories('repair.status')->random()->getAttribute('id')];
});
$factory->define(\App\Affair\Loan::class, function () {
    $faker = Faker\Factory::create('zh_TW');
    $time = mt_rand(0, 1);
    // get randomal Timezone
    $began_date = Timezone::all()->random()->getAttribute('date_began_at');
    $day = (new Carbon($began_date))->addDays(rand(1, 10));
    // get date begin/end
    $begin = $day->toDateString();
    $end = $day->addDays(mt_rand(0, 10))->toDateString();
    return ['user_id' => User::all()->random()->getAttribute('id'), 'type' => Category::getCategories('loan.type')->random()->getAttribute('id'), 'date_began_at' => $begin, 'date_ended_at' => $end, 'time_began_at' => $time ? $day->addHours(mt_rand(0, 7))->toTimeString() : null, 'time_ended_at' => $time ? $day->addHours(mt_rand(7, 15))->toTimeString() : null, 'remark' => $faker->realText(32), 'status' => Category::getCategories('loan.status')->random()->getAttribute('id'), 'long_term_token' => $begin != $end ? mt_rand(0, 127) : 1 << date('w', strtotime($begin))];
});
$factory->define(\App\Affair\Timezone::class, function () {
    $faker = Faker\Factory::create('zh_TW');
    $day = Carbon::now()->startOfDay()->addDays(mt_rand(1, 365));
    $stu_start = clone $day;
    $lab_start = clone $day;
    if (mt_rand(0, 1) === 1) {
        $type = Category::getCategoryId('time.type', 'semester');
    } else {
        $type = Category::getCategoryId('time.type', 'vacation');
 /**
  * Delete classroom borrow infomation
  *
  * @param Int id
  * @return Json
  */
 public function deleteClassroomBorrowInfo($id)
 {
     $result = Timezone::find($id)->delete();
     return response()->json(['status' => $result ? 0 : 2]);
 }
 /**
  * Get classroom borrow infomation.
  *
  * @return Json
  */
 public function getClassroomBorrowInfo(Request $request)
 {
     // get all timezone data that ended after today
     $day = Carbon::now()->toDateString();
     // get timezone datas
     $timezones_info = Timezone::with(['type'])->where('date_ended_at', '>=', $day)->get();
     return response()->json($timezones_info);
 }