<template>
  <v-container fluid class="body-color">
    <!-- ****** EDIT USERS DETAILS ****** -->
    <v-dialog v-model="editUserDialog" max-width="1100px" scrollable>
      <v-card flat class="card-style pa-2" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Edit User</label>
            <v-icon size="24" class="right" @click="editUserDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-card-text>
          <v-layout>
            <v-flex xs12 class="text-xs-center text-sm-center text-md-center text-lg-center my-4">
              <v-avatar size="120px">
                <img src="/static/icon/user.png" v-if="!editedItem.profilePicUrl && !imageUrl" />
                <img
                  :src="editedItem.profilePicUrl"
                  v-else-if="editedItem.profilePicUrl && !imageUrl"
                />
                <img
                  v-if="imageUrl"
                  :src="imageUrl"
                  height="150"
                  style="border-radius:50%; width:150px"
                />
              </v-avatar>
              <input
                type="file"
                style="display:none"
                ref="image"
                accept="image/*"
                @change="onFilePicked"
              />
            </v-flex>
          </v-layout>
          <v-layout wrap>
            <v-flex xs12 sm6>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right">Name:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-text-field
                    v-model="editedItem.name"
                    placeholder="fill your Name"
                    name="name"
                    type="text"
                    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">Email ID:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-text-field
                    placeholder="fill your email"
                    v-model="editedItem.email"
                    type="text"
                    name="email"
                    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-sm-only hidden-xs-only">Date of Birth:</label>
                  <label class="right hidden-lg-only hidden-xl-only hidden-md-only">D.O.B:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-menu
                    ref="menu"
                    :close-on-content-click="false"
                    v-model="menu2"
                    :nudge-right="40"
                    lazy
                    transition="scale-transition"
                    offset-y
                    full-width
                    min-width="290px"
                  >
                    <v-text-field
                      slot="activator"
                      v-model="editedItem.dob"
                      placeholder="Select date"
                    ></v-text-field>
                    <v-date-picker
                      ref="picker"
                      v-model="editedItem.dob"
                      :max="new Date().toISOString().substr(0, 10)"
                      min="1950-01-01"
                      @input="menu2 = false"
                    ></v-date-picker>
                  </v-menu>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm6>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right">Gender:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-select
                    :items="gender"
                    v-model="editedItem.gender"
                    label="Select gender"
                    name="gender"
                    required
                  ></v-select>
                </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">Religion:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-text-field
                    v-model="editedItem.religion"
                    placeholder="fill your Religion"
                    name="religion"
                    type="text"
                    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">JoiningDate:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-menu
                    ref="menu"
                    :close-on-content-click="false"
                    v-model="menu3"
                    :nudge-right="40"
                    lazy
                    transition="scale-transition"
                    offset-y
                    full-width
                    min-width="290px"
                  >
                    <v-text-field
                      slot="activator"
                      v-model="editedItem.joiningDate"
                      placeholder="Select date"
                    ></v-text-field>
                    <v-date-picker
                      ref="picker"
                      v-model="editedItem.joiningDate"
                      :max="new Date().toISOString().substr(0, 10)"
                      min="1950-01-01"
                      @input="menu3 = false"
                    ></v-date-picker>
                  </v-menu>
                </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">Phone:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-text-field
                    v-model="editedItem.phone"
                    placeholder="fill your MobileNo"
                    name="mobileNo"
                    type="number"
                    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-sm-only hidden-xs-only">Select Role:</label>
                  <label class="right hidden-lg-only hidden-xl-only hidden-md-only">Role:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-autocomplete
                    v-model="editedItem.role"
                    :label="editedItem.role"
                    :items="userRole"
                    item-text="name"
                    item-value="role"
                    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-sm-only hidden-xs-only">Uplaod Image:</label>
                  <label class="right hidden-lg-only hidden-xl-only hidden-md-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">Address:</label>
                </v-flex>
                <v-flex xs8 class="ml-3">
                  <v-text-field
                    name="input-4-3"
                    v-model="editedItem.address"
                    placeholder="fill Your  Address"
                    required
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
          </v-layout>
          <v-layout>
            <v-flex xs12 sm12>
              <v-card-actions class="hidden-sm-only hidden-xs-only">
                <v-spacer></v-spacer>
                <v-btn round dark :loading="loading" @click="save" class="add-button">Save</v-btn>
              </v-card-actions>
              <v-card-actions class="hidden-lg-only hidden-xl-only hidden-md-only">
                <v-spacer></v-spacer>
                <v-btn round dark :loading="loading" @click="save" class="add-button">Save</v-btn>
                <v-spacer></v-spacer>
              </v-card-actions>
            </v-flex>
          </v-layout>
        </v-card-text>
      </v-card>
    </v-dialog>
    <!-- ****** PROFILE User DETAILS ******  -->
    <v-dialog v-model="viewUserDialog" max-width="560px" scrollable>
      <v-card flat class="card-style pa-3" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">View User</label>
            <v-icon size="24" class="right" @click="viewUserDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-card-text>
          <v-container grid-list-md>
            <v-layout wrap>
              <v-flex>
                <v-flex align-center justify-center layout text-xs-center>
                  <v-avatar size="100px">
                    <img src="/static/icon/user.png" v-if="!editedItem.profilePicUrl" />
                    <img :src="editedItem.profilePicUrl" v-else-if="editedItem.profilePicUrl" />
                  </v-avatar>
                </v-flex>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>Name:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.name }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>Email:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.email }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>Gender:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.gender }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>Religion:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.religion }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>Role:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.role }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>Phone:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.phone }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>JoiningDate:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ dates(editedItem.joiningDate) }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <label class="right hidden-sm-only hidden-xs-only">
                        <b>Date of Birth:</b>
                      </label>
                      <label class="right hidden-lg-only hidden-xl-only hidden-md-only">
                        <b>D.O.B:</b>
                      </label>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ dates(editedItem.dob) }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs4 sm6>
                    <h5 class="right my-1">
                      <b>Address:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.address }}</h5>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
          </v-container>
        </v-card-text>
      </v-card>
    </v-dialog>

    <!-- ****** EXISTING-USER TABLE DATA****** -->
    <v-toolbar color="transparent" flat>
      <v-btn
        fab
        dark
        class="open-dialog-button hidden-xl-only hidden-md-only hidden-lg-only"
        small
        @click="addUserDialog = true"
      >
        <v-icon dark>add</v-icon>
      </v-btn>
      <v-btn
        round
        class="open-dialog-button hidden-sm-only hidden-xs-only"
        dark
        @click="addUserDialog = true"
      >
        <v-icon class="white--text pr-1" size="20">add</v-icon>Add User
      </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 lg2 md3 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="Users" :pagination.sync="pagination" :search="search">
      <template slot="items" slot-scope="props">
        <td class="td td-row">{{ props.index + 1 }}</td>
        <td class="td td-row text-xs-center">
          <v-avatar size="40">
            <img :src="props.item.profilePicUrl" v-if="props.item.profilePicUrl" />
            <img src="/static/icon/user.png" v-else-if="!props.item.profilePicUrl" />
          </v-avatar>
        </td>
        <td class="text-xs-center td td-row">{{ props.item.name}}</td>
        <td class="text-xs-center td td-row">{{ props.item.email }}</td>
        <td class="text-xs-center td td-row">{{ dates(props.item.dob) }}</td>
        <td class="text-xs-center td td-row">{{ dates(props.item.joiningDate)}}</td>
        <td class="text-xs-center td td-row">{{ props.item.phone }}</td>
        <td class="text-xs-center td td-row">
          <v-switch
            class="pl-3"
            v-model="props.item.status"
            @change="suspendStatus(props.item.status,props.item._id)"
          ></v-switch>
        </td>
        <td class="text-xs-center td td-row">
          <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; "
                class="mr-3"
                @click="deleteItem(props.item)"
                src="/static/icon/delete.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>
    <!-- DIALOG BOX - Add User Data -->
    <v-dialog v-model="addUserDialog" max-width="900px">
      <v-card flat class="card-style pa-2" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Add User</label>
            <v-icon size="24" class="right" @click="$refs.form.reset();addUserDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-form ref="form" v-model="valid" lazy-validation>
          <v-container fluid>
            <v-layout>
              <v-flex
                xs12
                class="text-xs-center text-sm-center text-md-center text-lg-center my-4 mr-4"
              >
                <v-avatar size="80px">
                  <img src="/static/icon/user.png" v-if="!imageUrl" />
                </v-avatar>
                <img
                  :src="imageUrl"
                  height="150"
                  v-if="imageUrl"
                  style="border-radius:50%; width:150px"
                />
              </v-flex>
            </v-layout>
            <v-layout>
              <v-flex xs12 sm6>
                <v-layout>
                  <v-flex xs4 class="pt-4 subheading">
                    <label class="right">Name:</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-text-field
                      v-model="addUser.name"
                      placeholder="fill your 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 class="pt-4 subheading">
                    <label class="right">Email ID:</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-text-field
                      placeholder="fill your email"
                      :rules="emailRules"
                      v-model="addUser.email"
                      type="text"
                      name="email"
                      required
                    ></v-text-field>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
            <v-layout>
              <v-flex xs12 sm6>
                <v-layout>
                  <v-flex xs4 class="pt-4 subheading">
                    <label class="right hidden-sm-only hidden-xs-only">Date of Birth:</label>
                    <label class="right hidden-lg-only hidden-xl-only hidden-md-only">D.O.B:</label>
                  </v-flex>
                  <v-flex xs8 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"
                        :rules="dateRules"
                        v-model="addUser.dob"
                        placeholder="Select DOB"
                      ></v-text-field>
                      <v-date-picker
                        ref="picker"
                        v-model="addUser.dob"
                        :max="new Date().toISOString().substr(0, 10)"
                        min="1950-01-01"
                        @input="menu = false"
                      ></v-date-picker>
                    </v-menu>
                  </v-flex>
                </v-layout>
              </v-flex>
              <v-flex xs12 sm6>
                <v-layout>
                  <v-flex xs4 class="pt-4 subheading">
                    <label class="right">Gender:</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-select
                      v-model="addUser.gender"
                      :items="gender"
                      label="Selct Gender "
                      :rules="genderRules"
                      required
                    ></v-select>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
            <v-layout>
              <v-flex xs12 sm6>
                <v-layout>
                  <v-flex xs4 class="pt-4 subheading">
                    <label class="right">Religion:</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-text-field
                      v-model="addUser.religion"
                      placeholder="fill your Religion"
                      name="Religion"
                      type="text"
                      :rules="religionRules"
                      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">JoiningDate:</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-menu
                      ref="menu1"
                      :close-on-content-click="false"
                      v-model="menu1"
                      :nudge-right="40"
                      lazy
                      transition="scale-transition"
                      offset-y
                      full-width
                      min-width="290px"
                    >
                      <v-text-field
                        slot="activator"
                        :rules="joinDateRules"
                        v-model="addUser.joinDate"
                        placeholder="Select date"
                      ></v-text-field>
                      <v-date-picker
                        ref="picker"
                        v-model="addUser.joinDate"
                        :max="new Date().toISOString().substr(0, 10)"
                        min="1950-01-01"
                        @input="menu1 = false"
                      ></v-date-picker>
                    </v-menu>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
            <v-layout>
              <v-flex xs12 sm6>
                <v-layout>
                  <v-flex xs4 class="pt-4 subheading">
                    <label class="right">Phone :</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-text-field
                      v-model="addUser.mobileNo"
                      placeholder="fill your Phone Number"
                      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-sm-only hidden-xs-only">Select Role:</label>
                    <label class="right hidden-lg-only hidden-xl-only hidden-md-only">Role:</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-autocomplete
                      v-model="addUser.role"
                      :rules="role"
                      :items="userRole"
                      item-text="name"
                      item-value="name"
                      placeholder="Select Role 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">Username:</label>
                  </v-flex>
                  <v-flex xs8 class="ml-3">
                    <v-text-field
                      v-model="addUser.userName"
                      placeholder="fill your User Name"
                      type="text"
                      :rules="userNameRules"
                      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-sm-only hidden-xs-only">Uplaod Image:</label>
                    <label class="right hidden-lg-only hidden-xl-only hidden-md-only">Image:</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>
                    <input
                      type="file"
                      style="display:none"
                      ref="image"
                      accept="image/*"
                      @change="onFilePicked"
                    />
                  </v-flex>
                </v-layout>
              </v-flex>
              <v-flex xs12 sm6>
                <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
                      name="input-4-3"
                      v-model="addUser.presentAddress"
                      :rules="presentAddress"
                      placeholder="fill Your present Address"
                      required
                    ></v-text-field>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
            <v-layout>
              <v-flex xs12 sm12>
                <v-layout class="right">
                  <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";
import _ from 'lodash';

export default {
  data: () => ({
    component: "report-generate",
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    color: "",
    text: "",
    show: true,
    showSearch: false,
    showLoader: false,
    loading: false,
    addUserDialog: false,
    date: null,
    search: "",
    menu: false,
    menu1: false,
    menu2: false,
    menu3: false,
    viewUserDialog: false,
    editUserDialog: false,
    valid: true,
    isActive: true,
    newActive: false,
    pagination: {
      rowsPerPage: 10
    },
    imageData: {},
    imageName: "",
    imageUrl: "",
    imageFile: "",
    nameRules: [v => !!v || "Name is required"],
    dateRules: [v => !!v || " DOB is required"],
    genderRules: [v => !!v || " Gender Name is required"],
    pincode: [v => !!v || " Pincode is required"],
    role: [v => !!v || "Role Name is required"],
    permanentAddress: [v => !!v || " Permanent Address is required"],
    presentAddress: [v => !!v || " Present Address  is required"],
    mobileNoRules: [v => !!v || "Phone Number is required"],
    religionRules: [v => !!v || "Religion Name is required"],
    joinDateRules: [v => !!v || " Join Date is required"],
    userNameRules: [v => !!v || " User Name is required"],
    passwordRules: [v => !!v || " Password is required"],
    errorMessages: "",
    userRole: [],
    emailRules: [
      v => !!v || "E-mail is required",
      v =>
        /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(v) ||
        "E-mail must be valid"
    ],
    gender: ["Male", "Female"],
    headers: [
      {
        text: "No",
        align: "",
        sortable: false,
        value: "No"
      },
      {
        text: "Profile Pic",
        value: "profilePicUrl",
        sortable: false,
        align: "center"
      },
      { text: "Name", value: "name", sortable: false, align: "center" },
      { text: "Email", value: "email", sortable: false, align: "center" },
      { text: "DOB", value: "dob", sortable: false, align: "center" },
      {
        text: "Joining Date",
        value: "joiningDate",
        sortable: false,
        align: "center"
      },
      {
        text: "Phone",
        value: "phone",
        sortable: false,
        align: "center"
      },
      {
        text: "Status",
        value: "status",
        sortable: false,
        align: "center"
      },
      { text: "Action", value: "", sortable: false, align: "center" }
    ],
    Users: [],
    editedIndex: -1,
    upload: "",
    editedItem: {
      role: "",
      name: "",
      email: "",
      dob: null,
      gender: "",
      role: "",
      address: "",
      phone: "",
      religion: "",
      joiningDate: null
    },
    addUser: {
      role: "",
      name: "",
      email: "",
      dob: null,
      gender: "",
      pincode: "",
      role: "",
      permanentAddress: "",
      presentAddress: "",
      mobileNo: "",
      religion: "",
      joiningDate: null
    }
  }),
  watch: {
    menu(val) {
      val && this.$nextTick(() => (this.$refs.picker.activePicker = "YEAR"));
    },
    menu1(val) {
      val && this.$nextTick(() => (this.$refs.picker.activePicker = "YEAR"));
    }
  },
  methods: {
    save(date) {
      this.$refs.menu.save(date);
    },
    save(date) {
      this.$refs.menu1.save(date);
    },
    pickFile() {
      this.$refs.image.click();
    },
    onFilePicked(e) {
      // console.log(e)
      const files = e.target.files;
      this.upload = e.target.files[0];
      console.log("imageData-upload========>", this.upload);
      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);
          // console.log("upload=======>", this.imageData.imageUrl);
          console.log("imageFile", this.imageUrl);
        });
      } else {
        this.imageName = "";
        this.imageFile = "";
        this.imageUrl = "";
      }
    },
    dates: function(date) {
      return moment(date).format("MMMM DD, YYYY");
    },
    getUsersList() {
      this.showLoader = true;
      var token = this.$store.state.token;
      http()
        .get("/getUsersList", {
          headers: { Authorization: "Bearer " + token }
        })
        .then(response => {
          this.Users = 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.Users.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.editedItem.dob =
        this.editedItem.dob != undefined
          ? (this.editedItem.dob = this.editedItem.dob.substring(0, 10))
          : (this.editedItem.dob = "");
      for (let i = 0; i < this.userRole.length; i++) {
        if (this.userRole[i].role === this.editedItem.role) {
          this.editedItem.role = this.userRole[i].name;
        }
      }
      this.editUserDialog = true;
    },
    profile(item) {
      this.editedIndex = this.Users.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.viewUserDialog = true;
      for (let i = 0; i < this.userRole.length; i++) {
        if (this.userRole[i].role === this.editedItem.role) {
          this.editedItem.role = this.userRole[i].name;
        }
      }
    },
    deleteItem(item) {
      let deleteUser = {
        userId: item._id
      };
      http()
        .delete(
          "/deleteUser",
          confirm("Are you sure you want to delete this?") && {
            params: deleteUser
          }
        )
        .then(response => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.getUsersList();
        })
        .catch(error => {
          this.snackbar = true;
          this.color = "error";
          this.text = error.response.data.message;
        });
    },
    close() {
      this.editUserDialog = false;
    },
    closeProfileDialog() {
      this.viewUserDialog = false;
    },
    submit() {
      if (this.$refs.form.validate()) {
        let addUserData = {
          name: this.addUser.name,
          email: this.addUser.email,
          dob: this.addUser.dob,
          gender: this.addUser.gender,
          religion: this.addUser.religion,
          role: this.addUser.role,
          address: this.addUser.presentAddress,
          phone: this.addUser.mobileNo,
          religion: this.addUser.religion,
          joiningDate: this.addUser.joinDate
        };
        if (this.imageUrl) {
          var str = this.imageUrl;
          const [baseUrl, imageUrl] = str.split(/,/);
          addUserData.profilePicUrl = imageUrl;
        }
        this.loading = true;
        http()
          .post("/createUser", addUserData)
          .then(response => {
            this.imageUrl = "";
            this.getUsersList();
            this.snackbar = true;
            this.addUserDialog = false;
            this.text = response.data.message;
            this.color = "green";
            this.clear();
            this.loading = false;
          })
          .catch(error => {
            this.loading = false;
            this.snackbar = true;
            this.color = "error";
            this.text = error.response.data.message;
          });
      }
    },
    clear() {
      this.$refs.form.reset();
      this.imageUrl = "";
    },
    save() {
      this.loading = true;
      for (let i = 0; i < this.userRole.length; i++) {
        if (this.userRole[i].name === this.editedItem.role) {
          this.editedItem.role = this.userRole[i].role;
        }
      }
      this.editedItem.userId = this.editedItem._id;
      if (this.imageUrl) {
        var str = this.imageUrl;
        const [baseUrl, imageUrl] = str.split(/,/);
        this.editedItem.upload = imageUrl;
      }
      http()
        .put("/updateUser", this.editedItem)
        .then(response => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.loading = false;
          this.getUsersList();
          this.close();
        })
        .catch(error => {
          this.loading = false;
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    getRole() {
      this.showLoader = true;
      var token = this.$store.state.token;
      http()
        .get("/getRolesList", {
          headers: { Authorization: "Bearer " + token }
        })
        .then(response => {
          this.userRole = response.data.data;
          this.showLoader = false;
          this.userRole = response.data.data;
          var removedRoles = _.remove(this.userRole, function(c) {
            //remove if color is green or yellow
            return c.name === "ADMIN" ||c.name === "SUPERADMIN" || c.name === "TEACHER" || c.name === "STUDENT" || c.name === "PARENT" ;
          });   
        })
        .catch(error => {
          this.showLoader = false;
          
        });
    },
    suspendStatus(suspendStatus, id) {
      let suspendStatusData = {
        userId: id,
        status: suspendStatus
      };
      this.showLoader = true;
      http()
        .put("/suspendUserAccount", suspendStatusData)
        .then(response => {
          this.getUsersList();
          this.text = response.data.message;
          this.snackbar = true;
          this.showLoader = false;
        })
        .catch(error => {
          // console.log(error.response.data.data);
          this.snackbar = true;
          this.text = error.response.data.message;
          this.showLoader = false;
        });
    },
    displaySearch() {
      (this.show = false), (this.showSearch = true);
    },
    closeSearch() {
      this.showSearch = false;
      this.show = true;
      this.search = "";
    }
  },
  mounted() {
    this.getUsersList();
    this.getRole();
  }
};
</script>