<template>
  <v-container fluid class="body-color">
    <!-- ****** EDIT ACADEMIC YEAR ****** -->
    <v-dialog v-model="editAcademinYearDialog" max-width="600px" persistent>
      <v-card flat class="Card-style pa-2">
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Edit Academic Year</label>
            <v-icon size="24" class="right" @click="editAcademinYearDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-container fluid>
          <v-flex xs12 sm12>
            <v-layout>
              <v-flex xs4 class="pt-4 subheading">
                <label class="right">Year:</label>
              </v-flex>
              <v-flex xs8 sm7 class="ml-3">
                <v-text-field
                  v-model="editedItem.year"
                  placeholder="fill your Title"
                  name="name"
                  type="text"
                ></v-text-field>
              </v-flex>
            </v-layout>
          </v-flex>
          <v-flex xs12 sm12>
            <v-layout>
              <v-flex xs4 class="pt-4 subheading">
                <label class="right">Year Title:</label>
              </v-flex>
              <v-flex xs8 sm7 class="ml-3">
                <v-text-field
                  v-model="editedItem.yearTitle"
                  placeholder="fill your Year Title"
                  name="name"
                  type="text"
                ></v-text-field>
              </v-flex>
            </v-layout>
          </v-flex>
          <v-flex xs12 sm12>
            <v-layout>
              <v-flex xs4 class="pt-4 subheading">
                <label class="right hidden-xs-only hidden-sm-only">Starting Date:</label>
                <label class="right hidden-md-only hidden-lg-only hidden-xl-only">StartingDate:</label>
              </v-flex>
              <v-flex xs8 sm7 class="ml-3">
                <v-menu
                  ref="menu"
                  :close-on-content-click="false"
                  v-model="menu"
                  :nudge-right="40"
                  lazy
                  transition="scale-transition"
                  offset-y
                  full-width
                  min-width="290px"
                >
                  <v-text-field
                    slot="activator"
                    v-model="editedItem.startingDate"
                    placeholder="Select date"
                  ></v-text-field>
                  <v-date-picker
                    color="info"
                    ref="picker"
                    v-model="editedItem.startingDate"
                    @input="$refs.menu.save(editedItem.startingDate)"
                  ></v-date-picker>
                </v-menu>
              </v-flex>
            </v-layout>
          </v-flex>
          <v-flex xs12 sm12>
            <v-layout>
              <v-flex xs4 class="pt-4 subheading">
                <label class="right hidden-xs-only hidden-sm-only">Ending Date:</label>
                <label class="right hidden-md-only hidden-lg-only hidden-xl-only">EndingDate:</label>
              </v-flex>
              <v-flex xs8 sm7 class="ml-3">
                <v-menu
                  ref="menuEndDate"
                  :close-on-content-click="false"
                  v-model="menuEndDate"
                  :nudge-right="40"
                  lazy
                  transition="scale-transition"
                  offset-y
                  full-width
                  min-width="290px"
                >
                  <v-text-field
                    slot="activator"
                    v-model="editedItem.endingDate"
                    placeholder="Select date"
                  ></v-text-field>
                  <v-date-picker
                    color="info"
                    ref="picker"
                    v-model="editedItem.endingDate"
                    @input="$refs.menuEndDate.save(editedItem.endingDate)"
                  ></v-date-picker>
                </v-menu>
              </v-flex>
            </v-layout>
          </v-flex>
          <v-layout>
            <v-flex xs12>
              <v-card-actions class="hidden-xs-only hidden-sm-only">
                <v-spacer></v-spacer>
                <v-btn round dark @click="save" class="add-button">Save</v-btn>
                <v-spacer></v-spacer>
              </v-card-actions>
              <v-card-actions class="hidden-md-only hidden-lg-only hidden-xl-only">
                <v-spacer></v-spacer>
                <v-btn round dark @click="save" class="add-button">Save</v-btn>
                <v-spacer></v-spacer>
              </v-card-actions>
            </v-flex>
          </v-layout>
        </v-container>
      </v-card>
    </v-dialog>
    <!-- ****** PROFILE ACADEMIC YEAR DEATILS ******  -->
    <v-dialog v-model="viewAcademinYearDialog" max-width="500px" persistent>
      <v-card flat class="Card-style pa-3">
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">View Academic Year</label>
            <v-icon size="24" class="right" @click="viewAcademinYearDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-card-text>
          <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 xs7>
                    <h5 class="my-1">{{ editedItem.year }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1">
                      <b>Year Title:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs7>
                    <h5 class="my-1">{{ editedItem.yearTitle }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1 hidden-xs-only hidden-sm-only">
                      <b>Starting Date:</b>
                    </h5>
                    <h5 class="right my-1 hidden-md-only hidden-lg-only hidden-xl-only">
                      <b>StartingDate:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs7>
                    <h5 class="my-1">{{dates(editedItem.startingDate)}}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1 hidden-xs-only hidden-sm-only">
                      <b>Ending Date:</b>
                    </h5>
                    <h5 class="right my-1 hidden-md-only hidden-lg-only hidden-xl-only">
                      <b>EndingDate:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs7>
                    <h5 class="my-1">{{dates(editedItem.endingDate)}}</h5>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
          </v-container>
        </v-card-text>
      </v-card>
    </v-dialog>
    <!-- ****** EXISTING  ACADEMIC YEAR TABLE ****** -->
    <v-toolbar color="transparent" flat>
      <v-btn
        fab
        dark
        class="open-dialog-button hidden-xl-only hidden-md-only hidden-lg-only"
        small
        @click="addAcademicYearDialog = true"
      >
        <v-icon dark>add</v-icon>
      </v-btn>
      <v-btn
        round
        class="open-dialog-button hidden-sm-only hidden-xs-only"
        dark
        @click="addAcademicYearDialog = true"
      >
        <v-icon class="white--text pr-1" size="20">add</v-icon>Add Academic Year
      </v-btn>
      <v-spacer></v-spacer>
      <v-card-title class="body-1" v-show="show">
        <v-btn icon large flat @click="displaySearch">
          <v-avatar size="27">
            <img src="/static/icon/search.png" alt="icon" />
          </v-avatar>
        </v-btn>
      </v-card-title>
      <v-flex xs8 sm8 md3 lg2 v-if="showSearch">
        <v-layout>
          <v-text-field
            autofocus
            v-model="search"
            label="Search"
            prepend-inner-icon="search"
            color="primary"
          ></v-text-field>
          <v-icon @click="closeSearch" color="error">close</v-icon>
        </v-layout>
      </v-flex>
    </v-toolbar>
    <v-data-table
      :headers="headers"
      :items="getAcademicYearList"
      :pagination.sync="pagination"
      :search="search"
    >
      <template slot="items" slot-scope="props">
        <tr class="tr">
          <td class="td td-row">{{ props.index + 1}}</td>
          <td class="td td-row text-xs-center">{{ props.item.year}}</td>
          <td class="td td-row text-xs-center">{{ props.item.yearTitle}}</td>
          <td class="td td-row text-xs-center">{{ dates(props.item.startingDate)}}</td>
          <td class="td td-row text-xs-center">{{ dates(props.item.endingDate)}}</td>
          <td class="td td-row text-xs-center">
            <span>
              <v-tooltip top>
                <img
                  slot="activator"
                  style="cursor:pointer; width:25px; height:25px; "
                  class="mr-3"
                  @click="profile(props.item)"
                  src="/static/icon/view.png"
                />
                <span>View</span>
              </v-tooltip>
              <v-tooltip top>
                <img
                  slot="activator"
                  style="cursor:pointer; width:20px; height:18px; "
                  class="mr-3"
                  @click="editItem(props.item)"
                  src="/static/icon/edit.png"
                />
                <span>Edit</span>
              </v-tooltip>
              <v-tooltip top>
                <img
                  slot="activator"
                  style="cursor:pointer; width:20px; height:20px; "
                  @click="deleteItem(props.item)"
                  class="mr-3"
                  src="/static/icon/delete.png"
                />
                <span>Delete</span>
              </v-tooltip>
            </span>
          </td>
        </tr>
      </template>
      <v-alert
        slot="no-results"
        :value="true"
        color="error"
        icon="warning"
      >Your search for "{{ search }}" found no results.</v-alert>
    </v-data-table>
    <!-- ****** ADD ACADEMIC YEAR ****** -->
    <v-dialog
      v-model="addAcademicYearDialog"
      max-width="600px"
      v-if="addAcademicYearDialog"
      persistent
    >
      <v-card flat class="Card-style pa-2">
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Add Academic Year</label>
            <v-icon size="24" class="right" @click="closeAddAcademicYearModel">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-form ref="form" v-model="valid" lazy-validation>
          <v-container fluid>
            <v-flex xs12>
              <v-layout>
                <v-flex xs5 sm4 class="pt-4 subheading">
                  <label class="right">Year:</label>
                </v-flex>
                <v-flex xs7 class="ml-3">
                  <v-text-field
                    v-model="addAcademicYear.year"
                    placeholder="fill Year (2019-2020)"
                    type="text"
                    :rules="yearRules"
                    required
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12>
              <v-layout>
                <v-flex xs5 sm4 class="pt-4 subheading">
                  <label class="right">Year Title:</label>
                </v-flex>
                <v-flex xs7 class="ml-3">
                  <v-text-field
                    placeholder="fill your Year Title"
                    :rules="yearTitleRules"
                    v-model="addAcademicYear.yearTitle"
                    type="text"
                    required
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12>
              <v-layout>
                <v-flex xs5 sm4 class="pt-4 subheading">
                  <label class="right hidden-xs-only hidden-sm-only">Starting Date:</label>
                  <label class="right hidden-md-only hidden-lg-only hidden-xl-only">StartingDate:</label>
                </v-flex>
                <v-flex xs7 class="ml-3">
                  <v-menu
                    ref="menuStart"
                    :close-on-content-click="false"
                    v-model="menuStart"
                    :nudge-right="40"
                    :return-value.sync="addAcademicYear.startingDate"
                    app
                    lazy
                    transition="scale-transition"
                    offset-y
                    full-width
                    min-width="290px"
                  >
                    <v-text-field
                      slot="activator"
                      :rules="startDateRules"
                      v-model="addAcademicYear.startingDate"
                      append-icon="event"
                      placeholder="Select starting date"
                    ></v-text-field>
                    <v-date-picker
                      color="info"
                      v-model="addAcademicYear.startingDate"
                      @input="$refs.menuStart.save(addAcademicYear.startingDate)"
                    ></v-date-picker>
                  </v-menu>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12>
              <v-layout>
                <v-flex xs5 sm4 class="pt-4 subheading">
                  <label class="right">Ending Date:</label>
                </v-flex>
                <v-flex xs7 class="ml-3">
                  <v-menu
                    ref="menu1"
                    :close-on-content-click="false"
                    v-model="menu1"
                    :nudge-right="40"
                    :return-value.sync="addAcademicYear.endingDate"
                    app
                    lazy
                    transition="scale-transition"
                    offset-y
                    full-width
                    min-width="290px"
                  >
                    <v-text-field
                      slot="activator"
                      :rules="endDateRules"
                      v-model="addAcademicYear.endingDate"
                      append-icon="event"
                      placeholder="Select ending date"
                    ></v-text-field>
                    <v-date-picker
                      color="info"
                      v-model="addAcademicYear.endingDate"
                      @input="$refs.menu1.save(addAcademicYear.endingDate)"
                    ></v-date-picker>
                  </v-menu>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-layout>
              <v-flex xs12>
                <v-layout>
                  <v-spacer></v-spacer>
                  <v-btn @click="clear" round dark class="clear-button">clear</v-btn>
                  <v-btn @click="submit" round dark :loading="loading" class="add-button">Add</v-btn>
                </v-layout>
              </v-flex>
            </v-layout>
          </v-container>
        </v-form>
      </v-card>
    </v-dialog>
    <div class="loader" v-if="showLoader">
      <v-progress-circular indeterminate color="white"></v-progress-circular>
    </div>
    <v-snackbar
      :timeout="timeout"
      :top="y === 'top'"
      :right="x === 'right'"
      :vertical="mode === 'vertical'"
      v-model="snackbar"
      :color="color"
    >{{ text }}</v-snackbar>
  </v-container>
</template>

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

export default {
  data: () => ({
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 5000,
    text: "",
    color: "",
    loading: false,
    date: null,
    show: true,
    showSearch: false,
    search: "",
    showLoader: false,
    addAcademicYearDialog: false,
    editAcademinYearDialog: false,
    viewAcademinYearDialog: false,
    valid: true,
    pagination: {
      rowsPerPage: 10,
    },
    date: null,
    menu1: false,
    menu: false,
    menuStart: false,
    menuEndDate: false,
    yearRules: [(v) => !!v || "Year is required"],
    yearTitleRules: [(v) => !!v || "Year Title is required"],
    startDateRules: [(v) => !!v || "startDate is required"],
    endDateRules: [(v) => !!v || "endDate is required"],
    headers: [
      {
        text: "No",
        align: "center",
        sortable: false,
        value: "No",
      },
      { text: "Year", value: "year", sortable: false, align: "center" },
      {
        text: "Year Title",
        value: "yearTitle",
        sortable: false,
        align: "center",
      },

      {
        text: "Starting Date",
        value: "startingDate",
        sortable: false,
        align: "center",
      },
      {
        text: "Ending Date",
        value: "endingDate",
        sortable: false,
        align: "center",
      },
      { text: "Action", value: "", sortable: false, align: "center" },
    ],
    getAcademicYearList: [],
    editedIndex: -1,
    addAcademicYear: {},
    editedItem: {},
    token: "",
  }),
  watch: {
    addAcademicYearDialog: function (val) {
      if (!val) {
        this.addAcademicYear = [];
      }
    },
  },
  methods: {
    dates: function (date) {
      return moment(date).format("MMMM DD, YYYY");
    },
    getAcademicYear() {
      this.showLoader = true;
      http()
        .get("/getAcademicsList", {
          headers: { Authorization: "Bearer " + this.token },
        })
        .then((response) => {
          this.getAcademicYearList = 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);
          }
        });
    },
    editItem(item) {
      this.editedIndex = this.getAcademicYearList.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.editedItem.schoolEventId = item._id;
      this.editAcademinYearDialog = true;
    },
    profile(item) {
      this.editedIndex = this.getAcademicYearList.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.viewAcademinYearDialog = true;
    },

    deleteItem(item) {
      let deleteAcademic = {
        academicId: item._id,
      };
      http()
        .delete(
          "/deleteAcademic",
          confirm("Are you sure you want to delete this?") && {
            params: deleteAcademic,
          }
        )
        .then((response) => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.getAcademicYear();
        })
        .catch((error) => {
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    close() {
      this.editAcademinYearDialog = false;
    },
    closeAddAcademicYearModel() {
      this.addAcademicYearDialog = false;
      this.addAcademicYear = [];
    },
    submit() {
      if (this.$refs.form.validate()) {
        this.loading = true;
        http()
          .post("/createAcademic", this.addAcademicYear)
          .then((response) => {
            this.snackbar = true;
            this.text = response.data.message;
            this.color = "green";
            this.getAcademicYear();
            this.clear();
            this.loading = false;
            this.addAcademicYearDialog = false;
          })
          .catch((error) => {
            this.snackbar = true;
            this.text = error.response.data.message;
            this.color = "error";
            this.loading = false;
          });
      }
    },
    clear() {
      this.$refs.form.reset();
    },
    save() {
      this.editedItem.academicId = this.editedItem._id;
      http()
        .put("/updateAcademic", this.editedItem, {
          headers: { Authorization: "Bearer " + this.token },
        })
        .then((response) => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.getAcademicYear();
          this.close();
        })
        .catch((error) => {
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    displaySearch() {
      (this.show = false), (this.showSearch = true);
    },
    closeSearch() {
      this.showSearch = false;
      this.show = true;
      this.search = "";
    },
  },
  mounted() {
    this.token = this.$store.state.token;
    this.getAcademicYear();
  },
};
</script>