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

const { t } = useI18n();
const valid = ref(true);
const dialog = ref(false);
const title = ref("");
const zipFiles = ref();
const Form = ref();

const mainStore = useMainStore();

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

async function Submit() {
  dialog.value = true;
  await (Form as any).value.validate();
  if (valid.value) {
    await mainStore.uploadPlot(title.value, 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('title')"
            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-form>
      </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>

    <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>
