import { Component, OnInit,ViewChild } from '@angular/core';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { AppComponent } from '../../../../..';
import { FramePagerComponent } from '../../../../../core/pager/pager.component';
import { BsModalService } from 'ngx-bootstrap/modal';

@Component({
  selector: 'app-junior-std-record-audith',
  templateUrl: './junior-std-record-audith.component.html',
  styleUrls: ['./junior-std-record-audith.component.scss']
})
export class JuniorStdRecordAudithComponent extends FlexTableComponent implements OnInit {

  public query = {
    "year_code": "",
    "school_id": "",
    "join_school_id":"",
    "mr_id": "",
    "status": "",
    "ticket_no" : "",
    "is_xj":"",
    "major_code":""
  };

  QUERY_URL = "pl/plStdRecord/provincePage.htm?std_type=1";
  SCHOOL_URL = "cf/cfOrganization/list.htm?org_type=1&status=1"; //主考学校
  JION_SCHOOL_URL = "cf/cfOrganization/queryOrgForStdRecordBySchoolId.htm?org_type=5"; //衔接学校
  MAJOR_LIST_URL = "pl/plMajorRecord/list.htm";
  YEAR_CODE_URL = "pl/plExamTime/list.htm";
  AUDIT_URL = "pl/plStdRecord/audit.htm";
  SCHOOL_NAME_URL = "cf/cfOrganization/get.htm ";
  MAJOR_NAME_URL = "pl/plMajorRecord/find.htm";
  SCHOOL_FILE_URL = "pl/plStdRecordFile/download.htm";
  FIND_URL = "pl/plStdRecordFile/find.htm";
  LASTEST_YEAR_CODE_URL = "pl/plStdRecordTime/getBatch.htm";

  SCHOOL_LIST_URL = "pl/plMajorApplyJoinSchool/list.htm"; //查找主考学校下所有的衔接学校


  majorList = [];
  joinSchoolList = [];
  apply_form = 1; //专本衔接考生
  source_status = 2;//待考试院审核
  dest_status = 3; //审核通过并归档
  refuse_status = 4; //驳回
  shcool_name: any; //主考学校名称
  major_name: any; //专业名称

  @ViewChild('mainPager')
  mainPager:FramePagerComponent;

  constructor(public bsModalService: BsModalService) {
    super();
  }

  ngOnInit() {
    FrameRequest.requestArray("pl/plMajor/queryMajorByStatus.htm", {status:1,major_level:1}, (arrary) => {
      this.majorList = arrary;
    });

    setTimeout(() => {
      FrameRequest.requestEntity(this.LASTEST_YEAR_CODE_URL, {std_type: this.apply_form}, (entity) => {
        if (entity) {
          this.query.year_code = entity.year_code;
          //this.chooseMajor();
          this.framePager.reload();
        }
      });
    },500);
  }

  chooseMajor() {
    
    FrameRequest.requestArray(this.MAJOR_LIST_URL, {
      year_code: this.query.year_code === null ? ''  : this.query.year_code ,
      school_id: this.query.school_id === null ?  '' : this.query.school_id ,
      apply_form: this.apply_form
    }, (array) => {
      this.majorList = array;
    });

    FrameRequest.requestArray(this.JION_SCHOOL_URL, {
      year_code: this.query.year_code === null ? ''  : this.query.year_code ,
      school_id: this.query.school_id === null ?  '' : this.query.school_id ,
      apply_form: this.apply_form
    }, (array) => {
      this.joinSchoolList = array;
    });

    
  }

  search(param) {
    param.status = this.query.status;
    if (!this.query.year_code) {
      this.query.year_code = "";
    }
    param.year_code = this.query.year_code;
    if(!this.query.mr_id) {
      this.query.mr_id = "";
    }
    param.mr_id = this.query.mr_id;
    if(!this.query.school_id) {
      this.query.school_id = "";
    }
    param.school_id = this.query.school_id;

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

    param.ticket_no = this.query.ticket_no;

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

    if(!this.query.major_code) {
      this.query.major_code = "";
    }
    param.major_code = this.query.major_code;
  }
  
  
  auditXj() {
    if (!this.query.year_code) {
      AppComponent.alert({ title: "信息提示", text: "请选择批次!" });
      return true;
    }

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

    let msg = null;
    if (!this.query.mr_id) {
      if(!this.query.school_id){
        msg = "确定要审核通过并归档所有学籍认证通过的考生吗？";

      }else{
        this.query.mr_id = '';
        FrameRequest.requestEntity(this.SCHOOL_NAME_URL, { id: this.query.school_id }, (entity) => {
          if (entity) {
            this.shcool_name = entity.org_name;
            msg = "确定要审核通过并归档" + (this.shcool_name === undefined ? "" : this.shcool_name) + "所有学籍认证通过的考生吗？";
          }
        });
      }
      
    } else {
      
      if(this.query.school_id){
        FrameRequest.requestEntity(this.SCHOOL_NAME_URL, { id: this.query.school_id }, (entity) => {
          if (entity) {
            this.shcool_name = entity.org_name;
          }
        });

      }else{
        this.shcool_name = '';
      }
      
      FrameRequest.requestEntity(this.MAJOR_NAME_URL, { id: this.query.mr_id }, (entity) => {
        if (entity) {
          this.major_name = entity.major_name;
          msg = "确定要审核通过并归档" + (this.shcool_name === undefined ? "" : this.shcool_name) +
            (this.major_name === undefined ? "" : this.major_name) + "专业学籍认证通过的考生吗？";
        }
      });

    }
    setTimeout(() => {
      AppComponent.confirm(msg, () => {
        FrameRequest.request(this.AUDIT_URL, {
          year_code: this.query.year_code,
          school_id: this.query.school_id,
          mr_id: this.query.mr_id,
          source_status: this.source_status,
          dest_status: this.dest_status,
          std_type: this.apply_form,
          is_xj:"1"
        }, () => {
          AppComponent.GobalComponent.successMsg("审核通过并归档成功");
          this.framePager.reload();
        });
      });
    }, 500);


  }

  //审核通过并归档
  audit() {
    if (!this.query.year_code) {
      AppComponent.alert({ title: "信息提示", text: "请选择批次!" });
      return true;
    }

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

    let msg = null;
    if (!this.query.mr_id) {
      if(!this.query.school_id){
        msg = "确定要审核通过并归档所有考生吗？";

      }else{
        this.query.mr_id = '';
        FrameRequest.requestEntity(this.SCHOOL_NAME_URL, { id: this.query.school_id }, (entity) => {
          if (entity) {
            this.shcool_name = entity.org_name;
            msg = "确定要审核通过并归档" + (this.shcool_name === undefined ? "" : this.shcool_name) + "所有考生吗？";
          }
        });
      }
      
    } else {
      
      if(this.query.school_id){
        FrameRequest.requestEntity(this.SCHOOL_NAME_URL, { id: this.query.school_id }, (entity) => {
          if (entity) {
            this.shcool_name = entity.org_name;
          }
        });

      }else{
        this.shcool_name = '';
      }
      
      FrameRequest.requestEntity(this.MAJOR_NAME_URL, { id: this.query.mr_id }, (entity) => {
        if (entity) {
          this.major_name = entity.major_name;
          msg = "确定要审核通过并归档" + (this.shcool_name === undefined ? "" : this.shcool_name) +
            (this.major_name === undefined ? "" : this.major_name) + "专业考生吗？";
        }
      });

    }
    setTimeout(() => {
      AppComponent.confirm(msg, () => {
        FrameRequest.request(this.AUDIT_URL, {
          year_code: this.query.year_code,
          school_id: this.query.school_id,
          mr_id: this.query.mr_id,
          source_status: this.source_status,
          dest_status: this.dest_status,
          std_type: this.apply_form
        }, () => {
          AppComponent.GobalComponent.successMsg("审核通过并归档成功");
          this.framePager.reload();
        });
      });
    }, 500);


  }


  checkCondtion() {
    if (!this.query.year_code) {
      AppComponent.alert({ title: "信息提示", text: "请选择批次!" });
      return true;
    }
    if (!this.query.school_id) {
      AppComponent.alert({ title: "信息提示", text: "请选择主考学校!" });
      return true;
    }
    // if (!this.query.mr_id) {
    //   AppComponent.alert({ title: "信息提示", text: "请选择专业!" });
    //   return true;
    // }
    return false;
  }

  //驳回学校
  reject() {
    if (this.checkCondtion()) {
      return;
    }
    let msg = null;
    if (!this.query.mr_id) {
      this.query.mr_id = '';
      FrameRequest.requestEntity(this.SCHOOL_NAME_URL, { id: this.query.school_id }, (entity) => {
        if (entity) {
          this.shcool_name = entity.org_name;
          msg = "是否驳回" + (this.shcool_name === undefined ? "" : this.shcool_name) + "所有考生的备案？"
        }
      });
    } else {
      FrameRequest.requestEntity(this.SCHOOL_NAME_URL, { id: this.query.school_id }, (entity) => {
        if (entity) {
          this.shcool_name = entity.org_name;
        }
      });
      FrameRequest.requestEntity(this.MAJOR_NAME_URL, { id: this.query.mr_id }, (entity) => {
        if (entity) {
          this.major_name = entity.major_name;
          msg = "是否驳回" + (this.shcool_name === undefined ? "" : this.shcool_name) +
            (this.major_name === undefined ? "" : this.major_name) + "专业考生的备案？";
        }
      });
    }
    setTimeout(() => {
      AppComponent.confirm(msg, () => {
        FrameRequest.request(this.AUDIT_URL, {
          year_code: this.query.year_code,
          school_id: this.query.school_id,
          mr_id: this.query.mr_id,
          source_status: this.source_status,
          dest_status: this.refuse_status,
          std_type: this.apply_form
        }, () => {
          AppComponent.GobalComponent.successMsg("已驳回学校");
          this.framePager.reload();
        });
      });
    }, 500);

  }


  //查看主考学校上报的文件
  checkSchoolFile() {
    if (this.checkCondtion()) {
      return;
    }
    FrameRequest.requestEntity(this.FIND_URL, { 
      year_code: this.query.year_code, 
      std_type: this.apply_form, 
      school_id: this.query.school_id}, 
      (entity) =>{
      if(!entity) {
        AppComponent.alert({ 'title': '提示', text: '该批次该主考学校还没有上传文件！' });
      } else {
        FrameRequest.download(this.SCHOOL_FILE_URL, { year_code: this.query.year_code, 
          std_type: this.apply_form,
          school_id: this.query.school_id });
      }
    });
  }

  //导出
  exportJion() {
    AppComponent.GobalComponent.setLoading(true);

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

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

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

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

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

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

    FrameRequest.download("pl/plStdRecord/exportProvinceJion.htm", {
     mr_id:this.query.mr_id,
      year_code : this.query.year_code,
      status : this.query.status,
      ticket_no : this.query.ticket_no,
      school_id : this.query.school_id,
      join_school_id : this.query.join_school_id,
      is_xj:this.query.is_xj,
      std_type: this.apply_form,
      major_code: this.query.major_code
    }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("考生备案导出成功");
    });
  }

  //导出
  exportJionAndExists() {
    if (!this.query.year_code) {
      AppComponent.alert({ title: "信息提示", text: "请选择批次!" });
      return true;
    }
    AppComponent.GobalComponent.setLoading(true);

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

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

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

    FrameRequest.download("pl/plStdRecord/exportProvinceJionAndExists.htm", {
     mr_id:this.query.mr_id,
      year_code : this.query.year_code,
      status : this.query.status,
      ticket_no : this.query.ticket_no,
      school_id : this.query.school_id,
      join_school_id : this.query.join_school_id,
      std_type: this.apply_form
    }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("考生备案导出成功");
    });
  }

  goUploadFile(editFormTemplateModal, modalcss?) {
    if (!this.query.year_code) {
      AppComponent.alert({ 'title': '提示', text: '请选择批次！' });
      return;
    }
    let css = modalcss ? modalcss : "modal-lg";
    this.bsModalWin = this.bsModalService.show(editFormTemplateModal, { class: css });
  }
}
