<template>
  <div>
    <v-tabs grow slider-color="black">
      <v-tab
        ripple
        @click="activeTab('existing')"
        v-bind:class="{ active: isActive }"
        id="tab"
        class="subheading"
      >Existing Class</v-tab>
      <v-tab
        ripple
        @click="activeTab('new')"
        v-bind:class="{ active: newActive }"
        id="tab1"
        User
        class="subheading"
      >Add New Class</v-tab>

      <!-- ****** EDITS  ADD ClASS  ****** -->
      <v-tab-item>
        <v-snackbar
          :timeout="timeout"
          :top="y === 'top'"
          :right="x === 'right'"
          :vertical="mode === 'vertical'"
          v-model="snackbar"
          color="success"
        >{{ text }}</v-snackbar>
        <v-dialog v-model="dialog" max-width="500px">
          <v-toolbar color="grey lighten-2">
            <v-spacer></v-spacer>
            <v-toolbar-title>
              <h3>Edit Class</h3>
            </v-toolbar-title>
            <v-spacer></v-spacer>
          </v-toolbar>
          <v-card>
            <v-card-text>
              <v-container>
                <v-layout wrap justify-center>
                  <v-flex xs12 sm9>
                    <v-form ref="form" v-model="valid" lazy-validation>
                      <v-layout style="position:relative;">
                        <v-flex xs4 lg2 class="pt-4 subheading">
                          <label>Class:</label>
                        </v-flex>
                        <v-flex xs10>
                          <v-autocomplete
                            v-model="editedItem.classNum"
                            :label="editedItem.classNum"
                            :items="classList"
                            :rules="nameRules"
                          ></v-autocomplete>
                        </v-flex>
                      </v-layout>
                      <v-card-actions>
                        <v-btn round dark @click.native="close">Cancel</v-btn>
                        <v-spacer></v-spacer>
                        <v-btn round dark @click="save">Save</v-btn>
                      </v-card-actions>
                    </v-form>
                  </v-flex>
                </v-layout>
              </v-container>
            </v-card-text>
          </v-card>
        </v-dialog>

        <!-- ****** PROFILE VIEW STUDENTS ******  -->
        <v-dialog v-model="dialog1" max-width="600px">
          <v-toolbar color="grey lighten-2">
            <v-spacer></v-spacer>
            <v-toolbar-title>
              <h3>Class Profile</h3>
            </v-toolbar-title>
            <v-spacer></v-spacer>
            <v-icon @click="close1">close</v-icon>
          </v-toolbar>
          <v-card>
            <v-flex align-center justify-center layout text-xs-center>
              <v-avatar size="50px" style="position:absolute; top:20px;">
                <img src="/static/icon/user.png" />
              </v-avatar>
            </v-flex>
            <v-card-text>
              <v-container grid-list-md>
                <v-layout wrap>
                  <v-flex>
                    <br />
                    <br />
                    <v-layout>
                      <v-flex xs6 sm6>
                        <h5 class="right my-1">Class Name:</h5>
                      </v-flex>
                      <v-flex sm6 xs6>
                        <h5 class="my-1">{{ editedItem.classNum }}</h5>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-container>
            </v-card-text>
          </v-card>
        </v-dialog>

        <v-snackbar
          :timeout="timeout"
          :top="y === 'top'"
          :right="x === 'right'"
          :vertical="mode === 'vertical'"
          v-model="snackbar"
          color="success"
        >{{ text }}</v-snackbar>

        <!-- ****** EXISTING-USERS Classess Table ****** -->
        <v-data-table
          :headers="headers"
          :items="desserts"
          :pagination.sync="pagination"
          :search="search"
        >
          <template slot="items" slot-scope="props">
            <td>{{ props.index }}</td>
            <td class="text-xs-center">{{ props.item.classNum}}</td>
            <td class="text-xs-center">
              <span>
                <img
                  style="cursor:pointer; width:25px; height:18px; "
                  class="mr-5"
                  @click="profile(props.item)"
                  src="/static/icon/eye1.png"
                />
                <img
                  style="cursor:pointer; width:20px; height:18px; "
                  class="mr-5"
                  @click="editItem(props.item)"
                  src="/static/icon/edit1.png"
                />
                <img
                  style="cursor:pointer; width:20px; height:20px; "
                  class="mr-5"
                  @click="deleteItem(props.item)"
                  src="/static/icon/delete1.png"
                />
              </span>
            </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-tab-item>

      <!-- ****** ADD multiple Classess ****** -->
      <v-tab-item>
        <v-container>
          <v-snackbar
            :timeout="timeout"
            :top="y === 'top'"
            :right="x === 'right'"
            :vertical="mode === 'vertical'"
            v-model="snackbar"
            color="success"
          >{{ text }}</v-snackbar>
          <v-flex xs12 sm8 offset-sm2 class="top">
            <v-card flat>
              <v-container fluid fill-height>
                <v-layout align-center>
                  <v-flex xs12 class="mt-4">
                    <v-form ref="form" v-model="valid" lazy-validation>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Class :</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-autocomplete
                            v-model="addclasses.classNum"
                            placeholder="fill your class Name"
                            type="text"
                            :items="classList"
                            :rules="nameRules"
                            required
                          ></v-autocomplete>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs12 sm9 offset-sm2>
                          <v-card-actions>
                            <v-spacer></v-spacer>
                            <v-btn @click="submit" round dark :loading="loading">Add</v-btn>
                            <v-spacer></v-spacer>
                          </v-card-actions>
                        </v-flex>
                      </v-layout>
                    </v-form>
                  </v-flex>
                </v-layout>
              </v-container>
            </v-card>
          </v-flex>
        </v-container>
      </v-tab-item>
    </v-tabs>
    <div class="loader" v-if="showLoader">
      <v-progress-circular indeterminate color="white"></v-progress-circular>
    </div>
  </div>
</template>

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

export default {
  data: () => ({
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    text: "",
    showLoader: false,
    loading: false,
    date: null,
    search: "",
    dialog: false,
    dialog1: false,
    valid: true,
    isActive: true,
    newActive: false,
    AddUsercredentials: {},
    pagination: {
      rowsPerPage: 15
    },
    nameRules: [v => !!v || " Class Name is required"],
    headers: [
      {
        text: "No",
        align: "left",
        sortable: false,
        value: "No"
      },
      { text: "Class No", value: "classNum", sortable: false, align: "center" },

      { text: "Action", value: "", sortable: false, align: "center" }
    ],
    desserts: [],
    classList: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
    editedIndex: -1,
    editedItem: {
      classNum: ""
    },
    addclasses: {
      classNum: ""
    }
  }),
  methods: {
    getClassList() {
      this.showLoader = true;
      var token = this.$store.state.token;
      http()
        .get("/getClassesList")
        .then(response => {
          this.desserts = 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);
          }
        });
    },
    editItem(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },
    profile(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog1 = true;
    },
    deleteItem(item) {
      let deleteStudent = {
        classId: item._id
      };
      http()
        .delete(
          "/deleteClass",
          confirm("Are you sure you want to delete this?") && {
            params: deleteStudent
          }
        )
        .then(response => {
          // console.log("deleteUers",deleteStudent)
          if ((this.snackbar = true)) {
            this.text = "Successfully delete Existing Class";
          }
          this.getClassList();
        })
        .catch(error => {
          // console.log(error);
        });
    },
    activeTab(type) {
      switch (type) {
        case "existing":
          this.newActive = false;
          this.isActive = true;
          break;

        default:
          this.newActive = true;
          this.isActive = false;
          break;
      }
    },
    close() {
      this.dialog = false;
      setTimeout(() => {
        this.editedItem = Object.assign({}, this.defaultItem);
        this.editedIndex = -1;
      }, 300);
    },
    close1() {
      this.dialog1 = false;
    },
    submit() {
      if (this.$refs.form.validate()) {
        let addClass = {
          classNum: this.addclasses.classNum
        };
        console.log(addClass);
        this.loading = true;
        http()
          .post("/createClass", addClass)
          .then(response => {
            this.getClassList();
            if ((this.snackbar = true)) {
              this.text = "New class added successfully";
            }
            this.clear();
            this.loading = false;
          })
          .catch(error => {
            // console.log(error);
            this.loading = false;
            if ((this.snackbar = true)) {
              this.text = error.response.data.message;
            }
          });
      }
    },
    clear() {
      this.$refs.form.reset();
    },
    save() {
      let editClass = {
        classId: this.editedItem._id,
        classNum: this.editedItem.classNum
      };
      http()
        .put("/updateClass", editClass)
        .then(response => {
          if ((this.snackbar = true)) {
            this.text = "Successfully Edit Existing Class";
          }
          this.getClassList();
        })
        .catch(error => {
          // console.log(error);
        });
      this.close();
    }
  },
  mounted() {
    this.getClassList();
    // console.log("this.search",this.search)
  },
  created() {
    this.$root.$on("app:search", search => {
      this.search = search;
    });
  },
  beforeDestroy() {
    // dont forget to remove the listener
    this.$root.$off("app:search");
  }
};
</script>
<style>
.active {
  background-color: black;
  color: white !important;
}
.activebtn {
  color: black !important;
}
</style>