import {ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
import {FramePageComponent} from "../../../frame/core/page/frame.page";
import {BsModalService} from "ngx-bootstrap/modal";
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePager } from '../../../../core/frame/pager/frame-pager/frame-pager';
import { InitialReviewPdfComponent } from '../initial-review-pdf/initial-review-pdf.component';
import { InitialReviewImageComponent } from '../initial-review-image/initial-review-image.component';
import { InitialReviewVideoComponent } from '../initial-review-video/initial-review-video.component';
import { InitialReviewRejectComponent } from '../initial-review-reject/initial-review-reject.component';
import { ReviewPromiseComponent } from '../../final-review/review-promise/review-promise.component';
import { InitialReviewPdfLookComponent } from '../initial-review-pdf-look/initial-review-pdf-look.component';
import { InitialReviewImageLookComponent } from '../initial-review-image-look/initial-review-image-look.component';
import { InitialReviewRejectFileComponent } from '../initial-review-reject-file/initial-review-reject-file.component';
import { Router } from '@angular/router';

@Component({
  selector: 'app-cf-initial-review-page',
  templateUrl: './initial-review-page.component.html',
  styleUrls: ['./initial-review-page.component.scss']
})
export class InitialReviewPageComponent extends FramePageComponent implements OnInit {
  @ViewChild('stdPager', {static: false}) stdPager: FramePager;
  uploadList = [];
  schoolArray = [];
  batchArray = [];
  stdMain : any;//考生主记录
  query = {
    school_id : '',
    batch_id : '',
    main_id : '',
    main_status : ''
  };
  curStep = "main";
  type = 1;
  rejectList = [];
  constructor(private modalService: BsModalService, private router: Router, private detectRef: ChangeDetectorRef) {
    super(modalService);
    this.modalComponent = InitialReviewPdfComponent;
  }

  ngOnInit() {
    //debugger;
    this.AjaxService.requestArray('./cf/school/list.htm', {}, (array) => {
      this.schoolArray = array;
      if (this.schoolArray.length == 1) {
        this.query.school_id = this.schoolArray[0].school_id;
        this.queryBatchArray();
      } 
    });
    AjaxService.requestEntity("cf/batch/current/get.htm",{},(batch)=>{
      //是否需要承诺书
      if (batch.need_promise == 1) {
        let parm = {batch_id: batch.batch_id, biz_type: "initial"};
        AjaxService.requestMap("review/promise/find.htm",parm ,(map)=>{
          if (map && !map.sgin) {
            //没有签
            map.promise.batch_id = batch.batch_id;
            this.showComponentModal(ReviewPromiseComponent, { 'ResponseEntity': map.promise }, {class: 'modal-7p'});
          }
        });
      }
    });
  }
  

  queryBatchArray() {
    //查询班级老师列表
    AjaxService.requestArray("cf/batch/list.htm",{school_id: this.query.school_id},(array)=>{
      this.batchArray = array;
      if (this.batchArray.length == 1) {
        this.query.batch_id = this.batchArray[0].batch_id;
      }
    });   
  }

  specialList = [];
  specialWarn = false;
  goStdDetail(v) {
    this.curStep = "std";
    this.stdMain = v;
    this.query.main_id = v.apply_id;
    this.query.school_id = v.school_id;
    this.query.batch_id = v.batch_id;
    this.query.main_status = v.status;
    this.stdMain.showSpecialColor = false;
    //查询限制、特殊处理考生列表
    AjaxService.requestArray("std/specila/list.htm",{school_id: this.query.school_id},(array)=>{
      this.specialList = array;
      for (const s of this.specialList) {
        if (s.exam_id == this.stdMain.exam_id && s.std_name == this.stdMain.std_name) {
          this.stdMain.showSpecialColor = true;
          break;
        }
      }
    }); 
  }

  goBack() {
    this.curStep = "main";
  }

  requestData() {
    this.pager.reload();
  }

  //材料查看
  showMaterilModal(v) {
    this.onModalClose = () => {
      if (!this.stdPager) {
        this.detectRef.detectChanges();
      }
      this.stdPager.reload();//刷新详情列表
    };
    let imageList = [];
    //查询当前考生材料要展示的
    AjaxService.requestArray("std/apply/upload/list.htm",{detail_id: v.detail_id},(array)=>{
      this.uploadList = array;
      for (const u of this.uploadList) {
        if (u.file_type.toLowerCase().indexOf('jpg') >= 0 || u.file_type.toLowerCase().indexOf('png') >= 0) {
          imageList.push(u.file_path);
          
        } else if (u.file_type.toLowerCase() == 'pdf') {
          v.file_path = u.file_path;
          this.showComponentModal(InitialReviewPdfComponent, { 'ResponseEntity': v }, {class: 'modal-9p'});
          break;

        } else if (u.file_type.toLowerCase() == 'mp4') {
          v.file_path = u.file_path;
          this.showComponentModal(InitialReviewVideoComponent, { 'ResponseEntity': v }, {class: 'modal-7p'});
          break;

        }
      }
      //图片显示
      if (imageList.length > 0) {
        v.imageList = imageList;
        this.showComponentModal(InitialReviewImageComponent, { 'ResponseEntity': v }, {class: 'modal-7p'});
      }
    }); 
    
  }

  //打回回看（单个查看）以前的版本
  // showMaterilRejectModal(v) {
  //   let imageList = [];
  //   if(v.reject_url) {
  //     if(v.material_type=='pdf') {
  //       v.file_path = v.reject_url;
  //       this.showComponentModal(InitialReviewPdfLookComponent, { 'ResponseEntity': v }, {class: 'modal-9p'});
  //     } else if(v.material_type.toLowerCase().indexOf('jpg') >= 0 || v.material_type.toLowerCase().indexOf('png') >= 0) {
  //       imageList.push( v.reject_url);
  //     }
  //   }
  //   //图片显示
  //   if (imageList.length > 0) {
  //     v.imageList = imageList;
  //     this.showComponentModal(InitialReviewImageLookComponent, { 'ResponseEntity': v }, {class: 'modal-7p'});
  //   }
  // }

  showMaterilRejectModal(v) {
    let imageList = [];
    if(v.log_type=='pdf') {
      v.file_path = v.log_value;
      this.showComponentModal(InitialReviewPdfLookComponent, { 'ResponseEntity': v }, {class: 'modal-9p'});
    } else if(v.log_type.toLowerCase().indexOf('jpg') >= 0 || v.log_type.toLowerCase().indexOf('png') >= 0) {
      imageList.push( v.log_value);
    }
  //图片显示
  if (imageList.length > 0) {
    v.imageList = imageList;
    this.showComponentModal(InitialReviewImageLookComponent, { 'ResponseEntity': v }, {class: 'modal-7p'});
  }
  }


  //显示材料历史列表
  showRejectList(v) {
    this.AjaxService.requestArray('review/initial/reject/list.htm', {detail_id: v.detail_id, std_id: v.std_id}, (array) => {
      this.rejectList = array;
      this.curStep = 'reject';
    });
    //this.showComponentModal(InitialReviewRejectFileComponent, { 'ResponseEntity': v }, {class: 'modal-9p'});
  }


  //反馈考生
  doFeedBack() {
    if (!this.stdPager) {
      this.detectRef.detectChanges();
    }
    let notReview = false;
    for (const d of this.stdPager.getRecords()) {
      if (d.back_status != 'InitialAppr') {
        notReview = true;
      }
    }
    if (notReview) {
      MsgService.errorAlert("明细列表存在没有审核通过记录，请审核");
      return;
    }
    let _this = this;
    let param = {apply_id: this.query.main_id, initial_remark: ""};
    MsgService.confirmAlert('确定通过吗，该操作会反馈考生！', () => {
      MsgService.showLoading();
      AjaxService.request('review/initial/main/approve.htm', param, (entity) => {
        MsgService.hideLoading();
        MsgService.successToastr("初审通过!");
        this.curStep = "main";
        if (!this.pager) {
          this.detectRef.detectChanges();
        }
        this.pager.reload();
      });
    });
  }
  
  //退回
  doReject() {
    if (!this.stdPager) {
      this.detectRef.detectChanges();
    }
    let hasReject = false;
    let notReview = false;
    for (const d of this.stdPager.getRecords()) {
      if (d.back_status == 'Reject') {
        hasReject = true;
      }
      if (d.back_status == 'New' || d.back_status == 'Apply' || d.back_status == 'SecondApply') {
        notReview = true;
      }
    }
    if (notReview) {
      MsgService.errorAlert("明细列表存在没有审核记录，不能直接通过");
      return;
    }
    if (!hasReject) {
      MsgService.errorAlert("明细列表不存在退回状态，不能退回");
      return;
    }
    this.onModalClose = () => {
      this.curStep = "main";
    };
    this.showComponentModal(InitialReviewRejectComponent, { 'ResponseEntity': this.query }, {class: 'modal-7p'});
  }

  showTab(type) {
    alert(type);
    this.type = type;
  }

  backStd() {
    this.curStep = 'std';
  }

  downloadMaterial(v) {
    this.FuncService.ajaxDownload('review/initial/std/download.htm', {std_id: v.std_id});
  }

}
