<template>
  <div>
    <!-- ****** EXISTING GLOBAL PAYMENT TABLE ****** -->
    <v-form ref="form" v-model="valid" lazy-validation>
      <v-container fluid>
        <v-flex xs12 sm12 lg12>
          <v-layout wrap>
            <v-flex xs12 sm12 lg10>
              <v-layout wrap>
                <v-flex xs12 sm12 lg4>
                  <v-flex xs12 sm4 lg4 class="subheading">
                    <label class="mt-4">Class:</label>
                  </v-flex>
                  <v-flex xs12 sm12 lg10>
                    <v-select
                      :items="addclass"
                      label="Select Class"
                      v-model="selectStudents.select"
                      item-text="classNum"
                      item-value="_id"
                      name="Select Class"
                      :rules="classRules"
                      @change="getSection(selectStudents.select)"
                      required
                    ></v-select>
                  </v-flex>
                </v-flex>
                <v-flex xs12 sm12 lg4>
                  <v-flex xs12 sm4 lg4 class="subheading">
                    <label class="mt-4">Section:</label>
                  </v-flex>
                  <v-flex xs12 sm12 lg10 class>
                    <v-select
                      :items="addSection"
                      label="Select Section"
                      v-model="selectStudents.selectSection"
                      item-text="name"
                      item-value="_id"
                      name="Select Section"
                      :rules="sectionRules"
                      @change="getStudents()"
                      required
                    ></v-select>
                  </v-flex>
                </v-flex>
                <v-flex xs12 sm12 lg4>
                  <v-flex xs12 sm4 lg4 class="subheading">
                    <label class="mt-4">Student:</label>
                  </v-flex>
                  <v-flex xs12 sm12 lg10 class>
                    <v-select
                      :items="studentData"
                      label="Select Student"
                      v-model="selectStudents.selectStudent"
                      item-text="name"
                      item-value="_id"
                      :rules="studentRules"
                      required
                    ></v-select>
                  </v-flex>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg2>
              <v-flex xs12 sm12 lg12>
                <v-btn @click="findStudents()" round dark :loading="loading" class="right mt-4">Search</v-btn>
              </v-flex>
            </v-flex>
          </v-layout>
        </v-flex>
      </v-container>
    </v-form>
    <!-- <v-data-table
      :headers="headers"
      :items="desserts"
      :pagination.sync="pagination"
      :search="search"
    >
      <template slot="items" slot-scope="props">
        <td id="td" class="text-xs-center">{{ props.item.rollNo}}</td>
        <td id="td" class="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 id="td" class="text-xs-center">{{ props.item.name}}</td>
        <td id="td" class="text-xs-center">{{ props.item.email }}</td>
        <td id="td" class="text-xs-center">{{ dates(props.item.dob) }}</td>
        <td id="td" class="text-xs-center">{{ props.item.gender }}</td>
        <td id="td" class="text-xs-center">{{ props.item.parentId.fatherName }}</td>
        <td id="td" class="text-xs-center">{{ props.item.parentId.motherName }}</td>
        <td id="td" class="text-xs-center">{{ props.item.establishmentYear }}</td>
        <td id="td" class="text-xs-center">{{ props.item.mobile}}</td>

        <td class="text-xs-center">
          <span>
            <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>
      </template>
      <v-alert
        slot="no-results"
        :value="true"
        color="error"
        icon="warning"
      >Your search for "{{ search }}" found no results.</v-alert>
    </v-data-table>-->
    <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 moment from "moment";

export default {
  data: () => ({
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    text: "",
    showLoader: false,
    loading: false,
    search: "",
    valid: true,
    addclass: [],
    selectStudents: {},
    addSection: [],
    classRules: [v => !!v || " Class Name is required"],
    sectionRules: [v => !!v || " Section Name is required"],
    studentRules: [v => !!v || "Student Name is required"],
    // pagination: {
    //   rowsPerPage: 15
    // },
    // headers: [
    //   {
    //     text: "Roll No.",
    //     align: "center",
    //     sortable: false,
    //     value: "rollNo"
    //   },
    //   {
    //     text: "Profile Pic",
    //     value: "profilePicUrl",
    //     sortable: false,
    //     align: "center"
    //   },
    //   { text: "Name", value: "name", sortable: false, align: "center" },
    //   { text: "Email", value: "email", sortable: false, align: "center" },
    //   { text: "Dob", value: "dob", sortable: false, align: "center" },
    //   { text: "Gender", value: "gender", sortable: false, align: "center" },
    //   {
    //     text: "Father Name",
    //     value: "fatherName",
    //     sortable: false,
    //     align: "center"
    //   },
    //   {
    //     text: "Mother Name",
    //     value: "motherName",
    //     sortable: false,
    //     align: "center"
    //   },
    //   {
    //     text: "Academic Year",
    //     value: "establishmentYear",
    //     sortable: false,
    //     align: "center"
    //   },
    //   { text: "Mobile No", value: "mobile", sortable: false, align: "center" },
    //   { text: "Action", value: "", sortable: false, align: "center" }
    // ],
    studentData: []
  }),
  methods: {
    // getSections(_id) {
    //   var token = this.$store.state.token;
    //   this.showLoader = true;
    //   http()
    //     .get(
    //       "/getSectionsList",
    //       { params: { classId: _id } },
    //       {
    //         headers: { Authorization: "Bearer " + token }
    //       }
    //     )
    //     .then(response => {
    //       this.addSection = response.data.data;
    //       this.showLoader = false;
    //       // console.log("getSectionsList=====>", this.addSection);
    //     })
    //     .catch(err => {
    //       this.showLoader = false;
    //       // console.log("err====>", err);
    //       // this.$router.replace({ path: '/' });
    //     });
    // },
    getSection(_id) {
      var token = this.$store.state.token;
      this.showLoader = true;
      http()
        .get(
          "/getSectionsList",
          { params: { classId: _id } },
          {
            headers: { Authorization: "Bearer " + token }
          }
        )
        .then(response => {
          this.addSection = response.data.data;
          this.showLoader = false;
          // console.log("getSectionsList=====>", this.addSection);
        })
        .catch(err => {
          this.showLoader = false;
          // console.log("err====>", err);
          // this.$router.replace({ path: '/' });
        });
    },
    getStudents() {
      this.showLoader = true;
      http()
        .get("/getStudentWithClass", {
          params: {
            classId: this.selectStudents.select,
            sectionId: this.selectStudents.selectSection
          }
        })
        .then(response => {
          this.studentData = response.data.data;
          this.showLoader = false;
          console.log("getSectionsList=====>", this.addSection);
        })
        .catch(err => {
          console.log("err====>", err);
          this.showLoader = false;
        });
    }
    // dates: function(date) {
    //   return moment(date).format("MMMM DD, YYYY");
    // },
    // getParentDetails() {
    //   if (this.parentData.email) {
    //     http()
    //       .get("getParticularParent", {
    //         params: { email: this.parentData.email },
    //         headers: {
    //           Authorization: "Bearer " + this.$store.state.token
    //         }
    //       })
    //       .then(response => {
    //         this.showNext = true;
    //         this.showParent = false;
    //         this.parentData = response.data.data;
    //         this.parentId = response.data.data._id;
    //       })
    //       .catch(error => {
    //         console.log("err====>", error.response.data.message);
    //       });
    //   }
    // }
  },
  mounted() {
    var token = this.$store.state.token;
    http()
      .get("/getClassesList", {
        headers: { Authorization: "Bearer " + token }
      })
      .then(response => {
        this.addclass = response.data.data;
      })
      .catch(error => {
        this.showLoader = false;
        if (error.response.status === 401) {
          this.$router.replace({ path: "/" });
          this.$store.dispatch("setToken", null);
          this.$store.dispatch("Id", null);
          this.$store.dispatch("Role", null);
        }
      });
  },
  created() {
    this.$root.$on("app:search", search => {
      this.search = search;
    });
  },
  beforeDestroy() {
    // dont forget to remove the listener
    this.$root.$off("app:search");
  }
};
</script>
