<template>
  <v-container fluid class="body-color">
    <!-- ****** Edit  Member  ****** -->
    <v-dialog v-model="editBookDialog" max-width="500px" persistent>
      <v-card flat class="card-style pa-2" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Edit Book</label>
            <v-icon size="24" class="right" @click="editBookDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-card-text>
          <v-container>
            <v-layout wrap justify-center>
              <v-flex xs12>
                <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-3">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-spacer></v-spacer>
                    <v-btn round dark @click="save" class="add-button">Save</v-btn>
                    <v-spacer></v-spacer>
                  </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="viewBookDialog" max-width="500px" persistent>
      <v-card flat class="card-style pa-3" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">View Book</label>
            <v-icon size="24" class="right" @click="viewBookDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <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>
    <!-- ****** BOOKS TABLE ****** -->

    <v-toolbar color="transparent" flat>
      <v-btn
        fab
        dark
        class="open-dialog-button hidden-xl-only hidden-md-only hidden-lg-only"
        small
        @click="addBookDialog = true"
      >
        <v-icon dark>add</v-icon>
      </v-btn>
      <v-btn
        round
        class="open-dialog-button hidden-sm-only hidden-xs-only"
        dark
        @click="addBookDialog = true"
      >
        <v-icon class="white--text pr-1" size="20">add</v-icon>Add Book
      </v-btn>
      <v-spacer></v-spacer>
      <v-card-title class="body-1" v-show="show">
        <v-btn icon large flat @click="displaySearch">
          <v-avatar size="27">
            <img src="/static/icon/search.png" alt="icon" />
          </v-avatar>
        </v-btn>
      </v-card-title>
      <v-flex xs8 sm8 md3 lg2 v-if="showSearch">
        <v-layout>
          <v-text-field
            autofocus
            v-model="search"
            label="Search"
            prepend-inner-icon="search"
            color="primary"
          ></v-text-field>
          <v-icon @click="closeSearch" color="error">close</v-icon>
        </v-layout>
      </v-flex>
    </v-toolbar>
    <v-data-table
      :headers="headers"
      :items="bookData"
      :pagination.sync="pagination"
      :search="search"
    >
      <template slot="items" slot-scope="props">
        <td class="td td-row text-xs-center">{{ props.index + 1}}</td>
        <td class="td td-row text-xs-center">{{ props.item.name }}</td>
        <td class="td td-row text-xs-center">{{ props.item.author }}</td>
        <td class="td td-row text-xs-center">{{ props.item.subjectCode }}</td>
        <td class="td td-row text-xs-center">{{ props.item.price }}</td>
        <td class="td td-row text-xs-center">{{ props.item.quantity }}</td>
        <td class="td td-row text-xs-center">{{ props.item.rackNo }}</td>
        <td class="td td-row text-xs-center">
          <span
            v-if="props.item.status === 'AVAILABLE'"
            class="green lighten-1 pa-1 px-2 white--text paymentStatus"
          >{{ props.item.status}}</span>
          <span
            v-if="props.item.status === 'UNAVAILABLE'"
            class="red lighten-1 pa-1 px-2 white--text paymentStatus"
          >{{ props.item.status}}</span>
        </td>
        <td class="text-xs-center td td-row">
          <span>
            <!-- <v-tooltip top>
              <img
                slot="activator"
                style="cursor:pointer; width:25px; height:25px; "
                class="mr-3"
                @click="profile(props.item)"
                src="/static/icon/view.png"
              />
              <span>View</span>
            </v-tooltip>-->
            <v-tooltip top>
              <img
                slot="activator"
                style="cursor:pointer; width:20px; height:18px; "
                class="mr-3"
                @click="editItem(props.item)"
                src="/static/icon/edit.png"
              />
              <span>Edit</span>
            </v-tooltip>
            <v-tooltip top v-if="role != 'TEACHER' ">
              <img
                slot="activator"
                style="cursor:pointer; width:20px; height:20px; "
                @click="deleteItem(props.item)"
                src="/static/icon/delete.png"
              />
              <span>Delete</span>
            </v-tooltip>
          </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>
    <!-- ****** ADD BOOK ****** -->
    <v-dialog v-model="addBookDialog" max-width="600px" v-if="addBookDialog" persistent>
      <v-card flat class="card-style pa-2" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Add Book</label>
            <v-icon size="24" class="right" @click="$refs.form.reset();addBookDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <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 xs7 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 xs7 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 xs7 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 xs7 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 xs7 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 xs7 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>
                    <v-card-actions>
                      <v-spacer></v-spacer>
                      <v-btn @click="clear" round dark class="clear-button">clear</v-btn>
                      <v-btn @click="submit" round dark :loading="loading" class="add-button">Add</v-btn>
                    </v-card-actions>
                  </v-flex>
                </v-layout>
              </v-form>
            </v-flex>
          </v-layout>
        </v-container>
      </v-card>
    </v-dialog>
    <v-snackbar
      :timeout="timeout"
      :top="y === 'top'"
      :right="x === 'right'"
      :vertical="mode === 'vertical'"
      v-model="snackbar"
      :color="color"
    >{{ text }}</v-snackbar>
    <div class="loader" v-if="showLoader">
      <v-progress-circular indeterminate color="white"></v-progress-circular>
    </div>
  </v-container>
</template>

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

export default {
  data: () => ({
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 5000,
    text: "",
    color: "",
    show: true,
    showSearch: false,
    showLoader: false,
    loading: false,
    date: null,
    search: "",
    editBookDialog: false,
    viewBookDialog: false,
    valid: true,
    validEdit: true,
    addBookDialog: false,
    pagination: {
      rowsPerPage: 10,
    },
    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: "",
        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: "Action", value: "", sortable: false, align: "center" },
    ],
    bookData: [],
    select: "",
    token: "",
    editedItem: {},
    BooksData: {},
  }),
  watch: {
    addBookDialog: function (val) {
      if (!val) {
        this.BooksData = [];
      }
    },
  },
  methods: {
    getBookData() {
      this.showLoader = true;
      http()
        .get("/getBooksList", {
          params: {
            schoolId: this.$store.state.schoolId,
          },
          headers: { Authorization: "Bearer " + this.token },
        })
        .then((response) => {
          this.bookData = response.data.data;
          this.showLoader = false;
        })
        .catch((error) => {
          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.editBookDialog = true;
    },
    profile(item) {
      this.editedIndex = this.bookData.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.viewBookDialog = true;
    },
    deleteItem(item) {
      let deleteStudent = {
        bookId: item._id,
      };
      http()
        .delete(
          "/deleteBook",
          confirm("Are you sure you want to delete this?") && {
            params: deleteStudent,
          }
        )
        .then((response) => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.getBookData();
        })
        .catch((error) => {
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    closeEditProfile() {
      this.editBookDialog = false;
    },
    close2() {
      this.dialog2 = false;
    },
    submit() {
      this.BooksData.quantity = Number(this.BooksData.quantity);
      this.BooksData.schoolId = this.$store.state.schoolId;
      if (this.$refs.form.validate()) {
        this.loading = true;
        http()
          .post("/createBook", this.BooksData)
          .then((response) => {
            this.getBookData();
            this.snackbar = true;
            this.text = response.data.message;
            this.color = "green";
            this.addBookDialog = false;
            this.clear();
            this.loading = false;
          })
          .catch((error) => {
            this.snackbar = true;
            this.text = error.response.data.message;
            this.color = "error";
            this.loading = false;
          });
      }
    },
    clear() {
      this.$refs.form.reset();
    },
    save() {
      this.editedItem.bookId = this.editedItem._id;
      http()
        .put("/updateBook", this.editedItem)
        .then((response) => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.getBookData();
          this.closeEditProfile();
        })
        .catch((error) => {
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
          // console.log(error);
        });
    },
    displaySearch() {
      (this.show = false), (this.showSearch = true);
    },
    closeSearch() {
      this.showSearch = false;
      this.show = true;
      this.search = "";
    },
  },
  mounted() {
    this.token = this.$store.state.token;
    this.getBookData();
    this.role = this.$store.state.role;
  },
};
</script>
