/**
  * Show the form for editing the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function edit($id)
 {
     if (!isset($id) || intval($id) <= 0) {
         return redirect('/history');
     }
     $detail = BookingDetail::where('bookingID', $id)->get();
     // dd($detail);
     return view('history.detail', ['data' => $detail]);
 }
 public function summaryByAdmin($id)
 {
     $booking = Booking::where('code', $id)->get()->first();
     $detail = BookingDetail::where('bookingCode', $id)->get();
     foreach ($detail as $key => $value) {
         # code...
         $zone = Zone::where('code', $value->zoneCode)->get()->first();
         $detail->zoneName = $zone->name;
     }
     $user = session()->get('member');
     //var_dump($booking , $detail);
     return view('booking.summarybyadmin', ['booking' => $booking, 'detail' => $detail, 'user' => $user]);
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update($id)
 {
     $a = Booking::where('id', $id)->update(['verify' => 1]);
     $b = BookingDetail::where('bookingID', $id)->update(['verify' => 1]);
     if ($a <= 0 || $b <= 0) {
         return response()->json(['result' => false, 'message' => 'can not find this booking.']);
     }
     $temp_booking = ViewBookingAndDetail::where('active', 1)->where('id', $id)->groupBy('id')->orderBy('zoneNumber')->get();
     foreach ($temp_booking as $key => $value) {
         $value->detail = BookingDetail::where('bookingID', $value->id)->get();
         $value->user = User::where('id', $value->userID)->first();
     }
     return response()->json(['result' => true, 'message' => 'success', 'data' => $temp_booking]);
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @return \Illuminate\Http\Response
  */
 public function update($date)
 {
     if ($date == null) {
         return response()->json(['result' => false, 'message' => 'date can not be null.']);
     }
     $booking = Booking::where('sale_at', $date . ' 00:00:00')->where('status', 'BK')->update(['status' => 'RM', 'active' => 0]);
     $booking_list = Booking::where('sale_at', $date . ' 00:00:00')->where('status', 'RM')->get();
     foreach ($booking_list as $key => $book) {
         BookingDetail::where('bookingID', $book->id)->update(['status' => 'RM', 'active' => 0]);
     }
     if ($booking != null) {
         return response()->json(['result' => true, 'data' => $booking, 'message' => 'success']);
     }
     return response()->json(['result' => false, 'message' => 'clear fail.']);
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     if ($id == null) {
         return response()->json(array('result' => false, 'message' => 'Booking id can not be null.'));
     }
     $rules = array('id' => 'required', 'code' => 'required', 'canCheckIn' => 'required|boolean:true', 'bookingDetail' => 'required|array');
     $validator = Validator::make($request->all(), $rules);
     if ($validator->fails()) {
         return response()->json(array('result' => false, 'message' => 'invalid input field.'));
     }
     $date = new DateTime();
     $date->setTimezone(new DateTimeZone('Asia/Bangkok'));
     $booking = Booking::where('id', $id)->first();
     $booking->status = 'CN';
     $booking->checkin_at = $date->format('Y-m-d H:i:s');
     $result = $booking->save();
     if (!$result) {
         return response()->json(array('result' => false, 'message' => 'Booking id $id check-in has error.'));
     }
     BookingDetail::where('bookingID', $id)->update(['status' => 'CN']);
     return response()->json(array('result' => true, 'message' => 'success.'));
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function show($id)
 {
     //
     $booking = Booking::where('id', $id)->first();
     if ($booking == null) {
         return response()->json(['result' => false, 'message' => 'can not get this booking detail.']);
     }
     $booking->detail = BookingDetail::where('bookingID', $booking->id)->get();
     $user = User::where('id', $booking->userID)->first();
     return response()->json(['result' => true, 'message' => 'success', 'data' => $booking, 'user' => $user]);
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function show($id)
 {
     //
     $booking = Booking::where('code', $id)->get();
     if ($booking == null) {
         return response()->json(['result' => false, 'message' => 'can not get this booking detail.']);
     }
     $grandprice = 0;
     foreach ($booking as $key => $book) {
         $book->detail = BookingDetail::where('bookingID', $book->id)->get();
         $grandprice += $book->totalPrice;
     }
     $user = User::where('id', $booking[0]->userID)->first();
     return response()->json(['result' => true, 'message' => 'success', 'data' => $booking, 'user' => $user, 'grandprice' => $grandprice]);
 }