<template>
  <v-app id="pages-dasboard">
    <v-tabs grow slider-color="gray">
      <v-tab
        ripple
        @click="activeTab('existing')"
        v-bind:class="{ active: isActive }"
        id="tab"
        class="subheading"
      >Existing Books</v-tab>
      <v-tab
        ripple
        @click="activeTab('new')"
        v-bind:class="{ active: newActive }"
        id="tab1"
        User
        class="subheading"
      >Add New Books</v-tab>

      <!-- ****** EDIT  SECTION  ****** -->
      <v-tab-item>
        <v-snackbar
          :timeout="timeout"
          :top="y === 'top'"
          :right="x === 'right'"
          :vertical="mode === 'vertical'"
          v-model="snackbar"
          color="success"
        >{{ text }}</v-snackbar>
        <v-dialog v-model="dialog" max-width="600px">
          <v-toolbar color="grey lighten-2">
            <v-spacer></v-spacer>
            <v-toolbar-title>
              <h3>Edit Books</h3>
            </v-toolbar-title>
            <v-spacer></v-spacer>
          </v-toolbar>
          <v-card>
            <v-card-text>
              <v-container>
                <v-layout wrap justify-center>
                  <v-flex xs12 sm9>
                    <v-form>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right pr-3">Name:</label>
                        </v-flex>
                        <v-flex xs8>
                          <v-text-field v-model="editedItem.name" placeholder="fill your Name"></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right pr-3">Author:</label>
                        </v-flex>
                        <v-flex xs8>
                          <v-text-field
                            v-model="editedItem.author"
                            placeholder="fill your Author Name"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right pr-2">Subject Code:</label>
                        </v-flex>
                        <v-flex xs8>
                          <v-text-field
                            v-model="editedItem.subjectCode"
                            placeholder="fill your  Subject Code"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right pr-3">Price :</label>
                        </v-flex>
                        <v-flex xs8>
                          <v-text-field
                            v-model="editedItem.price"
                            placeholder="fill your  Subject Code"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right pr-3">Quantity:</label>
                        </v-flex>
                        <v-flex xs8>
                          <v-text-field
                            v-model="editedItem.quantity"
                            type="number"
                            placeholder="fill your Quantity"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right pr-3">Rack No:</label>
                        </v-flex>
                        <v-flex xs8>
                          <v-text-field v-model="editedItem.rackNo" placeholder="fill your Rack No"></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-card-actions>
                        <v-btn round dark @click.native="close">Cancel</v-btn>
                        <v-spacer></v-spacer>
                        <v-btn round dark @click="save">Save</v-btn>
                      </v-card-actions>
                    </v-form>
                  </v-flex>
                </v-layout>
              </v-container>
            </v-card-text>
          </v-card>
        </v-dialog>

        <!-- ****** PROFILE VIEW BOOKS DATA ******  -->

        <v-dialog v-model="dialog1" max-width="600px">
          <v-toolbar color="grey lighten-2">
            <v-spacer></v-spacer>
            <v-toolbar-title>
              <h3>Books</h3>
            </v-toolbar-title>
            <v-spacer></v-spacer>
            <v-icon @click="close1">close</v-icon>
          </v-toolbar>
          <v-card>
            <v-card-text>
              <v-container grid-list-md>
                <v-layout wrap>
                  <v-flex>
                    <v-layout>
                      <v-flex xs5 sm6>
                        <h5 class="right my-1">
                          <b>Name:</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.name }}</h5>
                      </v-flex>
                    </v-layout>
                    <v-layout>
                      <v-flex xs5 sm6>
                        <h5 class="right my-1">
                          <b>Author:</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.author }}</h5>
                      </v-flex>
                    </v-layout>
                    <v-layout>
                      <v-flex xs5 sm6>
                        <h5 class="right my-1">
                          <b>Subject Code :</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.subjectCode }}</h5>
                      </v-flex>
                    </v-layout>
                    <v-layout>
                      <v-flex xs5 sm6>
                        <h5 class="right my-1">
                          <b>Price :</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.price }}</h5>
                      </v-flex>
                    </v-layout>
                    <v-layout>
                      <v-flex xs5 sm6>
                        <h5 class="right my-1">
                          <b>Quantity :</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.quantity}}</h5>
                      </v-flex>
                    </v-layout>
                    <v-layout>
                      <v-flex xs5 sm6>
                        <h5 class="right my-1">
                          <b>Rack No :</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.rackNo}}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-container>
            </v-card-text>
          </v-card>
        </v-dialog>

        <v-snackbar
          :timeout="timeout"
          :top="y === 'top'"
          :right="x === 'right'"
          :vertical="mode === 'vertical'"
          v-model="snackbar"
          color="success"
        >{{ text }}</v-snackbar>

        <!-- ****** EXISTING-BOOKS TABLE ****** -->

        <v-data-table
          :headers="headers"
          :items="bookData"
          :pagination.sync="pagination"
          :search="search"
        >
          <template slot="items" slot-scope="props">
            <td id="td" class="text-xs-center">{{ props.index + 1}}</td>
            <td id="td" class="text-xs-center">{{ props.item.name }}</td>
            <td id="td" class="text-xs-center">{{ props.item.author }}</td>
            <td id="td" class="text-xs-center">{{ props.item.subjectCode }}</td>
            <td id="td" class="text-xs-center">{{ props.item.price }}</td>
            <td id="td" class="text-xs-center">{{ props.item.quantity }}</td>
            <td id="td" class="text-xs-center">{{ props.item.rackNo }}</td>
            <td id="td" class="text-xs-center">{{ props.item.status }}</td>
            <!-- <td id="td" class="text-xs-center">{{ props.item.session}}</td> -->

            <td class="text-xs-center">
              <span>
                <img
                  style="cursor:pointer; width:25px; height:18px; "
                  class="mr-5"
                  @click="profile(props.item)"
                  src="/static/icon/eye1.png"
                />
                <img
                  style="cursor:pointer; width:20px; height:18px; "
                  class="mr-5"
                  @click="editItem(props.item)"
                  src="/static/icon/edit1.png"
                />
                <img
                  style="cursor:pointer; width:20px; height:20px; "
                  class="mr-5"
                  @click="deleteItem(props.item)"
                  src="/static/icon/delete1.png"
                />
              </span>
            </td>
          </template>
          <v-alert
            slot="no-results"
            :value="true"
            color="error"
            icon="warning"
          >Your search for "{{ search }}" found no results.</v-alert>
        </v-data-table>
      </v-tab-item>

      <!-- ****** ADD multiple Students ****** -->

      <v-tab-item>
        <v-container>
          <v-snackbar
            :timeout="timeout"
            :top="y === 'top'"
            :right="x === 'right'"
            :vertical="mode === 'vertical'"
            v-model="snackbar"
            color="success"
          >{{ text }}</v-snackbar>
          <v-flex xs12 sm8 offset-sm2 class="top">
            <v-card flat>
              <v-container fluid fill-height>
                <v-layout align-center>
                  <v-flex xs12 class="mt-4">
                    <v-form ref="form" v-model="valid" lazy-validation>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Name:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-text-field
                            v-model="BooksData.name"
                            placeholder="fill your Name"
                            type="text"
                            :rules="nameRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Author:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-text-field
                            v-model="BooksData.author"
                            placeholder="fill your Author Name"
                            type="text"
                            :rules="authorRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Subject Code :</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-text-field
                            v-model="BooksData.subjectCode"
                            placeholder="fill your Subject Code"
                            type="text"
                            :rules="subjectRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Price:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-text-field
                            v-model="BooksData.price"
                            placeholder="fill your Price"
                            :rules="priceRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Quantity:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-text-field
                            v-model="BooksData.quantity"
                            placeholder="fill your Quantity"
                            :rules="quantityRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Rack No:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-text-field
                            v-model="BooksData.rackNo"
                            placeholder="fill your Rack No"
                            :rules="rackNoRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs12 sm9 offset-sm2>
                          <v-card-actions>
                            <v-btn @click="clear" round dark>clear</v-btn>
                            <v-spacer></v-spacer>
                            <v-btn @click="submit" round dark :loading="loading">Add</v-btn>
                          </v-card-actions>
                        </v-flex>
                      </v-layout>
                    </v-form>
                  </v-flex>
                </v-layout>
              </v-container>
            </v-card>
          </v-flex>
        </v-container>
      </v-tab-item>
    </v-tabs>
    <div class="loader" v-if="showLoader">
      <v-progress-circular indeterminate color="white"></v-progress-circular>
    </div>
  </v-app>
</template>

<script>
import http from "@/Services/http.js";
import Util from "@/util";

export default {
  data: () => ({
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    text: "",
    showLoader: false,
    loading: false,
    date: null,
    search: "",
    dialog: false,
    dialog1: false,
    valid: true,
    validEdit: true,
    isActive: true,
    newActive: false,
    pagination: {
      rowsPerPage: 15
    },
    nameRules: [v => !!v || " Name is required"],
    authorRules: [v => !!v || "Author Name Monthly"],
    subjectRules: [v => !!v || "Subject Code  is required"],
    priceRules: [v => !!v || "Price  is required"],
    quantityRules: [v => !!v || "Quantity  is required"],
    rackNoRules: [v => !!v || "Rack No.  is required"],

    headers: [
      {
        text: "No",
        align: "center",
        sortable: false,
        value: "No"
      },
      {
        text: "Name",
        value: "name",
        sortable: false,
        align: "center"
      },
      { text: "Authour", value: "author", sortable: false, align: "center" },
      {
        text: "Subject Code",
        value: "subjectCode",
        sortable: false,
        align: "center"
      },
      { text: "Price", value: "price", sortable: false, align: "center" },
      { text: "Quantity", value: "quantity", sortable: false, align: "center" },
      { text: "Rack No", value: "rackNo", sortable: false, align: "center" },
      { text: "Status", value: "status", sortable: false, align: "center" },

      // { text: "Session", value: "session", sortable: false, align: "center" },
      { text: "Action", value: "", sortable: false, align: "center" }
    ],
    bookData: [],
    select: "",
    token: "",
    editedItem: {},
    BooksData: {}
  }),
  methods: {
    getBookData() {
      this.showLoader = true;
      http()
        .get("/getBooksList", {
          headers: { Authorization: "Bearer " + this.token }
        })
        .then(response => {
          this.bookData = response.data.data;
          this.showLoader = false;
          // console.log("getAllfeetypes=====>",response.data.data)
        })
        .catch(error => {
          // console.log("err====>", err);
          this.showLoader = false;
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
          }
        });
    },
    editItem(item) {
      this.editedIndex = this.bookData.indexOf(item);
      this.editedItem = Object.assign({}, item);
      console.log(this.editedItem);
      this.dialog = true;
    },
    profile(item) {
      this.editedIndex = this.bookData.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog1 = true;
    },
    deleteItem(item) {
      let deleteStudent = {
        bookId: item._id
      };
      http()
        .delete(
          "/deleteBook",
          confirm("Are you sure you want to delete this?") && {
            params: deleteStudent
          }
        )
        .then(response => {
          if ((this.snackbar = true)) {
            this.text = "Successfully delete Existing Delete Student ";
          }
          this.getBookData();
        })
        .catch(error => {
          // console.log(error);
        });
    },
    activeTab(type) {
      switch (type) {
        case "existing":
          this.newActive = false;
          this.isActive = true;
          break;

        default:
          this.newActive = true;
          this.isActive = false;
          break;
      }
    },
    close() {
      this.dialog = false;
      setTimeout(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      }, 300);
    },
    close1() {
      this.dialog1 = false;
    },
    close2() {
      this.dialog2 = false;
    },
    submit() {
      this.BooksData.quantity = Number(this.BooksData.quantity);
      if (this.$refs.form.validate()) {
        this.loading = true;
        http()
          .post("/createBook", this.BooksData)
          .then(response => {
            console.log(response);
            this.getBookData();
            if ((this.snackbar = true)) {
              this.text = "New Book added successfully";
            }

            this.clear();
            this.loading = false;
          })
          .catch(error => {
            // console.log(error);
            if ((this.snackbar = true)) {
              this.text = error.response.data.message;
            }
            this.loading = false;
          });
      }
    },
    clear() {
      this.$refs.form.reset();
    },
    save() {
      this.editedItem.bookId = this.editedItem._id;
      http()
        .put("/updateBook", this.editedItem)
        .then(response => {
          if ((this.snackbar = true)) {
            this.text = "Successfully Edit Existing Book";
          }
          this.getBookData();
          this.close();
        })
        .catch(error => {
          this.text = error.response.data.message;
          // console.log(error);
        });
    }
  },
  mounted() {
    this.token = this.$store.state.token;
    this.getBookData();
  },
  created() {
    this.$root.$on("app:search", search => {
      this.search = search;
    });
  },
  beforeDestroy() {
    // dont forget to remove the listener
    this.$root.$off("app:search");
  }
};
</script>

<style scoped>
.active {
  background-color: gray;
  color: white !important;
}
.activebtn {
  color: black !important;
}
</style>