import { Component, OnInit, TemplateRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { AppComponent } from '../../../../..';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { FrameFunction } from '../../../../../shared/frame/frame_function';

@Component({
  selector: 'app-by-turn-in-apply',
  templateUrl: './by-turn-in-apply.component.html',
  styleUrls: ['./by-turn-in-apply.component.scss']
})
export class ByTurnInApplyComponent  extends FlexTableComponent implements OnInit {

  public query = {
    query: "",
    time_config_id: "",
    prov_code: "",
    audit_school_id: "",
    autid_city_id: "",
    status:""
  };

  QUERY_URL = "by/byTurnInApply/page.htm";
  ADD_URL = "by/byTurnInApply/add.htm";
  TIME_URL = "by/byTurnTimeConfig/list.htm?turn_type=3";
  PROV_URL = "frame/zone/province/list.htm";
  TO_AUDIT_URL = "by/byTurnInApply/toAudit.htm";
  AUDIT_URL = "by/byTurnInApply/audit.htm";
  PRINT_URL = "by/byTurnInApply/print.htm";

  SCHOOL_URL = "cf/cfOrganization/list.htm?org_type=1&status=1";
  CITY_URL = "cf/cfOrganization/list.htm?org_type=2&status=1";
  PRINT_TURN_IN_URL = "by/byTurnInApply/printTurnin.htm";

  scoreList = []; //转考的课程信息
  apply: any; //转考信息
  appr_type: any;
  reject_advice: any;
  curOrg:any;
  turnin_photo_url: any; 

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      appr_type: [null, Validators.required],
      reject_advice: [null],
    });
  }

  ngOnInit() {
   
  }

  //转考审批界面
  showAudit(template: TemplateRef<any>, editShowType,item) {
    FrameRequest.requestMap(this.TO_AUDIT_URL, { id: item.id }, (map) => {
      this.scoreList = map['scoreList'];
      this.apply= map['apply'];
      this.curOrg= map['curOrg'];
      this.turnin_photo_url = map['turnin_photo_url'];

      this.editShowType = editShowType;
      this.reject_advice = "";
      this.appr_type = "";
    });
    
  }

  loadData() {
    let checkedArray = this.framePager.getCheckedArray((item) => item.id);
    if (checkedArray && checkedArray.length > 1) {
      AppComponent.alert({ title: '提示', text: '请选择一条记录' });
      return false;
    }
    if (checkedArray && checkedArray.length === 0) {
      AppComponent.alert({ title: '提示', text: '请选择审批数据' });
      return false;
    }
    FrameRequest.requestMap(this.TO_AUDIT_URL, { id: checkedArray.join() }, (map) => {
      this.scoreList = map['scoreList'];
      this.apply= map['apply'];
      this.curOrg= map['curOrg'];
      this.turnin_photo_url = map['turnin_photo_url'];
    });
    return true;
  }

  //审批
  audit() {
    if (!this.check()) return;
    FrameRequest.request(this.AUDIT_URL, {
      isPass: this.appr_type,
      reject_advice: this.reject_advice,
      id: this.apply.id
    }, () => {
      AppComponent.GobalComponent.successMsg("审核成功");
      this.hideModal();
      this.framePager.reload();
    });
  }

  //检查
  check() {
    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    if (this.appr_type === undefined) this.appr_type = "";
    if (this.reject_advice === undefined) this.reject_advice = "";
    if (this.appr_type === '') return true;
    if (Number(this.appr_type) === 0 && !this.reject_advice) {
      AppComponent.alert({ title: '提示', text: '请填写驳回意见' });
      return false;
    }
    return true;
  }

  printTurnin() {
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray && (checkedArray.length == 0 || checkedArray.length > 1)) {
      AppComponent.alert({ title: '提示', text: '请选择一条记录' });
      return ;
    }

    if(checkedArray[0].status == '1'){
      AppComponent.confirm("请不要忘记审核上报外省转入申请单",()=>{
          FrameRequest.download(this.PRINT_TURN_IN_URL, {id: checkedArray[0].id}, () => {
            AppComponent.GobalComponent.successMsg("转入登记表生成成功");
          });
      }); 
    }else{
      FrameRequest.download(this.PRINT_TURN_IN_URL, {id: checkedArray[0].id}, () => {
        AppComponent.GobalComponent.successMsg("转入登记表生成成功");
      });
    }

   
  }

}
