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

    <!-- ****** PROFILE VIEW STUDENTS LIBRARY MEMBER DEATILS ******  -->

    <v-dialog v-model="dialogStudents" max-width="1100px" scrollable>
      <v-card>
        <v-toolbar dark class="card-styles" flat>
          <v-spacer></v-spacer>
          <v-toolbar-title>View Student Profile</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-icon @click="closeViewStudent">close</v-icon>
        </v-toolbar>
        <v-card-text style="height: 700px;">
          <v-flex align-center justify-center layout text-xs-center class="mt-3">
            <v-avatar size="160px">
              <img src="/static/icon/user.png" v-if="!editedItem.profilePicUrl" />
              <img :src="editedItem.profilePicUrl" v-else-if="editedItem.profilePicUrl" />
            </v-avatar>
          </v-flex>
          <v-container grid-list-md>
            <v-layout wrap>
              <v-flex xs12>
                <v-layout wrap>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm5>
                        <h5 class="right my-1">
                          <b>Full Name:</b>
                        </h5>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1 left">{{ editedItem.name }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm4>
                        <h5 class="right my-1">
                          <b>Email:</b>
                        </h5>
                      </v-flex>
                      <v-flex sm8 xs6>
                        <h5 class="my-1 left">{{ editedItem.email }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm5>
                        <b>
                          <h5 class="right my-1">
                            <b>Gender:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1 left">{{ editedItem.gender }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm4>
                        <b>
                          <h5 class="right my-1">
                            <b>D.O.B:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1">{{ dates(editedItem.dob) }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm5>
                        <b>
                          <h5 class="right my-1">
                            <b>Blood Group:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1 left">{{ editedItem.bloodGroup }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm4>
                        <b>
                          <h5 class="right my-1">
                            <b>Allergies:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm8 xs6>
                        <h5 class="my-1">{{ editedItem.allergies }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm5>
                        <b>
                          <h5 class="right my-1">
                            <b>Height:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1 left">{{ editedItem.height }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm4>
                        <b>
                          <h5 class="right my-1">
                            <b>Weight:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm8 xs6>
                        <h5 class="my-1">{{ editedItem.weight }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm5>
                        <b>
                          <h5 class="right my-1">
                            <b>City:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1 left">{{ editedItem.city }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm4>
                        <b>
                          <h5 class="right my-1">
                            <b>State:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm8 xs6>
                        <h5 class="my-1">{{ editedItem.state }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm5>
                        <b>
                          <h5 class="right my-1">
                            <b>Pincode:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1">{{ editedItem.pincode }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm5>
                    <v-layout>
                      <v-flex xs6 sm5>
                        <b>
                          <h5 class="right my-1">
                            <b>Country:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm7 xs6>
                        <h5 class="my-1">{{ editedItem.country }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex sm5 xs6>
                        <b>
                          <h5 class="right my-1">
                            <b>Mobile No:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm6 xs6>
                        <h5 class="my-1">{{ editedItem.mobile }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm4>
                        <b>
                          <h5 class="right my-1">
                            <b>Library ID :</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm8 xs6>
                        <h5 class="my-1">{{ editedItem.libraryId }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm5>
                    <v-layout>
                      <v-flex xs6 sm6>
                        <b>
                          <h5 class="right my-1">
                            <b>Library Fee:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm6 xs6>
                        <h5 class="my-1">{{ editedItem.libraryFee }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm6>
                        <b>
                          <h5 class="my-1 right">
                            <b>Academic Year:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm5 xs6>
                        <h5 class="my-1">{{ editedItem.establishmentYear }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm5>
                    <v-layout>
                      <v-flex xs6 sm6>
                        <b>
                          <h5 class="my-1 right">
                            <b>Medical Notes:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm5 xs6>
                        <h5 class="my-1">{{ editedItem.medicalNotes }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm6>
                        <b>
                          <h5 class="right my-1">
                            <b>Roll No. :</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.rollNo }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
                <v-layout wrap>
                  <v-flex xs12 sm5>
                    <v-layout>
                      <v-flex xs6 sm6>
                        <b>
                          <h5 class="my-1 right">
                            <b>present Address:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm5 xs6>
                        <h5 class="my-1">{{ editedItem.presentAddress }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                  <v-flex xs12 sm6>
                    <v-layout>
                      <v-flex xs6 sm6>
                        <b>
                          <h5 class="right my-1">
                            <b>present Address:</b>
                          </h5>
                        </b>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.permanentAddress }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
          </v-container>
        </v-card-text>
      </v-card>
    </v-dialog>

    <!-- Edit Student Library Member Dialog -->

    <v-dialog v-model="editStudentdialog" max-width="600px" scrollable>
      <v-card flat>
        <v-toolbar dark class="card-styles" flat>
          <v-spacer></v-spacer>
          <v-toolbar-title>
            <h3>Edit Student Profile</h3>
          </v-toolbar-title>
          <v-spacer></v-spacer>
        </v-toolbar>
        <v-card-text style="height: 426px;">
          <v-form ref="form">
            <v-container fluid>
              <v-layout>
                <v-flex
                  xs12
                  class="text-xs-center text-sm-center text-md-center text-lg-center mr-4"
                >
                  <v-avatar size="160px">
                    <img src="/static/icon/user.png" v-if="!editedItem.profilePicUrl && !imageUrl" />
                    <img
                      :src="editedItem.profilePicUrl"
                      v-else-if="editedItem.profilePicUrl && !imageUrl"
                    />
                    <img
                      v-if="imageUrl"
                      :src="imageUrl"
                      height="150"
                      style="border-radius:50%; width:200px"
                    />
                  </v-avatar>
                </v-flex>
              </v-layout>
              <v-layout wrap>
                <v-flex xs12 sm12 md10>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right hidden-xs-only hidden-sm-only">Library ID:</label>
                      <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Library ID:</label>
                    </v-flex>
                    <v-flex xs8 class="ml-3">
                      <v-text-field
                        v-model="editedItem.libraryId"
                        placeholder="fill your Library ID"
                      ></v-text-field>
                    </v-flex>
                  </v-layout>
                </v-flex>
                <v-flex xs12 sm12 md10>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right hidden-xs-only hidden-sm-only">Library Fee:</label>
                      <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Library Fee:</label>
                    </v-flex>
                    <v-flex xs8 class="ml-3">
                      <v-text-field
                        v-model="editedItem.libraryFee"
                        placeholder="fill your Library Fee"
                      ></v-text-field>
                    </v-flex>
                  </v-layout>
                </v-flex>
              </v-layout>
              <v-layout>
                <v-flex xs12 sm12>
                  <v-layout class="right">
                    <v-flex xs6>
                      <v-btn
                        round
                        dark
                        class="clear-button"
                        @click.native="closeEditStudentDialog"
                      >Cancel</v-btn>
                    </v-flex>
                    <v-flex xs6>
                      <v-btn @click="save" round dark :loading="loading" class="add-button">Save</v-btn>
                    </v-flex>
                  </v-layout>
                </v-flex>
              </v-layout>
            </v-container>
          </v-form>
        </v-card-text>
      </v-card>
    </v-dialog>
    <!-- Add Student Library Member Dialog -->

    <v-dialog v-model="addLibrary" max-width="600px" v-if="addLibrary">
      <v-card flat class="card-style pa-2" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Add Library Member</label>
            <v-icon size="24" class="right" @click="closeAddLibraryModel">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-card-text>
          <v-layout wrap justify-center>
            <v-flex xs12 sm12 md10>
              <v-form ref="form" v-model="valid" lazy-validation>
                <v-layout>
                  <v-flex xs4 class="pt-4 subheading">
                    <label class="right pr-3">Library Id:</label>
                  </v-flex>
                  <v-flex xs8>
                    <v-text-field
                      v-model="addBook.libraryId"
                      :rules="libraryIdRules"
                      type="number"
                      placeholder="fill your Library ID"
                    ></v-text-field>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 class="pt-4 subheading">
                    <label class="right pr-3">Library Fee:</label>
                  </v-flex>
                  <v-flex xs8>
                    <v-text-field
                      v-model="addBook.libraryFee"
                      type="number"
                      :rules="libraryFeeRules"
                      placeholder="fill your Library Fee"
                    ></v-text-field>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs12 sm12>
                    <v-layout class="right">
                      <v-btn
                        round
                        dark
                        class="clear-button"
                        @click.native="addLibrary = false"
                      >Cancel</v-btn>
                      <v-btn round dark :loading="loading" class="add-button" @click="submit">Add</v-btn>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-form>
            </v-flex>
          </v-layout>
        </v-card-text>
      </v-card>
    </v-dialog>
    <!-- ****** EXISTING MEMBER TABLE ****** -->
    <v-card flat>
      <!-- <h4
        class="text-xs-center pt-4 ml-2 hidden-lg-only hidden-xl-only hidden-md-only"
      >Library Member</h4>-->
      <v-card-actions>
        <!-- <h3 class="right mt-2 ml-2 hidden-sm-only hidden-xs-only">Library Member</h3> -->
        <v-spacer></v-spacer>
        <v-flex xs12 sm4 md2>
          <v-select
            outline
            small
            :items="addclass"
            label="Select Class"
            v-model="selectStudents"
            item-text="classNum"
            item-value="_id"
            name="Select Class"
            @change="getAllStudents(selectStudents)"
            class="px-2"
            required
          ></v-select>
        </v-flex>
      </v-card-actions>
    </v-card>
    <v-card class="transparent">
      <v-data-table
        :headers="headers"
        :items="studentData"
        :pagination.sync="pagination"
        :search="search"
      >
        <template slot="items" slot-scope="props">
          <tr class="tr">
            <td class="td td-row text-xs-center">{{ props.index + 1 }}</td>
            <td class="td td-row text-xs-center">
              <v-avatar>
                <img :src="props.item.profilePicUrl" v-if="props.item.profilePicUrl" />
                <img src="/static/icon/user.png" v-else-if="!props.item.profilePicUrl" />
              </v-avatar>
            </td>
            <td class="td td-row text-xs-center">{{ props.item.name}}</td>
            <td class="td td-row text-xs-center">{{ props.item.rollNo }}</td>
            <td class="td td-row text-xs-center">{{ props.item.email }}</td>
            <td class="td td-row text-xs-center">
              <v-tooltip top>
                <img
                  slot="activator"
                  v-if="!props.item.libraryId"
                  style="cursor:pointer; width:25px; height:18px; "
                  @click="addLibraryData(props.item)"
                  src="/static/icon/add.png"
                />
                <span>Add</span>
              </v-tooltip>
              <span v-if="props.item.libraryId">
                <v-tooltip top>
                  <img
                    slot="activator"
                    style="cursor:pointer; width:25px; height:18px; "
                    class="mr5"
                    @click="profile(props.item)"
                    src="/static/icon/eye1.png"
                  />
                  <span>View</span>
                </v-tooltip>
                <v-tooltip top>
                  <img
                    slot="activator"
                    style="cursor:pointer; width:20px; height:18px; "
                    class="mr5"
                    @click="editItem(props.item)"
                    src="/static/icon/edit1.png"
                  />
                  <span>Edit</span>
                </v-tooltip>
                <v-tooltip top>
                  <img
                    slot="activator"
                    style="cursor:pointer; width:20px; height:20px; "
                    class="mr5"
                    @click="deleteItem(props.item)"
                    src="/static/icon/delete1.png"
                  />
                  <span>Delete</span>
                </v-tooltip>
              </span>
            </td>
          </tr>
        </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-card>
    <div class="loader" v-if="showLoader">
      <v-progress-circular indeterminate color="white"></v-progress-circular>
    </div>
  </div>
</template>

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

export default {
  data: () => ({
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    text: "",
    showLoader: false,
    loading: false,
    addLibrary: false,
    dialogStudents: false,
    editStudentdialog: false,
    valid: true,
    libraryIdRules: [(v) => !!v || "Library Id Fee is required"],
    libraryFeeRules: [(v) => !!v || "Library Fee is required"],
    date: null,
    search: "",
    addclass: [],
    pagination: {
      rowsPerPage: 15,
    },
    addBook: {},
    imageName: "",
    imageUrl: "",
    imageFile: "",
    headers: [
      {
        text: "No",
        align: "center",
        sortable: false,
        value: "index",
      },
      {
        text: "Photo",
        value: "uploadCover",
        sortable: false,
        align: "center",
      },
      { text: "Name", value: "name", sortable: false, align: "center" },
      { text: "Roll No.", value: "rollNo", sortable: false, align: "center" },
      { text: "Email", value: "email", sortable: false, align: "center" },
      { text: "Action", value: "", sortable: false, align: "center" },
    ],
    studentData: [],
    editedItem: {},
    parentId: "",
    token: "",
    selectStudents: {},
    editedIndex: -1,
  }),
  watch: {
    addLibrary: function (val) {
      if (!val) {
        this.addBook = [];
      }
    },
  },
  methods: {
    getAllStudents() {
      this.showLoader = true;
      http()
        .get("/getStudentsList", {
          params: {
            classId: this.selectStudents,
            schoolId: this.$store.state.schoolId,
          },
          headers: { Authorization: "Bearer " + this.token },
        })
        .then((response) => {
          this.studentData = response.data.data;
          this.showLoader = false;
        })
        .catch((error) => {
          // console.log("err====>", err);
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
            this.$store.dispatch("Role", null);
          }
        });
    },
    addLibraryData(item) {
      this.addBook.studentId = item._id;
      this.addLibrary = true;
    },
    editItem(item) {
      this.editedIndex = this.studentData.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.editStudentdialog = true;
    },
    dates: function (date) {
      return moment(date).format("MMMM DD, YYYY");
    },
    profile(item) {
      console.log("item", item);
      this.editedIndex = this.studentData.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialogStudents = true;
    },
    deleteItem(item) {
      let deleteStudentLibraryData = {
        studentId: item._id,
        libraryId: item.libraryId,
      };
      http()
        .put(
          "/deleteLibrary",
          confirm("Are you sure you want to delete this?") &&
            deleteStudentLibraryData
        )
        .then((response) => {
          this.snackbar = true;
          this.text = "Delete Successfully";
          this.getAllStudents();
        })
        .catch((error) => {
          // console.log(error);
        });
    },
    close() {
      this.dialog = false;
    },
    closeEditStudentDialog() {
      this.editStudentdialog = false;
    },
    closeViewStudent() {
      this.dialogStudents = false;
    },
    closeAddLibraryModel() {
      this.addLibrary = false;
      this.addclass = [];
      this.addBook = [];
    },
    submit() {
      if (this.$refs.form.validate()) {
        this.loading = true;
        this.addBook.schoolId = this.$store.state.schoolId;
        http()
          .put("/addLibrary", this.addBook)
          .then((response) => {
            this.snackbar = true;
            this.text = "New Library Member added successfully";
            this.getAllStudents();
            this.clear();
            this.addLibrary = false;
            this.loading = false;
          })
          .catch((error) => {
            // console.log(error);
            this.snackbar = true;
            this.text = error.response.data.message;
            this.loading = false;
          });
      }
    },
    clear() {
      this.$refs.form.reset();
    },
    save() {
      this.editedItem.studentId = this.editedItem._id;
      http()
        .put("/updateLibrary", this.editedItem)
        .then((response) => {
          this.snackbar = true;
          this.text = "Edit  Library Member Successfully";
          this.getAllStudents();
          this.closeEditStudentDialog();
        })
        .catch((error) => {
          this.snackbar = true;
          this.text = error.response.data.statusText;
        });
    },
    getAllClass() {
      http()
        .get("/getClassesList", {
          params: {
            schoolId: this.$store.state.schoolId,
          },
          headers: { Authorization: "Bearer " + this.token },
        })
        .then((response) => {
          this.addclass = response.data.data;
        })
        .catch((err) => {
          // console.log("err====>", err);
          this.$router.replace({ path: "/" });
        });
    },
  },
  mounted() {
    // this.getStudentList();
    this.token = this.$store.state.token;
    this.getAllClass();
  },
  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>