<template>
  <v-container fluid class="body-color">
    <!-- ****** EDITS  Gallery  ****** -->
    <v-dialog v-model="editGalleryDialog" max-width="1000px" scrollable persistent>
      <v-card class="card-style pa-2" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Edit Gallery</label>
            <v-icon size="24" class="right" @click="editGalleryDialog = false">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-card-text>
          <v-layout row>
            <v-flex xs12 class="text-xs-center text-sm-center text-md-center text-lg-center my-4">
              <input
                type="file"
                style="display: none"
                ref="editImage"
                accept="image/*"
                multiple
                @change="editFilePicked"
              />
              <v-layout justify-center>
                <v-flex
                  xs6
                  sm12
                  md3
                  v-for="Image in editedItem.imageUrl"
                  :key="Image._id"
                  v-if="editedItem.imageUrl"
                  class="profile-image-wrapper imgNews"
                >
                  <img :src="Image.imageLink" height="160" width="160" alt="Gallery" class="pa-2" />
                  <v-icon
                    class="red edit-profile-icon"
                    dark
                    @click="deleteImage(Image._id,editedItem._id)"
                  >close</v-icon>
                </v-flex>
                <v-flex v-for="(file, index) in editFiles" :key="index">
                  <img :src="file" height="160" width="160" class="pa-2" />
                </v-flex>
              </v-layout>
              <img
                src="/static/icon/user.png"
                v-if="editedItem.imageUrl == '' && editFiles == ''"
                height="160"
                width="160"
                alt="Gallery"
              />
            </v-flex>
          </v-layout>
          <v-layout wrap>
            <v-flex xs12 sm12>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right">Title:</label>
                </v-flex>
                <v-flex xs8 sm6 class="ml-3">
                  <v-text-field
                    v-model="editedItem.title"
                    placeholder="fill your Title"
                    name="name"
                    type="text"
                    required
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12 sm12>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right">Description:</label>
                </v-flex>
                <v-flex xs8 sm6 class="ml-3">
                  <v-text-field
                    placeholder="fill your Description"
                    v-model="editedItem.description"
                    type="text"
                    name="email"
                    required
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-layout>
              <v-flex xs4 class="pt-4 subheading">
                <label class="right hidden-xs-only hidden-sm-only">Select Class:</label>
                <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Class:</label>
              </v-flex>
              <v-flex xs8 sm6 class="ml-3">
                <v-select
                  :items="addclass"
                  label="Select Class"
                  v-model="editedItem.classNum"
                  item-text="classNum"
                  item-value="_id"
                  name="Select Class"
                  required
                ></v-select>
              </v-flex>
            </v-layout>
            <v-flex xs12>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right hidden-xs-only hidden-sm-only">Add New Images:</label>
                  <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Upload:</label>
                </v-flex>
                <v-flex xs8 sm6 class="ml-3">
                  <v-text-field
                    label="Select Image"
                    @click="editPickFile"
                    v-model="editImageName"
                    append-icon="attach_file"
                    multiple
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
          </v-layout>
          <v-flex xs12>
            <div
              v-for="(editImage,index) in editedItem.youTubeLinkUrl"
              :key="index"
              v-if="editImage.youTubeLink !=[]"
            >
              <v-layout wrap>
                <v-flex xs12 sm12 md4 class="pt-4 subheading">
                  <label class="right hidden-xs-only hidden-sm-only">You Tube Link Url:</label>
                  <label class="hidden-md-only hidden-lg-only hidden-xl-only">You Tube Link Url:</label>
                </v-flex>
                <v-layout>
                  <v-flex xs10 sm10 sm5 md9 class="ml-3">
                    <v-text-field v-model="editImage.youTubeLink" type="text" name="link" required></v-text-field>
                  </v-flex>
                  <v-flex xs2 class="pt-4">
                    <v-icon @click="deleteUrl(index,editImage._id,editedItem._id)">cancel</v-icon>
                  </v-flex>
                </v-layout>
              </v-layout>
            </div>
          </v-flex>
          <v-flex xs12>
            <div v-for="(updateImage,index) in updates" :key="index">
              <v-layout wrap>
                <v-flex xs12 sm12 md4 class="pt-4 subheading">
                  <label class="right hidden-xs-only hidden-sm-only">You Tube Url:</label>
                  <label class="hidden-md-only hidden-lg-only hidden-xl-only">You Tube Url:</label>
                </v-flex>
                <v-layout>
                  <v-flex xs10 sm12 md9 class="ml-3">
                    <v-text-field
                      v-model="updateImage.youTubeLink"
                      label="Upload new you tube link url"
                      required
                    ></v-text-field>
                  </v-flex>
                  <v-flex xs2 class="pt-4">
                    <v-icon @click="deleteUpdate(index)" v-if="index !=0">cancel</v-icon>
                    <v-icon @click="update">add_circle</v-icon>
                  </v-flex>
                </v-layout>
              </v-layout>
            </div>
          </v-flex>
          <v-layout>
            <v-flex xs12 sm12 md11 lg11>
              <v-card-actions class="hidden-xs-only hidden-sm-only">
                <v-spacer></v-spacer>
                <v-btn
                  round
                  dark
                  @click="save"
                  :loading="editGalleryLoading"
                  class="add-button mr-4"
                >Save</v-btn>
              </v-card-actions>
              <v-card-actions class="hidden-md-only hidden-lg-only hidden-xl-only">
                <v-spacer></v-spacer>
                <v-btn
                  round
                  dark
                  @click="save"
                  :loading="editGalleryLoading"
                  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 Gallery ******  -->

    <v-dialog v-model="viewProfileGallery" max-width="1000" scrollable persistent>
      <v-card flat class="card-style pa-3" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">View Gallery</label>
            <v-icon size="24" class="right" @click="closeProfileGallery">cancel</v-icon>
          </v-flex>
        </v-layout>
        <v-card-text>
          <v-layout row wrap>
            <v-flex xs12 class="text-xs-center text-sm-center text-md-center text-lg-center my-4">
              <v-avatar size="100" v-if="editedItem.imageUrl == ''" class="mt-4 pa-2">
                <img src="/static/icon/user.png" width="240" height="180" />
              </v-avatar>
              <span
                v-for="(image,i) in editedItem.imageUrl"
                :key="i"
                class="mt-4 pa-2"
                v-if="editedItem.imageUrl"
              >
                <img :src="image.imageLink" alt="Gallery" width="240" height="180" class="imgNews" />
              </span>
            </v-flex>
          </v-layout>
          <v-container grid-list-md>
            <v-layout wrap>
              <v-flex xs12>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1">
                      <b>Title:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.title }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1">
                      <b>Description:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1">{{ editedItem.description }}</h5>
                  </v-flex>
                </v-layout>
                <v-layout>
                  <v-flex xs5 sm6>
                    <h5 class="right my-1">
                      <b>You Tube Link Url:</b>
                    </h5>
                  </v-flex>
                  <v-flex sm6 xs8>
                    <h5 class="my-1 ml-3">
                      <ul v-for="youTubeLinkUrl in editedItem.youTubeLinkUrl">
                        <li>{{ youTubeLinkUrl.youTubeLink }}</li>
                      </ul>
                    </h5>
                  </v-flex>
                </v-layout>
              </v-flex>
            </v-layout>
          </v-container>
        </v-card-text>
      </v-card>
    </v-dialog>

    <!-- ****** Gallery TABLE ****** -->

    <v-toolbar color="transparent" flat>
      <v-btn
        fab
        dark
        class="open-dialog-button hidden-xl-only hidden-md-only hidden-lg-only"
        small
        @click="addGalleryDialog = true"
      >
        <v-icon dark>add</v-icon>
      </v-btn>
      <v-btn
        round
        class="open-dialog-button hidden-sm-only hidden-xs-only"
        dark
        @click="addGalleryDialog = true"
      >
        <v-icon class="white--text pr-1" size="20">add</v-icon>Add Gallery
      </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 md3 lg2 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="desserts"
      :pagination.sync="pagination"
      :search="search"
    >
      <template slot="items" slot-scope="props">
        <tr class="tr">
          <td class="td td-row">{{ props.index + 1}}</td>
          <td class="td td-row text-xs-center">{{ props.item.title}}</td>
          <td class="td td-row text-xs-center">{{ props.item.description}}</td>
          <td class="td td-row text-xs-center">
            <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>
        </tr>
      </template>
      <v-alert
        slot="no-results"
        :value="true"
        color="error"
        icon="warning"
      >Your search for "{{ search }}" found no results.</v-alert>
    </v-data-table>
    <!-- ****** ADD Gallery  ****** -->
    <v-dialog v-model="addGalleryDialog" max-width="600px" v-if="addGalleryDialog" persistent>
      <v-card flat class="card-style pa-2" dark>
        <v-layout>
          <v-flex xs12>
            <label class="title text-xs-center">Add Gallery</label>
            <v-icon size="24" class="right" @click="closeAddGalleryModel">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">
                <input
                  type="file"
                  style="display: none"
                  ref="image"
                  accept="image/*"
                  multiple
                  @change="onFilePicked"
                />
                <v-layout justify-center>
                  <v-flex v-for="(file,index) in files" :key="index" v-if="files">
                    <img :src="file" height="150" width="150" />
                  </v-flex>
                </v-layout>
                <img src="/static/icon/user.png" v-if="files ==''" height="100" width="100;" />
              </v-flex>
            </v-layout>
            <v-flex xs12>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right">Title:</label>
                </v-flex>
                <v-flex xs8 sm8 md7 class="ml-3">
                  <v-text-field
                    v-model="addGallery.title"
                    placeholder="fill your Title"
                    name="name"
                    type="text"
                    :rules="titleRules"
                    required
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right">Description:</label>
                </v-flex>
                <v-flex xs8 sm8 md7 class="ml-3">
                  <v-text-field
                    placeholder="fill your Description"
                    :rules="descriptionRules"
                    v-model="addGallery.description"
                    type="text"
                    name="email"
                    required
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12>
              <v-layout>
                <v-flex xs4 class="pt-4 subheading">
                  <label class="right hidden-xs-only hidden-sm-only">Select Class:</label>
                  <label class="right hidden-lg-only hidden-md-only hidden-xl-only">Class:</label>
                </v-flex>
                <v-flex xs8 sm8 md7 class="ml-3">
                  <v-select
                    :items="addclass"
                    label="Select Class"
                    v-model="addGallery.classNum"
                    item-text="classNum"
                    item-value="_id"
                    name="Select Class"
                    required
                  ></v-select>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-flex xs12>
              <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 sm8 md7 class="ml-3">
                  <v-text-field
                    label="Select Image"
                    @click="pickFile"
                    v-model="imageName"
                    append-icon="attach_file"
                    multiple
                  ></v-text-field>
                </v-flex>
              </v-layout>
            </v-flex>
            <v-layout>
              <v-flex xs12>
                <div v-for="(youTubeLink,index) in finds" :key="index">
                  <v-layout>
                    <v-flex xs4 class="pt-4 subheading">
                      <label class="right">You Tube Url:</label>
                    </v-flex>
                    <v-flex xs8 sm8 md6 class="ml-3">
                      <v-text-field
                        placeholder="fill your youtube link"
                        v-model="youTubeLink.value"
                        type="text"
                        name="link"
                        required
                      ></v-text-field>
                    </v-flex>
                    <v-flex xs2 class="pt-4">
                      <v-icon @click="deleteFind(index)" v-if="index !=0 ">cancel</v-icon>
                      <v-icon @click="addFind">add_circle</v-icon>
                    </v-flex>
                  </v-layout>
                </div>
              </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>
    <v-snackbar
      :timeout="timeout"
      :top="y === 'top'"
      :right="x === 'right'"
      :vertical="mode === 'vertical'"
      v-model="snackbar"
      :color="color"
    >{{ text }}</v-snackbar>
    <div class="loader" v-if="showLoader">
      <v-progress-circular indeterminate color="white"></v-progress-circular>
    </div>
  </v-container>
</template>

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

export default {
  data: () => ({
    snackbar: false,
    y: "top",
    x: "right",
    mode: "",
    timeout: 3000,
    text: "",
    loading: false,
    color: "",
    date: null,
    search: "",
    show: true,
    addGalleryDialog: false,
    showSearch: false,
    showLoader: false,
    editGalleryDialog: false,
    viewProfileGallery: false,
    valid: true,
    editGalleryLoading: false,
    addclass: [],
    addSection: [],
    finds: [{ value: "" }],
    updates: [{ youTubeLink: "" }],
    youTubeLink: {},
    addGallery: {
      youTubeLinkUrl: [],
    },
    pagination: {
      rowsPerPage: 10,
    },
    imageName: "",
    imageUrl: "",
    imageFile: "",
    image: [],
    upload: "",
    editImageName: "",
    editFiles: [],
    files: [],
    token: "",
    titleRules: [(v) => !!v || " Tilte is required"],
    descriptionRules: [(v) => !!v || " Description is required"],
    headers: [
      {
        align: "",
        text: "No",
        sortable: false,
        value: "index",
      },
      { text: "Title", value: "title", sortable: false, align: "center" },
      {
        text: "Description",
        value: "description",
        sortable: false,
        align: "center",
      },
      { text: "Action", value: "", sortable: false, align: "center" },
    ],
    desserts: [],
    editedIndex: -1,
    editedItem: {},
    index: "",
  }),
  watch: {
    addGalleryDialog: function (val) {
      if (!val) {
        this.addGallery = [];
        this.imageName = "";
        this.youTubeLinkUrl = [];
        this.youTubeLink = {};
        this.index = "";
        this.files = [];
      }
    },
  },
  methods: {
    pickFile() {
      this.$refs.image.click();
    },
    editPickFile() {
      this.$refs.editImage.click();
    },
    onFilePicked(e) {
      const files = e.target.files;
      /** fetch Image Name **/
      if (files[0] !== undefined) {
        this.imageName = files[0].name;
        if (this.imageName.lastIndexOf(".") <= 0) {
          return;
        }
        this.files = [];
        // console.log("files", this.files);
        /** Select many image and showing many image add to Gallery card **/
        const test = Array.from(files).forEach((file, idx) => {
          const fr = new FileReader();
          const getResult = new Promise((resolve) => {
            fr.onload = (e) => {
              this.files.push(
                // id: idx,
                e.target.result
              );
            };
            console.log("uploadImage=======>", this.files);
          });
          fr.readAsDataURL(file);
          return getResult.then((file) => {
            return file;
          });
        });
        const fr = new FileReader();
        fr.readAsDataURL(files[0]);
        fr.addEventListener("load", () => {
          this.imageFile = files; // this is an image file that can be sent to server...
        });
      } else {
        this.imageName = "";
        this.imageFile = "";
        this.imageUrl = "";
      }
    },
    editFilePicked(e) {
      const files = e.target.files;
      /** fetch Image Name **/
      if (files[0] !== undefined) {
        this.editImageName = files[0].name;
        if (this.editImageName.lastIndexOf(".") <= 0) {
          return;
        }
        /** Select many image and showing many image add to Gallery card **/
        const test = Array.from(files).forEach((file, idx) => {
          const fr = new FileReader();
          const getResult = new Promise((resolve) => {
            fr.onload = (e) => {
              this.editFiles.push(
                // id: idx,
                e.target.result
              );
            };
            console.log("uploadeditFilesImage=======>", this.editFiles);
          });
          fr.readAsDataURL(file);
          return getResult.then((file) => {
            return file;
          });
        });
        const fr = new FileReader();
        fr.readAsDataURL(files[0]);
        // fr.addEventListener("load", () => {
        //   this.imageFile = files; // this is an image file that can be sent to server...
        // });
      } else {
        this.editImageName = "";
        this.editFiles = [];
      }
    },
    getGalleryList() {
      this.showLoader = true;
      http()
        .get("/getGalleryList", {
          headers: { Authorization: "Bearer " + this.token },
        })
        .then((response) => {
          this.desserts = response.data.data;
          this.showLoader = false;
        })
        .catch((error) => {
          // console.log("err====>", err);
          this.showLoader = false;
          if (error.response.status === 401) {
            this.$router.replace({ path: "/" });
            this.$store.dispatch("setToken", null);
            this.$store.dispatch("Id", null);
          }
        });
    },
    editItem(item) {
      this.files = [];
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.editGalleryDialog = true;
    },
    profile(item) {
      this.editedIndex = this.desserts.indexOf(item);
      this.editedItem = Object.assign({}, item);
      this.viewProfileGallery = true;
    },
    deleteItem(item) {
      let deleteGallery = {
        galleryId: item._id,
      };
      http()
        .delete(
          "/deleteGallery",
          confirm("Are you sure you want to delete this?") && {
            params: deleteGallery,
            headers: {
              Authorization: "Bearer " + this.token,
            },
          }
        )
        .then((response) => {
          this.snackbar = true;
          this.text = "Successfully delete Existing Gallery";
          this.color = "green";
          this.getGalleryList();
        })
        .catch((error) => {
          // console.log(error);
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    deleteImage(imageId, id) {
      let deleteImages = {
        galleryId: id,
        imageId: imageId,
      };
      http()
        .put("/deleteImage", deleteImages)
        .then((response) => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.getGalleryList();
          this.close();
        })
        .catch((error) => {
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    close() {
      this.editGalleryDialog = false;
    },
    closeProfileGallery() {
      this.viewProfileGallery = false;
    },
    closeAddGalleryModel() {
      this.addGalleryDialog = false;
      this.addGallery = [];
      this.imageName = "";
      this.youTubeLinkUrl = [];
      this.youTubeLink = {};
      this.index = "";
      this.files = [];
    },
    submit() {
      for (let i = 0; i < this.finds.length; i++) {
        this.addGallery.youTubeLinkUrl.push(this.finds[i].value);
      }
      if (this.$refs.form.validate()) {
        if (this.files) {
          var ary = [];
          var imageData = [];
          ary = this.files;
          for (let i = 0; i < ary.length; i++) {
            const [baseUrl, imageUrl] = ary[i].split(/,/);
            imageData.push(imageUrl);
            this.addGallery.upload = imageData;
          }
        }
        if (this.addGallery.youTubeLinkUrl == "") {
          var galleryData = {
            classId: this.addGallery.classNum,
            title: this.addGallery.title,
            description: this.addGallery.description,
            upload: this.addGallery.upload,
          };
          this.loading = true;
          http()
            .post("/createGallery", galleryData)
            .then((response) => {
              this.getGalleryList();
              this.addGalleryDialog = false;
              this.loading = false;
              this.snackbar = true;
              this.text = response.data.message;
              this.color = "green";
              this.clear();
              this.files = "";
            })
            .catch((error) => {
              this.snackbar = true;
              this.text = error.response.data.message;
              this.color = "error";
              this.loading = false;
            });
        } else if (this.addGallery.youTubeLinkUrl) {
          this.loading = true;
          http()
            .post("/createGallery", this.addGallery)
            .then((response) => {
              this.getGalleryList();
              this.addGalleryDialog = false;
              this.files = "nmn";
              this.loading = false;
              this.snackbar = true;
              this.color = "green";
              this.text = response.data.message;
              this.clear();
              this.removeAddFind();
              this.files = "";
            })
            .catch((error) => {
              this.snackbar = true;
              this.text = error.response.data.message;
              this.loading = false;
              this.color = "error";
            });
        }
      }
    },
    clear() {
      this.$refs.form.reset();
      this.files = [];
    },
    save() {
      this.editedItem.gelleryId = this.editedItem._id;
      var linkUrl = [];
      for (let i = 0; i < this.updates.length; i++) {
        linkUrl.push(this.updates[i].youTubeLink);
      }
      if (this.editFiles) {
        var ary = [];
        var imageData = [];
        ary = this.editFiles;
        for (let i = 0; i < ary.length; i++) {
          const [baseUrl, imageUrl] = ary[i].split(/,/);
          imageData.push(imageUrl);
          this.editedItem.editFiles = imageData;
        }
      }
      if (linkUrl != "") {
        var updateData = {
          galleryId: this.editedItem._id,
          description: this.editedItem.description,
          classId: this.editedItem.classNum,
          uploadImage: this.editedItem.editFiles,
          title: this.editedItem.title,
          updateYouTubeLinkUrl: linkUrl,
        };
      } else if (linkUrl == "") {
        var updateData = {
          galleryId: this.editedItem._id,
          description: this.editedItem.description,
          classId: this.editedItem.classNum,
          uploadImage: this.editedItem.editFiles,
          title: this.editedItem.title,
        };
      }
      this.editGalleryLoading = true;
      http()
        .put("/updateGallery", updateData)
        .then((response) => {
          this.getGalleryList();
          this.close();
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.editGalleryLoading = false;
          this.editImageName = "";
          this.editFiles = [];
        })
        .catch((error) => {
          this.editGalleryLoading = false;
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    getAllClasses() {
      http()
        .get("/getClassesList", {
          headers: { Authorization: "Bearer " + this.token },
        })
        .then((response) => {
          this.addclass = response.data.data;
        })
        .catch((err) => {
          // console.log("err====>", err);
        });
    },
    removeAddFind: function () {
      this.finds = [{ value: "" }];
    },
    addFind: function () {
      this.finds.push({ value: "" });
    },
    update: function () {
      this.updates.push({ youTubeLink: "" });
    },
    deleteFind: function (index) {
      this.finds.splice(index, 1);
      if (index === 0) this.addFind();
    },
    deleteUpdate: function (index) {
      this.updates.splice(index, 1);
      if (index === 0) this.update();
    },
    deleteUrl: function (index, youTubelinkId, id) {
      this.editedItem.youTubeLinkUrl.splice(index, 1);
      if (index === 0) this.update();
      let deleteYouTubeUrl = {
        galleryId: id,
        youTubeId: youTubelinkId,
      };
      http()
        .put("/deleteYoutubeLink", deleteYouTubeUrl)
        .then((response) => {
          this.snackbar = true;
          this.text = response.data.message;
          this.color = "green";
          this.getGalleryList();
          this.close();
        })
        .catch((error) => {
          this.snackbar = true;
          this.text = error.response.data.message;
          this.color = "error";
        });
    },
    displaySearch() {
      this.show = false;
      this.showSearch = true;
    },
    closeSearch() {
      this.showSearch = false;
      this.show = true;
      this.search = "";
    },
  },
  mounted() {
    this.token = this.$store.state.token;
    this.getAllClasses();
    this.getGalleryList();
  },
};
</script>