import { Component, OnInit } from '@angular/core';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { BsModalService } from 'ngx-bootstrap/modal';
import {MsgService} from "../../../../../core/service/msg.service";

@Component({
  selector: 'app-shichang-prepare',
  templateUrl: './shichang-prepare.component.html',
  styleUrls: ['./shichang-prepare.component.scss']
})
export class ShichangPrepareComponent extends FramePageComponent implements OnInit {

  currentStep = 'Init';
  cert_id : any;
  material = [];
  materials:any;
  std: any;
  subjectArr = [];
  coutDown: number = 90;
  private intervalId: any;
  count_flag = false;


  constructor(modalService: BsModalService) {
    super(modalService);
  }

  ngOnInit(): void {
  }

  changeCert() {
    this.count_flag = false;
    this.material = [];
    this.std = {};
    this.currentStep = 'Init';
    this.materials = [];
    this.AjaxService.requestMap('sc/readmaterial.htm', {cert_id: this.cert_id}, (map) => {
        this.materials = map['Material'];
        this.std = map['StdReg'];
        this.coutDown = map['CountDown'];
        if(this.std!==undefined||this.std.name!=null)
          this.currentStep = 'Prepare';
    });
  }

  //考试备考
  start() {
    if(this.count_flag)
      return;
    this.currentStep = 'Doding';
    this.count_flag = true; 
    var intervalId = setInterval(() => {
      console.log(this.coutDown);
      if(this.coutDown > 0) {
        this.coutDown--;
      } else if(this.coutDown == 0)  {
        this.end();
      }
      if(!this.count_flag)
        clearInterval(intervalId);
    },1000);

    if(this.materials && this.materials.length > 0) {
      this.material = this.materials[0].material_name.split(',');
      // if(arr && arr.length > 0) {
      //   this.one_pic = arr[0];
      //   this.two_pic = arr[1];
      // }
    }
  }

  end() {
    clearTimeout(this.intervalId);
    this.AjaxService.requestMap('sc/readmaterialend.htm', {std_id: this.std.std_id}, (map) => {
      MsgService.successToastr("备考完成！");
      this.count_flag = false;
      this.material = [];
      this.materials[0].read_end_time=1;
      this.std = {};
      this.subjectArr = [];
      this.cert_id = null;
      this.currentStep = 'Init';
    });
  }
}
