<template>
  <div>
    <v-toolbar dark>
      <v-spacer />
      <v-toolbar-title>콘텐츠 리스트</v-toolbar-title>
      <v-spacer />
    </v-toolbar>

    <br>

    <v-container>
      <v-row>
        <v-col cols="10" />
        <v-col>
          <v-btn
            block
            color="#9970ff"
            dark
            depressed
            @click="showWriteDialog"
          >
            콘텐츠 등록
          </v-btn>
        </v-col>
      </v-row>
      <v-row>
        <v-col>
          <v-simple-table class="elevation-10">
            <template>
              <thead>
                <tr>
                  <th class="text-center">
                    썸네일
                  </th>
                  <th class="text-center">
                    제목
                  </th>
                  <th class="text-center">
                    내용
                  </th>
                  <th class="text-center">
                    크리에이터
                  </th>
                  <th class="text-center">
                    테마
                  </th>
                  <th class="text-center">
                    태그
                  </th>
                  <th class="text-center">
                    가격
                  </th>
                  <th class="text-center">
                    한정판
                  </th>
                  <th class="text-center">
                    19금
                  </th>
                  <th class="text-center">
                    시간
                  </th>
                  <th class="text-center">
                    듣기
                  </th>
                  <th class="text-center">
                    등록일
                  </th>
                  <th class="text-center">
                    오픈 예정일
                  </th>
                  <th class="text-center">
                    관리
                  </th>
                </tr>
              </thead>
              <tbody>
                <tr
                  v-for="item in audio_contents"
                  :key="item.audioContentId"
                >
                  <td align="center">
                    <v-img
                      max-width="100"
                      max-height="100"
                      :src="item.coverImageUrl"
                    />
                  </td>
                  <td>{{ item.title }}</td>
                  <td style="max-width: 350px !important; word-break:break-all; height: auto;">
                    {{ item.detail }}
                  </td>
                  <td>{{ item.creatorNickname }}</td>
                  <td>{{ item.theme }}</td>
                  <td style="max-width: 200px !important; word-break:break-all; height: auto;">
                    {{ item.tags }}
                  </td>
                  <td v-if="item.price > 0">
                    {{ item.price }} 캔
                  </td>
                  <td v-else>
                    무료
                  </td>
                  <td
                    v-if="item.totalContentCount > 0 && item.remainingContentCount > 0"
                    style="min-width: 100px !important; word-break:break-all; height: auto;"
                  >
                    {{ item.totalContentCount - item.remainingContentCount }} / {{ item.totalContentCount }}
                  </td>
                  <td
                    v-else-if="item.totalContentCount > 0 && item.remainingContentCount <= 0"
                    style="min-width: 100px !important; word-break:break-all; height: auto;"
                  >
                    Sold Out
                  </td>
                  <td v-else>
                    X
                  </td>
                  <td>
                    <div v-if="item.isAdult">
                      O
                    </div>
                    <div v-else>
                      X
                    </div>
                  </td>
                  <td>{{ item.remainingTime }}</td>
                  <td>
                    <vuetify-audio
                      :file="item.contentUrl"
                      :downloadable="true"
                      :auto-play="false"
                    />
                  </td>
                  <td>{{ item.date }}</td>
                  <td>{{ item.releaseDate }}</td>
                  <td>
                    <v-row>
                      <v-col>
                        <v-btn
                          :disabled="is_loading"
                          @click="showModifyDialog(item)"
                        >
                          수정
                        </v-btn>
                      </v-col>
                      <v-spacer />
                      <v-col>
                        <v-btn
                          :disabled="is_loading"
                          @click="deleteConfirm(item)"
                        >
                          삭제
                        </v-btn>
                      </v-col>
                    </v-row>
                  </td>
                </tr>
              </tbody>
            </template>
          </v-simple-table>
        </v-col>
      </v-row>
      <v-row class="text-center">
        <v-col>
          <v-pagination
            v-model="page"
            :length="total_page"
            circle
            @input="next"
          />
        </v-col>
      </v-row>
    </v-container>

    <v-row>
      <v-dialog
        v-model="show_modify_dialog"
        max-width="1000px"
        persistent
      >
        <v-card>
          <v-card-title>
            콘텐츠 수정
          </v-card-title>
          <div class="image-select">
            <label for="image">
              커버 이미지 등록
            </label>
            <v-file-input
              id="image"
              v-model="audio_content.cover_image"
              accept="image/*"
              @change="imageAdd"
            />
          </div>
          <img
            v-if="audio_content.cover_image_url"
            :src="audio_content.cover_image_url"
            alt=""
            class="image-preview"
          >
          <v-card-text>
            <v-row align="center">
              <v-col cols="4">
                제목
              </v-col>
              <v-col cols="8">
                <v-text-field
                  v-model="audio_content.title"
                  label="제목"
                  required
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row align="center">
              <v-col cols="4">
                내용
              </v-col>
              <v-col cols="8">
                <v-textarea
                  v-model="audio_content.detail"
                  label="내용"
                  required
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row>
              <v-col cols="4">
                연령제한
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="audio_content.is_adult"
                  type="checkbox"
                >
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row>
              <v-col cols="4">
                댓글 가능
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="audio_content.is_comment_available"
                  type="checkbox"
                >
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-actions v-show="!is_loading">
            <v-spacer />
            <v-btn
              color="blue darken-1"
              text
              @click="cancel"
            >
              취소
            </v-btn>
            <v-btn
              color="blue darken-1"
              text
              @click="modify"
            >
              수정
            </v-btn>
          </v-card-actions>
        </v-card>
      </v-dialog>
    </v-row>

    <v-row>
      <v-dialog
        v-model="show_create_dialog"
        max-width="1000px"
        persistent
      >
        <v-card>
          <v-card-title>
            콘텐츠 등록
          </v-card-title>
          <div class="image-select">
            <label for="image">
              커버 이미지 등록
            </label>
            <v-file-input
              id="image"
              v-model="audio_content.cover_image"
              accept="image/*"
              @change="imageAdd"
            />
          </div>
          <img
            v-if="audio_content.cover_image_url"
            :src="audio_content.cover_image_url"
            alt=""
            class="image-preview"
          >
          <div class="content-select">
            <label for="content">
              콘텐츠 등록
            </label>
            <v-file-input
              id="content"
              v-model="audio_content.content_file"
              accept="audio/*"
              @change="contentAdd"
            />
            <v-row
              v-if="audio_content.content_file_name !== undefined"
              align="center"
            >
              <v-col cols="4" />
              <v-col cols="8">
                <v-text-field
                  v-model="audio_content.content_file_name"
                  label=""
                  disabled
                />
              </v-col>
            </v-row>
          </div>
          <v-card-text>
            <v-row align="center">
              <v-col cols="4">
                제목
              </v-col>
              <v-col cols="8">
                <v-text-field
                  v-model="audio_content.title"
                  label="제목"
                  required
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row align="center">
              <v-col cols="4">
                내용
              </v-col>
              <v-col cols="8">
                <v-textarea
                  v-model="audio_content.detail"
                  label="내용"
                  required
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row align="center">
              <v-col cols="4">
                태그
              </v-col>
              <v-col cols="8">
                <v-text-field
                  v-model="audio_content.tags"
                  label="예 : #연애 #커버곡 #태그"
                  required
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row align="center">
              <v-col cols="4">
                테마
              </v-col>
              <v-col cols="8">
                <v-select
                  v-model="audio_content.theme_id"
                  :items="themeList"
                  item-text="name"
                  item-value="value"
                  label="테마 선택"
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text v-show="is_limited_edition === false">
            <v-row align="center">
              <v-col cols="4">
                예약 공개
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="is_active_reservation"
                  type="checkbox"
                >
              </v-col>
            </v-row>
            <v-row
              v-if="is_active_reservation"
              align="center"
            >
              <v-col cols="4">
                예약 날짜와 시간
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <DatePicker
                  v-model="reservation_date"
                  value-type="format"
                />
                &nbsp;&nbsp;
                <vue-timepicker
                  v-model="reservation_time"
                  format="HH:mm"
                  input-class="skip-error-style"
                  :minute-interval="15"
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row align="center">
              <v-col cols="4">
                가격
              </v-col>
              <v-col cols="8">
                <v-text-field
                  v-model="audio_content.price"
                  label="가격"
                  required
                />
                ※ 유료 콘텐츠 가격을 입력하면 해당 콘텐츠의 미리듣기 시간을 설정할 수 있습니다.<br>미리듣기 시간은 최소 30초 이상 설정해야 합니다.
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text v-show="audio_content.price > 0">
            <v-row align="center">
              <v-col cols="4">
                미리듣기 생성
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="audio_content.is_generate_preview"
                  type="checkbox"
                >
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text v-show="audio_content.price > 0">
            <v-row align="center">
              <v-col cols="4">
                미리듣기 시간설정
              </v-col>
              <v-col cols="8">
                <v-row align="center">
                  <v-col cols="5">
                    <v-text-field
                      v-model="audio_content.preview_start_time"
                      label="시작시간(00:00:00)"
                    />
                  </v-col>
                  <v-col cols="2">
                    ~
                  </v-col>
                  <v-col cols="5">
                    <v-text-field
                      v-model="audio_content.preview_end_time"
                      label="종료시간(00:00:30)"
                    />
                  </v-col>
                </v-row>
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text v-show="audio_content.price > 0">
            <v-row align="center">
              <v-col cols="4">
                한정판
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="is_limited_edition"
                  type="checkbox"
                >
              </v-col>
            </v-row>
            <v-row
              v-if="is_limited_edition"
              align="center"
            >
              <v-col cols="4">
                판매할 콘텐츠 개수
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <v-text-field
                  v-model="audio_content.limited"
                  label="판매할 콘텐츠 개수"
                />
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text v-show="audio_content.price > 0 && is_limited_edition === false">
            <v-row>
              <v-col cols="4">
                대여만 가능
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="audio_content.is_only_rental"
                  type="checkbox"
                >
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row>
              <v-col cols="4">
                연령제한
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="audio_content.is_adult"
                  type="checkbox"
                >
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-text>
            <v-row>
              <v-col cols="4">
                댓글 가능
              </v-col>
              <v-col
                cols="8"
                align="left"
              >
                <input
                  v-model="audio_content.is_comment_available"
                  type="checkbox"
                >
              </v-col>
            </v-row>
          </v-card-text>
          <v-card-actions v-show="!is_loading">
            <v-spacer />
            <v-btn
              color="blue darken-1"
              text
              @click="cancel"
            >
              취소
            </v-btn>
            <v-btn
              color="blue darken-1"
              text
              @click="save"
            >
              등록
            </v-btn>
          </v-card-actions>
        </v-card>
      </v-dialog>
    </v-row>

    <v-dialog
      v-model="show_delete_confirm_dialog"
      max-width="400px"
      persistent
    >
      <v-card>
        <v-card-text />
        <v-card-text>
          "{{ selected_audio_content.title }}"을 삭제하시겠습니까?
        </v-card-text>
        <v-card-actions v-show="!is_loading">
          <v-spacer />
          <v-btn
            color="blue darken-1"
            text
            @click="deleteCancel"
          >
            취소
          </v-btn>
          <v-btn
            color="blue darken-1"
            text
            @click="deleteAudioContent"
          >
            확인
          </v-btn>
        </v-card-actions>
      </v-card>
    </v-dialog>

    <v-dialog
      v-model="is_loading"
      max-width="400px"
      persistent
    >
      <v-card>
        <v-card-text>
          <v-progress-circular
            indeterminate
          />
        </v-card-text>
      </v-card>
    </v-dialog>
  </div>
</template>

<script>
import * as api from '@/api/audio_content'
import VuetifyAudio from 'vuetify-audio'
// Main JS (in UMD format)
import VueTimepicker from 'vue2-timepicker'
import DatePicker from 'vue2-datepicker';

import 'vue2-datepicker/index.css';
// CSS
import 'vue2-timepicker/dist/VueTimepicker.css'

export default {
  name: "AudioContentList",

  components: {VuetifyAudio, VueTimepicker, DatePicker},

  data() {
    return {
      is_loading: false,
      is_active_reservation: false,
      is_limited_edition: false,
      reservation_date: '',
      reservation_time: '',
      show_create_dialog: false,
      show_modify_dialog: false,
      show_delete_confirm_dialog: false,
      page: 1,
      total_page: 0,
      search_word: '',
      audio_content: {
        price: 0,
        is_adult: false,
        is_generate_preview: true,
        is_comment_available: true,
      },
      audio_contents: [],
      themeList: [],
      selected_audio_content: {},
      utm_source: '',
      utm_medium: '',
      utm_campaign: '',
    }
  },

  async created() {
    await this.getAudioContentThemeList()
    await this.getAudioContent()
  },

  methods: {
    notifyError(message) {
      this.$dialog.notify.error(message)
    },

    notifySuccess(message) {
      this.$dialog.notify.success(message)
    },

    deleteConfirm(item) {
      this.selected_audio_content = item
      this.show_delete_confirm_dialog = true
    },

    deleteCancel() {
      this.selected_audio_content = {}
      this.show_delete_confirm_dialog = false
    },

    showWriteDialog() {
      this.show_create_dialog = true
      this.audio_content.price = '0'
    },

    showModifyDialog(item) {
      this.selected_audio_content = item

      this.audio_content.id = item.audioContentId
      this.audio_content.title = item.title
      this.audio_content.detail = item.detail
      this.audio_content.is_adult = item.isAdult
      this.audio_content.is_comment_available = item.isCommentAvailable
      this.audio_content.cover_image_url = item.coverImageUrl
      this.show_modify_dialog = true
    },

    cancel() {
      this.selected_audio_content = {}
      this.audio_content = {
        price: 0,
        is_adult: false,
        is_comment_available: true,
      }
      this.reservation_time = ''
      this.reservation_time = ''
      this.is_active_reservation = false
      this.is_limited_edition = false
      this.show_create_dialog = false
      this.show_modify_dialog = false
      this.show_delete_confirm_dialog = false
    },

    contentAdd(payload) {
      const file = payload;
      if (file) {
        this.audio_content.content_file_name = file.name
        URL.revokeObjectURL(file)
      } else {
        this.audio_content.content_file_name = null
      }
    },

    imageAdd(payload) {
      const file = payload;
      if (file) {
        this.audio_content.cover_image_url = URL.createObjectURL(file)
        URL.revokeObjectURL(file)
      } else {
        this.audio_content.cover_image_url = null
      }
    },

    async getAudioContentThemeList() {
      this.is_loading = true
      try {
        const res = await api.getAudioContentThemeList()
        if (res.status === 200 && res.data.success === true) {
          this.themeList = res.data.data.map((item) => {
            return {name: item.theme, value: item.id}
          })
        } else {
          this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        }

        this.is_loading = false
      } catch (e) {
        this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        this.is_loading = false
      }
    },

    isValidTimeFormat(time) {
      // 정규 표현식을 사용하여 "HH:mm:ss" 형식인지 확인
      const regex = /^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/;
      return regex.test(time);
    },

    async save() {
      if (
        this.audio_content.title === null ||
        this.audio_content.title === undefined ||
        this.audio_content.title.trim().length <= 0
      ) {
        this.notifyError("제목을 입력하세요")
        return
      }

      if (
        this.audio_content.detail === null ||
        this.audio_content.detail === undefined ||
        this.audio_content.detail.trim().length <= 0
      ) {
        this.notifyError("내용을 입력하세요")
        return
      }

      if (
        this.audio_content.theme_id === null ||
        this.audio_content.theme_id === undefined ||
        this.audio_content.theme_id <= 0
      ) {
        this.notifyError("테마를 선택하세요")
        return
      }

      if (this.is_limited_edition === true) {
        if (isNaN(parseInt(this.audio_content.limited))) {
          this.notifyError("한정판 개수는 숫자만 입력가능합니다.")
          return
        } else if (this.audio_content.limited < 1) {
          this.notifyError("한정판 개수를 1 이상 입력해 주세요.")
          return
        }
      }

      if (this.is_loading) return;

      this.is_loading = true

      try {
        const formData = new FormData()
        const request = {
          title: this.audio_content.title,
          detail: this.audio_content.detail,
          tags: this.audio_content.tags,
          price: this.audio_content.price,
          themeId: this.audio_content.theme_id,
          isAdult: this.audio_content.is_adult,
          isGeneratePreview: this.audio_content.price > 0 ? this.audio_content.is_generate_preview : false,
          isOnlyRental: this.audio_content.is_only_rental,
          isCommentAvailable: this.audio_content.is_comment_available
        }

        if (this.audio_content.limited > 0) {
          request.isOnlyRental = false
        }

        if (this.audio_content.price > 0 && this.is_limited_edition === true) {
          request.limited = this.audio_content.limited
        }

        if (this.is_limited_edition === false && this.is_active_reservation === true) {
          if (this.reservation_date.trim() === '' || this.reservation_time.trim() === '') {
            this.notifyError("오픈 예정 날짜와 시간을 선택해 주세요.")
            this.is_loading = false
            return
          }

          request.releaseDate = this.reservation_date + ' ' + this.reservation_time
        }

        let previewStartTime = this.audio_content.preview_start_time;
        let previewEndTime = this.audio_content.preview_end_time;

        if (
          previewStartTime !== undefined &&
          previewStartTime !== null &&
          previewStartTime.trim() !== '' &&
          previewEndTime !== undefined &&
          previewEndTime !== null &&
          previewEndTime.trim() !== ''
        ) {
          if (
            !this.isValidTimeFormat(previewStartTime) ||
            !this.isValidTimeFormat(previewEndTime)
          ) {
            this.notifyError("미리 듣기 시간 형식은 00:30:00 과 같아야 합니다")
            this.is_loading = false
            return
          }

          // 시간을 분해하고 계산하는 나머지 코드는 이전과 동일합니다.
          const [h1, m1, s1] = previewStartTime.split(':').map(Number);
          const [h2, m2, s2] = previewEndTime.split(':').map(Number);
          const totalSeconds1 = h1 * 3600 + m1 * 60 + s1;
          const totalSeconds2 = h2 * 3600 + m2 * 60 + s2;
          const timeDifference = totalSeconds2 - totalSeconds1;

          if (timeDifference < 30) {
            this.notifyError("미리 듣기의 최소 시간은 30초 입니다.")
            this.is_loading = false
            return
          }

          request["previewStartTime"] = previewStartTime
          request["previewEndTime"] = previewEndTime
        } else {
          if (
            (previewStartTime !== undefined && previewStartTime.trim() !== '') ||
            (previewEndTime !== undefined && previewEndTime.trim() !== '')
          ) {
            this.notifyError("미리 듣기 시작 시간과 종료 시간 둘 다 입력을 하거나 둘 다 입력 하지 않아야 합니다.")
            this.is_loading = false
            return
          }
        }

        formData.append("coverImage", this.audio_content.cover_image)
        formData.append("contentFile", this.audio_content.content_file)
        formData.append("request", JSON.stringify(request))

        const res = await api.createAudioContent(formData)
        if (res.status === 200 && res.data.success === true) {
          this.is_loading = false
          this.cancel()
          this.notifySuccess("등록한 콘텐츠가 업로드 중입니다.\n" +
            "콘텐츠 등록이 완료되면 알림을 보내드립니다.\n" +
            "이 페이지를 나가도 콘텐츠는 자동으로 등록됩니다.")

          this.audio_contents = []
          await this.getAudioContent()
        } else {
          this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        }
      } catch (e) {
        this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
      } finally {
        this.is_loading = false
      }
    },

    async modify() {
      if (
        this.audio_content.title === null ||
        this.audio_content.title === undefined ||
        this.audio_content.title.trim().length <= 0
      ) {
        this.notifyError("제목을 입력하세요")
        return
      }

      if (
        this.audio_content.detail === null ||
        this.audio_content.detail === undefined ||
        this.audio_content.detail.trim().length <= 0
      ) {
        this.notifyError("내용을 입력하세요")
        return
      }

      if (this.is_loading) return;

      this.is_loading = true

      try {
        const formData = new FormData()
        const request = {id: this.audio_content.id}

        if (
          this.selected_audio_content.title !== this.audio_content.title &&
          this.audio_content.title.trim().length > 0
        ) {
          request.title = this.audio_content.title
        }

        if (
          this.selected_audio_content.detail !== this.audio_content.detail &&
          this.audio_content.detail.trim().length > 0
        ) {
          request.detail = this.audio_content.detail
        }

        if (this.selected_audio_content.curationId !== this.audio_content.curation_id) {
          request.curationId = this.audio_content.curation_id
        }

        if (this.selected_audio_content.isAdult !== this.audio_content.is_adult) {
          request.isAdult = this.audio_content.is_adult
        }

        if (this.selected_audio_content.isCommentAvailable !== this.audio_content.is_comment_available) {
          request.isCommentAvailable = this.audio_content.is_comment_available
        }

        if (this.audio_content.cover_image !== null) {
          formData.append("coverImage", this.audio_content.cover_image)
        }

        formData.append("request", JSON.stringify(request))

        const res = await api.modifyAudioContent(formData)
        if (res.status === 200 && res.data.success === true) {
          this.cancel()
          this.notifySuccess('수정되었습니다.')

          this.audio_contents = []
          await this.getAudioContent()
        } else {
          this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        }
      } catch (e) {
        this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
      } finally {
        this.is_loading = false
      }
    },

    async deleteAudioContent() {
      if (this.is_loading) return;
      this.is_loading = true

      try {
        let request = {id: this.selected_audio_content.audioContentId, isActive: false}

        const formData = new FormData()
        formData.append("request", JSON.stringify(request))

        const res = await api.modifyAudioContent(formData)
        if (res.status === 200 && res.data.success === true) {
          this.cancel()
          this.notifySuccess('삭제되었습니다.')

          this.audio_contents = []
          await this.getAudioContent()
        } else {
          this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        }
      } catch (e) {
        this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
      } finally {
        this.is_loading = false
      }
    },

    async next() {
      if (this.search_word.length < 2) {
        this.search_word = ''
        await this.getAudioContent()
      } else {
        await this.searchAudioContent()
      }
    },

    async getAudioContent() {
      this.is_loading = true
      try {
        const res = await api.getAudioContentList(this.page)
        if (res.status === 200 && res.data.success === true) {
          const data = res.data.data

          const total_page = Math.ceil(data.totalCount / 10)
          this.audio_contents = data.items

          if (total_page <= 0)
            this.total_page = 1
          else
            this.total_page = total_page
        } else {
          this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        }

        this.is_loading = false
      } catch (e) {
        this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
        this.is_loading = false
      }
    },

    async searchAudioContent() {
      if (this.search_word.length === 0) {
        await this.getAudioContent()
      } else if (this.search_word.length < 2) {
        this.notifyError('검색어를 2글자 이상 입력하세요.')
      } else {
        this.is_loading = true
        try {
          const res = await api.searchAudioContent(this.search_word, this.page)
          if (res.status === 200 && res.data.success === true) {
            const data = res.data.data

            const total_page = Math.ceil(data.totalCount / 10)
            this.audio_contents = data.items

            if (total_page <= 0)
              this.total_page = 1
            else
              this.total_page = total_page
          } else {
            this.notifyError(res.data.message || '알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
          }

          this.is_loading = false
        } catch (e) {
          this.notifyError('알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.')
          this.is_loading = false
        }
      }
    },
  }
}
</script>

<style scoped>
.content-select label {
  display: inline-block;
  padding: 10px 20px;
  background-color: #232d4a;
  color: #fff;
  vertical-align: middle;
  font-size: 15px;
  cursor: pointer;
  border-radius: 5px;
  margin-top: 30px;
}

.image-select label {
  display: inline-block;
  padding: 10px 20px;
  background-color: #232d4a;
  color: #fff;
  vertical-align: middle;
  font-size: 15px;
  cursor: pointer;
  border-radius: 5px;
}

.v-file-input {
  position: absolute;
  width: 0;
  height: 0;
  padding: 0;
  overflow: hidden;
  border: 0;
}

.image-preview {
  max-width: 100%;
  max-height: 250px;
  width: 250px;
  object-fit: cover;
  margin-top: 10px;
}
</style>
