import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
import { FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { BsModalService } from "ngx-bootstrap/modal";
import { FlexTableComponent } from "../../../../shared/common/FlexTable.component";
import { AppComponent } from "../../../../app.component";
import { FrameRequest } from "../../../../shared/frame/frame-request";
import { FramePagerComponent } from "../../../../core/pager/pager.component";
import { FrameValidator } from "../../../../shared/frame/Frame-validator";
import { FrameUploaderComponent } from '../../../../core/uploader/uploader.component';
import { ActivatedRoute,Params } from "@angular/router";

@Component({
  selector: "app-std-std_enrol_volume",
  templateUrl: "./std-enrol-volume.component.html",
  styleUrls: ["./std-enrol-volume.component.scss"]
})
export class StdEnrolVolumeComponent extends FlexTableComponent
  implements OnInit {

  public query = {
    year_code: "",
    enrol_agent_id: ""
  };
 
  QUERY_URL = "std/stdEnrolVolume/page.htm";
  EDIT_URL = "std/stdEnrolVolume/edit.htm";
  GET_URL = "pl/stdEnrolVolume/get.htm";
  ADD_URL = "pl/stdEnrolVolume/add.htm";

  TIME_URL = "pl/plExamTime/listtime.htm";
 

  CURR_YEAR_CODE_URL = "pl/plExamTime/getCurrentYearCode.htm";
  YEAR_CODE_URL = "pl/plExamTime/list.htm";
  AREA_LIST_URL = "cf/cfOrganization/list.htm?org_type=4&status=1"; //考区列表
  GET_DEPT_URL = "frame/optr/getDeptType.htm"; //登录用户所属机构类型
  
  examAgentArray = [];
  yearCodeArray = [];
  logOrgType = "-1";

  constructor(
    public bsModalService: BsModalService,
	fb: FormBuilder,
	private activeInfo:ActivatedRoute
  ) {
    super();
    
    this.formModel = fb.group({
      id: [null],
      'year_code': [null, Validators.required],
      'enrol_agent_id': [null, Validators.required],
      'exam_batch': [null, Validators.required],
      'max_volume': [null, Validators.required],
      
    });
  }

  search(param) {
    if (!this.query.enrol_agent_id) {
      this.query.enrol_agent_id = "";
    }
    if (!this.query.year_code) {
      this.query.year_code = "";
    }

    
    param.year_code = this.query.year_code;
    param.enrol_agent_id = this.query.enrol_agent_id;
   
   
  }

  ngOnInit() {
    
    FrameRequest.requestArray(this.YEAR_CODE_URL,{}, async (array) => {
      this.yearCodeArray = array;
    });

    FrameRequest.requestEntity(this.GET_DEPT_URL, {}, (entity) => {
      this.logOrgType = entity;
      // 考试院账号，则展示 所有考区
      if(this.logOrgType == '0'){
        FrameRequest.requestArray(this.AREA_LIST_URL,{}, async (array) => {
          this.examAgentArray = array;
        });
      }else{
        // 非考试院账号，只展示当前考区
        FrameRequest.requestEntity("cf/cfOrganization/queryExamArea.htm",{}, async (entity) => {
          this.examAgentArray = [entity];
          
        });
      }

    });

    FrameRequest.requestEntity(this.CURR_YEAR_CODE_URL, {}, (entity) => {
      this.query.year_code = entity;
      this.framePager.reload();
    });

  }


  deleteVolume() {
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray.length === 0) {
      AppComponent.alert({ 'title': '提示', text: '请选择等待删除的记录！' });
      return;
    }

    AppComponent.confirm("确定删除选中的考位限制记录吗？", () => {
      let checkIds = this.framePager.getCheckedArray((item) => item.id);
      FrameRequest.request("pl/stdEnrolVolume/delete.htm", { ids: checkIds.join()}, () => {
        AppComponent.GobalComponent.successMsg("删除成功");
        this.framePager.reload();
      });
    });
  }

  deleteOne(volume) {
    if(null == volume){
      AppComponent.alert({ 'title': '提示', text: '请选择等待删除的记录！' });
      return;
    }

    AppComponent.confirm("确定删除选中的考位限制记录吗？", () => {
      FrameRequest.request("pl/stdEnrolVolume/delete.htm", { ids: volume.id}, () => {
        AppComponent.GobalComponent.successMsg("删除成功");
        this.framePager.reload();
      });
    });
  }




}