<template>
  <div class="container">
    <div class="container">
      <div class="row">
        <div class="col-md-12 col-lg-8">
          <div class="title-box-d">
            <br />
            <h1 class="title-d" style="text-align:left; font-size: 250%">My Cart</h1>
          </div>
        </div>
      </div>
      <!-- ListView -->
      <ListView
        :items="cartItems"
        :displayColumns="columns"
        :displayFormats="formats"
        :editable="true"
        :deleteable="true"
        @onEdit="Edit"
        @onDelete="Delete"
      />
      <div class="row">
        <div class="col-md-12" style="margin-bottomL 1em">
          <!-- RestaurantFeeDel -->
        </div>
      </div>
      <div class="row">
        <div class="col-md-2 offset-md-8" style="margin-bottomL 1em">
          <label style="margin-top: 0.5em">
            <b>Delivery Fee:</b>
          </label>
        </div>
        <div class="col-md-2" style="margin-bottomL 1em">
          <label style="margin-top: 0.5em">
            <b>{{deliveryFee | toCurrency}}</b>
          </label>
        </div>
      </div>
      <div class="row">
        <div class="col-md-2 offset-md-8" style="margin-bottomL 1em">
          <label style="margin-top: 0.5em">
            <b>Order Total:</b>
          </label>
        </div>
        <div class="col-md-2" style="margin-bottomL 1em">
          <label style="margin-top: 0.5em">
            <b>{{OrderTotal | toCurrency}}</b>
          </label>
        </div>
      </div>
      <modal name="menu" :width="800" :height="600">
        <Cart :showItem="CurrentItem" @Close="CloseItem" />
      </modal>
      <br />
      <div class="row">
        <div class="col-md-12" style="margin-bottomL 1em">
          <div class="row offset-md-4">
            <div class="col-md-3">
              <button
                @click="CheckOut()"
                class="btn btn-b-n"
                type="button"
                data-dismiss="modal"
                :disabled="cartItems.length == 0"
              >Check Out</button>
            </div>
            <div class="col-md-3">
              <button
                @click="ClearCart()"
                class="btn btn-b-n"
                type="button"
                data-dismiss="modal"
                :disabled="cartItems.length == 0"
              >Clear Cart</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
import ListView from "../shared/listView";
import Cart from "./cartItem";

export default {
  name: "MyCart",
  components: {
    ListView,
    Cart,
  },
  data() {
    return {
      wait: true,
      columns: ["image", "name", "description", "qty", "subTotal"],
      formats: ["image", "", "", "", "money"],
      item: {},
    };
  },
  methods: {
    ...mapActions("cart", ["getDeliveryFee"]),
    New() {},
    Edit(item) {
      this.item = item;
      this.$modal.show("menu");
    },
    Delete(item) {
      this.cartItems.pop(item);
    },
    CloseItem(value) {
      if (value) {
        this.$modal.hide("menu");
      }
    },
    CheckOut() {
      this.$router.push("/ConfirmOrder");
    },
    ClearCart() {
      this.deliveryFee = 0;
      this.cartItems.splice(0, this.cartItems.length);
    },
  },
  computed: {
    ...mapState("cart", ["cartItems", "deliveryFee"]),
    CurrentItem() {
      return this.item;
    },
    OrderTotal() {
      var tot = 0;
      for (let i = 0; i < this.cartItems.length; i++) {
        tot = tot + this.cartItems[i].subTotal;
      }
      return tot + this.deliveryFee;
    },
  },
  mounted() {
    if (this.cartItems.length > 0) {
      this.getDeliveryFee(this.cartItems[0].restaurantId);
    }
  },
};
</script>
