<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-container-lt">
        <el-input
          placeholder="视频标题"
          style="width: 200px;"
          class="filter-item"
          clearable
          v-model="listQuery.videoTitle"
        />

        <!-- <el-select
        clearable
        filterable
        class="filter-item"
        v-model="listQuery.warehouseId"
        :placeholder="$t('place.selectWarehouse')"
      >
        <el-option
          v-for="item in warehouses"
          :key="item.warehouseId"
          :label="item.warehouseName"
          :value="item.warehouseId"
        >
        </el-option>
      </el-select> -->

        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
          >搜索</el-button
        >
      </div>
      <div class="filter-container-rt">
        <!-- <el-button class="filter-item" @click="exportExcel" type="primary"
          >导出EXCEL</el-button
        >
        <el-button
          v-waves
          class="filter-item"
          type="primary"
          icon="el-icon-upload"
          @click="handleUpload"
        >
          导入
        </el-button> -->
      </div>
    </div>

    <div class="table-container">
      <el-table
        style="width: 100%"
        v-loading="listLoading"
        :key="tableKey"
        :data="list"
        row-key="id"
        stripe
        border
        fit
        highlight-current-row
      >
      <el-table-column
          label="序号"
          type="index"
          width="60"
          align="center"
        />
        <el-table-column
          label="视频ID"
          min-width="100"
          align="center"
          prop="id"
        />

        <el-table-column
          label="视频标题"
          min-width="150"
          align="center"
          prop="videoTitle"
        />
        <el-table-column
          label="视频封面"
          min-width="110"
          align="center"
          prop="videoCoverImageUrl"
        >
          <template slot-scope="scope">
            <div
              v-if="scope.row.videoCoverImageUrl"
              class="video"
              @click="handlePlay(scope.row)"
            >
              <img :src="scope.row.videoCoverImageUrl" class="video-img" />
              <i class="el-icon-video-play" />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="SKU"
          min-width="100"
          align="center"
          prop="sku"
        />

        <el-table-column
          label="SKUID"
          min-width="100"
          align="center"
          prop="skuId"
        />
        <el-table-column
          label="审核状态"
          min-width="100"
          align="center"
          prop="reviewStatus"
        >
          <template slot-scope="scope">
            <span
              class="status-tag"
              :class="{
                'status-pending': scope.row.reviewStatus === 0,
                'status-success': scope.row.reviewStatus === 1,
                'status-error': scope.row.reviewStatus === 2
              }"
            ></span>
            {{ getValueByKey(scope.row.reviewStatus, auditStatus) }}
          </template>
        </el-table-column>

        <el-table-column
          label="审核时间"
          min-width="100"
          align="center"
          prop="reviewTime"
        />
        <el-table-column
          label="审核人"
          min-width="100"
          align="center"
          prop="reviewer"
        />
        <el-table-column
          label="发布状态"
          min-width="100"
          align="center"
          prop="publishStatus"
        >
          <template slot-scope="scope">
            <span
              class="status-tag"
              :class="{
                'status-pending': scope.row.publishStatus === 0,
                'status-success': scope.row.publishStatus === 1,
                'status-error': scope.row.publishStatus === 2
              }"
            ></span>
            {{ getValueByKey(scope.row.publishStatus, auditStatus) }}
          </template>
        </el-table-column>
        <el-table-column
          label="发布时间"
          min-width="100"
          align="center"
          prop="publishTime"
        />
        <el-table-column
          label="发布人"
          min-width="100"
          align="center"
          prop="publisher"
        />
        <el-table-column
          label="关联热词"
          min-width="100"
          align="center"
          prop="hotWords"
        />

        <el-table-column
          label="直播时间"
          min-width="100"
          align="center"
          prop="liveTime"
        />
        <!-- <el-table-column
          label="直播场次"
          min-width="100"
          align="center"
          prop="liveSession"
        /> -->
        <el-table-column
          label="直播中控"
          min-width="120"
          align="center"
          prop="liveController"
        >
        </el-table-column>

        <el-table-column
          label="直播间号"
          min-width="100"
          align="center"
          prop="liveRoomId"
        />
        <el-table-column
          label="操作人"
          min-width="100"
          align="center"
          prop="creator"
        />

        <el-table-column
          label="创建时间"
          min-width="100"
          align="center"
          prop="createTime"
        />
        <el-table-column
          label="操作"
          align="center"
          min-width="250"
          v-bind="device !== 'mobile' ? { fixed: 'right' } : {}"
        >
          <template slot-scope="scope">
            <div class="button">
              <el-button
                type="primary"
                :loading="
                  scope.row && scope.row.id
                    ? downloadingRows[scope.row.id]
                    : false
                "
                size="mini"
                @click="handleDownload(scope.row)"
              >
                下载
              </el-button>
              <el-button
                type="success"
                size="mini"
                @click="handlePlay(scope.row)"
              >
                播放
              </el-button>
              <el-button
                type="danger"
                size="mini"
                @click="handleDelete(scope.row)"
              >
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <swPage
      v-if="total > 0"
      key="2"
      :listQuery="listQuery"
      :total="total"
      pos="btmRight"
      @retPage="retPage"
    />
    <!-- 视频播放 -->
    <video-player
      ref="videoPlayer"
      :video-url="currentVideoUrl"
      :video-title="currentVideoTitle"
      @close="handlePlayerClose"
    />
  </div>
</template>

<script>
import waves from "@/directive/waves";
import swPage from "@/views/common/swPage";
import { auditStatus } from "@/constants/index";
import { getValueByKey } from "@/utils/index";
// import { getToken } from "@/utils/auth";
import { fetchOralVideoList, deleteOralVideoList } from "@/api/video";
import downloadUtil from "@/utils/downloadUtil";
import VideoPlayer from "@/components/VideoPlayer";
import { mapGetters } from "vuex";

export default {
  name: "oralVideo",
  directives: {
    waves
  },
  components: {
    swPage,
    VideoPlayer
  },
  computed: {
    ...mapGetters(["device"]) // 从 vuex 中获取设备类型
  },
  data() {
    return {
      auditStatus,
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: false,
      downloadingRows: {},
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        videoTitle: null
      },
      currentVideoUrl: null,
      currentVideoTitle: null
    };
  },
  //页面创建的时候执行
  created() {
    this.getList();
  },
  methods: {
    getValueByKey,
    getList() {
      this.listLoading = true;
      fetchOralVideoList(this.listQuery).then(res => {
        if (200 == res.code) {
          this.total = res.data.total;
          this.list = res.data.rows;
        }
        this.listLoading = false;
      });
    },
    retPage() {
      //分页
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 视频下载
    async handleDownload(row) {
      const { videoUrl, videoTitle, id, sku } = row;
      this.$set(this.downloadingRows, id, true);
      try {
        const title = sku ? videoTitle + "_" + sku : videoTitle;
        const res = await downloadUtil.videoDownload(videoUrl, title);
        if (res) {
          this.$message.success("下载成功");
        }
      } catch (error) {
        this.$message.error(error.message);
      } finally {
        this.$set(this.downloadingRows, id, false);
      }
    },
    // 视频播放
    handlePlay(row) {
      const { videoUrl, videoTitle } = row;
      ``;
      if (!videoUrl) {
        this.$message.error("视频地址不存在");
        return;
      }
      this.currentVideoUrl = videoUrl;
      this.currentVideoTitle = videoTitle;
      this.$refs.videoPlayer.show();
    },
    handlePlayerClose() {
      this.currentVideoUrl = "";
      this.currentVideoTitle = "";
    },
    handleDelete(row) {
      //删除
      this.$confirm("是否删除该视频?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const { id } = row;
        deleteOralVideoList([id]).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 3000
          });
          const index = this.list.indexOf(row);
          this.list.splice(index, 1);
        });
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "@/styles/layout.scss";
.button {
  .el-button {
    margin-bottom: 10px;
    &:last-child {
      margin-bottom: 0;
    }
  }
}
.video {
  position: relative;
  display: flex;
  align-content: center;
  justify-content: center;
  &:hover {
    i {
      opacity: 1;
    }
    .video-img {
      opacity: 0.6;
    }
  }
  &-img {
    width: 60px;
    height: 60px;
    border: 1px solid #eee;
    object-fit: cover;
    cursor: pointer;
  }
  i {
    width: 60px;
    height: 60px;
    position: absolute;
    top: 85%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    font-weight: bold;
    color: #fff;
    opacity: 0;
    cursor: pointer;
  }
}

.status-tag {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}

.status-pending {
  background-color: #e6a23c;
}

.status-success {
  background-color: #67c23a;
}

.status-error {
  background-color: #f56c6c;
}
</style>
