def editRestaurant(restaurant_id):
  if request.method == 'POST':
    restaurant_dao.updateRestaurant(restaurant_id, request.form['name'])
    flash("Edited restaurant!")
    return redirect(url_for('restaurantMenus'))
  else:
    restaurant = restaurant_dao.getRestaurant(restaurant_id)
    return render_template('edit-restaurant.html', restaurant=restaurant)
def newMenuItem(restaurant_id):
  restaurant = restaurant_dao.getRestaurant(restaurant_id)
  if request.method == 'POST':
    restaurant_dao.addMenuItem(restaurant_id, request.form['name'], request.form['course'], request.form['price'], request.form['description'])
    flash("New menu item created!")
    return redirect(url_for('restaurantMenu', restaurant_id=restaurant_id))
  else:
    return render_template('new-menu-item.html', restaurant=restaurant)
def deleteRestaurant(restaurant_id):
  if request.method == 'POST':
    restaurant_dao.deleteRestaurant(restaurant_id)
    flash("Restaurant deleted!")
    return redirect(url_for('restaurantMenus'))
  else:
    restaurant = restaurant_dao.getRestaurant(restaurant_id)
    return render_template('delete-restaurant.html', restaurant=restaurant)
def editMenuItem(restaurant_id, menu_id):
  if request.method == 'POST':
    restaurant_dao.updateMenuItem(menu_id, request.form['name'], request.form['course'], request.form['price'], request.form['description'])
    flash("Edited menu item!")
    return redirect(url_for('restaurantMenu', restaurant_id=restaurant_id))
  else:
    restaurant = restaurant_dao.getRestaurant(restaurant_id)
    item = restaurant_dao.getMenuItemByRestaurant(restaurant_id, menu_id)
    return render_template('edit-menu-item.html', restaurant=restaurant, item=item)
def deleteMenuItem(restaurant_id, menu_id):
  if request.method == 'POST':
    restaurant_dao.deleteMenuItem(menu_id)
    flash("Menu item deleted!")
    return redirect(url_for('restaurantMenu', restaurant_id=restaurant_id))
  else:
    restaurant = restaurant_dao.getRestaurant(restaurant_id)
    item = restaurant_dao.getMenuItemByRestaurant(restaurant_id, menu_id)
    return render_template('delete-menu-item.html', restaurant=restaurant, item=item)
  def do_GET(self):
    try:
      if self.path.endswith("/restaurants"):
        restaurants = restaurant_dao.getRestaurants()
        output = ""
        output += "<html><body>"
        output += "<h3><a href='/restaurants/new'>Make a new Restaurant</a></h3>"
        for restaurant in restaurants:
          output += "<div>"
          output += "<h3>%s</h3>" % restaurant.name
          output += "<a href='/restaurant/%s/edit'>Edit</a><br />" % restaurant.id
          output += "<a href='/restaurant/%s/delete'>Delete</a>" % restaurant.id
          output += "</div>"
        
        output += "</body></html>"

        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write(output)
        return
      elif self.path.endswith("/restaurants/new"):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        output = ""
        output += "<html><body>"
        output += "<h1>New Restaurant</h1>"
        output += '''<form method='POST' enctype='multipart/form-data' action='/restaurants/new'><input name="name" placeholder="Restaurant name" type="text" ><input type="submit" value="Create"></form>'''
        output += "</body></html>"
        self.wfile.write(output)
        return
      elif self.path.endswith("/edit"):
        id = self.path.split("/")[2]
        restaurant = restaurant_dao.getRestaurant(id)
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        output = ""
        output += "<html><body>"
        output += '''<form method='POST' enctype='multipart/form-data' action='/restaurant/%s/edit'><h3>%s</h3></br /><input type="text" name="name" placeholder="New name"><input type="submit" value="Update" /></form>''' % (restaurant.id,restaurant.name)
        output += "</body></html>"
        self.wfile.write(output)
        return
      elif self.path.endswith("/delete"):
        id = self.path.split("/")[2]
        restaurant = restaurant_dao.getRestaurant(id)
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        output = ""
        output += "<html><body>"
        output += "<h3>Are you sure you want to delete: %s</h3>" % restaurant.name
        output += '''<form method='POST' enctype='multipart/form-data' action='/restaurant/%s/delete'><a href="/restaurants"><input type="button" value="no" /></a><input type="submit" value="Submit"> </form>''' % restaurant.id
        output += "</body></html>"
        self.wfile.write(output)
        return


    except IOError:
      self.send_error(404, 'File Not Found: %s' % self.path)
def restaurantMenuItem(restaurant_id, menu_id):
  restaurant = restaurant_dao.getRestaurant(restaurant_id)
  item = restaurant_dao.getMenuItemByRestaurant(restaurant_id, menu_id)

  return render_template('menu-item.html', restaurant=restaurant, item=item)
def restaurantMenu(restaurant_id):
  restaurant = restaurant_dao.getRestaurant(restaurant_id)
  items = restaurant_dao.getMenuItemsByRestaurant(restaurant_id)

  return render_template('restaurant.html', restaurant=restaurant, items=items)