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

      <!-- ****** EDITS  STUDENTS DETAILS ****** -->

      <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="1500px" scrollable>
          <v-card flat>
            <v-toolbar color="grey lighten-2" flat>
              <v-spacer></v-spacer>
              <v-toolbar-title>
                <h3>Edit School</h3>
              </v-toolbar-title>
              <v-spacer></v-spacer>
            </v-toolbar>
            <v-card-text style="height: 686px;">
              <v-form ref="form">
                <v-container fluid>
                  <v-layout>
                    <v-flex
                      xs12
                      class="text-xs-center text-sm-center text-md-center text-lg-center"
                    >
                      <v-avatar size="160px">
                        <img
                          src="/static/icon/user.png"
                          v-if="!editedItem.schoolLogoUrl && !imageUrl"
                        />
                        <img
                          :src="editedItem.schoolLogoUrl"
                          v-else-if="editedItem.schoolLogoUrl && !imageUrl"
                        />
                        <img
                          v-if="imageUrl"
                          :src="imageUrl"
                          height="150"
                          style="border-radius:50%; width:200px"
                        />
                      </v-avatar>
                      <input
                        type="file"
                        style="display: none"
                        ref="image"
                        accept="image/*"
                        @change="onFilePicked"
                      />
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Full Name:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Name:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field v-model="editedItem.name" placeholder="fill your full Name"></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Email ID:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">EmailID:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field placeholder="fill your email" v-model="editedItem.email"></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Society Name:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            placeholder="fill your Society Name"
                            v-model="editedItem.societyName"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">City:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field v-model="editedItem.city" placeholder="fill your City Name"></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">State:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.state"
                            placeholder="fill your City Name"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Pincode:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.pinCode"
                            placeholder="fill your pincode"
                            type="number"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Fax No:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field v-model="editedItem.faxNo" placeholder="fill your fax No"></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Country:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.country"
                            placeholder="fill your Country"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Mobile No:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Mobile:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.mobile"
                            placeholder="fill your MobileNo"
                            name="mobileNo"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">LandLine No:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >LandLineNo:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.landLineNo"
                            placeholder="fill your LandLine No"
                            type="number"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Registration No:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >Registration-No:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.registrationNo"
                            placeholder="fill your Registration No"
                            type="number"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Uplaod Image:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Uplaod:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            label="Select Image"
                            @click="pickFile"
                            v-model="imageName"
                            append-icon="attach_file"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">School Details:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >SchoolDetail:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.schoolDetails"
                            placeholder="fill your School Details"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Academic Year:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Year:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.establishmentYear"
                            placeholder="fill your Academic Year"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Web Site Url:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >WebSiteUrl:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.websiteUrl"
                            placeholder="fill your MobileNo"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm4>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Address:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="editedItem.address"
                            placeholder="Select your Address"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm12>
                      <v-layout>
                        <v-flex xs6>
                          <v-btn round dark @click.native="close">Cancel</v-btn>
                        </v-flex>
                        <v-flex xs6>
                          <v-btn @click="save" round dark :loading="loading" class="right">Save</v-btn>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                </v-container>
              </v-form>
            </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 STUDENTS TABLE ****** -->
        <v-data-table
          :headers="headers"
          :items="schoolList"
          :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">
              <v-avatar>
                <img :src="props.item.schoolLogoUrl" v-if="props.item.schoolLogoUrl" />
                <img src="/static/icon/user.png" v-else-if="!props.item.schoolLogoUrl" />
              </v-avatar>
            </td>
            <td id="td" class="text-xs-center">{{ props.item.name}}</td>
            <td id="td" class="text-xs-center">{{ props.item.email }}</td>
            <td id="td" class="text-xs-center">{{ props.item.city }}</td>
            <td id="td" class="text-xs-center">{{ props.item.state }}</td>
            <td id="td" class="text-xs-center">{{ props.item.mobile}}</td>
            <td class="text-xs-center">
              <span>
                <router-link :to="{ name:'viewSchoolDashboard',params: { schoolId:props.item._id } }">
                  <v-tooltip top>
                    <img
                      slot="activator"
                      style="cursor:pointer; width:25px; height:18px; "
                      class="mr5"
                      src="/static/icon/eye1.png"
                    />
                    <span>View</span>
                  </v-tooltip>
                </router-link>
                <v-tooltip top>
                  <img
                    slot="activator"
                    style="cursor:pointer; width:20px; height:18px; "
                    class="mr5"
                    @click="editItem(props.item)"
                    src="/static/icon/edit1.png"
                  />
                  <span>Edit</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 STUDENTS DETAILS****** -->
      <v-tab-item>
        <v-container fluid>
          <v-layout align-center justify-center fill-height>
            <v-flex xs12 sm12 md10 lg11>
              <!-- <v-container fluid> -->
              <v-snackbar
                :timeout="timeout"
                :top="y === 'top'"
                :right="x === 'right'"
                :vertical="mode === 'vertical'"
                v-model="snackbar"
                color="success"
              >{{ text }}</v-snackbar>
              <v-flex xs12 sm12>
                <v-form ref="form" v-model="valid" lazy-validation>
                  <v-layout>
                    <v-flex
                      xs12
                      class="text-xs-center text-sm-center text-md-center text-lg-center"
                    >
                      <v-avatar size="100px">
                        <img src="/static/icon/user.png" v-if="!imageUrl" />
                      </v-avatar>
                      <input
                        type="file"
                        style="display: none"
                        ref="image"
                        accept="image/*"
                        @change="onFilePicked"
                      />
                      <img
                        :src="imageData.imageUrl"
                        height="150"
                        v-if="imageUrl"
                        style="border-radius:50%; width:200px"
                      />
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 sm4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Full Name:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Name:</label>
                        </v-flex>
                        <v-flex xs8 sm8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.name"
                            placeholder="fill your full Name"
                            name="name"
                            type="text"
                            :rules="nameRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 sm4 class="pt-4 subheading">
                          <label class="right">Email ID:</label>
                        </v-flex>
                        <v-flex xs8 sm8 class="ml-3">
                          <v-text-field
                            placeholder="fill your email"
                            :rules="emailRules"
                            v-model="addSchool.email"
                            type="text"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 sm4 class="pt-4 subheading">
                          <label class="right hidden-sm-only hidden-xs-only">Society Name:</label>
                          <label
                            class="right hidden-lg-only hidden-xl-only hidden-md-only"
                          >SocietyName:</label>
                        </v-flex>
                        <v-flex xs8 sm8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.societyName"
                            placeholder="fill your Society Name"
                            :rules="cityRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">City:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.city"
                            placeholder="fill your City Name"
                            name="City"
                            type="text"
                            :rules="cityRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">State:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.state"
                            placeholder="fill your State Name"
                            name="state"
                            type="text"
                            :rules="stateRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Pincode:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.pinCode"
                            placeholder="fill your pincode"
                            name="pincode"
                            type="number"
                            :rules="pincode"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right">Fax No:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field v-model="addSchool.faxNo" label="fill your Fax No" required></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Country:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Country:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-autocomplete
                            v-model="addSchool.country"
                            :items="countries"
                            :rules="country"
                            placeholder="Select Country Name"
                            required
                          ></v-autocomplete>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Mobile No:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Mobile:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.mobile"
                            placeholder="fill your MobileNo"
                            name="mobileNo"
                            type="number"
                            :rules="mobileNoRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">LandLine No:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >LandLineNo:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.landLineNo"
                            :items="countries"
                            :rules="landlineNoRule"
                            placeholder="fill your LandLine No"
                            type="number"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Registration No:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >Registration-No:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.registrationNo"
                            placeholder="Fill your Registration No"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">School Details:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >SchoolDetails:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.schoolDetails"
                            placeholder="Fill your School Details"
                            :rules="schoolDetailRule"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Uplaod Image:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Uplaod :</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            label="Select Image"
                            @click="pickFile"
                            v-model="imageName"
                            append-icon="attach_file"
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">Academic Year:</label>
                          <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Year:</label>
                        </v-flex>
                        <v-flex xs8 class="ml-3">
                          <v-text-field
                            v-model="addSchool.establishmentYear"
                            placeholder="fill your Academic Year"
                            :rules="establishmentYearRules"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout wrap>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 sm4 class="pt-4 subheading">
                          <label class="right hidden-xs-only hidden-sm-only">WebSite Url:</label>
                          <label
                            class="right hidden-lg-only hidden-md-only hidden-xl-only"
                          >WebSiteUrl:</label>
                        </v-flex>
                        <v-flex xs8 sm8 class="ml-3">
                          <v-text-field
                            name="input-4-3"
                            v-model="addSchool.websiteUrl"
                            :rules="presentAddress"
                            placeholder="fill Your Address"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                    <v-flex xs12 sm6>
                      <v-layout>
                        <v-flex xs4 sm4 class="pt-4 subheading">
                          <label class="right">Address:</label>
                        </v-flex>
                        <v-flex xs8 sm8 class="ml-3">
                          <v-text-field
                            name="input-4-3"
                            v-model="addSchool.address"
                            :rules="presentAddress"
                            placeholder="fill Your Address"
                            required
                          ></v-text-field>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs12 sm11 offset-md1>
                      <v-layout>
                        <v-flex xs6>
                          <v-btn round dark @click="clear()">Clear</v-btn>
                        </v-flex>
                        <v-flex xs6>
                          <v-btn @click="submit" round dark :loading="loading" class="right">Add</v-btn>
                        </v-flex>
                      </v-layout>
                    </v-flex>
                  </v-layout>
                </v-form>
              </v-flex>
            </v-flex>
          </v-layout>
        </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 moment from "moment";
import countryList from "@/script/country.js";

export default {
  data: () => ({
    e2: 0,
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    text: "",
    showLoader: false,
    loading: false,
    date: null,
    search: "",
    menu: false,
    menu1: false,
    dialog: false,
    valid: true,
    isActive: true,
    newActive: false,
    pagination: {
      rowsPerPage: 15
    },
    imageData: {},
    imageName: "",
    imageUrl: "",
    imageFile: "",
    nameRules: [v => !!v || "Full Name is required"],
    dateRules: [v => !!v || "DOB is required"],
    cityRules: [v => !!v || "City Name is required"],
    pincode: [v => !!v || " Pincode is required"],
    country: [v => !!v || " Country Name is required"],
    permanentAddress: [v => !!v || " Permanent Address is requiclearred"],
    presentAddress: [v => !!v || " Present Address  is required"],
    landlineNoRule: [v => !!v || " Present Address  is required"],
    mobileNoRules: [v => !!v || "Mobile Number is required"],
    stateRules: [v => !!v || "State Name is required"],
    schoolDetailRule: [v => !!v || "school Detail is required"],
    sectionRules: [v => !!v || " Section Name is required"],
    genderRules: [v => !!v || " Select Gender is required"],
    establishmentYearRules: [v => !!v || " Academic Year  is required"],
    errorMessages: "",
    emailRules: [
      v => !!v || "E-mail is required",
      v =>
        /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(v) ||
        "E-mail must be valid"
    ],
    countries: [],
    headers: [
      {
        text: "No.",
        align: "center",
        sortable: false,
        value: "rollNo"
      },
      {
        text: "school Logo",
        value: "schoolLogoUrl",
        sortable: false,
        align: "center"
      },
      { text: "Name", value: "name", sortable: false, align: "center" },
      { text: "Email", value: "email", sortable: false, align: "center" },
      { text: "City", value: "city", sortable: false, align: "center" },
      {
        text: "State",
        value: "state",
        sortable: false,
        align: "center"
      },
      {
        text: "Mobile",
        value: "mobile",
        sortable: false,
        align: "center"
      },
      { text: "Action", value: "", sortable: false, align: "center" }
    ],
    schoolList: [],
    editedIndex: -1,
    addSchool: {
      role: "ADMIN"
    },
    editedItem: {}
  }),
  methods: {
    getSchool() {
      this.showLoader = true;
      http()
        .get("/getSchoolList", {
          headers: { Authorization: "Bearer " + this.token }
        })
        .then(response => {
          this.schoolList = 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);
          }
        });
    },
    pickFile() {
      this.$refs.image.click();
    },
    dates: function(date) {
      return moment(date).format("MMMM DD, YYYY");
    },
    onFilePicked(e) {
      // console.log(e)
      const files = e.target.files;
      this.imageData.upload = e.target.files[0];
      if (files[0] !== undefined) {
        this.imageName = files[0].name;
        if (this.imageName.lastIndexOf(".") <= 0) {
          return;
        }
        const fr = new FileReader();
        fr.readAsDataURL(files[0]);
        fr.addEventListener("load", () => {
          this.imageUrl = fr.result;
          this.imageFile = files[0]; // this is an image file that can be sent to server...
          this.imageData.imageUrl = URL.createObjectURL(this.imageFile);
        });
      } else {
        this.imageName = "";
        this.imageFile = "";
        this.imageUrl = "";
      }
    },
    editItem(item) {
      this.editedIndex = this.schoolList.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },
    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);
    },
    submit() {
      if (this.$refs.form.validate()) {
        if (this.imageUrl) {
          var str = this.imageUrl;
          const [baseUrl, imageUrl] = str.split(/,/);
          this.addSchool.upload = imageUrl;
        }
        this.loading = true;
        http()
          .post("/createSchool", this.addSchool)
          .then(response => {
            this.snackbar = true;
            this.text = "New School added successfully";
            this.clear();
            this.getSchool();
            this.loading = false;
          })
          .catch(error => {
            this.snackbar = true;
            this.text = error.response.data.message;
            this.loading = false;
          });
      }
    },
    clear() {
      this.$refs.form.reset();
      this.imageUrl = "";
    },
    save() {
      if (this.imageUrl) {
        var str = this.imageUrl;
        const [baseUrl, imageUrl] = str.split(/,/);
        this.editedItem.upload = imageUrl;
      }
      this.editedItem.schoolId = this.editedItem._id;
      http()
        .put("/updateSchool", this.editedItem)
        .then(response => {
          this.snackbar = true;
          this.text = "Successfully School Existing User";
          this.imageUrl = "";
          this.getSchool();
          this.close();
        })
        .catch(error => {
          // console.log(error);
          if ((this.snackbar = true)) {
            this.text = error.response.data.statusText;
          }
        });
    }
  },
  mounted() {
    const getCountryList = countryList();
    this.countries = getCountryList;
    this.token = this.$store.state.token;
    this.getSchool();
  },
  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>