<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-container-lt">
        <el-form :inline="true" label-position="left" :model="listQuery">
          <el-form-item>
            <el-input clearable v-model="listQuery.sku" placeholder="搜索SKU"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input clearable v-model="listQuery.videoTitle" placeholder="视频标题"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="listQuery.processStatus" clearable filterable placeholder="处理状态">
              <el-option
                v-for="item in processStatusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="handleFilter">搜索</el-button>
        </el-form>
      </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="130" align="center" prop="videoCoverImageUrl">
          <template slot-scope="scope">
            <div class="video" v-if="scope.row.processStatus == 0">
              视频处理中
            </div>
            <div v-else-if="scope.row.rawVideoUrl" class="video" @click="handlePlay(scope.row.rawVideoUrl)">
              <img class="video-img" style="max-width: 100%;" :src="scope.row.videoCoverImageUrl" alt="">
              <i class="el-icon-video-play" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="视频封面" min-width="130" align="center" prop="videoCoverImageUrl">
          <template slot-scope="scope">
            <div class="video" v-if="scope.row.processStatus == 0">
              视频处理中
            </div>
            <div class="video" v-else>
              <el-image  class="video-last-img video" :src="scope.row.videoCoverImageUrl"
                :preview-src-list="[scope.row.videoCoverImageUrl]" :lazy="true" />
              <i class="el-icon-zoom-in" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="视频末帧" min-width="130" align="center" prop="videoEndFrameUrl">
          <template slot-scope="scope">
            <div class="video" v-if="scope.row.processStatus == 0">
              视频处理中
            </div>
            <div class="video" v-else>
              <el-image  class="video-last-img video" :src="scope.row.videoEndFrameUrl"
                :preview-src-list="[scope.row.videoEndFrameUrl]" :lazy="true" />
              <i class="el-icon-zoom-in" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="使用音乐" min-width="110" align="center" prop="videoMusic">
          <template slot-scope="scope">
            <span>{{ scope.row.musicClass}}</span>
          </template>
        </el-table-column>
        <el-table-column label="拼接视频个数" min-width="120" align="center" prop="numVideo" />
        <el-table-column label="SKU" min-width="100" align="center" prop="sku" />
        <el-table-column label="店铺名称" min-width="100" align="center" prop="storeName" />
        <el-table-column label="创建时间" min-width="100" align="center" prop="createTime" />
        <el-table-column label="处理状态" min-width="100" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.processStatus == 0">视频处理中</span>
            <span v-if="scope.row.processStatus == 1">处理成功</span>
            <span v-if="scope.row.processStatus == 2">
              <el-tooltip class="item" effect="dark" placement="top-start">
                <span style="color: #f16272;cursor: pointer;">处理失败 <i class="el-icon-warning"></i></span>
                <div style="max-width: 300px;" slot="content">{{ scope.row.processResult }}</div>
              </el-tooltip>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="下载次数" min-width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.downloadCount  ? scope.row.downloadCount : '-' }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="优化建议" min-width="100" align="center" prop="createTime">
          <template slot-scope="scope">
            <p v-if="roles.includes('superAdmin')">
              <span class="desc">优化</span>
              <a class="look-more" href="javascript:void(0);" @click="handleViewSuggestions(scope.row)">更多&nbsp;>></a>
            </p>
            <p v-else>
              <a class="edit" href="javascript:void(0);" @click="handleEditSuggestions(scope.row)"><i
                  class="el-icon-edit" /></a>
            </p>
          </template>
        </el-table-column> -->
        <!-- <el-table-column v-permission="['superAdmin']" label="使用账号" width="100" align="center" prop="createTime">
          <template slot-scope="scope">
            <el-popover placement="top-start" width="200" trigger="hover" :content="scope.row.videoDesc">
              <span class="desc" slot="reference"></span>
            </el-popover>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="生成账号" min-width="100" align="center" prop="createTime" /> -->
        <el-table-column label="操作" align="center" width="160" v-bind="device !== 'mobile' ? { fixed: 'right' } : {}">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="下载" placement="top">
              <el-button type="primary" :loading="scope.row && scope.row.id
                ? downloadingRows[scope.row.id]
                : false
                " size="mini" icon="el-icon-download" circle @click="handleDownload(scope.row)" />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="播放" placement="top">
              <el-button type="success" size="mini" icon="el-icon-caret-right" circle @click="handlePlay(scope.row.videoUrl)" />
            </el-tooltip>
            <el-tooltip class="more" effect="dark" content="删除" placement="top">
              <el-button type="danger" size="mini" icon="el-icon-delete-solid" circle
                @click="handleDelete(scope.row)" />
            </el-tooltip>
            <el-tooltip class="more" style="margin: 10px 0 0 0;" effect="dark" content="更多操作" placement="top">
              <el-dropdown trigger="click" @command="command => handleAdvise(command, scope.row)">
                <el-button size="mini" type="info" icon="el-icon-more" circle title="更多操作" />
                <el-dropdown-menu class="dropdown-menu" slot="dropdown">
                  <el-dropdown-item command="addAdvise">优化建议</el-dropdown-item>
                  <el-dropdown-item command="editAdvise">查看建议</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-tooltip>
            <!-- <el-tooltip class="more" effect="dark" content="更多" placement="top">
              <el-dropdown trigger="click" @command="command => handleMore(command, scope.row)">
                <el-button size="mini" type="primary" icon="el-icon-more" circle title="更多" />
                <el-dropdown-menu class="dropdown-menu" slot="dropdown">
                  <el-dropdown-item command="changeTopic">修改话题</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-tooltip> -->
          </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" />
    <!-- 优化建议 -->
    <suggestion-table ref="suggestionTable" />
    <!-- 编辑优化建议 -->
    <edit-suggestion ref="editSuggestion" @confirm="handleCofirmSuggestion" />
  </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 ExcelImport from "@/components/ExcelImport";
import EditSuggestion from "@/components/Suggestion/editSuggestion";
import SuggestionTable from "@/components/Suggestion/suggestionTable";

import { fetchOralVideoList, deleteOralVideoList, addSuggestion } from "@/api/video";
import downloadUtil from "@/utils/downloadUtil";
import VideoPlayer from "@/components/VideoPlayer";
import { mapGetters } from "vuex";
import permission from "@/directive/permission";


export default {
  name: "catwalkVideoIndex",
  directives: {
    waves,
    permission
  },
  components: {
    swPage,
    VideoPlayer,
    SuggestionTable,
    EditSuggestion
    // ExcelImport,
    // changeTopic
  },
  computed: {
    processStatusOptions() {
      return [
        {value: 0, label: '处理中'},
        {value: 1, label: '处理成功'},
        {value: 2, label: '处理失败'}
      ]
    },
    ...mapGetters(["device", "roles"]) // 从 vuex 中获取设备类型
  },
  data() {
    return {
      auditStatus,
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: false,
      downloadingRows: {},
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        videoTitle: null,
        type:2,
        processStatus: null,
        sku: ''
      },
      currentVideoUrl: null,
      currentVideoTitle: null,
      timer: null
    };
  },
  //页面创建的时候执行
  created() {
    this.getList();
    this.timer = setInterval(() => {
      this.getList('notLoading');
    }, 300000)
  },
  methods: {
    getValueByKey,
    getList(notLoading) {
      !notLoading && (this.listLoading = true);
      const params = {}
      for (const key in this.listQuery) {
        const value = this.listQuery[key]
        if (value !== null && value !== '' && value !== undefined) {
          params[key] = value
        }
      }
      fetchOralVideoList(params).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.fileDownload(videoUrl, title);
        if (res) {
          const params = {
            "videoId": id,
            "operationType": 2
          }
          addSuggestion(params).then(res =>{
            if(res.code == 200){}
          })
          this.$message.success("下载成功");
        }
      } catch (error) {
        this.$message.error(error.message);
      } finally {
        this.$set(this.downloadingRows, id, false);
      }
    },
    // 视频播放
    handlePlay(url) {
      if (!url) {
        this.$message.error("视频地址不存在");
        return;
      }
      this.currentVideoUrl = url;
      this.currentVideoTitle = url.split("/").pop() ;
      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);
        });
      });
    },
    handleAdvise(command, row) {
      if (command == 'addAdvise') {
        this.handleEditSuggestions(row)
      }
      if (command == 'editAdvise') {
        this.handleViewSuggestions(row)
      }
    },
    //更多
    handleMore(command, row) {
      if (command === "delete") {
        this.handleDelete(row); // 删除
      } else if (command === "changeTopic") {
        this.changeTopic(row); // 修改话题
      }
    },
    handleViewSuggestions(row) {
      this.$refs.suggestionTable.show(row);
    },
    handleEditSuggestions(row) {
      this.$refs.editSuggestion.show(row);
    },
    handleCofirmSuggestion() {
      this.retPage()
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "@/styles/layout.scss";

.button {
  .el-button {
    margin-bottom: 10px;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

.video-last-img {
  display: flex;
  align-items: center;
  justify-content: center;

  /deep/.el-image__preview {
    width: 80px;
    height: 80px;
    border: 1px solid #eee;
    object-fit: cover;
    object-position: top;
    cursor: pointer;
  }

}

.video {
  position: relative;
  display: flex;
  align-content: center;
  justify-content: center;
  cursor: pointer;

  &:hover {
    i {
      opacity: 1;
      pointer-events: none;
    }

    .video-img {
      opacity: 0.6;
    }
  }

  &-img {
    width: 80px;
    height: 80px;
    border: 1px solid #eee;
    object-fit: cover;
    object-position: top;
    cursor: pointer;
  }

  i {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 20px;
    height: 20px;
    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;
}

.filter-btn {
  height: 40px;
}

.desc {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 1.5;
  max-height: 3em;
}

.more {
  margin-left: 10px;
}

.look-more {
  font-size: 12px;
  color: #AE8877;
  text-decoration: underline;
}

.edit {
  font-size: 16px;
  color: #AE8877;
}
</style>
