import { Component, OnInit } from '@angular/core';
import { MyService } from "../my.service";
import { ActivatedRoute, Router } from "@angular/router";

@Component({
  selector: 'ydlife-file-upload',
  templateUrl: './file-upload.component.html',
  styleUrls: ['./file-upload.component.scss']
})
export class FileUploadComponent implements OnInit {
  fileUploadItemList: Array<any>;
  // 判断是文件下载还是教育培训还是培训视频
  type: string;
  categoryList: Array<any>;
  selectedCategoryId: any;
  fileUploadType:any;
  constructor(private myService: MyService, private activatedRoute: ActivatedRoute,private router:Router) {
  }

  viewPdf(fileUploadItem){
    this.router.navigate(['/pdfView'],{queryParams:{path:`assets/pdfjs/web/viewer.html?file=${fileUploadItem.filePath}&isneeddownload=${fileUploadItem.isDownload=='1'?'true':'false'}`}})
    sessionStorage.setItem('permissions',JSON.stringify({isDownload: fileUploadItem.isDownload,isControlPlayback:fileUploadItem.isControlPlayback}))
  }
  setVideoPath(fileUploadItem){
    sessionStorage.setItem('videoPath',fileUploadItem.filePath);
    sessionStorage.setItem('permissions',JSON.stringify({isDownload: fileUploadItem.isDownload,isControlPlayback:fileUploadItem.isControlPlayback}))
  }

  ngOnInit() {
    this.type = this.activatedRoute.snapshot.data[0]['type'];
    if(this.type === 'trainingVideo' || this.type === 'yd_trainning_file_type'){
      this.fileUploadType = true;
    }else{
      this.fileUploadType = this.activatedRoute.snapshot.queryParams.fileUploadType ? this.activatedRoute.snapshot.queryParams.fileUploadType : null;
    }
    this.dropOptionsQuery();
    if (this.type === 'fileUpload') {
        this.fileUpload(3, 0, 19, 'yd_download_file_type', this.selectedCategoryId);
    } else if (this.type === 'training') {
      this.fileUpload(3, 0, 23, 'yd_trainning_file_type', this.selectedCategoryId);
    } else if (this.type === 'trainingVideo') {
      this.fileUpload(3, 0, 23, 'yd_trainning_file_type', 'trainingVideo');
    } else if(this.type === 'yd_trainning_file_type'){
      const path = this.activatedRoute.snapshot.routeConfig.path;
      if(path == 'prejobTraining'){
        this.selectedCategoryId = 'salestopic';
      }else if(path == 'advanceTraining'){
        this.selectedCategoryId = 'conception';
      }else if(path == 'cffpTraining'){
        this.selectedCategoryId = 'lawbiz';
      }else if(path == 'productTraining'){
        this.selectedCategoryId = 'product';
      }else if(path == 'newTraining'){
        this.selectedCategoryId = 'requirementanalys'
      }
      this.fileUpload(3, 0, 23, 'yd_trainning_file_type', this.selectedCategoryId);
    }
  }
  // 判断是不是视频文件
  judgmentFile(picUrl,type='video') {
    let filename = picUrl; //文件路径地址
    let index1 = filename.lastIndexOf(".");
    let index2 = filename.length;
    let postf = filename.substring(index1, index2).toLowerCase(); //获取文bai件后缀名duzhi
    if(type==='video'){
      //判断文件后缀名是否等于视频文件的后缀名
      if (postf===".avi"||postf===".mp4"||postf===".rmvb"||postf===".mov") {
        return true
      }else{
        return false
      }
    }else if(type==='pdf'){
        //判断文件后缀名是否等于pdf
        if (postf===".pdf") {
          return true
        }else{
          return false
        }
    }
    
  }

  //  文件下载
  fileUpload(itemType, itemId, fileCategory, code, mdDropOptionId) {
    const customerId = JSON.parse(localStorage.getItem('lifeCustomerInfo')).customerId;
    this.myService.filePath(itemType, itemId, fileCategory, code, mdDropOptionId,customerId).subscribe(res => {
      if (res['success']) {
        this.fileUploadItemList = res['data']['fileUploadItemList'];
      }
    });
  }

  //培训资料分类
  dropOptionsQuery() {
    const code = this.type === 'fileUpload' ? 'yd_download_file_type' : 'yd_trainning_file_type'
    this.myService.dropOptionsQuery({ code: code }).subscribe(res => {
      if (res['success']) {
        this.categoryList = res['data']['dropMasterInfoList'][0]['dropOptionsInfoList'];
        if (this.categoryList.length > 0) {
          if(this.fileUploadType){
            if(this.fileUploadType == 'management'){
              this.selectedCategoryId = 'salestopic';
            }else if(this.fileUploadType == 'introduce'){
              this.selectedCategoryId = 'conception';
            }else if(this.fileUploadType == 'newRecruits'){
              this.selectedCategoryId = 'userPackage';
            }else if(this.fileUploadType == 'commissionPolicy'){
              this.selectedCategoryId = 'product';
            }
          }else{
            this.selectedCategoryId = this.categoryList[0].dropOptionCode;
          }
          this.switchCategory(this.selectedCategoryId)
        }
      } else {
        this.categoryList = [];
      }
    })
  }

  switchCategory(e) {
    this.selectedCategoryId = e;
    if (this.type === 'fileUpload') {
      this.fileUpload(3, 0, 19, 'yd_download_file_type', this.selectedCategoryId);
    } else if (this.type === 'training') {
      this.fileUpload(3, 0, 23, 'yd_trainning_file_type', this.selectedCategoryId);
    }
  }
}
