<template>
  <v-container fluid class="body-color">
    <!-- ****** 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.selectClassId"
                      item-text="classNum"
                      item-value="_id"
                      name="Select Class"
                      :rules="classRules"
                      @change="getSection()"
                      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.selectId"
                      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="getInvoicesData()"
                  round
                  dark
                  :loading="loading"
                  class="right mt-4 open-dialog-button"
                >Search</v-btn>
              </v-flex>
            </v-flex>
          </v-layout>
        </v-flex>
      </v-container>
    </v-form>
    <v-container fluid grid-list-md>
      <v-flex xs12>
        <v-layout wrap>
          <v-flex xs12 sm12 md4>
            <v-card
              flat
              class="pa-3"
              v-for="(invoiceData,i) in studentInvoiceData"
              :key="i"
              v-if="i === 0"
            >
              <v-layout>
                <v-flex xs12>
                  <v-avatar
                    size="80px"
                    style="margin: auto;display:block;margin-bottom:10px !important"
                  >
                    <img src="/static/icon/user.png" v-if="!invoiceData.studentId.profilePicUrl" />
                    <img
                      :src="invoiceData.studentId.profilePicUrl"
                      v-else-if="invoiceData.studentId.profilePicUrl"
                    />
                  </v-avatar>
                </v-flex>
              </v-layout>
              <v-layout class="studentProfile">
                <v-flex xs4 sm3 md3>
                  <h4>
                    <b>Name</b>
                  </h4>
                </v-flex>
                <v-flex sm9 xs8 md9>
                  <h4>: {{ invoiceData.studentId.name }}</h4>
                </v-flex>
              </v-layout>
              <v-layout class="studentProfile">
                <v-flex xs4 sm3 md3>
                  <h4>
                    <b>Class</b>
                  </h4>
                </v-flex>
                <v-flex sm9 xs8 md9>
                  <h4>: {{ invoiceData.classId.classNum }}</h4>
                </v-flex>
              </v-layout>
              <v-layout class="studentProfile">
                <v-flex xs4 sm3 md3>
                  <h4>
                    <b>Roll No</b>
                  </h4>
                </v-flex>
                <v-flex sm9 xs8 md9>
                  <h4>: {{ invoiceData.studentId.rollNo }}</h4>
                </v-flex>
              </v-layout>
            </v-card>
          </v-flex>
          <v-flex xs12 sm12 md8 v-show="showInvoiceTable">
            <v-card>
              <v-data-table
                :headers="headers"
                :items="studentInvoiceData"
                :search="search"
                hide-actions
              >
                <template slot="items" slot-scope="props">
                  <td id="td" class="text-xs-center">{{ props.item.invoiceNumber }}</td>
                  <td id="td" class="text-xs-center">{{ props.item.totalAmount }}</td>
                  <td id="td" class="text-xs-center">{{ props.item.totalDiscount }}</td>
                  <td id="td" class="text-xs-center">{{ props.item.totalSubTotal }}</td>
                  <td id="td" class="text-xs-center">{{ props.item.paymentStatus}}</td>
                  <td id="td" class="text-xs-center">{{ dates(props.item.date) }}</td>
                  <td class="text-xs-center">
                    <router-link
                      :to="{ name:'ViewInvoice',params: { viewInvoiceId:props.item._id } }"
                    >
                      <v-tooltip top>
                        <img
                          slot="activator"
                          style="cursor:pointer; width:25px; height:18px; "
                          @click="profile(props.item)"
                          src="/static/icon/eye1.png"
                        />
                        <span>View</span>
                      </v-tooltip>
                    </router-link>
                  </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>
            </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-container>
</template>

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

export default {
  data: () => ({
    snackbar: false,
    showInvoiceTable: 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"],
    headers: [
      {
        text: "Invoice Number",
        align: "center",
        sortable: false,
        value: "invoiceNumber"
      },
      {
        text: "Total Pay",
        value: "totalAmount",
        sortable: false,
        align: "center"
      },
      {
        text: "Weaver",
        value: "totalDiscount",
        sortable: false,
        align: "center"
      },
      {
        text: "	Total Collection",
        value: "totalSubTotal",
        sortable: false,
        align: "center"
      },
      {
        text: "Clearance",
        value: "paymentStatus",
        sortable: false,
        align: "center"
      },
      { text: "Payment Date", value: "date", sortable: false, align: "center" },
      { text: "Action", value: "", sortable: false, align: "center" }
    ],
    studentData: [],
    studentInvoiceData: []
  }),
  methods: {
    dates: function(date) {
      return moment(date).format("MMMM DD, YYYY");
    },
    getSection() {
      var token = this.$store.state.token;
      this.showLoader = true;
      http()
        .get(
          "/getSectionsList",
          {
            params: {
              classId: this.selectStudents.selectClassId,
              schoolId: this.$store.state.schoolId
            }
          },
          {
            headers: { Authorization: "Bearer " + token }
          }
        )
        .then(response => {
          this.addSection = response.data.data;
          this.showLoader = false;
          // console.log("getSectionsList=====>", this.addSection);
        })
        .catch(error => {
          this.showLoader = false;
          // console.log("err====>", err);
          // this.$router.replace({ path: '/' });
        });
    },
    getStudents() {
      this.showLoader = true;
      http()
        .get("/getStudentWithClass", {
          params: {
            classId: this.selectStudents.selectClassId,
            sectionId: this.selectStudents.selectSection,
            schoolId: this.$store.state.schoolId
          }
        })
        .then(response => {
          this.studentData = response.data.data;
          this.showLoader = false;
        })
        .catch(err => {
          console.log("err====>", err);
          this.showLoader = false;
        });
    },
    getInvoicesData() {
      this.showLoader = true;
      this.showInvoiceTable = true;
      http()
        .get("/getInvoicesList", {
          params: {
            classId: this.selectStudents.selectClassId,
            studentId: this.selectStudents.selectId,
            schoolId: this.$store.state.schoolId
          }
        })
        .then(response => {
          this.studentInvoiceData = response.data.data;
          // console.log("this.studentInvoiceData", this.studentInvoiceData);
          this.showLoader = false;
        })
        .catch(err => {
          console.log("err====>", err);
          this.showLoader = false;
        });
    }
  },
  mounted() {
    var token = this.$store.state.token;
    http()
      .get("/getClassesList", {
        params: {
          schoolId: this.$store.state.schoolId
        },
        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>


<style  scoped>
.studentProfile {
  border: 1px solid lightgrey;
}
</style>