/**
  * Display a listing of the normal classroom.
  *
  * @param Request $request
  * @return
  */
 public function indexClassroom(Request $request)
 {
     // get date
     $date = $request->input('date', date('Y-m-d', time()));
     $classroom_list = Property::with(['category', 'status', 'loanClassroom' => function ($query) use($date) {
         $query->where('date_ended_at', '>=', $date);
     }])->where('category', Category::getCategoryId('property', 'classroom'))->get();
     return response()->json($classroom_list);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $repair = Repair::where('user_id', Auth::user()->id)->find($id);
     if ($repair === null) {
         return response()->json(['status' => 2]);
     }
     $repair->status = Category::getCategoryId('repair.status', 'canceled');
     $result = $repair->save();
     return response()->json(['status' => $result === true ? 0 : 2]);
 }
 /**
  * Update the repair list's status to finished
  * and also update the aim property to status -normal.
  *
  * @param  Request  $request
  * @return Json
  */
 public function update(Request $request)
 {
     // get repair list
     $repair_list = $request->input('repair_list');
     if (!is_array($repair_list)) {
         return response()->json(['status' => 2]);
         // paramater error.
     }
     $property_list = Repair::whereIn('id', $repair_list)->get()->pluck('property_id');
     // update repair status
     $affect_repairs = Repair::whereIn('id', $repair_list)->update(['status' => Category::getCategoryId('repair.status', 'finished')]);
     return response()->json(['status' => 0]);
 }
 /**
  * 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);
     }
 }
 /**
  * check loan data conflict or not.
  *
  * @param array $date_info
  * @param array $time_info
  * @param int $LTK
  * @return bool
  */
 public static function checkConflict($p_id, $date_info, $time_info, $LTK)
 {
     $loans = new Loan();
     $conflict_num = self::getConflictQuery($p_id, $date_info, $time_info, $LTK, $loans)->where('status', '=', Category::getCategoryId('loan.status', 'accepted'))->count();
     return $conflict_num > 0;
 }
    $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');
        $stu_start->subDays(7);
    }
    return ['zone_name' => $faker->realText(mt_rand(10, 15)), 'type' => $type, 'date_began_at' => $day->toDateString(), 'date_ended_at' => $day->addDays(mt_rand(30, 150))->toDateString(), 'stu_date_began_at' => $stu_start->toDateString(), 'lab_date_began_at' => $lab_start->toDateString()];
});
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $affect_rows = Property::where('id', '=', $id)->update(['status' => Category::getCategoryId('property.status', 'deleted')]);
     return response()->json(['status' => $affect_rows == 1 ? 0 : 2]);
 }
 /**
  * Set classroom borrow infomation.
  *
  * @param Request $request
  * @return Json
  */
 public function setClassroomBorrowInfo(Request $request)
 {
     // parse timezone info
     $timezone_info = array_only($request->all(), ['zone_name', 'date_began_at', 'date_ended_at', 'stu_date_began_at', 'lab_date_began_at']);
     Timezone::create(array_merge($timezone_info, ['type' => Category::getCategoryId('time.type', $request->input('type'))]));
     return response()->json(['status' => 0]);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroyClassroomBorrow($id)
 {
     $affect_rows = Loan::where('id', '=', $id)->where('user_id', '=', Auth::user()->id)->where('loan.status', '=', Category::getCategroyId('loan.status', 'submitted'))->update(['status' => Category::getCategoryId('loan.status', 'canceled')]);
     return response()->json(['status' => $affect_rows == 1 ? 0 : 2]);
 }