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

      <!-- ****** EDIT  SECTION  ****** -->
      <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="600px">
          <v-toolbar color="grey lighten-2">
            <v-spacer></v-spacer>
            <v-toolbar-title>
              <h3>Edit Fee Type</h3>
            </v-toolbar-title>
            <v-spacer></v-spacer>
          </v-toolbar>
          <v-card>
            <v-container fluid>
              <v-layout wrap justify-center>
                <v-flex xs12 sm9>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right pr-3">Fee Type:</label>
                    </v-flex>
                    <v-flex xs8>
                      <v-text-field
                        v-model="editedItem.feeType"
                        placeholder="fill your Fee Type"
                        :rules="feetTypeRules"
                        required
                      ></v-text-field>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right pr-3">Note:</label>
                    </v-flex>
                    <v-flex xs8>
                      <v-textarea
                        name="input-7-1"
                        v-model="editedItem.note"
                        placeholder="fill your Note"
                        :rules="noteRules"
                        multi-line
                        required
                      ></v-textarea>
                    </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-flex>
              </v-layout>
            </v-container>
          </v-card>
        </v-dialog>

        <!-- ****** PROFILE VIEW SECTION DATA ******  -->

        <v-dialog v-model="dialog1" max-width="600px">
          <v-toolbar color="grey lighten-2">
            <v-spacer></v-spacer>
            <v-toolbar-title>
              <h3>Fee Type</h3>
            </v-toolbar-title>
            <v-spacer></v-spacer>
            <v-icon @click="close1">close</v-icon>
          </v-toolbar>
          <v-card>
            <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>Fee Type:</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.feeType }}</h5>
                      </v-flex>
                    </v-layout>
                    <v-layout>
                      <v-flex xs5 sm6>
                        <h5 class="right my-1">
                          <b>Note:</b>
                        </h5>
                      </v-flex>
                      <v-flex sm6 xs8>
                        <h5 class="my-1">{{ editedItem.note }}</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-FEETYPE TABLE ****** -->

        <v-data-table
          :headers="headers"
          :items="feeTypeList"
          :pagination.sync="pagination"
          :search="search"
        >
          <template slot="items" slot-scope="props">
            <td id="td" class="text-xs-center">{{ props.index + 1}}</td>
            <td id="td" class="text-xs-center">{{ props.item.feeType }}</td>
            <td id="td" class="text-xs-center">{{ props.item.note }}</td>
            <!-- <td id="td" class="text-xs-center">{{ props.item.session}}</td> -->

            <td class="text-xs-center">
              <!-- <span> -->
              <v-tooltip top>
                <img
                  slot="activator"
                  style="cursor:pointer; width:25px; height:18px; "
                  class="mr-5"
                  @click="profile(props.item)"
                  src="/static/icon/eye1.png"
                />
                <span>View</span>
              </v-tooltip>
              <v-tooltip top>
                <img
                  slot="activator"
                  style="cursor:pointer; width:20px; height:18px; "
                  class="mr-5"
                  @click="editItem(props.item)"
                  src="/static/icon/edit1.png"
                />
                <span>Edit</span>
              </v-tooltip>
              <v-tooltip top>
                <img
                  slot="activator"
                  style="cursor:pointer; width:20px; height:20px; "
                  class="mr-5"
                  @click="deleteItem(props.item)"
                  src="/static/icon/delete1.png"
                />
                <span>Delete</span>
              </v-tooltip>
              <!-- </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 Fee Type ****** -->

      <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">Fee Type:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-text-field
                            v-model="feeTypeData.feeType"
                            placeholder="fill your Fee Type"
                            type="text"
                            :rules="feetTypeRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Note:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-textarea
                            name="input-7-1"
                            v-model="feeTypeData.note"
                            placeholder="fill your Note"
                            type="text"
                            :rules="noteRules"
                            multi-line
                            required
                          ></v-textarea>
                        </v-flex>
                      </v-layout>
                      <!-- <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Monthly:</label>
                        </v-flex>
                        <v-flex xs6 class="ml-3">
                          <v-checkbox
                            v-model="feeTypeData.monthly"
                            :rules="monthlyRules"
                            multi-line
                            required
                          ></v-checkbox>
                        </v-flex>
                      </v-layout>-->
                      <v-layout>
                        <v-flex xs12 sm9 offset-sm2>
                          <v-card-actions>
                            <v-btn @click="clear" round dark>clear</v-btn>
                            <v-spacer></v-spacer>
                            <v-btn @click="submit" round dark :loading="loading">Add</v-btn>
                          </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>
  </v-app>
</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,
    validEdit: true,
    isActive: true,
    newActive: false,
    AddUsercredentials: {},
    pagination: {
      rowsPerPage: 15
    },
    feetTypeRules: [v => !!v || " Fee Type Name is required"],
    monthlyRules: [v => !!v || " Select Monthly"],
    noteRules: [v => !!v || " Note is required"],
    headers: [
      {
        text: "No",
        align: "center",
        sortable: false,
        value: "No"
      },
      {
        text: "fee Type",
        value: "feeType",
        sortable: false,
        align: "center"
      },
      { text: "Note", value: "note", sortable: false, align: "center" },
      // { text: "Session", value: "session", sortable: false, align: "center" },
      { text: "Action", value: "", sortable: false, align: "center" }
    ],
    feeTypeList: [],
    select: "",
    token: "",
    editedItem: {},
    feeTypeData: {}
  }),
  methods: {
    getFeeTypeList() {
      this.showLoader = true;
      http()
        .get("/getFeesList", {
          params: { schoolId: this.$store.state.schoolId },
          headers: { Authorization: "Bearer " + this.token }
        })
        .then(response => {
          this.feeTypeList = response.data.data;
          this.showLoader = false;
          // console.log("getAllfeetypes=====>",response.data.data)
        })
        .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);
          }
        });
    },
    editItem(item) {
      this.editedIndex = this.feeTypeList.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },
    profile(item) {
      this.editedIndex = this.feeTypeList.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog1 = true;
    },
    deleteItem(item) {
      let deleteFeeType = {
        feeId: item._id
      };
      http()
        .delete(
          "/deleteFee",
          confirm("Are you sure you want to delete this?") && {
            params: deleteFeeType
          }
        )
        .then(response => {
          if ((this.snackbar = true)) {
            this.text = "Successfully delete Existing feetype";
          }
          this.getFeeTypeList();
        })
        .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;
    },
    close2() {
      this.dialog2 = false;
    },
    submit() {
      if (this.$refs.form.validate()) {
        this.loading = true;
        this.schoolId = this.$store.state.schoolId;
        http()
          .post("/createFee", this.feeTypeData)
          .then(response => {
            this.getFeeTypeList();
            this.snackbar = true;
            this.text = "New feetype added successfully";
            this.clear();
            this.loading = false;
          })
          .catch(error => {
            // console.log(error);
            this.snackbar = true;
            this.text = error.response.data.message;
            this.loading = false;
          });
      }
    },
    clear() {
      this.$refs.form.reset();
    },
    save() {
      this.editedItem.feeId = this.editedItem._id;
      http()
        .put("/updateFee", this.editedItem)
        .then(response => {
          if ((this.snackbar = true)) {
            this.text = "Successfully Edit Existing Fee Type";
          }
          this.getFeeTypeList();
          this.close();
        })
        .catch(error => {
          this.text = error.response.data.message;
          // console.log(error);
        });
    }
  },
  mounted() {
    this.token = this.$store.state.token;
    this.getFeeTypeList();
  },
  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>
.active {
  background-color: gray;
  color: white !important;
}
.activebtn {
  color: black !important;
}
</style>