<template>
  <div class="sv-compose">
    <div class="sv-compose__card">
      <div class="sv-compose__head">
        <div>
          <h2 class="sv-compose__title">
            {{ $t('SHOPEE_VIDEO_COMPOSE_TITLE') }}
          </h2>
          <p class="sv-compose__desc">
            {{ $t('SHOPEE_VIDEO_COMPOSE_DESC') }}
          </p>
        </div>
        <span
          class="sv-compose__badge"
          :class="`is-${source}`"
        >{{ sourceLabel }}</span>
      </div>

      <div
        v-if="loadError"
        class="sv-compose__empty"
      >
        <Icon type="ios-alert-outline" />
        <p>{{ $t('SHOPEE_VIDEO_DRAFT_MISSING') }}</p>
      </div>

      <div
        v-else
        class="sv-compose__body"
      >
        <div class="sv-compose__media">
          <video
            v-if="videoUrl"
            :src="videoUrl"
            controls
            class="sv-compose__video"
          />
        </div>

        <div class="sv-compose__form">
          <div
            class="sv-field"
            :class="{ 'is-invalid': attempted && !hasCampaign }"
          >
            <label class="sv-field__label">{{ $t('SHOPEE_VIDEO_CAMPAIGN') }}</label>
            <AutoComplete
              v-model="campaignName"
              transfer
              icon="ios-albums-outline"
              :placeholder="$t('SHOPEE_VIDEO_CAMPAIGN_INPUT')"
            >
              <Option
                v-for="campaign in campaignSuggestions"
                :key="campaign._id"
                :value="campaign.name"
              >
                {{ campaign.name }}
              </Option>
            </AutoComplete>
            <p class="sv-field__tip">
              {{ $t('SHOPEE_VIDEO_CAMPAIGN_TIP') }}
            </p>
          </div>

          <div
            class="sv-field"
            :class="{ 'is-invalid': attempted && !hasProducts }"
          >
            <label class="sv-field__label">{{ $t('SHOPEE_VIDEO_PRODUCTS') }}</label>
            <ProductPicker v-model="products" />
          </div>

          <div
            class="sv-field"
            :class="{ 'is-invalid': attempted && !hasCaption }"
          >
            <label class="sv-field__label">{{ $t('SHOPEE_VIDEO_CAPTION') }}</label>
            <Input
              v-model="caption"
              type="textarea"
              :rows="3"
              :placeholder="$t('SHOPEE_VIDEO_CAPTION_PLACEHOLDER')"
            />
          </div>

          <p
            v-if="logText"
            class="sv-compose__log"
            :style="{ color: logColor }"
          >
            {{ logText }}
          </p>

          <div
            v-if="!published"
            class="sv-checklist"
          >
            <p class="sv-checklist__title">
              {{ $t('SHOPEE_VIDEO_REQUIRE_TITLE') }}
            </p>
            <div
              v-for="req in requirements"
              :key="req.key"
              class="sv-checklist__item"
              :class="{ 'is-done': req.done }"
            >
              <Icon :type="req.done ? 'md-checkmark-circle' : 'ios-radio-button-off'" />
              <span>{{ req.label }}</span>
            </div>
          </div>

          <div class="sv-compose__actions">
            <Button
              class="sv-compose__draft"
              size="large"
              icon="ios-bookmark-outline"
              :loading="savingDraft"
              :disabled="!canSaveDraft"
              @click="saveDraft"
            >
              {{ $t('SHOPEE_VIDEO_DRAFT_BTN') }}
            </Button>
            <Button
              class="sv-compose__publish"
              type="error"
              size="large"
              icon="md-cloud-upload"
              :loading="publishing"
              :disabled="isBusy || published"
              @click="publish"
            >
              {{ $t('SHOPEE_VIDEO_PUBLISH_BTN') }}
            </Button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { defineComponent } from '@vue/composition-api';
import ProductPicker from '@/vue/components/Shopee/Video/ProductPicker.vue';
import videoMixin from './mixin/video';
import campaignMixin from './mixin/campaign';
import publishMixin from './mixin/publish';

export default defineComponent({
  components: {
    ProductPicker,
  },
  mixins: [videoMixin, campaignMixin, publishMixin],
  async created() {
    this.caption = this.$route.query.title || '';

    if (this.$route.query.source) {
      this.source = this.$route.query.source;
    }

    const blobId = this.$route.query.blobId || '';

    this.sourceUrl = this.$route.query.sourceUrl || this.$route.query.videoUrl || '';

    if (blobId) {
      await this.loadVideoFromBlob(blobId);
    } else {
      this.videoUrl = this.$route.query.videoUrl || '';
    }

    if (!this.videoUrl) {
      this.loadError = true;
      return;
    }

    await this.loadShopAndCampaigns();
  },
});
</script>

<style lang="scss" scoped>
.sv-compose {
  padding: 24px 16px;
  display: flex;
  justify-content: center;

  &__card {
    width: 100%;
    max-width: 880px;
    background: #fff;
    border-radius: 14px;
    box-shadow: 0 6px 24px rgba(17, 24, 39, 0.08);
    padding: 24px;
  }

  &__head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 20px;
  }

  &__title {
    font-size: 20px;
    font-weight: 700;
    color: #1f2329;
    margin: 0;
  }

  &__desc {
    font-size: 13px;
    color: #8a8f99;
    margin: 4px 0 0;
  }

  &__badge {
    flex-shrink: 0;
    font-size: 12px;
    font-weight: 600;
    padding: 4px 12px;
    border-radius: 999px;
    color: #fff;
    background: #1f2329;

    &.is-tiktok {
      background: #010101;
    }

    &.is-shopee {
      background: #ee4d2d;
    }
  }

  &__body {
    display: flex;
    gap: 24px;
    align-items: flex-start;
  }

  &__media {
    width: 320px;
    flex-shrink: 0;
    position: sticky;
    top: 16px;
  }

  &__video {
    width: 100%;
    max-height: 460px;
    background: #000;
    border-radius: 12px;
    display: block;
  }

  &__form {
    flex: 1;
    min-width: 0;
  }

  &__log {
    font-size: 13px;
    margin: 4px 0 12px;
  }

  &__actions {
    display: flex;
    gap: 10px;
  }

  &__draft {
    flex-shrink: 0;
  }

  &__publish {
    flex: 1;
  }

  &__empty {
    text-align: center;
    padding: 48px 0;
    color: #b0b4bb;

    i {
      font-size: 40px;
    }

    p {
      margin-top: 8px;
      font-size: 14px;
    }
  }
}

.sv-field {
  margin-bottom: 18px;

  &__label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: #4b5059;
    margin-bottom: 6px;
  }

  &__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 6px;

    .sv-field__label {
      margin-bottom: 0;
    }
  }

  &__action {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 3px 8px;
    border: none;
    background: transparent;
    font-size: 12.5px;
    font-weight: 600;
    line-height: 1;
    color: #ee4d2d;
    cursor: pointer;
    border-radius: 14px;
    transition: background 0.15s ease, color 0.15s ease;

    i {
      font-size: 15px;
    }

    &:hover {
      background: rgba(238, 77, 45, 0.1);
    }

    &.is-active {
      color: #8a8f99;

      &:hover {
        background: rgba(0, 0, 0, 0.05);
      }
    }
  }

  &__row {
    display: flex;
    gap: 8px;
    margin-top: 8px;
  }

  &__create {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    padding: 10px;
    background: #fbfbfc;
    border: 1px solid #eef0f2;
    border-radius: 8px;

    .ivu-input-wrapper {
      flex: 1;
    }
  }

  &__create-btn {
    flex-shrink: 0;
    white-space: nowrap;
  }

  &__tip {
    font-size: 12px;
    color: #b0b4bb;
    margin: 6px 0 0;
  }

  &.is-invalid {
    .sv-field__label {
      color: #ed4014;
    }

    ::v-deep .ivu-input,
    ::v-deep .ivu-input:hover,
    ::v-deep .ivu-input:focus {
      border-color: #ed4014;
      box-shadow: 0 0 0 2px rgba(237, 64, 20, 0.12);
    }

    ::v-deep .sv-picker__list {
      border-color: #ed4014;
    }
  }
}

.sv-checklist {
  background: #fbfbfc;
  border: 1px solid #eef0f2;
  border-radius: 10px;
  padding: 12px 14px;
  margin: 4px 0 14px;

  &__title {
    font-size: 12px;
    font-weight: 600;
    color: #6b7079;
    margin: 0 0 8px;
  }

  &__item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: #9aa0a8;
    padding: 3px 0;
    transition: color 0.15s ease;

    i {
      font-size: 18px;
      color: #cdd1d7;
      transition: color 0.15s ease;
    }

    &.is-done {
      color: #2d2f33;

      i {
        color: #19be6b;
      }
    }
  }
}

.sv-collapse-enter-active,
.sv-collapse-leave-active {
  transition: max-height 0.24s ease, opacity 0.2s ease, margin-top 0.24s ease, transform 0.24s ease;
  overflow: hidden;
}

.sv-collapse-enter,
.sv-collapse-leave-to {
  max-height: 0;
  margin-top: 0;
  opacity: 0;
  transform: translateY(-4px);
}

.sv-collapse-enter-to,
.sv-collapse-leave {
  max-height: 80px;
  opacity: 1;
}

@media (max-width: 760px) {
  .sv-compose__body {
    flex-direction: column;
  }

  .sv-compose__media {
    width: 100%;
    position: static;
  }

  .sv-compose__video {
    max-height: 320px;
  }
}
</style>
