<template>
  <div class="app-container home">
    <!-- <TabTitle :text="nowText" /> -->
    <el-card class="tab-title">
      <div class="cleartitle" style="justify-content: flex-start">
        <img src="@/assets/images/logo_video.png" width="25px" alt=""> <span>{{nowText}}</span>
      </div>
    </el-card>
    <div class="add-but">
          <el-button  plain @click="router.go('-1')">返回</el-button>
    </div>

    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="24" :lg="24">
        <el-card class="right-list">
          <div class="backcolor"  v-if="videoLoading" :style="backImg" >
              <div style="width: 500px;">
                <h2 style="font-weight: 700;font-style: normal;font-size: 28px;color: #FFFFFF;">{{form.algorithmName}}</h2>
                <p style="font-weight: 700;font-style: normal;font-size: 17px;color: rgba(255, 255, 255, 0.6);line-height: 21px;">
                  {{form.cardCopywriting}}
                </p>
              </div> 
              <div style="width: 400px;height: 225px;overflow: hidden;" >
                <video  v-if="videoLoading" controls ref="videoEle" class="drawImg" width="100%" style="border-radius: 10px;">
                  <source :src="backVideo" type="video/mp4" />
                </video>
              </div> 
              <!-- <ImagePreview :src="form.cardImg"/> -->
          </div>


          <div class="form-title" style="display: flex;justify-content: flex-start;align-items: center;">
              <span>应用场景</span>
          </div>
          <div  style="background-color: #E6F3FF; min-height: 270px;">
            <div v-if="applicationList.length==0" class="text-style" style="text-align: center;line-height: 270px;font-size: 26px;">
              暂无数据
            </div>
            <div class="alg-list">
              <el-card  v-for="item in applicationList" :key="item.name" class="alg-item" shadow="hover" >
                <template v-slot:default >
                  <h2>{{ item.sceneName }}</h2>
                  <p style="color: #999999;font-size: 12px;">
                    {{ item.sceneDetail }}
                  </p>
                </template>
              </el-card>
            </div>
       
          </div>


          <div class="form-title" style="display: flex;justify-content: flex-start;align-items: center;">
              <span>算法试用</span>
          </div>
          <div style="background-color: #E6F3FF;display: grid;grid-template-columns: 1fr 2fr 2fr; gap: 16px;padding: 16px;min-height: 650px;position: relative;">
                <el-card style="position: relative;height: 100%;">
                  <div class="text-style" style="margin: 10px 0;">
                    试用图片与视频：
                  </div>
                  <div class="upload-box">
                    <div class="img-box" v-if="imgVideoData.length==0">
                      <img src="@/assets/images/default/Image.png"  width="100%" height="480px"/>
                    </div>
                    <div v-else style="height: 120px;width: 100%; margin: 5px; overflow: hidden;cursor: pointer;border-radius: 10px;"
                     v-for="item in imgVideoData" :key="item.id"
                     @click="handBuiltImg(item.caseFile)"
                     >
                       <img width="100%" v-if="getFileType(item.caseFile)=='0'" :src="'http://192.168.3.82'+item.caseFile" alt="" style="border-radius: 10px;"> 
                       <video width="100%"  v-else controls  class="drawImg" style="border-radius: 10px;">
                          <source :src="'http://192.168.3.82'+item.caseFile" type="video/mp4" />
                       </video>
                    </div>
                  </div>
                  <ImgVideoUpload 
                      style="position: absolute;bottom: 15px;left: 20px;width: 135px;" 
                      :modelValue="uploadUrl" 
                      :fileType="['png', 'jpg', 'mp4']"
                      :isImgLayout="false" 
                      :limit='5' :fileSize="20" 
                      :isShowTip="false"
                      @update:modelObj="getImgObj"
                     />
                </el-card>
      
                <el-card style="position: relative;">
                  <div class="text-style" style="margin: 10px 0;">
                    原始数据：支持自定义上传
                  </div>
                  <div class="img-box" v-if="nowImg.length==0">
                    <img src="@/assets/images/default/Image-1.png" width="100%" height="480px"/>
                  </div>
                  <div class="img-box" v-else  style="max-height: 366px;overflow: hidden;">
                    <ImagePreview v-if="getFileType(nowImg)=='0'" :src="nowImg"  width="100%"/>
                    <video width="100%"  v-else controls  class="drawImg">
                        <source :src="'http://192.168.3.82'+nowImg" type="video/mp4" />
                    </video>
                  </div>
                  <el-button type="primary" plain @click="openIdentify" style="position: absolute;bottom: 15px;" :loading="identify">开始识别</el-button>
                </el-card>
                <el-card style="position: relative;"  element-loading-text="正在识别中请稍后...">
                  <div class="text-style" style="margin: 10px 0;">
                    算法识别结果：
                  </div>
                  <div v-if="!identify">
                    <div class="img-box" v-if="outFilePath.length==0">
                      <div>{{ resultStr }}</div>
                      <img src="@/assets/images/default/Image-1.png" width="100%" height="480px" />
                    </div>
                    <div class="img-box" v-else style="max-height: 366px;overflow: hidden;">
                      <ImagePreview v-if="getFileType(outFilePath)=='0'" :src="outFilePath"  width="100%"/>
                      <video width="100%"  v-else controls  class="drawImg">
                          <source :src="'http://192.168.3.82'+outFilePath" type="video/mp4" />
                      </video>
                    </div>
                  </div>
          
                  <!-- 进度条模块 -->
                  <div v-else style="text-align: center;">
                    <el-progress :percentage="progressValue" />
                    正在识别中请稍后...
                  </div>
                  <div style="display: flex;justify-content: space-between;position: absolute;bottom: 15px;">
                    <!-- <el-button type="primary" plain>重置区域</el-button> -->
                    <!-- <el-button type="primary" plain @click="downVideo">下载识别视频</el-button> -->
                    <el-button  type="primary" plain @click="downVideo(outFilePath)">下载识别视频</el-button>
                  </div>
                </el-card>
          </div>
          <div class="form-title" style="display: flex;justify-content: flex-start;align-items: center;">
              <span>算法边界</span>
          </div>
          <div style="background-color: #E6F3FF;display: grid;grid-template-columns:  1fr 1fr; gap: 16px;padding: 16px;">
                <el-card style="position: relative; padding: 20px;">
                  <div class="img-box" v-if="effectiveList.length==0">
                    <img src="@/assets/images/default/Image-1.png" width="100%"/>
                  </div>
                  <div v-else >
                    <div class="text-boundary">
                    有效的拍摄方式：
                        <div v-for="(item,index) in effectiveList" :key="item.id" style="text-indent: 10px;">{{index+1}}、{{item.remark}}</div>
                    </div>
                    <el-divider />
                    <div class="img-box  gird-layout">
                      <div  class="img-item"  v-for="item in invalidImgList" :key="item.id">
                        <ImagePreview style="height: 100%;" :src="item.caseFile"/>
                      </div>
                    </div>
                   <img class="top-tip" src="@/assets/images/success.png" alt="">
                  </div>
                </el-card>
                <el-card style="position: relative;padding: 20px;">
              
                  <div class="img-box" v-if="invalidList.length==0">
                    <img src="@/assets/images/default/Image-1.png" width="100%"/>
                  </div>
                  <div v-else >
                    <div class="text-boundary">
                    无效的拍摄方式：
                        <div v-for="(item,index) in invalidList" :key="item.id" style="text-indent: 10px;">{{index+1}}、{{item.remark}}</div>
                    </div>
                    <el-divider />
                      <div class="img-box gird-layout" >
                        <div class="img-item" v-for="item in invalidImgList" :key="item.id" >
                          <ImagePreview style="height: 100%;" :src="item.caseFile"/>
                        </div>
                      </div>
                    <img class="top-tip" src="@/assets/images/close.png" alt="">
                  </div>
             
                </el-card>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="Index">
import { ArrowDown } from '@element-plus/icons-vue'
import { Search } from '@element-plus/icons-vue'
import { detailAlg ,listCase} from "@/api/algorithmList/index.js";
import { listScene,detailScene } from "@/api/algorithmList/scene.js";
import { listBoundary, detailBoundary } from "@/api/algorithmList/boundary.js";
import { useWebSockets} from './websockt.js'; // 注意文件路径可能需要调整  
const { handeUpload , outFilePath,resultStr,identify} = useWebSockets();
const router = useRouter();
const route = useRoute()
const nowImg=ref('')
const { roleId } =route.params
const { proxy } = getCurrentInstance();
const algFormRef = ref(null)
const uploadUrl = ref('')
const videoLoading = ref(false)
const backImg = ref({})
const backVideo = ref({})
const applicationList = ref([])
const effectiveList = ref([])
const invalidList = ref([])
const invalidImgList = ref([])
const effectiveImgList = ref([])
const imgVideoData = ref([])

const progressValue = ref(0)

const nowText=ref('算法详情')
const baseUrl = 'http://192.168.3.82'
const data = reactive({
  form: {},
  queryParams: {
    postCode: undefined,
    searchValue: '',
    status: undefined
  },
  rules: {
    postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
    postSort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
  }
});
const { queryParams, form, rules } = toRefs(data);

function getDetials(){
  videoLoading.value=false
  detailAlg(roleId).then(res=>{
    console.log(res)
    form.value=res.data
    const banner=  new URL(judgeImgUrl(res.data.algorithmBanner))
    backImg.value = {
      backgroundImage: `url(${banner})`,
    }
    backVideo.value = judgeImgUrl(res.data.algorithmBannerVideo)
    videoLoading.value=true
  })
  getListCase()
  listScene({ algorithmId: roleId}).then(res => {
    applicationList.value = res.rows
  })
  listBoundary({ algorithmId: roleId}).then(res=>{
    effectiveList.value = res.rows.filter(item => item.caseType === '0')
    invalidList.value = res.rows.filter(item => item.caseType === '1')
    invalidImgList.value = res.rows.filter(item => item.caseType === '1'&&item.caseFile)
    effectiveImgList.value = res.rows.filter(item => item.caseType === '0'&&item.caseFile)
  })
}
/** 查询案例文件列表 */
function getListCase() {
  listCase({ algorithmId: roleId,caseType:2}).then(response => {
    imgVideoData.value = response.rows
  });
}
function getImgObj(obj){
    nowImg.value = obj.fileName
    handeUpload(obj)
}
function judgeImgUrl(item){
    if(item&&item!=null){
      if (item.indexOf(baseUrl) === -1) {
        return baseUrl + item
      } else {
        return item
      }
    }else{
      return 'http://192.168.3.82/profile/upload/2024/08/28/banner%20background_20240828095250A003.png'
    }

}
function handBuiltImg(item){
  nowImg.value = item
}
function openIdentify(){
  if(proxy.isNotEmpty(nowImg.value)){
    incrementValue()
    handeUpload({fileName:nowImg.value})
  }else{
    proxy.$modal.msgWarning(`请上传图片!`);
  }
}
// 判断当前文件类型
function getFileType(fileName) {
    const videoType =['mp4','avi','mov','wmv','flv','mkv','rmvb','3gp','mpg','mpeg','webm']
    const ImgType = ['jpg','jpeg','png','gif','bmp','tiff','webp',]
    let fileType = fileName.split(".").pop();
    if (videoType.includes(fileType)) {
      return "1";

    }
    if (ImgType.includes(fileType)) {
      return "0";
    }
}

function downVideo(url){
  if(url) window.open(baseUrl+url)
}

// 进度条
function incrementValue() {
  progressValue.value = 0; // 初始化进度值为0
  const interval = 300; // 变化间隔时间，单位毫秒
  const totalTime = 30000; // 总时间，单位毫秒
  const intervalId = setInterval(() => {
    progressValue.value++; // 增加值
    if (progressValue.value >= 100) {
      clearInterval(intervalId); // 当值达到或超过100时，停止变化
    }
    if(!identify.value){
      progressValue.value=100;
      clearInterval(intervalId);
    }
  }, interval);

  // 如果需要在30秒后停止，即使还没达到100
  // setTimeout(() => {
  //   progressValue.value=100;
  //   console.log('123', 123)
  //   clearInterval(intervalId);
  // }, totalTime);
}
getDetials()

</script>

<style scoped lang="scss">

    .upload-box{
      width: 100%;
      border-radius: 5px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .img-box{
      // width: 500px;
      width: 100%;
      min-height: 200px;
      // border: 1px solid #d8dce5;
      border-radius: 5px;
      text-align: center;
    }
   .form-title{
      display: flex;
      align-items:center;/*for vertical align*/
      background-image: url('../../../assets/images/details_top.png');
      background-size: 100% 40px; /* 设置背景图的高度为200px，宽度自动 */
      height: 40px;
      font-family: PingFang SC, PingFang SC;
      font-size: 16px;
      font-weight: 600;
      font-style: normal;
      text-transform: none;
      border-radius: 5px;
      padding-left: 15px;
      margin-top: 10px;
      color: #fff;
    }
    // .form-title::before{
    //   content:"";
    //   display: inline-block;
    //   width: 10px; /* 矩形的宽度 */
    //   height: 30px; /* 矩形的高度 */
    //   background-color: #1890FF; /* 矩形的背景颜色 */
    //   margin-right: 10px;
    //   border-radius: 8px;
    // }
   .search-input{
    display: flex;
    width: 400px;
    margin-right: 10px;
   }
   .add-but{
    position: absolute;
    display: flex;
    top: 30px;
    right: 35px;
   }
   .left-list{
    min-height: 700px;
   }
   .right-list{
    min-height: 700px;
    .alg-list{
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
      padding: 16px;
      gap: 16px;
      .alg-item{
        width: 100%;
        background-image: url('../../../assets/images/alg_item.png') !important;
        background-size: 100% 235px; /* 设置背景图的高度为200px，宽度自动 */
        min-height: 235px;
        cursor: pointer;
        position: relative;
        background-color: #ffffff00;
        border: 0;
      }
    }
   }
   .cleartitle{
    font-size: 16px;
    font-weight: 600;
    display: flex;
    justify-content: space-between;
  }
  .backcolor{
    height: 350px;
    width: 100%;
    padding: 10px;
    border-radius: 10px;
    background-image: linear-gradient(to right, #6f86d6 0%, #48c6ef 100%);
    background-size:100% 350px ;
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  .tab-title {
    display: flex;
    align-items: center;
    /*for vertical align*/
    font-size: 20px;
    height: 50px;
    color: #1890FF;
    background-color: #fff;
    margin-bottom: 10px;
    border-radius: 5px;
    padding: 0;

  .el-card__body {
    padding: 0 !important;
  }
  }
  .tab-title::before {
    content: "";
    display: none;
    width: 0px;
    height: 28px;
    background-color: #1890FF;
    margin-right: 8px;
    border-radius: 8px;
    margin-left: 8px;
}
  .text-boundary{
    font-family: PingFang SC, PingFang SC;
    font-weight: 600;
    font-size: 14px;
    color: #292D32;
    // line-height: 34px;
    text-align: left;
    font-style: normal;
    text-transform: none;
    min-height: 120px;
  }
  .text-style{
    font-family: PingFang SC, PingFang SC;
    font-weight: 600;
    font-size: 14px;
    color: #292D32;
    // line-height: 34px;
    text-align: left;
    font-style: normal;
    text-transform: none;
  }
 .gird-layout{
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 16px;
    padding: 16px;
  }
  .img-item{
    border: 1px solid #279bda;
    width: 150px;
    height: 150px;
  }
  .top-tip{
    position: absolute;
    width: 35px;
    top: 25px;
    right: 21px;

  }
</style>

