import { Component, OnInit, Input, ViewChild, Output, EventEmitter } from "@angular/core";
import { FlexTableComponent } from "../../../../shared/common/FlexTable.component";
import { FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { BsModalService } from "ngx-bootstrap/modal";
import { FramePagerComponent } from "../../../../core/pager/pager.component";
import { FrameRequest } from "../../../../shared/frame/frame-request";
import * as _ from 'lodash';
import { AppComponent } from "../../../..";

@Component({
  selector: "std_ticket",
  templateUrl: "./std_ticket.component.html",
  styleUrls: ["./std_ticket.component.scss"]
})
export class StdTicketComponent extends FlexTableComponent implements OnInit {
  public query = {
    "exam_time_id":"",
    "query":""
  };

  QUERY_URL = "std/stdTicket/page.htm";
  DELETE_URL = "std/stdTicket/delete.htm";
  GET_URL = "std/stdTicket/get.htm";
  ADD_URL = "std/stdTicket/add.htm";
  EDIT_URL = "std/stdTicket/edit.htm";
  COURSE_LIST_URL = "pl/plCourse/listCourseByExamTimeId.htm";
  AREA_LIST_URL = "cf/cfOrganization/list.htm?org_type=4&status=1"; //考区列表
  GET_TYPE_URL = "frame/optr/getDeptType.htm"; //获取登录用户的机构类型
  EXPORT_URL = "std/stdTicket/enrolExport.htm?isQuery=1";

  examAgentArray = [];
  courseList = [];
  flag: boolean = true;



  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
  }

  ngOnInit() {
    FrameRequest.requestEntity("pl/plExamTime/getCurrentExamTimeId.htm", {}, (entity) => {
      if(entity) {
        this.query.exam_time_id = entity.id;
        this.framePager.reload();
      }

      FrameRequest.requestArray(this.COURSE_LIST_URL,{exam_time_id:this.query.exam_time_id}, async (array) => {
        this.courseList = array;
      });
      
    });

    FrameRequest.requestArray(this.AREA_LIST_URL,{}, async (array) => {
      this.examAgentArray = array;
    });
   
    FrameRequest.requestEntity(this.GET_TYPE_URL, {}, (entity) => {
      if(entity == 0) {
        this.flag = false;
      } else {
        this.flag = true;
      }
    });

  }

  search(param){
    if(!this.query.exam_time_id){
      AppComponent.alert({ 'title': '提示', 'text': '请选择考试批次' });
      return;
    }

    let searchCondition = this.query;
    //参数合并
    _.assignIn(param,searchCondition);
    for (const key in param) {
      if (param.hasOwnProperty(key)) {
        const element = param[key];
        if(element == null) param[key] = "";
        
      }
    }
  }

  

 
  //下载准考证
  downTicket(item){
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("std/stdTicket/downTicket.htm",  {ticket_file: item.ticket_file,std_name:item.std_name}, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载准考证成功!");
    });

   //window.open('ses-upload/'+item.ticket_file);
   
  }

  //下载准考证
  buildTicket() {
   
	if(!this.query.exam_time_id){
		AppComponent.alert({ 'title': '提示', 'text': '请选择考试批次' });
		return;
	}
	FrameRequest.requestMap("std/stdTicket/buildTicketTime.htm",{exam_time_id:this.query.exam_time_id},(map)=>{
		if (map['inTime']) {
      
      if(map['is_build']){
            AppComponent.confirm("准考证已经生成，确定重新生成吗(预计三个小时完成)?",()=>{
              AppComponent.GobalComponent.setLoading(true);
              FrameRequest.requestMap("std/stdTicket/buildTicket.htm",  {exam_time_id:this.query.exam_time_id,again:false}, ()=>{
                AppComponent.GobalComponent.setLoading(false);
                AppComponent.GobalComponent.successMsg("准考证批量生成提交成功!");
              });
          });
      }else{
          AppComponent.confirm("确定批量生成准考证吗(预计三个小时完成)?",()=>{
            AppComponent.GobalComponent.setLoading(true);
            FrameRequest.requestMap("std/stdTicket/buildTicket.htm",  {exam_time_id:this.query.exam_time_id,again:false}, ()=>{
              AppComponent.GobalComponent.setLoading(false);
              AppComponent.GobalComponent.successMsg("准考证批量生成提交成功!");
            });
        });

         
      }
      
		}else{
			if (map['time']) {
				AppComponent.GobalComponent.errorMsg("不在准考证生成时间范围之内："+map['time'].start_time+" 到"+ map['time'].end_time);
			} else {
				AppComponent.GobalComponent.errorMsg("不在准考证生成时间范围之内,暂不允许生成准考证");
			}
			
		}
	});
   
    
    }


    //重新生成准考证
  buildTicketAgain() {
   
    if(!this.query.exam_time_id){
      AppComponent.alert({ 'title': '提示', 'text': '请选择考试批次' });
      return;
    }
    FrameRequest.requestMap("std/stdTicket/buildTicketTime.htm",{exam_time_id:this.query.exam_time_id},(map)=>{

              AppComponent.confirm("确定重新批量生成准考证吗(已生成的准考证将不再重新生成)?",()=>{
                AppComponent.GobalComponent.setLoading(true);
                FrameRequest.requestMap("std/stdTicket/buildTicket.htm",  {exam_time_id:this.query.exam_time_id,again:true}, ()=>{
                  AppComponent.GobalComponent.setLoading(false);
                  AppComponent.GobalComponent.successMsg("重新批量生成准考证提交成功!");
                });
            });
       
        
    
    });
     
      
      }

      //生成机考准考证
  buildTicketForComputerTest() {
   
    if(!this.query.exam_time_id){
      AppComponent.alert({ 'title': '提示', 'text': '请选择考试批次' });
      return;
    }
    
      AppComponent.confirm("确定批量生成机考准考证吗?",()=>{
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.requestMap("std/stdTicket/buildTicketForComputerTest.htm",  {exam_time_id:this.query.exam_time_id,again:false}, ()=>{
          AppComponent.GobalComponent.setLoading(false);
          AppComponent.GobalComponent.successMsg("机考准考证批量生成提交成功!");
        });
    });
     
      
      }
  
  
      //重新生成准考证
    buildTicketForComputerTestAgain() {
     
      if(!this.query.exam_time_id){
        AppComponent.alert({ 'title': '提示', 'text': '请选择考试批次' });
        return;
      }

      AppComponent.confirm("确定重新批量生成机考准考证吗(已生成的准考证将不再重新生成)?",()=>{
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.requestMap("std/stdTicket/buildTicketForComputerTest.htm",  {exam_time_id:this.query.exam_time_id,again:true}, ()=>{
          AppComponent.GobalComponent.setLoading(false);
          AppComponent.GobalComponent.successMsg("重新批量生成机考准考证提交成功!");
        });
    });
       
        
        }

}
