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 {InitialReviewDetailComponent} from '../../initial-review/initial-review-detail/initial-review-detail.component';

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

  constructor(private modalService: BsModalService, private detectRef: ChangeDetectorRef) {
    super(modalService);

  }
  @ViewChild('stdPager', {static: false}) stdPager: FramePager;
  uploadList = [];
  schoolArray = [];
  batchArray = [];
  tagArray = [];

  query = {
    main_id : '',
    school_id : '',
    batch_id : '',
    tag_ids : '',
    main_status : ''
  };
  curStep = 'main';
  stdMain : any;// 考生主记录

  specialList = [];
  specialWarn = false;

  // 检查批次模式对应的功能按钮
  // ****************批次模式对应的功能按钮*******************start*************************************
  authDtlDurSubmit = false;// 提交期间-是否明细审核按钮
  authMainDurSubmit = false;// 提交期间-是否显示审核主记录按钮
  msgDurSubmit = false;// 提交期间-审核主记录是否发送消息通知
  msgDurAuth = false;// 资格审核期间-审核主记录是否发送消息通知
  enableAuthLable = false;// 资格审核期间-是否开启标签

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

  queryAuthTagArray() {
    this.AjaxService.requestArray('./cf/authtag/list.htm', {school_id: this.query.school_id}, (array) => {
      this.tagArray = array;
    });
  }

  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() {
    // this.pager.pagerParams.school_id = this.query.school_id;
    // this.pager.pagerParams.batch_id = this.query.batch_id;
    // this.pager.pagerParams.tag_ids = this.query.tag_ids;
    this.pager.reload(this.query);
  }

  // 材料查看
  showMaterilModal(v) {

    this.onModalClose = () => {
      if (!this.stdPager) {
        this.detectRef.detectChanges();
      }
      this.stdPager.reload();// 刷新详情列表
    };
    v.std_name=this.stdMain.std_name;
    const imageList = [];
    // 查询当前考生材料要展示的
    AjaxService.requestMap('std/apply/upload/list.htm',{detail_id: v.detail_id},(map)=>{
      //

      v.textAttrs=map.textAttrs;
      v.uploadList=map.uploadList;
      this.showComponentModal(InitialReviewDetailComponent, { ResponseEntity: v }, {class: 'modal-8p'});
    });

  }
  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;
    // 检查批次对应模式功能按钮
    this.checkPattern();

    // 查询限制、特殊处理考生列表
    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';
  }
  // ****************批次模式对应的功能按钮********************end*************************************
  checkPattern() {
    // 获取当前服务器时间
    AjaxService.requestEntity('cf/pattern/now/get.htm',{},(data)=>{
      const now_time = Date.parse(data);

      if (this.FrameService.PatternMap) {
        const batch = this.FrameService.BatchMap[this.query.batch_id];
        const pattern = this.FrameService.PatternMap[batch.pattern_id];
        const uploadStartTime = Date.parse(batch.upload_start_time);
        const uploadEndTime = Date.parse(batch.upload_end_time);
        const intialReviewStart = Date.parse(batch.intial_review_start);
        const intialReviewEnd = Date.parse(batch.intial_review_end);
        // ******是在提交期间
        console.log('now_time: ' + now_time + ',uploadStartTime: ' + uploadStartTime + ',uploadEndTime: ' + uploadEndTime);
        if (now_time >= uploadStartTime && now_time <= uploadEndTime) {
          if (pattern.is_auth_dtl_dur_submit) {// 提交期间-审核明细 开启
            this.authDtlDurSubmit = true;
          }
          if (pattern.is_auth_main_dur_submit) {// 提交期间-审核主记录 开启
            this.authMainDurSubmit = true;
          }
          if (pattern.is_msg_dur_submit) {// 提交期间-审核主记录是否实时发送消息通知
            this.msgDurSubmit = true;
          }
        }
        // ******是在审核期间
        if (now_time >= intialReviewStart && now_time <= intialReviewEnd) {
          if (pattern.is_msg_dur_auth) {// 资格审核期间-审核主记录是否实时发送消息通知
            this.msgDurAuth = true;
          }
          if (pattern.is_enable_auth_lable) {// 资格审核期间-是否开启主记录标签
            this.enableAuthLable = true;
          }
        }
        // ******
      }

    });
  }

  export() {
    this.FuncService.ajaxDownload('review/std/auth/tag/export.htm',
      {school_id: this.query.school_id, batch_id: this.query.batch_id, tag_ids: this.query.tag_ids});
  }

}
