<?php

require_once 'model/session.php';
require_once 'model/restaurant.php';
require_once 'model/order.php';
$session = new Session();
$order = new Order();
$restaurant = new Restaurant();
$user_id = $session->get_session_data('id');
$restaurants = $restaurant->multipleFind('owner_id', $user_id);
$size = sizeof($restaurants);
$restaurant_ids = [];
$count = 1;
$arr = "(";
foreach ($restaurants as $key => $value) {
    if ($count == $size) {
        $arr .= $value['id'];
    } else {
        $arr .= $value['id'] . ",";
    }
    $count += 1;
}
$result = $order->execSQL('SELECT *,`order`.`id` as `orderId`,`order`.`status` as `orderStatus` FROM `order` INNER JOIN `users` ON `users`.`id` = `order`.`user_id` INNER JOIN `restaurant` ON `restaurant`.`id` = `order`.`restaurant_id` INNER JOIN `credit_cards` ON `credit_cards`.`id` = `order`.`card_id` INNER JOIN `delivery_address` ON `delivery_address`.`id` = `order`.`address_id` WHERE `order`.`restaurant_id` IN ' . $arr . ');');
$data = [];
while ($row = mysqli_fetch_object($result)) {
    array_push($data, (array) $row);
}
?>

<!DOCTYPE html>
<html lang="en">
?>
	<div class="container" style="margin-top:100px;margin-bottom:50px;min-height: 500px;">
		<?php 
if ($session->session_exist('user_type')) {
    ?>
		<?php 
    if ($session->get_session_data('user_type') == 'restaurant_owner') {
        ?>

		<h1>Your Restaurants</h1>
		<a href="new_restaurant.php" class="btn btn-warning" style="float:right;margin-top:-40px"><i class="fa fa-plus"></i> Add new restaurant</a>
		<div>
			<div class="col-sm-8">
			<?php 
        $restaurant = new Restaurant();
        $data = $restaurant->multipleFind('owner_id', $session->get_session_data('id'));
        // var_dump($data);
        foreach ($data as $key => $value) {
            echo '<div class="row" restaurant-id="' . $value['id'] . '">
				<div class="col-sm-2">
				<a href="/view-restaurant.php?id=' . $value['id'] . '"><img src=".' . $value['image'] . '" height="90px" width="90px"></a>
				</div>
				<div class="col-sm-6">
				<a href="/view-restaurant.php?id=' . $value['id'] . '"><h4>' . ($key + 1) . '. ' . $value['name'] . '</h4></a>
				<h4>' . $value['rating'] . ' star rating ' . $value['review_count'] . ' reviews</h4>';
            switch ((int) $value['status']) {
                case 1:
                    echo '<span class="label label-success">Approved</span>';
                    break;
                case 2:
                    echo '<span class="label label-warning">Blocked</span>';