import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { ModalService } from 'src/app/core/service/modal.service';
import { MsgService } from 'src/app/core/service/msg.service';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { MaterialPicPageComponent } from '../../material-pic/material-pic-page/material-pic-page.component';
import { MaterialVideoPageComponent } from '../../material-video/material-video-page/material-video-page.component';


@Component({
  selector: 'app-std-material-page',
  templateUrl: './std-material-page.component.html',
  styleUrls: ['./std-material-page.component.scss']
})
export class StdMaterialPageComponent extends FramePageComponent implements OnInit {

    schoolArray = [];
    batchArr = [];
    layout_status = "";
    query = {
      school_id : '',
      batch_id : ''
    };

  constructor(private modalService: BsModalService) {
    super(modalService);
    this.modalComponent = MaterialVideoPageComponent;
  }

  ngOnInit(): void {
    this.AjaxService.requestArray('./cf/school/list.htm', {}, (array) => {
      this.schoolArray = array;
    });
  }

  changeSchool() {
    this.AjaxService.requestArray("./cf/batch/list.htm", {school_id : this.query.school_id}, (array)=>{
      debugger;
      this.batchArr = array;
    });    
  }

  requestData() {
    this.pager.reload({school_id : this.query.school_id, batch_id : this.query.batch_id});
  }

  showMaterials(v, target) {
    if (target == 'pic') {
      this.showComponentModal(MaterialPicPageComponent, { 'ResponseEntity': v });
    } else if (target == 'video') {
      this.showComponentModal(MaterialVideoPageComponent, { 'ResponseEntity': v });
    }
  }

  //导入视频材料
  uploaVideoMertail() {
    debugger;
    //线程参数 和 业务参数
    let reqParam = {
      thread_name: "导入视频材料线程",
      error_flag: "InActive", 
      callback_type: "Swal",
      school_id : this.query.school_id,
      batch_id : this.query.batch_id,
    };
    //上传参数
    let uploadParam = {
      uploadTitle:'视频材料导入',
      templateFile:'VideoMaterial.xls',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose:true
    };
    ModalService.showUploadWindow(this.bsService,'./ex/material/video/upload.htm', reqParam, uploadParam);    
  }

  uploadWithThreadCallback(ajax) {
    debugger;
    console.log("upload callback --->" + ajax.errorCode + " , " + ajax.url);
    if (ajax.errorCode != 0 && ajax.url) {
      MsgService.errorToastr("导入报错!");
    } else {
      MsgService.successToastr("导入成功!");
    }
    this.pager.reload();
  }

  //删除视频材料
  deleteVideoMaterail() {
    debugger;
    if (!this.query.school_id) {
      MsgService.errorToastr('请选择学校！');
      return;
    }
    if (!this.query.batch_id) {
      MsgService.errorToastr('请选择批次！');
      return;
    }
    MsgService.confirmAlert("确定删除该批次下视频材料吗?", () => {
      this.AjaxService.request("ex/material/delete.htm", {school_id: this.query.school_id, batch_id: this.query.batch_id, type: 'video'}, (ret)=>{
        MsgService.successAlert('删除成功！');
        this.pager.reload();
      });
    });    
  }


  //导入图片作品材料
  uploaPicoMertail() {
    debugger;
    //线程参数 和 业务参数
    let reqParam = {
      thread_name: "图片材料导入线程",
      error_flag: "InActive", 
      callback_type: "Swal",
      school_id : this.query.school_id,
      batch_id : this.query.batch_id
    };
    //上传参数
    let uploadParam = {
      uploadTitle:'图片材料导入',
      templateFile:'PicMaterial.xls',
      uploadCallback: this.uploadWithThreadCallback.bind(this),
      autoClose:true
    };
    ModalService.showUploadWindow(this.bsService,'./ex/material/pic/upload.htm', reqParam, uploadParam);    
  }

  //删除图片材料
  deleteMaterail() {
    debugger;
    if (!this.query.school_id) {
      MsgService.errorToastr('请选择学校！');
      return;
    }
    if (!this.query.batch_id) {
      MsgService.errorToastr('请选择批次！');
      return;
    }
    MsgService.confirmAlert("确定删除该批次下材料吗?", () => {
      this.AjaxService.request("ex/material/delete.htm", {school_id: this.query.school_id, batch_id: this.query.batch_id}, (ret)=>{
        MsgService.successAlert('删除成功！');
        this.pager.reload();
      });
    });     
  }

}
