import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
import { FormBuilder, Validators,FormGroup } 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 { FrameUploaderComponent } from "../../../../core/uploader/uploader.component";

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

  public query = {
    "query": "",
    "apply_status": "",
    "school_id":"",
    "join_school_id":""
  };

  
  audit_type : any;
  apply_form = 1; //专本衔接考生
  joinSchoolList = [];

  public syn = {
    "old_exam_time_id": null,
    "exam_time_id":null
  };

  courseShowType: boolean = false;
  yesStds = [];
  oldStds = [];
  majorList = [];

  noCourses = [];
  yearCodeList = [];
  course: any;
  exam_time_id: any;
  
  @ViewChild('framePager2')
  framePager2: FramePagerComponent;

  SCHOOL_URL = "cf/cfOrganization/list.htm?org_type=1&status=1"; //主考学校
  JION_SCHOOL_URL = "cf/cfOrganization/queryOrgForStdRecordBySchoolId.htm?org_type=5"; //衔接学校
  QUERY_URL = "pl/plStdRecordApply/page.htm";
  GET_URL = "pl/plStdRecordApply/find.htm";
  DELETE_URL = "pl/plStdRecordApply/delete.htm";
  STD_RECORD_PAGE_URL = "pl/plStdRecord/page.htm";
  ADD_URL = "pl/plStdRecordApply/audit.htm";
  EDIT_URL = "pl/plStdRecordApply/audit.htm";


  COURSE_URL = "pl/plTextbook/get.htm";
  ADD_COURSE_URL = "pl/plTextbook/addCourse.htm";
  
  DELETE_COURSE_URL = "pl/plTextbook/deleteCourse.htm";
  UPLOAD_URL = "pl/plTextbook/upload.htm";
  DOWNLOAD_URL = "pl/plTextbook/download.htm";
  ERROR_DOWNLOAD_URL = "pl/plTextbook/downloadError.htm";
  YEAR_CODE_URL = "pl/plExamTime/list.htm";
  UPDATE_BATCH_URL = "pl/plTextbook/updateBatch.htm";
  TEXTBOOK_SYNC_URL = "pl/plTextbook/synTextBook.htm";

  filePath: any;//错误文件路径
  @ViewChild('uploader')
  uploader: FrameUploaderComponent;
  public auditFormModel: FormGroup;

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

    this.auditFormModel = fb.group({
      id: [null],
     
      'audit_remark': [null, Validators.compose([Validators.required])],
      'audit_type' : [null, Validators.compose([Validators.required])],
      
    });
    
    
  }

  ngOnInit() {
    FrameRequest.requestArray(this.JION_SCHOOL_URL, {
      year_code: '' ,
      school_id:'' 
    }, (array) => {
      this.joinSchoolList = array;
    });

    FrameRequest.requestArray("pl/plMajor/queryMajor.htm", {}, (array) => {
      this.majorList = array;
    });


  }

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

    if (!this.query.school_id) {
      this.query.school_id = "";
    }

    if (!this.query.join_school_id) {
      this.query.join_school_id = "";
    }

    param.query = this.query.query;
    param.apply_status = this.query.apply_status;
    param.school_id = this.query.school_id;
    param.join_school_id = this.query.join_school_id;
    param.std_type = this.apply_form;
    
  }

  search2(param) {
    param.query = this.query.query;
    param.status = 3;
    
  }

  deleteApply(item){
    if (item.apply_status != '待考试院审核') {
      AppComponent.alert({title:"信息提示",text:"只能删除待考试院审核状态的申请单!"});
      return;
    }

    AppComponent.confirm("确定删除吗?",()=>{
      FrameRequest.requestMap(this.DELETE_URL, {id:item.id}, () => {
        AppComponent.GobalComponent.successMsg('删除成功!');
         this.framePager.reload();
      });
  });

    
  }

  async preGoEditEvent(item){
 
    this.yesStds = [];
    this.oldStds = [];
    
	  if (item) {
		 this.reload(item.id);
	  }else{
      this.item = {};
    }

	  return true;
  }

  audit(){
    //this.item.audit_type = this.audit_type;
    if(!this.item.audit_type){
      AppComponent.alert({ title: '信息提示', text: '请选择审批类型！' });
      return false;
    }

    if(!this.item.audit_remark){
      AppComponent.alert({ title: '信息提示', text: '请填写审批意见！' });
      return false;
    }

    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.request("pl/plStdRecordApply/audit.htm", this.item, (ret)=>{
      AppComponent.GobalComponent.setLoading(false);
      this.hideModal();
      this.framePager.reload();

      AppComponent.GobalComponent.successMsg("审核成功!");

    });
  }



  //重新加载
  reload(id) {
    
    FrameRequest.requestArray("pl/plStdRecordApply/queryStdByApplyId.htm", { id: id }, (array) => {
      this.yesStds = array;
    });

    FrameRequest.requestArray("pl/plStdRecordApply/queryOldStdByApplyId.htm", { id: id }, (array) => {
      this.oldStds = array;
    });
    
  }

  callback() {

    let checkedArray = this.framePager.getCheckedArray();

      if (checkedArray.length === 0 || checkedArray.length > 1) {
        AppComponent.alert({ title: "信息提示", text: "请选择一条数据!" })
        return;
      }

      if (checkedArray[0].apply_status != '审核通过并归档') {
        AppComponent.alert({ title: "信息提示", text: "只能撤回审核通过并归档的申请单!" })
        return;
      }

      AppComponent.confirm("确定撤回申请单吗?",()=>{
        FrameRequest.requestMap("pl/plStdRecordApply/callbackForAudit.htm", {apply_id:checkedArray[0].id,apply_status:'待考试院审核'}, () => {
          AppComponent.GobalComponent.successMsg('撤回成功!');
          this.framePager.reload();
        });
      });

  }
    

}