<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { useMainStore } from "@/stores/main";
import { required } from "@/utils";
import { useI18n } from "vue-i18n";
import type { VideoCreate } from "@/interfaces";
import router from "@/router";

const { t } = useI18n();
const valid = ref(true);
const dialog = ref(false);
const title = ref("");
const zipFiles = ref();
const Form = ref();
let anchor = ref(0);
const anchorList = reactive([
  {
    anchor_id: 0,
    language_id: 1,
    name: "Angela",
  },
  {
    anchor_id: 1,
    language_id: 1,
    name: "Peggy",
  },
  {
    anchor_id: 2,
    language_id: 1,
    name: "Jocelyn",
  },
  {
    anchor_id: 3,
    language_id: 1,
    name: "Summer",
  },
]);

let anchorLang = ref("中文");
let items = reactive([
  { lang: "中文", id: 0 },
  { lang: "英文", id: 1 },
]);

// 取得圖片路徑
const getImageUrl = (name: string) => {
  return new URL(`../../assets/img/anchor/${name}.webp`, import.meta.url).href;
};

const mainStore = useMainStore();

watch(dialog, (newVal, oldVal) => {
  if (!newVal) {
    router.push("/main/progress");
  }
});

async function Submit() {
  setTimeout(() => {
    dialog.value = true;
  }, 2000);
  await (Form as any).value.validate();
  if (valid.value) {
    valid.value = false;

    const video_data: VideoCreate = {
      title: title.value,
      anchor_id: anchor.value,
      lang_id: 0,
    };

    await mainStore.uploadPlot(video_data, zipFiles.value[0]);
    // (Form as any).value.reset();
  }
}
</script>

<template>
  <v-container fluid>
    <v-card class="ma-3 pa-3">
      <v-card-title primary-title>
        <h3 class="card-title mb-3">{{ t("makeVideo") }}</h3>
      </v-card-title>
      <v-card-text>
        <v-form v-model="valid" ref="Form">
          <v-text-field
            :label="$t('videoTitle')"
            v-model="title"
            :rules="required"
            prepend-icon="title"
          >
          </v-text-field>
          <v-file-input
            v-model="zipFiles"
            :rules="[(v) => v.length || 'select zip file.']"
            accept=".zip"
            :label="$t('fileInput')"
            prepend-icon="folder_zip"
          ></v-file-input>

          <!-- <v-select
            v-model="anchorLang"
            :items="items"
            item-title="lang"
            item-value="id"
            prepend-icon="language"
            label="選擇語言"
          ></v-select> -->
        </v-form>

        <!-- <v-expansion-panels class="anchor-list">
          <v-expansion-panel title="選擇主播">
            <v-expansion-panel-text class="p-0">
              <v-sheet class="mx-auto">
                <v-slide-group
                  v-model="anchor"
                  selected-class="bg-success"
                  show-arrows
                >
                  <v-slide-group-item
                    v-for="n in anchorList"
                    :key="n.anchor_id"
                    v-slot="{ isSelected, toggle, selectedClass }"
                  >
                    <v-card
                      color="grey-lighten-1"
                      :class="['ma-4', selectedClass]"
                      @click="toggle"
                      :title="n.name"
                    >
                      <img :src="getImageUrl(n.name)" alt="" />
                    </v-card>
                  </v-slide-group-item>
                </v-slide-group>
              </v-sheet>
            </v-expansion-panel-text>
          </v-expansion-panel>
        </v-expansion-panels> -->
      </v-card-text>
      <v-card-actions>
        <v-spacer></v-spacer>
        <v-btn @click="Submit" :disabled="!valid">
          {{ t("send") }}
        </v-btn>
      </v-card-actions>
    </v-card>

    <v-card class="ma-3 pa-3 mt-8">
      <v-card-title primary-title>
        <h3 class="text-center">使用教學</h3>
      </v-card-title>
      <v-card-text>
        <ul class="mt-5 step-list">
          <li>
            <h4>1. 取得快速製作模板</h4>
            <p class="excerpt">請點擊下方按鈕取得模板範例</p>
            <div class="mb-5">
              <a :href="'/example/影片範例.zip'" class="link-btn" download
                >點我下載</a
              >
            </div>
          </li>
          <li>
            <h4>2. 準備影片內容</h4>
            <p class="excerpt text-center">
              範例的資料夾內，有 "素材資料夾" 跟 "EXCEL 檔" <br />
              （您也可以自行創建資料夾）
            </p>
            <img src="@/assets/img/step/step-01.png" alt="" class="mb-4" />
            <p class="excerpt">素材資料夾裡面放照片或影片</p>
            <img src="@/assets/img/step/step-02.png" alt="" />
            <small class="d-block ms-4"
              >包含內容：圖片／影片（.jpg／.mp4）</small
            >
            <p class="mt-5 excerpt">
              EXCEL 檔整理成這個格式－大標、字幕、素材、發音
            </p>
            <img src="@/assets/img/step/step-03.png" alt="" />
            <ul class="point-list">
              <li>
                1. 字幕之間的斷句請使用符號【\】進行換行
                <br />
                （建議 10 個字內，若超過請使用換行符號）
              </li>
              <li>2. 字幕段落勿超過中文 25 字、英文 50 字</li>
              <li>3. 大標字數勿超過中文 15 字、英文 30 字</li>
              <li>4. 音檔請留空白</li>
            </ul>
            <p class="mt-5 excerpt">以下為顯示效果：</p>
            <img src="@/assets/img/step/step-04.png" alt="" />
            <p class="mt-5 excerpt">
              接下來同時選素材資料夾跟 EXCEL 檔，壓縮成 ZIP 檔
            </p>
            <img src="@/assets/img/step/step-05.png" alt="" class="my-5" />
          </li>
          <li>
            <h4>3. 上傳 ZIP 資料夾至 AI Spokesgirl 平台</h4>
            <p class="excerpt">影片檔名請寫上影片名稱</p>
            <img src="@/assets/img/step/step-06.png" alt="" />
            <h4 class="my-5 caption">
              點選“送出”之後需等待一段影片製作的時間 <br />
              請您耐心等候，待製作完畢可於影片清單查看
            </h4>
          </li>
        </ul>
      </v-card-text>
    </v-card>

    <template>
      <div class="text-center">
        <v-dialog v-model="dialog" width="auto">
          <v-card>
            <v-card-text>
              <section class="d-flex flex-column align-center">
                <v-icon
                  style="font-size: 70px"
                  icon="info"
                  color="orange-darken-3"
                />
                <p class="mt-3">影片處理需要約 5-10 分鐘，敬請耐心等候</p>
              </section>
            </v-card-text>
            <v-card-actions>
              <v-btn color="primary" block @click="dialog = false">{{
                t("close")
              }}</v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
      </div>
    </template>
  </v-container>
</template>

<style lang="scss">
.anchor-list {
  padding-left: 40px;
  img {
    width: 130px;
    height: 110px;
    object-fit: cover;
  }
  .v-card-item {
    padding: 0;
    text-align: center;
    .v-card-title {
      font-size: 18px;
    }
  }
  .bg-success {
    background: linear-gradient(
      -225deg,
      rgb(234, 84, 19) 35%,
      rgb(178, 69, 146) 100%
    ) !important;
  }
  .v-expansion-panel-title {
    height: 55px;
    min-height: 0;
  }
  .v-expansion-panel-text__wrapper {
    padding: 0 !important;
  }
}
.step-list {
  list-style: none;
  img {
    width: 100%;
    max-width: 800px;
  }
  li {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 16px;
    p {
      line-height: 32px;
    }
    h4 {
      margin: 20px auto;
      color: #ea5413;
      font-weight: bold;
      text-align: center;
      line-height: 34px;
      font-size: 20px;
    }
  }

  .link-btn {
    display: inline-block;
    padding: 12px 20px;
    margin-top: 25px;
    border-radius: 100px;
    text-decoration: none;
    color: #fff;
    background: #ea5413;
    transition: all 0.3s;
    &:hover {
      opacity: 0.8;
    }
  }

  .point-list {
    display: flex;
    flex-direction: column;
    align-items: baseline;
    margin-left: 40px;
  }

  .excerpt::before {
    content: "";
    font-weight: bold;
    display: inline-block;
    border: 5px solid #ea5413;
    border-radius: 20px;
    margin-right: 10px;
    margin-bottom: 2px;
  }
}
</style>
