<template>
  <v-app id="pages-dasboard" class="dashbaordBackground">
    <!-- ****** VIEW PROFIL NOTICE BOARD ******  -->
    <v-dialog v-model="dialogNotice" max-width="940px" scrollable>
      <v-card>
        <v-toolbar color="grey lighten-2" flat>
          <v-spacer></v-spacer>
          <v-toolbar-title>
            <h3>Notice Board</h3>
          </v-toolbar-title>
          <v-spacer></v-spacer>
          <v-icon @click="closeNotice">close</v-icon>
        </v-toolbar>
        <v-card-text>
          <v-layout>
            <v-flex align-center justify-center layout text-xs-center class="mt-2">
              <img src="/static/icon/user.png" width="70px" v-if="!notice.eventImageUrl" />
              <img :src="notice.eventImageUrl" width="280px" v-else-if="notice.eventImageUrl" />
            </v-flex>
          </v-layout>
          <v-container grid-list-md>
            <v-layout wrap>
              <v-flex>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1">
                      <b>Title:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ notice.title }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1">
                      <b>Description:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ notice.description }}</h5>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
          </v-container>
        </v-card-text>
      </v-card>
    </v-dialog>
    <v-container fluid grid-list-xl>
      <v-layout row wrap class="pt-3">
        <!-- ***** Total Students ***** -->
        <v-flex xs12 class="pt-2">
          <v-layout row wrap>
            <v-flex xs12 sm12 lg3 md3>
              <router-link :to="{ name:'Students' }">
                <v-card width="90" height="90" class="ml-3 pa-1 white iconCard" dark>
                  <img
                    src="/static/schoolIcons/Students.png"
                    class="iconOfDashboard"
                    width="80"
                    alt="Students"
                  />
                </v-card>
                <v-card class="pa-3" style="margin-top: -44px;">
                  <div class="body-2 grey--text text-xs-right">Students</div>
                  <br />
                  <h3 class="headline text-xs-right pb-4">{{ students.length }}</h3>
                </v-card>
              </router-link>
            </v-flex>
            <!-- ***** Total Parents ***** -->
            <v-flex xs12 sm12 lg3 md3>
              <router-link :to="{ name:'parents' }">
                <v-card width="90" height="90" class="ml-3 pa-1 white agentIcon" dark>
                  <img
                    src="/static/schoolIcons/Parents.png"
                    width="80"
                    class="iconOfDashboard"
                    alt="Parents"
                  />
                </v-card>
                <v-card class="pa-3" style="margin-top: -44px;">
                  <div class="body-2 grey--text text-xs-right">Parents</div>
                  <br />
                  <h3 class="headline text-xs-right pb-4">{{ parents.length }}</h3>
                </v-card>
              </router-link>
            </v-flex>
            <!-- ***** Total Teachers***** -->
            <v-flex xs12 sm12 lg3 md3>
              <router-link :to="{ name:'teachers' }">
                <v-card width="90" height="90" class="ml-3 pa-1 white farmerIcon" dark>
                  <img
                    src="/static/schoolIcons/Teachers.png"
                    width="80"
                    class="iconOfDashboard"
                    alt="Teachers"
                  />
                </v-card>
                <v-card class="pa-3" style="margin-top: -44px;">
                  <div class="body-2 grey--text text-xs-right">Teachers</div>
                  <br />
                  <h3 class="headline text-xs-right pb-4">{{ teachers.length }}</h3>
                </v-card>
              </router-link>
            </v-flex>
            <!-- ***** Total Class***** -->
            <v-flex xs12 sm12 lg3 md3>
              <router-link :to="{ name:'addclass' }">
                <v-card width="90" height="90" class="ml-3 pa-1 white khasraIcon" dark>
                  <img
                    src="/static/schoolIcons/Class.png"
                    width="80"
                    class="iconOfDashboard"
                    alt="class"
                  />
                </v-card>
                <v-card class="pa-3" style="margin-top: -44px;">
                  <div class="body-2 grey--text text-xs-right">Class</div>
                  <br />
                  <h3 class="headline text-xs-right pb-4">{{ classes.length}}</h3>
                </v-card>
              </router-link>
            </v-flex>
          </v-layout>
        </v-flex>
      </v-layout>
      <v-layout wrap>
        <v-flex xs12 sm12 md4>
          <v-card >
            <v-flex xs12 class="text-xs-center">
              <v-avatar size="60px" class="mx-auto" style="margin: auto;display:block;">
                <img src="/static/icon/user.png" v-if="!userData.schoolLogoUrl" />
                <img :src="userData.schoolLogoUrl" v-else-if="userData.schoolLogoUrl" />
              </v-avatar>
              <h5 class="pt-1">
                <i>{{ userData.name }}</i>
              </h5>
              <!-- <p class="grey--text">Student</p> -->
            </v-flex>
            <table class="feeTypeTable tableRsponsive" height="192px">
              <tr>
                <td>
                  <v-icon>person</v-icon>
                </td>
                <td class="fixedSize">Username</td>
                <td>{{ userData.name }}</td>
              </tr>
              <tr>
                <td>
                  <v-icon>mail</v-icon>
                </td>
                <td class="fixedSize">Email</td>
                <td>{{ userData.email }}</td>
              </tr>
              <tr>
                <td>
                  <v-icon>call</v-icon>
                </td>
                <td class="fixedSize">Phone</td>
                <td>{{ userData.mobile }}</td>
              </tr>
              <tr>
                <td>
                  <v-icon>home</v-icon>
                </td>
                <td class="fixedSize">Address</td>
                <td>{{ userData.address }}</td>
              </tr>
            </table>
          </v-card>
        </v-flex>
        <v-flex xs12 sm12 md8>
          <v-card>
            <v-card-title class="subheading">Notice</v-card-title>
            <v-data-table :items="noticeData" class="elevation-1" hide-actions hide-headers>
              <template slot="items" slot-scope="props" v-if="props.index < 5">
                <td>{{ props.index + 1}}</td>
                <td>{{ props.item.title}}</td>
                <td class="noticeDecritpion">{{ props.item.description}}</td>

                <td class="text-xs-center">
                  <span>
                    <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>
                  </span>
                </td>
              </template>
            </v-data-table>
          </v-card>
        </v-flex>
      </v-layout>
      <v-card class="pt-4 mt-3" flat>
        <full-calendar
          ref="calendar"
          defaultView="month"
          droppable="false"
          :events="events"
          :config="config"
        ></full-calendar>
      </v-card>
    </v-container>
    <v-dialog v-model="dialog" max-width="500">
      <v-card color="grey lighten-4" flat>
        <v-toolbar dark color="fixcolors">
          <v-spacer></v-spacer>
          <v-btn icon @click="dialog= false">
            <v-icon>close</v-icon>
          </v-btn>
        </v-toolbar>
        <v-flex class="py-4">
          <v-list-tile>
            <v-list-tile-action>
              <v-icon>edit</v-icon>
            </v-list-tile-action>
            <v-list-tile-content>
              <v-list-tile-title>{{ selected.title }}</v-list-tile-title>
            </v-list-tile-content>
          </v-list-tile>
          <v-list-tile>
            <v-list-tile-action>
              <v-icon>access_time</v-icon>
            </v-list-tile-action>
            <v-list-tile-content>
              <v-list-tile-title>{{ date(selected.start)}}</v-list-tile-title>
            </v-list-tile-content>
          </v-list-tile>
        </v-flex>
      </v-card>
    </v-dialog>
    <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() {
    return {
      showLoader: false,
      dialog: false,
      dialogNotice: false,
      HolidaysList: [],
      EventsList: [],
      events: [],
      config: {
        eventClick: event => {
          this.selected = event;
          this.dialog = true;
        }
      },
      selected: {},
      notice: {},
      userData: {},
      students: "",
      parents: "",
      teachers: "",
      classes: "",
      noticeData: []
    };
  },
  mounted() {
    this.token = this.$store.state.token;
    this.getData();
    this.getStudents();
    this.getTeachers();
    this.getParents();
    this.getClasses();
    this.getNoticeData();
    this.getUserData();
  },
  methods: {
    closeNotice() {
      this.dialogNotice = false;
    },
    profile(item) {
      // this.editedIndex = this.desserts.indexOf(item);
      this.notice = Object.assign({}, item);
      this.dialogNotice = true;
    },
    date: function(date) {
      return moment(date).format("MMMM DD, YYYY  HH:mm:ss");
    },
    refreshEvents() {
      this.$refs.calendar.$emit("refetch-events");
    },
    removeEvent() {
      this.$refs.calendar.$emit("remove-event", this.selected);
      this.selected = {};
    },
    eventSelected(event) {
      this.selected = event;
      console.log("this.selected", this.selected);
    },
    // eventDropStart: function(event) {
    //   event.editable = false;
    // },
    eventCreated(...test) {
      console.log(test);
    },
    getData() {
      this.showLoader = true;
      http()
        .get("/getHolidaysList", {})
        .then(response => {
          this.HolidaysList = response.data.data;
          // this.events = response.data.data;
          this.showLoader = false;
        })
        .catch(err => {
          // console.log("err====>", err);
          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);
          }
        });
      http()
        .get("/getSchoolEventsList")
        .then(response => {
          this.EventsList = response.data.data;
          let allData = [];
          allData = this.HolidaysList.concat(this.EventsList);
          this.events = allData;
          this.showLoader = false;
        })
        .catch(error => {
          // console.log("err====>", err);
          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);
          }
        });
    },
    getStudents() {
      http()
        .get("/getStudentsList")
        .then(response => {
          this.students = response.data.data;
          this.showLoader = false;
        })
        .catch(error => {
          // console.log("err====>", err);
          this.showLoader = false;
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
          }
        });
    },
    getParents() {
      http()
        .get("/getParentsList")
        .then(response => {
          this.parents = response.data.data;
          this.showLoader = false;
        })
        .catch(error => {
          // console.log("err====>", err);
          this.showLoader = false;
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
          }
        });
    },
    getTeachers() {
      http()
        .get("/getTeachersList")
        .then(response => {
          this.teachers = response.data.data;
          this.showLoader = false;
        })
        .catch(error => {
          // console.log("err====>", err);
          this.showLoader = false;
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
          }
        });
    },
    getClasses() {
      http()
        .get("/getClassesList")
        .then(response => {
          this.classes = response.data.data;
          this.showLoader = false;
        })
        .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);
          }
        });
    },
    getNoticeData() {
      this.showLoader = true;
      http()
        .get("/getEventsList")
        .then(response => {
          this.noticeData = response.data.data;
          this.showLoader = false;
        })
        .catch(error => {
          this.showLoader = false;
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
          }
        });
    },
    getUserData() {
      this.showLoader = true;
      http()
        .get("/getParticularUserDetail")
        .then(response => {
          this.userData = response.data.data;
          this.showLoader = false;
        })
        .catch(error => {
          this.showLoader = false;
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
          }
        });
    }
    // eventSources() {
    //   const self = this;
    //   return [
    //     {
    //       events(start, end, timezone, callback) {
    //         setTimeout(() => {
    //           callback(self.events.filter(() => Math.random() > 0.5));
    //         }, 1000);
    //       }
    //     }
    //   ];
    // }
  }
};
</script>

<style>
@import "fullcalendar/dist/fullcalendar.css";

a {
  text-decoration: none !important;
}
.fc button {
  background: grey !important;
  border: none;
  border-radius: 4px;
  color: white;
  padding: 6px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 18px;
  margin: 8px 2px !important;
  cursor: pointer;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
  box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.iconCard {
  position: relative !important;
  border-radius: 4px;
  z-index: 1;
}
.agentIcon {
  position: relative !important;
  border-radius: 4px;
  z-index: 1;
}
.farmerIcon {
  position: relative !important;
  border-radius: 4px;
  z-index: 1;
}
.khasraIcon {
  position: relative !important;
  border-radius: 4px;
  z-index: 1;
}
.maleFarmerIcon {
  position: relative !important;
  border-radius: 4px;
  z-index: 1;
}

.dashbaordBackground {
  background: #f1f2f7 !important;
}
table {
  border-collapse: collapse;
  border: 1px solid #e2e7eb;
}
tr {
  border: 1px solid #e2e7eb;
  /* text-align: center; */
}
td {
  padding: 6px;
}
.fixedSize {
  padding: 6px 20px !important;
}
table.feeTypeTable {
  table-layout: auto !important;
  width: 100% !important;
}
@media screen and (max-width: 380px) {
  .tableRsponsive {
    display: block;
    position: relative;
    overflow: scroll;
  }
}
</style>