<template>
  <view class="page">
    <view class="back_bar"> <BackBar title="选择视频" /></view>
    <view class="videos">
      <view
        v-for="(item, index) of videos"
        :key="index"
        :class="{ 'video-item': true }"
        @click="selectVideo(item.id)"
      >
        <view :class="{ 'video-box': true, active: selectId.includes(item.id) }">
          <wd-img class="video-img" :src="item.videoUrl">
            <template #error>
              <view class="error-wrap"> 加载失败 </view>
            </template>
            <template #loading>
              <view class="loading-wrap">
                <wd-loading />
              </view>
            </template>
          </wd-img>
          <wd-img
            v-if="selectId.includes(item.id)"
            class="checked"
            :src="checked"
            width="40rpx"
            height="40rpx"
          />
        </view>
        <view class="video-name">{{ item.name }}</view>
      </view>
    </view>
    <view class="footer">
      <button class="footer-btn" @click="submit">确定</button>
    </view>
  </view>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import checked from '@/static/svg/checked.svg';
import { onLoad } from '@dcloudio/uni-app';
const videos = ref<
  {
    name: string;
    id: number;
    videoUrl: string;
  }[]
>([]);
const selectId = ref<number[]>([]);
function selectVideo(id: number) {
  if (selectId.value.includes(id)) {
    selectId.value = selectId.value.filter(e => e !== id);
  } else {
    selectId.value.push(id);
  }
}

const plan = ref(null);

function submit() {
  if (selectId.value.length === 0) {
    uni.showToast({
      title: '请先选择视频再进行下一步',
      icon: 'none',
      duration: 3000,
    });
    return;
  }
  let data = videos.value.filter(item => selectId.value.includes(item.id));
  plan.value.planList[plan.value.index] = {
    ...plan.value.planList[plan.value.index],
    courseTimeId: data[0].id,
    courseTimeName: data[0].name,
  };
  data.slice(1).forEach(item => {
    plan.value.planList.push({
      ...plan.value.planList[plan.value.index],
      courseTimeId: item.id,
      courseTimeName: item.name,
    });
  });
  plan.value.row = plan.value.planList[plan.value.index];
  uni.setStorageSync('plan', plan.value);
  // 回到发布页面
  uni.navigateTo({ url: '/pages/inspector/releasePlan/index' });
  uni.removeStorageSync('videoList');
}

onLoad(() => {
  const list = uni.getStorageSync('videoList');
  videos.value = list.map(item => {
    return {
      name: item.videoName,
      id: item.id,
      videoUrl: item.thumbUrl,
    };
  });
  plan.value = uni.getStorageSync('plan');
});
</script>
<style lang="scss" scoped>
.page {
  padding: 0 30rpx 30rpx 30rpx;
  box-sizing: border-box;
  .back_bar {
    margin-bottom: 30rpx;
  }
  .videos {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 30rpx;
    .video-item {
      width: calc(50% - 30rpx);
      height: 275rpx;
      border-radius: 20rpx;
      font-family: 'Kingnam Bobo';
      font-size: 38rpx;
      line-height: 56rpx;
      position: relative;
      //margin: 0 30rpx 30rpx 0;
      .video-name {
        font-size: 30rpx;
        color: #0a1f13;
        line-height: 45rpx;
        margin-top: 10rpx;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .video-box {
        width: 100%;
        height: 216rpx;
        position: relative;
        .checked {
          position: absolute;
          bottom: -1rpx;
          right: -1rpx;
        }
        .video-img {
          width: 100%;
          height: 100%;
          border-radius: 20rpx;
          overflow: hidden;
        }
        &.active {
          .loading-wrap,
          .error-wrap {
            width: 330rpx;
            height: 216rpx;
          }
          &::after {
            content: '';
            display: block;
            width: 100%;
            height: 100%;
            border: 2rpx solid #615dff;
            position: absolute;
            border-radius: 20rpx;
            left: 0;
            top: 0;
            z-index: 9;
          }
        }
      }
      .error-wrap {
        width: 100%;
        height: 216rpx;
        line-height: 216rpx;
        text-align: center;
        background: #e8e8e8;
        border-radius: 20rpx;
      }
      .loading-wrap {
        width: 100%;
        height: 216rpx;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
}
.footer {
  position: fixed;
  bottom: 0;
  height: 122rpx;
  width: 100vw;
  left: 0;
  padding-top: 16rpx;
  box-sizing: border-box;
  background: #fff;
  border-top: 1rpx solid #f6f6f6;
  display: flex;
  justify-content: center;
  .footer-btn {
    margin: auto;
    width: 440rpx;
    height: 90rpx;
    background: #615dff;
    font-size: 30rpx;
    line-height: 90rpx;
    color: #fff;
    font-weight: 600;
  }
}
</style>
