import { Component, OnInit, TemplateRef, ViewChild, ViewContainerRef } 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 {FrameUploaderComponent} from '../../../../core/uploader/uploader.component';
import { AppComponent } from "../../../../app.component";
import { FrameRequest } from "../../../../shared/frame/frame-request";
import { FramePagerComponent } from "../../../../core/pager/pager.component";
import { FrameVariable } from "../../../../shared/frame/frame-variable";
import { FrameFunction } from "../../../../shared/frame/frame_function";




@Component({
  selector: "app-pl-majorapplyquery",
  templateUrl: "./majorapplyquery.component.html",
  styleUrls: ["./majorapplyquery.component.scss"]
})
export class MajorApplyQueryComponent extends FlexTableComponent
  implements OnInit {

  @ViewChild('framePager')
  framePager:FramePagerComponent;
  
  @ViewChild('auditModal')
  auditModal:TemplateRef<any>;

/**
 * 机构类型.
 */
 OrgTypeEnum = {
   PROVINCE_EXAM:0,
   SCHOOL_EXAM:1,
   CITY_EXAM:2,
   ASSIST_EXAM:3,
   EXAM_AREA:4,
   STUDY_CENTER:5
  };

  masterSchoolList = [];//主考学校
  majorList = [];//专业列表-列表查询
  curOrg = {id:"",org_code:"",org_name:"",contact_number:"",org_type:""};//当前登录人的机构

  majorApplyStatus={
    NEW_0: 0,
    TO_AUDIT_1: 1,
    TO_ARCHIVE_2: 2,
    ARCHIVED_3: 3
  };



  public query = {
    query:"",
    yearCode: "",
    masterSchoolId:"",  //主考学校
    applyForm: "",  //申报形式
    majorId: "" 
  };

  
  

  
  GET_APPLY_INFO = "pl/plMajorApply/applyinfo.htm";
  GET_URL = "pl/plMajorApply/get.htm";
  QUERY_URL = "pl/plMajorApply/page.htm";
  DOWNLOAD_URL = "pl/plMajorApply/download.htm";

  applyMajorList = [];//已经申请的专业列表
  authList = [];//审核记录

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      'auth_remark' : [null, Validators.compose([Validators.required])],
      'apply_no' : [null, Validators.compose([Validators.required])],
      'auth_type' : [null, Validators.compose([Validators.required])]
    });
  }

  ngOnInit() {
    //基础数据(主考学校列表、专业列表、当前登录机构对象)
    FrameRequest.requestMap("pl/plMajorApply/basedata.htm", {org_type:[1],status:1}, (map)=>{
     // this.masterSchoolList = map.schoolList;
      //this.majorList = map.majorList;
      this.curOrg = map.curOrg;
    });

    FrameRequest.requestArray("cf/cfOrganization/list.htm?org_type=1&status=1", {}, (array) => {
      this.masterSchoolList = array;
    });

  }

  search(param){
    if (!this.query.yearCode) {
      this.query.yearCode="";
    }
    if (!this.query.masterSchoolId) {
      this.query.masterSchoolId="";
    }
     if (!this.query.applyForm) {
       this.query.applyForm="";
     }
    if (!this.query.majorId) {
      this.query.majorId="";
    }
    param.yearCode=this.query.yearCode;
    param.masterSchoolId=this.query.masterSchoolId;
    param.applyForm=this.query.applyForm;
    param.majorId=this.query.majorId;
  }
  
  getKeys(item) {
    return Object.keys(item);
  }

  //进入详情代理
  proxyGoEdit(item) {
    this.getApplyBaseInfo(item.id);
    this.goEdit(this.auditModal,'only','modal-lg',item);
  }

  //获取申报审核基础数据
  getApplyBaseInfo(applyId) {
    FrameRequest.requestMap(this.GET_APPLY_INFO, {id: applyId}, async (map) => {
      this.authList = map.authList;
      this.applyMajorList = map.applyMajorList;
    });
  }

  //下载申报资料
  downloadApplyFile(applyId) {
    FrameRequest.download(this.DOWNLOAD_URL,  { id: applyId });
  }



}