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

    <!-- ******  Progress Card Report Table****** -->

    <v-card flat>
      <v-card-actions>
        <v-layout>
          <h4 class="ml-2">Progress Card Report</h4>
        </v-layout>
      </v-card-actions>
    </v-card>
    <v-card flat>
      <v-form ref="form" v-model="valid" lazy-validation>
        <v-flex xs12 sm12 lg12>
          <v-layout wrap>
            <v-flex xs12 sm12 lg3>
              <v-layout>
                <v-flex xs3 sm6 lg2 class="subheading mt-4">
                  <label class="right">Class:</label>
                </v-flex>
                <v-flex xs12 sm12 lg8 class="ml-2">
                  <v-select
                    v-model="getReport.classId"
                    label="Select your class"
                    type="text"
                    :items="classList"
                    item-text="classNum"
                    item-value="_id"
                    :rules="classRules"
                    @change="getSections(getReport.classId)"
                    required
                  ></v-select>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg3>
              <v-layout>
                <v-flex xs3 sm6 lg2 class="subheading mt-4">
                  <label class="right">Section:</label>
                </v-flex>
                <v-flex xs12 sm12 lg8 class="ml-2">
                  <v-select
                    :items="addSection"
                    label="Select your Section"
                    v-model="getReport.sectionId"
                    item-text="name"
                    item-value="_id"
                    name="Select Section"
                    @change="getStudents"
                    :rules="sectionRules"
                    required
                  ></v-select>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg3>
              <v-layout>
                <v-flex xs3 sm6 lg2 class="subheading mt-4">
                  <label class="right">Student:</label>
                </v-flex>
                <v-flex xs12 sm12 lg8 class="ml-2">
                  <v-select
                    :items="getStudentsList"
                    label="Select your student"
                    v-model="getReport.studentId"
                    item-text="name"
                    item-value="_id"
                    :rules="studentRules"
                    required
                  ></v-select>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg3>
              <v-btn @click="getReport" round dark :loading="loading" class="right mt-3">Get Report</v-btn>
            </v-flex>
          </v-layout>
        </v-flex>
      </v-form>
    </v-card>
    <!-- <v-data-table
      :headers="headers"
      :items="getStudentsList"
      :pagination.sync="pagination"
      :search="search"
    >
      <template slot="items" slot-scope="props">
        <td class="text-xs-center">{{ props.index + 1}}</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.rollNo }}</td>
        <td id="td" class="text-xs-center">{{ props.item.email }}</td>
        <td class="text-xs-center">
          <span>
            <router-link :to="{ name:'viewMark',params: { markId:props.item._id } }">
              <img
                style="cursor:pointer; width:20px; height:18px; "
                class="mr-5"
                src="/static/icon/edit1.png"
              />
            </router-link>
          </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>
  </v-app>
</template>

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

export default {
  data: () => ({
    snackbar: false,
    color: "",
    y: "top",
    x: "right",
    mode: "",
    timeout: 10000,
    text: "",
    loading: false,
    search: "",
    showLoader: false,
    valid: true,
    addSection: [],
    classRules: [v => !!v || "Class is required"],
    sectionRules: [v => !!v || "Class is required"],
    studentRules: [v => !!v || "Student is required"],
    pagination: {
      rowsPerPage: 15
    },
    headers: [
      {
        align: "justify-center",
        text: "No",
        sortable: false,
        value: "No"
      },
      {
        text: "Profile Pic",
        vaue: "profilePicUrl",
        sortable: false,
        align: "center"
      },
      {
        text: "Name",
        vaue: "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" }
    ],
    classList: [],
    getReport: {},
    markData: [],
    examList: [],
    getStudentsList: [],
    token: ""
  }),
  methods: {
    clear() {
      this.$refs.form.reset();
    },
    getClass() {
      http()
        .get("/getClassesList", {
          headers: { Authorization: "Bearer " + this.token }
        })
        .then(response => {
          this.classList = response.data.data;
        })
        .catch(err => {
          // console.log("err====>", err);
        });
    },
    getSections(_id) {
      http()
        .get(
          "/getSectionsList",
          { params: { classId: _id } },
          {
            headers: { Authorization: "Bearer " + this.token }
          }
        )
        .then(response => {
          this.addSection = response.data.data;
        })
        .catch(err => {
          // console.log("err====>", err);
        });
    },
    getStudents() {
      this.showLoader = true;
      http()
        .get("/getStudentWithClass", {
          params: {
            classId: this.getReport.classId,
            sectionId: this.getReport.sectionId
          }
        })
        .then(response => {
          this.getStudentsList = response.data.data;
          this.showLoader = false;
          console.log("getSectionsList=====>", response.data.data);
        })
        .catch(error => {
          console.log("err====>", error);
          this.showLoader = false;
        });
    },
    getReport() {
      if (this.$refs.form.validate()) {
        this.showLoader = true;
        http()
          .get("/getParticularMark", {
            params: { studentId: this.getReport.studentId },
            headers: { Authorization: "Bearer " + this.token }
          })
          .then(response => {
            console.log("response.data.data", response.data.data);
            // this.examList = response.data.data;
            this.showLoader = false;
            this.clear();
          })
          .catch(error => {
            console.log("err====>", error);
            this.showLoader = false;
            //       this.snackbar = true;
            //       this.text = error.response.data.message;
            //       if (error.response.status === 401) {
            //         this.$router.replace({ path: "/" });
            //         this.$store.dispatch("setToken", null);
            //         this.$store.dispatch("Id", null);
            // }
          });
      }
    }
  },
  mounted() {
    this.token = this.$store.state.token;
    // this.getExamList();
    this.getClass();
  },
  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>s