<template>
<v-app id="pages-dasboard">
 <v-toolbar
    color="grey"
    fixed
    app
  >
  <v-toolbar-title class="ml-0 pl-3">
   <v-toolbar-side-icon @click.stop="handleDrawerToggle" class="hide"></v-toolbar-side-icon>
    </v-toolbar-title>
<!-- ****** SEARCH ALL EXISTING USERS ****** -->
    <v-flex xs7 sm3 class="userSearch">
      <v-text-field
        flat
        prepend-icon="search"
        label="Find your user"
        v-model="term"
        color="black"
        @change="getPatientList"
      >
      </v-text-field>
    </v-flex>
      <v-spacer></v-spacer>
      <h5 class="white--text name"> {{ userName }} </h5>
      <v-menu offset-y origin="center center" :nudge-bottom="10" transition="scale-transition">
     <v-btn icon large flat slot="activator">
          <v-avatar size="40px">
           <img src="/static/icon/user.png"/>
          </v-avatar>
      </v-btn>
        <v-list class="pa-0">
          <v-list-tile v-for="(item,index) in items" :to="!item.href ? { name: item.name } : null" :href="item.href" @click="item.click" ripple="ripple" :disabled="item.disabled" :target="item.target" rel="noopener" :key="index">
             <v-list-tile-action v-if="item.icon">
              <v-icon>{{ item.icon }}</v-icon>
            </v-list-tile-action>
            <v-list-tile-content>
              <v-list-tile-title>{{ item.title }}</v-list-tile-title>
            </v-list-tile-content>
          </v-list-tile>
        </v-list> 
      </v-menu>
  </v-toolbar>
  <v-tabs grow slider-color="black">
    <v-tab
      ripple
      @click="activeTab('existing')"
      v-bind:class="{ active: isActive }"
      id="tab"
      class="subheading"
    >Existing User</v-tab>
    <v-tab
      ripple
      @click="activeTab('new')"
      v-bind:class="{ active: newActive }"
      id="tab1"
      class="subheading"
    >Add New Users</v-tab>
<!-- ****** EDIT USERS Patient Dtails ****** -->
    <v-tab-item>
      <v-snackbar
        :timeout="timeout"
        :top="y === 'top'"
        :right="x === 'right'"
        :vertical="mode === 'vertical'"
        v-model="snackbar"
        color="success"
      >{{ text }}</v-snackbar>
      <v-dialog v-model="dialog" max-width="500px">
        <v-toolbar color="white">
          <v-spacer></v-spacer>
          <v-toolbar-title>Edit Profile</v-toolbar-title>
          <v-spacer></v-spacer>
        </v-toolbar>
        <v-card>
          <v-flex align-center justify-center layout text-xs-center>
            <v-avatar size="50px" style="position:absolute; top:10px; ">
              <img src="/static/icon/user.png">
            </v-avatar>
          </v-flex>
          <v-card-text>
            <v-container>
              <v-layout wrap justify-center>
                <v-flex xs12 sm9>
                  <v-form ref="form" v-model="valid" lazy-validation>
                    <v-layout style="position:relative; top:15px;">
                      <v-flex xs4 class="pt-4 subheading">
                        <label>Full Name:</label>
                      </v-flex>
                      <v-flex xs8>
                        <v-text-field
                          v-model="editedItem.name"
                          v-validate="'required'"
                          :rules="nameRules"
                          data-vv-name="Name"
                          required
                        ></v-text-field>
                      </v-flex>
                    </v-layout>
                    <v-layout>
                      <v-flex xs4 class="pt-4 subheading">
                        <label>Email ID:</label>
                      </v-flex>
                      <v-flex xs8>
                        <v-text-field
                          v-model="editedItem.email"
                          v-validate="'required|email'"
                          :rules="emailRules"
                          data-vv-name="E-mail"
                          required
                        ></v-text-field>
                      </v-flex>
                    </v-layout>
                     <v-layout>
                       <v-flex xs4 class="pt-4 subheading">
                        <label>Date of Birth:</label>
                      </v-flex>
                      <v-flex xs8>
                      <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"
                        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="menu1 = false"
                        ></v-date-picker>
                        </v-menu>
                      </v-flex>
                    </v-layout>
                    <v-card-actions>
                      <v-btn round dark @click.native="close">Cancel</v-btn>
                      <v-spacer></v-spacer>
                      <v-btn round dark @click="save">Save</v-btn>
                    </v-card-actions>
                  </v-form>
                </v-flex>
              </v-layout>
            </v-container>
          </v-card-text>
        </v-card>
      </v-dialog>
<!-- ****** PROFILE VIEW pateints Details ******  -->
      <v-dialog v-model="dialog1" max-width="600px">
        <v-toolbar color="white">
          <v-spacer></v-spacer>
          <v-toolbar-title>Profile</v-toolbar-title>
          <v-spacer></v-spacer>
          <v-icon @click="close1">close</v-icon>
        </v-toolbar>
        <v-card>
          <v-flex align-center justify-center layout text-xs-center>
            <v-avatar size="50px" style="position:absolute; top:20px;">
              <img src="/static/icon/user.png">
            </v-avatar>
          </v-flex>
          <v-card-text>
            <v-container grid-list-md>
              <v-layout wrap>
                <v-flex><br><br>
                   <v-layout>
                     <v-flex xs5 sm6>
                     <h5 class="right">Full Name:</h5>
                    </v-flex>
                    <v-flex sm6 xs8>
                    <h5>{{ editedItem.name }}</h5>
                  </v-flex>
                 </v-layout>
                  <v-layout>
                     <v-flex xs5 sm6>
                     <h5 class="right">Email:</h5>
                    </v-flex>
                    <v-flex  sm6 xs8>
                     <h5>{{ editedItem.email }}</h5>
                  </v-flex>
                 </v-layout>
                  <v-layout>
                     <v-flex xs6 sm6 >
                     <h5 class="right my-3">Date Of Birth:</h5>
                    </v-flex>
                    <v-flex sm6 xs8>
                    <h5 class="my-3">{{ editedItem.dob }}</h5>
                  </v-flex>
                 </v-layout>
                </v-flex>
              </v-layout>
            </v-container>
          </v-card-text>
        </v-card>
      </v-dialog>

      <v-dialog v-model="dialog2" width="700px">
        <v-card>
          <div id="dialog">
            <h4>
              <b>Report</b>
            </h4>
            <h5 id="name">
              <b>Patient Name:</b>
              {{ editedItem.name }}
            </h5>
            <h5 id="m">Select Month:</h5>
            <v-btn color="grey darken-3" id="G" v-on:click="component='report-generate'">Generate</v-btn>
            <v-spacer></v-spacer>
            <v-icon id="icon" @click="close2">close</v-icon>

            <span id="bt">
              <v-btn
                color="grey darken-2"
                v-on:click="component='one-month' , activebtn('existing')"
                v-bind:class="{ activebtn: isActivebtn }"
                flat
              >1 month</v-btn>
              <v-btn
                color="grey darken-2"
                v-on:click="component='three-month' , activebtn('new')"
                v-bind:class="{ activebtn: Activebtn1 }"
                flat
              >3 Month</v-btn>
              <v-btn
                color="grey darken-2"
                v-on:click="component='six-month' , activebtn('new1')"
                v-bind:class="{ activebtn: Activebtn2 }"
                flat
              >6 Month</v-btn>
              <v-btn
                color="grey darken-2"
                v-on:click="component='nine-month' , activebtn('new2')"
                v-bind:class="{ activebtn: Activebtn3 }"
                flat
              >9 Month</v-btn>
              <img id="e" @click="mail" src="/static/icon/email1.png">
              <img id="d" @click="download" src="/static/icon/download1.png">
            </span>
            <component v-bind:is="component"></component>
          </div>
        </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 PatientList Table ****** -->
      <v-data-table 
      :headers="headers" 
      :items="desserts" 
      :pagination.sync="pagination"
      >    <!-- </v-content>
     <v-footer class="pa-4" color="grey darken-2">
  
  </v-footer> -->
        <template slot="items" slot-scope="props">
          <td id="td" class="text-xs-center">{{ props.index}}</td>
          <td id="td" class="text-xs-center">{{ props.item.name}}</td>
          <td id="td" class="text-xs-center">{{ props.item.email }}</td>

          <td class="text-xs-center">
            <span>
              <img
                style="cursor:pointer; height:20px; "
                class="mr-5"
                @click="report(props.item)"
                src="/static/icon/List1.png"
              >
              <img
                style="cursor:pointer; width:25px; height:18px; "
                class="mr-5"
                @click="profile(props.item)"
                src="/static/icon/eye1.png"
              >
              <img
                style="cursor:pointer; width:20px; height:18px; "
                class="mr-5"
                @click="editItem(props.item)"
                src="/static/icon/edit1.png"
              >
              <img
                style="cursor:pointer; height:20px; "
                class="mr-5"
                @click="deleteItem(props.item)"
                src="/static/icon/delete1.png"
              >
            </span>
          </td>
        </template>
        <v-alert
          slot="no-results"
          :value="true"
          color="error"
          icon="warning"
        >Your search for "{{ term }}" found no results.</v-alert>
      </v-data-table>
    </v-tab-item>
<!-- ****** ADD USER PATIENT ****** -->
    <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>
                <v-flex offset-xs5>
                  <v-avatar size="55px">
                    <img src="/static/icon/user.png">
                  </v-avatar>
                </v-flex>
                <v-form ref="form" v-model="valid" lazy-validation>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right">Full Name:</label>
                    </v-flex>
                    <v-flex xs6 class="ml-3">
                      <v-text-field
                        v-model="editedItem.name"
                        placeholder="fill your full Name"
                        name="name"
                        type="text"
                        :rules="nameRules"
                        required
                      ></v-text-field>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right">Country:</label>
                    </v-flex>
                    <v-flex xs6  class="ml-3">
                        <v-select
                         v-model="editedItem.country"
                         :items="countries"
                         label="Select Country"
                         single-line
                       ></v-select>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right">Email ID:</label>
                    </v-flex>
                    <v-flex xs6 class="ml-3">
                      <v-text-field
                        placeholder="fill your email"
                        :rules="emailRules"
                        v-model="editedItem.email"
                        type="text"
                        name="email"
                        required
                      ></v-text-field>
                    </v-flex>
                  </v-layout>
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right">Date of Birth:</label>
                    </v-flex>
                    <v-flex xs6 class="ml-3">
                       <v-menu
                         ref="menu"
                         :close-on-content-click="false"
                         v-model="menu"
                         :nudge-right="40"
                         lazy
                         transition="scale-transition"
                         offset-y
                         full-width
                         min-width="290px"
                        >
                       <v-text-field
                        slot="activator"
                        v-model="editedItem.date"
                        placeholder="Select date"
                       ></v-text-field>
                       <v-date-picker
                        ref="picker"
                        v-model="editedItem.date"
                        :max="new Date().toISOString().substr(0, 10)"
                        min="1950-01-01"
                        @input="menu = false"
                        ></v-date-picker>
                        </v-menu>
                       </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>
</v-app>
</template>

<script>
// import AppToolbar from '@/components/AppToolbar';
import onemonth from "@/components/report/onemonth.vue";
import threemonth from "@/components/report/threemonth.vue";
import sixmonth from "@/components/report/sixmonth.vue";
import ninemonth from "@/components/report/ninemonth.vue";
import generatereport from "@/components/report/generatereport.vue";
import http from "@/Services/http.js";
import NotificationList from "@/components/widgets/list/NotificationList";
import Util from "@/util";

export default {
  components: {
    "one-month": onemonth,
    "three-month": threemonth,
    "six-month": sixmonth,
    "nine-month": ninemonth,
    "report-generate": generatereport,
 },
  data: () => ({
    component: "report-generate",
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    text: "",
    loading: false,
    date:null,
    term: "",
    modal: false,
    modaledit:false,
    menu: false,
    menu1:false,
    dialog: false,
    dialog1: false,
    dialog2: false,
    valid: true,
    isActive: true,
    newActive: false,
    isActivebtn: false,
    Activebtn1: false,
    Activebtn2: false,
    Activebtn3: false,
    loader: null,
    loading: false,
    loading2: false,
    loading3: false,
    loading4: false,
    details: [],
    countries:['US','Canada'],
    AddUsercredentials: {},
    pagination: {
      rowsPerPage: 15
    },
    rules: {
      required: value => !!value || "Date of Birth is Required.",
      min: v =>
        (/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/.test(
          v
        ) &&
          v.length > 0) ||
        "Please enter a date in the format dd/mm/yyyy"
    },
    nameRules: [v => !!v || " Full Name is required"],
    emailRules: [
      v => !!v || "E-mail is required",
       v => /^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(v) || 'E-mail must be valid'
    ],
    countryRules: [v => !!v || "  Country Name is required"],
    headers: [
      {
        text: "No",
        align: "center",
        sortable: false,
        value: "No"
      },
      { text: "Name", value: "name", sortable: false, align: "center" },
      { text: "Email", value: "email", sortable: false, align: "center" },
      { text: "", value: "", sortable: false, align: "center" }
    ],
    desserts: [],
    editedIndex: -1,
    editedItem: {
      No: "",
      name: "",
      email: "",
      date: null,
      country: ""
    },
    defaultItem: {
      No: "",
      name: "",
      email: "",
      date: null,
      dob: ""
    },
    userName:'',
    items: [
      {
        href: "/changepassword",
        title: "Change Password",
        click: e => {
          console.log(e);
        }
      },
      {
        href: "#",
        title: "Logout",
        click: e => {
          window.getApp.$emit("APP_LOGOUT");
        }
      }
    ]
  }),
   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)
    },
    getPatientList() {
      var token = this.$store.state.token;
      http()
        .get("/patientList?term=" + this.term, { 
          headers: { Authorization: "Bearer " + token } 
         })
        .then(response => {
          this.desserts = response.data.data.existingUser;
        })
        .catch(err => {
          console.log("err====>", err);
          this.$router.replace({ path: '/' });
        });
    },
    editItem(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog = true;
    },
    profile(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog1 = true;
    },
    report(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.dialog2 = true;
    },

    deleteItem(item) {
      let addUsers = {
        userId: item.userId
      };
      http()
        .post("/deletePatient", confirm('Are you sure you want to delete this?') && addUsers)
        .then(response => {
          if ((this.snackbar = true)) {
            this.text = "Successfully delete Existing User";
        }
          this.getPatientList();
        })
        .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;
      }
    },
    activebtn(type) {
      switch (type) {
        case "existing":
          this.Activebtn3 = false;
          this.Activebtn2 = false;
          this.Activebtn1 = false;
          this.isActivebtn = true;
          break;
        case "new":
          this.Activebtn3 = false;
          this.Activebtn2 = false;
          this.Activebtn1 = true;
          this.isActivebtn = false;
          break;
        case "new1":
          this.Activebtn3 = false;
          this.Activebtn2 = true;
          this.Activebtn1 = false;
          this.isActivebtn = false;
          break;
        default:
          this.Activebtn3 = true;
          this.Activebtn2 = false;
          this.Activebtn1 = false;
          this.isActivebtn = 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()) {
        let addUsers = {
          name: this.editedItem.name,
          dob: this.editedItem.date,
          email: this.editedItem.email,
          country: this.editedItem.country
        };
        // console.log(addUsers)
        http()
          .post("/patient", addUsers)
          .then(response => {
            this.getPatientList();
            if (this.snackbar = true) {
              this.text = "New user added successfully";
            }
            
          this.clear();
          })
          .catch(error => {
            console.log(error);
            if (this.snackbar = true) {
            this.text = error.response.data.message;
          }
          });
      }
    },
    mail() {
    },
    download() {
    },
    clear() {
      this.$refs.form.reset();
    },
    save() {
      let editUsers = {
        name: this.editedItem.name,
        dob: this.editedItem.dob,
        email: this.editedItem.email,
        userId: this.editedItem.userId
      };
      http()
        .post("/patientDetail", editUsers)
        .then(response => {
          if ((this.snackbar = true)) {
            this.text = "Successfully Edit Existing User";
          }
            this.getPatientList();
        })
        .catch(error => {
           console.log(error);
        });
      this.close();
    },
    handleDrawerToggle() {
      window.getApp.$emit("APP_DRAWER_TOGGLED");
    },
    handleFullScreen() {
      Util.toggleFullScreen();
    }
  },
  mounted() {
    this.getPatientList();
    this.userName = this.$store.state.name;
  },
  computed:{
    toolbarColor() {
      return this.$vuetify.options.extra.mainNav;
    }
  }
};
</script>
<style scoped>
.v-tabs__div {
    text-transform: none;
}
.v-input__prepend-outer {
    margin-right: 0px !important;
}
.v-card__actions .v-btn {
  margin: 0 15px;
  min-width: 120px;
}
 .primary {
    background-color: #aaa !important;
    border-color: #aaa !important;
}
h4 {
  background-repeat: no-repeat;
  padding: 8px;
  margin: auto;
  font-size: 25px;
}
#name {
  position: absolute;
  left: 100px;
  top: 17px;
}
#icon {
  position: absolute;
  right: 8px;
  top: 8px;
}
#m {
  position: relative;
  left: 135px;
  top: -15px;
}
#G {
  position: absolute;
  top: 38px;
  color: white;
}
#bt {
  position: relative;
  top: -20px;
  left: 115px;
}
#e {
  position: relative;
  top: 5px;
  right: -30px;
  height: 17px;
  cursor: pointer;
}
#d {
  position: relative;
  top: 5px;
  right: -70px;
  height: 17px;
  cursor: pointer;
}
#td {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}
#dialog {
  height: 550px;
}
.active {
  background-color: black;
  color: white !important;
}
.activebtn {
  color: black !important;
}
#flex {
  height: 300px;
}
.top{
  margin-top:100px;
}
.v-tabs__item a{
  font-size:16px !important; 
}
@media screen and (max-width: 769px){
  .top{
    margin-top:0 !important;
  } 
  .userSearch .v-icon {
    font-size: 20px !important;
    margin-left: 20px ;
  }
}
@media screen and (max-width: 380px) {
.right {
    float: none !important;
}
.subheading {
    font-size: 14px !important;
}
.v-card__actions .v-btn{
    margin: 0 0px;
    min-width: 100px;
}
/* .searchIcon .v-icon {
    font-size: 20px;
    margin-left: 20px;
} */
.subheading {
    font-size: 12px !important;
}  
h5 {
    font-size: 13px;
}
}
.v-icon{
  font-size:30px;
}
@media screen and (min-width: 1270px){
.hide{
  display: none;
}
/* }
@media screen and (max-width: 962px) {
.imglogo{
    position: absolute;
    top: 13px;
    left: 13px !important;
    width: 70px;
    height: 24px;
} */
}
@media screen and (max-width: 420px){
.userSearch .v-text-field .v-label{
  line-height: 24px !important;
}
.userSearch .v-label{
  font-size: 13px !important;
}
.v-list__tile {
  font-size:14px;
  padding: 0 10px;
}
.name{
  font-size:15px; 
}
}
</style>