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-enrol-syn",
  templateUrl: "./std-enrol-syn.component.html",
  styleUrls: ["./std-enrol-syn.component.scss"]
})
export class StdEnrolSynComponent extends FlexTableComponent implements OnInit {
  public query = {
    "year_code": "",
    "ticket_no": "",
    "course_id": "",
    "enrol_agent_id": "",
    "exam_batch": "",
    "status": "",
    "exam_time_id":"",
    "special_flag":"",
    "enrol_type" : ""
  };

  QUERY_URL = "std/stdEnrol/pageStdEnrol.htm?isQuery=1";
  DELETE_URL = "std/stdEnrol/delete.htm";
  GET_URL = "std/stdEnrol/get.htm";
  ADD_URL = "std/stdEnrol/add.htm";
  EDIT_URL = "std/stdEnrol/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/stdEnrol/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] = "";
        
      }
    }
  }

  freshPay(){
    if(!this.query.ticket_no){
      AppComponent.alert({ 'title': '提示', 'text': '请输入准考证' });
      return;
    }

    AppComponent.confirm("确定更新考生支付状态吗?",()=>{

      
      FrameRequest.requestArray("std/stdEnrol/freshPay.htm",{ticket_no:this.query.ticket_no}, async (array) => {
        
      });
    });
  }

  export() {
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.EXPORT_URL, {exam_time_id: this.query.exam_time_id,org_id:this.query.enrol_agent_id}, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("导出成功");
    });
  }

 
  //报考数据同步自考前置库
  syn() {
    if(!this.query.exam_time_id){
      AppComponent.alert({ 'title': '提示', 'text': '请选择考期' });
      return;
    }
    
    AppComponent.confirm("确定同步报考数据到前置库吗",()=>{
      AppComponent.GobalComponent.setLoading(true);
      FrameRequest.requestMap("std/stdEnrol/syn.htm", {exam_time_id:this.query.exam_time_id}, () => {
        AppComponent.GobalComponent.successMsg('报考数据同步前置库成功');
        AppComponent.GobalComponent.setLoading(false);
      });
    })
      
  }

  //编排数据同步自考前置库
  synKw() {
    if(!this.query.exam_time_id){
      AppComponent.alert({ 'title': '提示', 'text': '请选择考期' });
      return;
    }
    
    AppComponent.confirm("确定同步编排数据到前置库吗",()=>{
      AppComponent.GobalComponent.setLoading(true);
      FrameRequest.requestMap("std/stdEnrol/synKw.htm", {exam_time_id:this.query.exam_time_id}, () => {
        AppComponent.GobalComponent.successMsg('编排数据同步前置库成功');
        AppComponent.GobalComponent.setLoading(false);
        
      });
    })
      
  }

}
