<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-show="showReport">
      <v-layout>
        <v-flex xs12 sm12>
          <v-btn class="grey" dark @click="print()">
            Print
            <v-icon right dark>print</v-icon>
          </v-btn>
        </v-flex>
      </v-layout>
      <v-flex xs12 sm12 id="printMe">
        <v-layout wrap>
          <v-flex xs12 sm12 md3>
            <v-card class="fixcolors white--text" flat>
              <v-card-actions class="pa-0">
                <v-spacer></v-spacer>
                <v-card-title class="pa-2">
                  <h3>Profile</h3>
                </v-card-title>
                <v-spacer></v-spacer>
              </v-card-actions>
            </v-card>
            <v-card flat>
              <v-layout
                wrap
                v-for="(value, id, index) in filterData"
                v-if="index == 0"
                :key="index"
              >
                <v-flex xs12 style="margin:16px;">
                  <v-layout>
                    <v-flex xs12>
                      <v-avatar
                        size="100px"
                        class="mx-auto"
                        style="margin: auto;display:block;margin-bottom:10px !important"
                      >
                        <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 style="text-align:center;">
                      <h3>
                        <b>{{ value[0].studentId.name }}</b>
                      </h3>
                      <p class="grey--text">Student</p>
                    </v-flex>
                  </v-layout>
                  <v-layout
                    style="border: 1px solid lightgrey;padding: 4px;border-top-right-radius: 4px;
                     border-top-left-radius: 4px;"
                  >
                    <v-flex xs6 sm3 class="py-1">
                      <h4>
                        <b>RollNo</b>
                      </h4>
                    </v-flex>
                    <v-flex sm9 xs6 class="py-1">
                      <h4>: {{ value[0].studentId.rollNo }}</h4>
                    </v-flex>
                  </v-layout>
                  <v-layout style="border: 1px solid lightgrey;padding: 4px;">
                    <v-flex xs6 sm3 class="py-1">
                      <h4>
                        <b>Class</b>
                      </h4>
                    </v-flex>
                    <v-flex sm9 xs6 class="py-1">
                      <h4>: {{value[0].classId.classNum }}</h4>
                    </v-flex>
                  </v-layout>
                  <v-layout
                    style="border: 1px solid lightgrey;padding: 4px;border-bottom-right-radius: 4px;
                     border-bottom-left-radius: 4px;"
                  >
                    <v-flex xs6 sm3 class>
                      <h4>
                        <b>Section</b>
                      </h4>
                    </v-flex>
                    <v-flex sm9 xs6 class>
                      <h4>: {{ value[0].sectionId.name}}</h4>
                    </v-flex>
                  </v-layout>
                </v-flex>
              </v-layout>
            </v-card>
          </v-flex>

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

          <v-flex xs12 sm12 md9>
            <v-card class="fixcolors white--text" flat>
              <v-card-actions class="pa-0">
                <v-spacer></v-spacer>
                <v-card-title class="pa-2">
                  <h3>Mark</h3>
                </v-card-title>
                <v-spacer></v-spacer>
              </v-card-actions>
            </v-card>
            <v-card
              v-for="(value, id, index) in filterData"
              :key="index"
              flat
              style="border: 1px solid lightgrey;margin-bottom:14px;"
            >
              <v-card-title class="subheading">{{value[0].examId.examName}}</v-card-title>
              <table
                class="mb-5 tableRsponsive feeTypeTable"
                style="border: 1px solid lightgrey;
                  border-collapse: collapse;!important
                  table-layout: auto !important;
                  width: 100% !important;"
              >
                <thead style="border: 1px solid lightgrey !important;">
                  <tr
                    class="info white--text"
                    style="border: 1px solid lightgrey !important;padding:4px;"
                  >
                    <td
                      rowspan="2"
                      style="border: 1px solid lightgrey !important;padding: 10px;"
                    >Subject</td>
                    <td
                      colspan="2"
                      v-for="exam in value[index].studentsMarks"
                      style="border: 1px solid lightgrey !important;padding: 10px;"
                    >{{exam.markDistributionId.distributionType}}</td>
                    <td style="border: 1px solid lightgrey !important;padding: 10px;">Total</td>
                  </tr>
                  <tr
                    v-for="(subject, ind) in value"
                    v-if="ind == 0"
                    style="border: 1px solid lightgrey !important;"
                  >
                    <template v-for="(exam, i) in subject.studentsMarks">
                      <td
                        class="bg-sky white--text"
                        style="border: 1px solid lightgrey !important;padding: 10px;"
                      >Mark</td>
                      <td
                        class="bg-sky-light white--text"
                        style="border: 1px solid lightgrey !important;padding: 10px;"
                      >Highest Mark</td>
                    </template>
                    <td
                      class="bg-sky white--text"
                      style="border: 1px solid lightgrey !important;padding: 10px;"
                    >Mark</td>
                  </tr>
                </thead>
                <tbody style="border: 1px solid lightgrey !important;">
                  <tr v-for="subject in value" style="border: 1px solid lightgrey !important;">
                    <td
                      style="border: 1px solid lightgrey !important;padding: 10px;"
                    >{{subject.subjectName}}</td>
                    <template v-for="(exam, i) in subject.studentsMarks">
                      <td
                        style="border: 1px solid lightgrey !important;padding: 10px;"
                      >{{exam.marksScored}}</td>
                      <td
                        style="border: 1px solid lightgrey !important;padding: 10px;"
                      >{{exam.markDistributionId.markValue}}</td>
                    </template>
                    <td
                      style="border: 1px solid lightgrey !important;padding: 10px;"
                    >{{subject.totalMarks}}</td>
                  </tr>
                </tbody>
              </table>
            </v-card>
          </v-flex>
        </v-layout>
      </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: [],
    output: null
  }),
  mounted() {
    this.token = this.$store.state.token;
    this.getClass();
  },
  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);
        });
    },
    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 = true;
      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"]);
          for (let data in this.filterData) {
            for (let item in this.filterData[data]) {
              var totalMarks = 0;
              for (
                let i = 0;
                i < this.filterData[data][item].studentsMarks.length;
                i++
              ) {
                totalMarks += this.filterData[data][item].studentsMarks[i]
                  .marksScored;
              }
              this.filterData[data][item].totalMarks = totalMarks;
            }
          }
          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);
          }
        });
    },
    print() {
      // Pass the element id here
      this.$htmlToPaper("printMe");
    }
  }
};
</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