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 { Router,ActivatedRoute } from '@angular/router';
import { InitialReviewPageComponent } from '../../initial-review/initial-review-page/initial-review-page.component';
import { InitialReviewPdfComponent } from '../../initial-review/initial-review-pdf/initial-review-pdf.component';
import { InitialReviewVideoComponent } from '../../initial-review/initial-review-video/initial-review-video.component';
import { InitialReviewImageComponent } from '../../initial-review/initial-review-image/initial-review-image.component';
import { InitialReviewRejectComponent } from '../../initial-review/initial-review-reject/initial-review-reject.component';

@Component({
  selector: 'app-initial-progress-page',
  templateUrl: './initial-progress-page.component.html',
  styleUrls: ['./initial-progress-page.component.scss']
})
export class InitialProgressPageComponent extends FramePageComponent implements OnInit {
  @ViewChild('summeryPager', {static: false}) summeryPager: FramePager;
  @ViewChild('mainPager', {static: false}) mainPager: FramePager;
  @ViewChild('stdPager', {static: false}) stdPager: FramePager;

  uploadList = [];
  schoolArray = [];
  batchArray = [];
  curStep = 'summery';
  stdMain : any;//考生主记录

  query = {
    main_id : '',
    school_id : '',
    batch_id : '',
    optr_id : '',
    status : ''
  };
  constructor(private modalService: BsModalService, private router: Router, private detectRef: ChangeDetectorRef) {
    super(modalService);
  }

  ngOnInit() {
    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();
      } 
    });
  }

  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;
        this.requestData();
      }
    });   
  }

  requestData() {
    //debugger;
    this.summeryPager.pagerParams["school_id"] = this.query.school_id;
    this.summeryPager.pagerParams["batch_id"] = this.query.batch_id;
    this.summeryPager.reload();
  }

  //总人数
  goMain(v, status) {
    //debugger;
    this.curStep = "main";
    this.query.status = status;
    this.query.optr_id = v.optr_id;
  }

  specialList = [];
  goStdDetail(v) {
    this.curStep = "std";
    this.stdMain = v;
    this.query.main_id = v.apply_id;
    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(step) {
    debugger;
    this.curStep = step;
  }

   //材料查看
   showMaterilModal(v) {
    //debugger;
    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)=>{
      //debugger;
      this.uploadList = array;
      for (const u of this.uploadList) {
        if (u.file_type.toLowerCase() == 'jpg' || u.file_type.toLowerCase == 'png') {
          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-3p'});
          break;

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

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

  //反馈考生
  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.mainPager) {
          this.detectRef.detectChanges();
        }
        this.mainPager.reload();
      });
    });
  }
  
  //退回
  doReject() {
    debugger;
    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'});
  }

  //初审结束时间过后、审核不通过的发消息通知
  endTimeFailNotice() {
    let param = {
      batch_id: this.query.batch_id
    };
    MsgService.confirmAlert('确定发微信通知吗！', () => {
      MsgService.showLoading();
      AjaxService.requestEntity("review/initial/notice/fail.htm",param,(entity)=>{
        MsgService.hideLoading();
        MsgService.successToastr("发送成功!");
      });
    });
  }
  
}
