<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>

    <!-- ******  Student Report TABLE****** -->
    <v-card flat>
      <v-card-actions>
        <v-layout>
          <h4 class="right mt-2 ml-2">Student Report</h4>
        </v-layout>
        <v-spacer></v-spacer>
      </v-card-actions>
    </v-card>
    <v-card flat class="elevation-0">
      <v-form ref="form" v-model="valid" lazy-validation>
        <v-flex xs12 sm12 lg12>
          <v-layout wrap>
            <v-flex xs12 sm12 lg4>
              <v-layout>
                <v-flex xs4 sm4 lg3 class="subheading mt-4">
                  <label class="right">Report For :</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    v-model="report.form"
                    label="Select your report for"
                    :items="formList"
                    item-text="name"
                    item-value="value"
                    @change="getReport(report.form)"
                    :rules="formRules"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg4 v-if="bloodGroup">
              <v-layout>
                <v-flex xs4 sm4 lg4 class="subheading mt-4">
                  <label class="right">Blood Group :</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    v-model="report.bloodGroup"
                    label="Select your Blood Group "
                    :items="bloodGroupList"
                    :rules="bloodGroupRules"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg4 v-if="genderShow">
              <v-layout>
                <v-flex xs4 sm4 lg4 class="subheading mt-4">
                  <label class="right">Gender:</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    v-model="report.gender"
                    label="Select your gender "
                    :items="gender"
                    :rules="genderRules"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg4 v-if="TransportShow">
              <v-layout>
                <v-flex xs4 sm4 lg4 class="subheading mt-4">
                  <label class="right">Route:</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    v-model="report.route"
                    label="Select your gender "
                    :items="route"
                    :rules="routeRules"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg4 v-if="HostelShow">
              <v-layout>
                <v-flex xs4 sm4 lg4 class="subheading mt-4">
                  <label class="right">Hostel:</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    v-model="report.route"
                    label="Select your hostel "
                    :items="hostelList"
                    :rules="hostelRules"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg4 v-if="countryShow">
              <v-layout>
                <v-flex xs4 sm4 lg4 class="subheading mt-4">
                  <label class="right">Country :</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    v-model="report.country"
                    label="Select your Country "
                    :items="countryList"
                    :rules="countryRules"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <!-- <v-flex xs12 sm12 lg4 v-show="BirthdayShow">
            <v-layout>
              <v-flex xs3 sm6 lg4 class="subheading mt-4">
                <label class="right">Country :</label>
              </v-flex>
              <v-flex xs12 sm12 lg8 class="ml-2">
                <v-select
                  v-model="report.country"
                  label="Select your Country "
                  :items="countryList"
                  required
                ></v-select>
              </v-flex>
            </v-layout>
            </v-flex>-->
            <v-flex xs12 sm12 lg4 v-if="classShow">
              <v-layout>
                <v-flex xs4 sm4 lg3 class="subheading mt-4">
                  <label class="right">Class:</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    v-model="report.classId"
                    label="Select your class"
                    type="text"
                    :rules="classRules"
                    :items="classList"
                    item-text="classNum"
                    item-value="_id"
                    @change="getSections(report.classId)"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg4 v-if="sectionShow">
              <v-layout>
                <v-flex xs4 sm4 lg3 class="subheading mt-4">
                  <label class="right">Section:</label>
                </v-flex>
                <v-flex xs7 sm6 lg8 class="ml-2">
                  <v-autocomplete
                    :items="addSection"
                    label="Select your Section"
                    v-model="report.sectionId"
                    :rules="sectionRules"
                    item-text="name"
                    item-value="_id"
                    name="Select Section"
                    required
                  ></v-autocomplete>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12 lg4 class="hidden-xs-only hidden-sm-only">
              <v-btn @click="getStudents" round dark :loading="loading" class="mt-3 ml-5">Get Report</v-btn>
            </v-flex>
            <v-flex xs12 sm12 lg4 class="hidden-lg-only hidden-md-only hidden-xl-only">
              <v-btn
                @click="getStudents"
                round
                dark
                :loading="loading"
                class="mt-3 right"
              >Get Report</v-btn>
            </v-flex>
          </v-layout>
        </v-flex>
      </v-form>
    </v-card>
    <v-data-table
      :headers="headers"
      :items="getStudentsReportList"
      :pagination.sync="pagination"
      :search="search"
      v-show="showTable"
    >
      <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 id="td" class="text-xs-center">{{ props.item.classId.classNum }}</td>
        <td id="td" class="text-xs-center">{{ props.item.sectionId.name }}</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 countryList from "@/script/country.js";

import moment from "moment";

export default {
  data: () => ({
    snackbar: false,
    color: "",
    y: "top",
    x: "right",
    mode: "",
    timeout: 10000,
    text: "",
    loading: false,
    valid: true,
    search: "",
    showLoader: false,
    bloodGroup: false,
    genderShow: false,
    countryShow: false,
    TransportShow: false,
    HostelShow: false,
    BirthdayShow: false,
    sectionShow: false,
    showTable: false,
    ClassShow: true,
    addSection: [],
    pagination: {
      rowsPerPage: 15
    },
    formRules: [v => !!v || "Form For is required"],
    bloodGroupRules: [v => !!v || "Blood group is required"],
    classRules: [v => !!v || "Class is required"],
    sectionRules: [v => !!v || "Class is required"],
    genderRules: [v => !!v || "Gender is required"],
    routeRules: [v => !!v || "RouteI is required"],
    hostelRules: [v => !!v || "Hostel is required"],
    countryRules: [v => !!v || "Country is required"],
    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: "Class",
        value: "classId",
        sortable: false,
        align: "center"
      },
      {
        text: "Section",
        value: "sectionId",
        sortable: false,
        align: "center"
      }
    ],
    classList: [],
    report: {},
    markData: [],
    formList: [
      {
        name: "Blood Group",
        value: "bloodGroup"
      },
      "Country",
      "Gender",
      "Transport",
      "Hostel"
      //   "Birthday"
    ],
    bloodGroupList: ["A+", "A-", "B+", "B-", "O+", "O-", "AB+", "AB-"],
    gender: ["Male", "female"],
    getStudentsReportList: [],
    studentReort: "",
    countryList: [],
    route: [],
    Hostel: [],
    hostelList: [],
    token: ""
  }),
  methods: {
    clear() {
      this.$refs.form.reset();
    },
    getClass() {
      this.showLoader = true;
      http()
        .get("/getClassesList", {
          headers: { Authorization: "Bearer " + this.token }
        })
        .then(response => {
          this.classList = response.data.data;
          this.showLoader = false;
        })
        .catch(err => {
          this.showLoader = false;
          // console.log("err====>", err);
        });
    },
    getReport() {
      if (this.report.form === "bloodGroup") {
        this.bloodGroup = true;
        this.genderShow = false;
        this.TransportShow = false;
        this.countryShow = false;
        this.BirthdayShow = false;
        this.HostelShow = false;
        this.classShow = false;
        this.sectionShow = false;
      }
      if (this.report.form === "Gender") {
        this.genderShow = true;
        this.bloodGroup = false;
        this.TransportShow = false;
        this.BirthdayShow = false;
        this.countryShow = false;
        this.HostelShow = false;
        this.classShow = true;
      }
      if (this.report.form === "Transport") {
        this.TransportShow = true;
        this.genderShow = false;
        this.bloodGroup = false;
        this.BirthdayShow = false;
        this.countryShow = false;
        this.HostelShow = false;
        this.classShow = true;
      }
      if (this.report.form === "Country") {
        this.countryShow = true;
        this.TransportShow = false;
        this.genderShow = false;
        this.bloodGroup = false;
        this.BirthdayShow = false;
        this.HostelShow = false;
        this.classShow = true;
      }
      if (this.report.form === "Hostel") {
        this.HostelShow = true;
        this.TransportShow = false;
        this.genderShow = false;
        this.bloodGroup = false;
        this.countryShow = false;
        this.BirthdayShow = false;
        this.classShow = true;
      }
      if (this.report.form === "Birthday") {
        this.BirthdayShow = true;
        this.TransportShow = false;
        this.genderShow = false;
        this.HostelShow = false;
        this.bloodGroup = false;
        this.countryShow = false;
        this.classShow = true;
      }
    },
    getStudents() {
      if (this.$refs.form.validate()) {
        this.showLoader = true;
        http()
          .get("/getStudentReport", {
            params: {
              bloodGroup: this.report.bloodGroup,
              country: this.report.country,
              gender: this.report.gender,
              classId: this.report.classId,
              sectionId: this.report.sectionId
            },
            headers: { Authorization: "Bearer " + this.token }
          })
          .then(response => {
            this.getStudentsReportList = response.data.data;
            this.showTable = true;
            this.showLoader = false;
            this.clear();
            // console.log("getSectionsList=====>", response.data.data);
          })
          .catch(error => {
            console.log("err====>", error);
            this.showLoader = false;
          });
      }
    },
    getSections() {
      this.sectionShow = true;
      this.showLoader = true;
      http()
        .get(
          "/getSectionsList",
          { params: { classId: this.report.classId } },
          {
            headers: { Authorization: "Bearer " + this.token }
          }
        )
        .then(response => {
          this.addSection = response.data.data;
          this.showLoader = false;
        })
        .catch(error => {
          console.log("err====>", error);
          this.showLoader = false;
        });
    }
    //     getFormList() {
    //       this.showLoader = true;
    //       http()
    //         .get("/getExamsList", {
    //           headers: { Authorization: "Bearer " + this.token }
    //         })
    //         .then(response => {
    //           this.formList = response.data.data;
    //           this.showLoader = false;
    //         })
    //         .catch(error => {
    //           // console.log("err====>", err);
    //           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;
    const getCountryList = countryList();
    this.countryList = getCountryList;
    // console.log("getindustryList", this.countryList);
    // 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