<template>
  <v-app id="pages-dasboard">
    <!-- ******  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 xs9 sm6 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 xs9 sm6 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 xs9 sm6  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="getMarkReportList"
                round
                dark
                :loading="loading"
                class="right mt-3"
              >Get Report</v-btn>
            </v-flex>
          </v-layout>
        </v-flex>
      </v-form>
    </v-card>
    <!-- ****** PROFILE MARK  ****** -->

    <v-container fluid grid-list-md v-if="showReport">
      <v-flex xs12 sm12>
        <v-container fluid>
          <v-layout wrap>
            <v-flex xs12 sm12 md3 class="mt-4">
              <v-card flat>
                <v-toolbar dark class="fixcolors" flat>
                  <v-spacer></v-spacer>
                  <v-toolbar-title>
                    <h3>Profile</h3>
                  </v-toolbar-title>
                  <v-spacer></v-spacer>
                </v-toolbar>
                <v-card-text>
                  <v-container>
                    <v-layout wrap v-for="(value, id, index) in filterData" v-if="index == 0">
                      <v-flex xs12>
                        <v-layout>
                          <v-flex
                            xs12
                            class="text-xs-center text-sm-center text-md-center text-lg-center"
                          >
                            <v-avatar size="100px">
                              <img
                                src="/static/icon/user.png"
                                v-if="!value[0].studentId.profilePicUrl"
                              />
                              <img
                                :src="value[0].studentId.profilePicUrl"
                                v-else-if="value[0].studentId.profilePicUrl"
                              />
                            </v-avatar>
                          </v-flex>
                        </v-layout>
                        <v-layout>
                          <v-flex xs12 sm12>
                            <h3 class="text-xs-center">
                              <b>{{ value[0].studentId.name }}</b>
                            </h3>
                            <p class="text-xs-center grey--text">Student</p>
                          </v-flex>
                        </v-layout>
                        <v-layout style="border: 1px solid lightgrey;">
                          <v-flex xs6 sm6>
                            <h4 class="right py-0">
                              <b>Roll No:</b>
                            </h4>
                          </v-flex>
                          <v-flex sm6 xs6 >
                            <h4 class="py-0">{{ value[0].studentId.rollNo }}</h4>
                          </v-flex>
                        </v-layout>
                        <v-layout style="border: 1px solid lightgrey;" >
                          <v-flex xs6 sm6>
                            <h4 class="right py-0">
                              <b>Class:</b>
                            </h4>
                          </v-flex>
                          <v-flex sm6 xs6>
                            <h4 class="py-0">{{value[0].classId.classNum }}</h4>
                          </v-flex>
                        </v-layout>
                        <v-layout style="border: 1px solid lightgrey;">
                          <v-flex xs6 sm6 class="right">
                            <h4 class="right py-0">
                              <b>Section:</b>
                            </h4>
                          </v-flex>
                          <v-flex sm6 xs6 class="right">
                            <h4 class="py-0">{{ value[0].sectionId.name}}</h4>
                          </v-flex>
                        </v-layout>
                      </v-flex>
                    </v-layout>
                  </v-container>
                </v-card-text>
              </v-card>
            </v-flex>

            <!-- ****** TABLE DATA MARK  ****** -->

            <v-flex xs12 sm12 md9 class="mt-4">
              <v-toolbar dark class="fixcolors" flat>
                <v-spacer></v-spacer>
                <v-toolbar-title>
                  <h3>Mark</h3>
                </v-toolbar-title>
                <v-spacer></v-spacer>
              </v-toolbar>
              <v-card v-for="(value, id, index) in filterData">
                <v-card-title class="subheading">{{value[0].examId.examName}}</v-card-title>
                <table class="mb-4 tableRsponsive feeTypeTable">
                  <thead>
                    <tr class="info white--text">
                      <td rowspan="2">Subject</td>
                      <td
                        colspan="2"
                        v-for="exam in value[index].studentsMarks"
                      >{{exam.markDistributionId.distributionType}}</td>
                      <td>Total</td>
                    </tr>
                    <tr v-for="(subject, ind) in value" v-if="ind == 0">
                      <template v-for="(exam, i) in subject.studentsMarks">
                        <td class="bg-sky white--text">Mark</td>
                        <td class="bg-sky-light white--text">Highest Mark</td>
                      </template>
                      <td class="bg-sky white--text">Mark</td>
                    </tr>
                  </thead>
                  <tbody>
                    <tr v-for="subject in value">
                      <td>{{subject.subjectName}}</td>
                      <template v-for="(exam, i) in subject.studentsMarks">
                        <td>{{exam.marksScored}}</td>
                        <td>{{exam.markDistributionId.markValue}}</td>
                      </template>
                      <td>{{subject.totalMarks}}</td>
                    </tr>
                  </tbody>
                </table>
              </v-card>
            </v-flex>
          </v-layout>
        </v-container>
      </v-flex>
    </v-container>
    <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";
import _ from "underscore";

export default {
  data: () => ({
    showLoader: false,
    markData: [],
    token: "",
    markDistributions: [],
    markParticularDistributionData: [],
    loading: false,
    valid: true,
    loading: false,
    showReport: false,
    addSection: [],
    filterData: [],
    getStudentsList: [],
    classRules: [v => !!v || "Class is required"],
    sectionRules: [v => !!v || "Class is required"],
    studentRules: [v => !!v || "Student is required"],
    getReport: {},
    classList: []
  }),
  mounted() {
    this.token = this.$store.state.token;
    this.getClass();
  },
  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;
        });
    },
    getMarkReportList() {
      this.showLoader = false;
      this.showReport = true;
      http()
        .get("/getParticularMark", {
          params: { studentId: this.getReport.studentId },
          headers: { Authorization: "Bearer " + this.token }
        })
        .then(response => {
          let newData = response.data.data;
          for (var i = 0; i < newData.length; i++) {
            newData[i].examination = newData[i].examId._id;
          }
          this.filterData = _.groupBy(newData, ["examination"]);
          console.log("check filtered data", this.filterData);
          for (let data in this.filterData) {
            // console.log("this.filterData[data].length", this.filterData[data]);
            for (let item in this.filterData[data]) {
              // console.log("check", this.filterData[data][item].studentsMarks);
              var totalMarks = 0;
              for (
                let i = 0;
                i < this.filterData[data][item].studentsMarks.length;
                i++
              ) {
                // console.log(
                //   "check data",
                //   this.filterData[data][item].studentsMarks[i]
                // );
                totalMarks += this.filterData[data][item].studentsMarks[i]
                  .marksScored;
              }
              this.filterData[data][item].totalMarks = totalMarks;
            }
          }
          // var aray = [];
          // this.markData = response.data.data;
          // for (let i = 0; i < this.markData.length; i++) {
          //   console.log(this.markData[i].examId.examName);
          //   let index = -1;

          //   aray.forEach((dat_, k) => {
          //     if (dat_.category == this.markData[i].examId.examName) {
          //       index = k;
          //     }
          //   });
          //   if (index == -1) {
          //     var obj = {
          //       category: this.markData[i].examId.examName,
          //       extraData: []
          //     };
          //     obj.extraData.push(this.markData[i]);
          //     aray.push(obj);
          //   } else {
          //     console.log("=o", this.markData[i]);
          //     //  aray[index].extraData =

          //     aray[index].extraData.push(this.markData[i]);
          //   }
          //   console.log("aray-", aray);

          // for (let j = 0; j < this.markData[i].studentsMarks.length; j++) {
          //   //  console.log("this.studentsMarksSSSSSSSSSSSSS",this.markData[i].studentsMarks[j].marksObtained)

          //   this.markDistributions = this.markData[i].studentsMarks[
          //     j
          //   ].marksObtained;
          //   for (
          //     let k = 0;
          //     k < this.markData[i].studentsMarks[j].marksObtained.length;
          //     k++
          //   ) {
          //     this.markParticularDistributionData.push(
          //       this.markData[i].studentsMarks[j].marksObtained[k].marksScored
          //     );
          //     this.markParticularDistributionData.push(
          //       this.markData[i].studentsMarks[j].marksObtained[k]
          //         .markDistributionId.markValue
          //     );
          //     // console.log(
          //     //   "this.markParticularDistributionData",
          //     //   this.markParticularDistributionData
          //     // );
          //   }
          // }
          // }
          // this.markData = aray;
          // console.log("aray-2", aray);
          // 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);
          }
        });
    }
  }
};
</script>

<style scoped>
table {
  border-collapse: collapse;
  border: 1px solid #e2e7eb;
}

th,
td {
  border: 1px solid #e2e7eb;
  padding: 10px;
  text-align: center;
}
table.feeTypeTable {
  table-layout: auto !important;
  width: 100% !important;
}
.bg-sky {
  background-color: #98b2cc !important;
}
.bg-sky-light {
  background-color: #89a0b8;
}
.bg-purple {
  background-color: #9583ac;
}
.bg-skyDark {
  background-color: #956785;
}
@media screen and (max-width: 380px) {
  .tableRsponsive {
    display: block;
    position: relative;
    overflow: scroll;
  }
}
</style> s