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

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

  public query = {
    query: "",
    time_config_id: "",
    prov_code: "",
    status:""
  };
  QUERY_URL = "by/byTurnOutApply/page.htm";
  ADD_URL = "by/byTurnOutApply/add.htm";
  TIME_URL = "by/byTurnTimeConfig/list.htm?turn_type=2";
  PROV_URL = "frame/zone/province/list.htm";
  TO_AUDIT_URL = "by/byTurnOutApply/toAudit.htm";
  AUDIT_URL = "by/byTurnOutApply/audit.htm";
  PRINT_URL = "by/byTurnOutApply/print.htm";
  PRINT_PAGE_URL = "by/byTurnOutApply/printPage.htm";
  PRINT_TURN_OUT_URL = "by/byTurnOutApply/printTurnout.htm"

  GET_DEPT_URL = "frame/optr/getDeptType.htm"; //登录用户所属机构类型


  courseList = []; //转考的课程信息
  applyInfo: any; //转考信息
  appr_type: any;
  reject_advice: any;
  logOrgType:any;

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

  ngOnInit() {
    FrameRequest.requestEntity(this.GET_DEPT_URL, {}, (entity) => {
      this.logOrgType = entity;
      if(this.logOrgType ==0){
        this.query.status="3";
      }
      this.framePager.reload();
    });
  }

  changeStatus(){
    
    if (!this.query.time_config_id) {
      AppComponent.alert({ 'title': '提示', text: '请选择转考批次' });
      return;
    }

    AppComponent.confirm("确定将该转考批次状态是省级审核通过等待转出的申请单的状态全部修改成转至省外吗？", () => {
      FrameRequest.request("by/byTurnOutApply/changeStatus.htm", { time_config_id: this.query.time_config_id}, () => {
        AppComponent.GobalComponent.successMsg("转至省外成功");
        this.framePager.reload();
      });
    });
  }

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

  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.courseList = map['courseList'];
      this.applyInfo = map['apply'];
    });
    return true;
  }

  //审批
  audit() {
    // 省级账号只允许 审核省级审核3的申请单
    if (this.logOrgType == 0 && this.applyInfo.status !='3' && this.applyInfo.status !='8') {
      AppComponent.alert({ title: '提示', text: '只允许审核状态是省级审核的申请单。' });
      return ;
    }

    // 地市账号只允许 审核待一级审核1的申请单
    if (this.logOrgType != 0 && this.applyInfo.status !='1') {
      AppComponent.alert({ title: '提示', text: '只允许审核状态是待一级审核的申请单。' });
      return ;
    }

    if (!this.check()) return;
    AppComponent.confirm("确定审核吗?",()=>{
      FrameRequest.request(this.AUDIT_URL, {
        appr_type: this.appr_type,
        reject_advice: this.reject_advice,
        id: this.applyInfo.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;
  }

  //解决通过flex-select查询参数传递的问题
  search(param) {
    for (const key in this.query) {
      if (this.query[key] === undefined || this.query[key] === null) {
        this.query[key] = "";
      }
    }
    let searchCondition = this.query;
    //参数合并
    _.assignIn(param, searchCondition);
  }

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

  printPage() {
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray && (checkedArray.length == 0 || checkedArray.length > 1)) {
      AppComponent.alert({ title: '提示', text: '请选择一条记录' });
      return ;
    }
    
    if(this.logOrgType != 0){
        AppComponent.confirm("请不要忘记审核上报转至外省申请单",()=>{
          FrameRequest.download(this.PRINT_PAGE_URL, {id: checkedArray[0].id,std_name:checkedArray[0].std_name});
      }); 
    }else{
      FrameRequest.download(this.PRINT_PAGE_URL, {id: checkedArray[0].id,std_name:checkedArray[0].std_name});
    }

  }

  //转出申请表
  printTurnout() {
    let checkedArray = this.framePager.getCheckedArray((item) => item.id);
    if (checkedArray && (checkedArray.length == 0 || checkedArray.length > 1)) {
      AppComponent.alert({ title: '提示', text: '请选择一条记录' });
      return ;
    }
    FrameRequest.download(this.PRINT_TURN_OUT_URL, {id: checkedArray.join()}, () => {
      AppComponent.GobalComponent.successMsg("转出登记表生成成功");
    });
  }

  deleteScore(it){
    
      let parm = {
        id:it.id 
      };

      
        FrameRequest.request("by/byTurnOutApply/deleteScore.htm", parm, async ()=>{
            //刷新成绩显示
           
            AppComponent.GobalComponent.successMsg("删除转出成绩成功!");
            FrameRequest.requestMap(this.TO_AUDIT_URL, { id: this.applyInfo.id }, (map) => {
              this.courseList = map['courseList'];
            });
        });
  }

  resetScore(){
    let parm = {
      apply_id: this.applyInfo.id
    };

    AppComponent.confirm("确定重置转出成绩，重新从统考成绩表导入成绩吗?",()=>{
      FrameRequest.request("by/byTurnOutApply/resetScore.htm", parm, async ()=>{
          //刷新成绩显示
          
          AppComponent.GobalComponent.successMsg("重置转出成绩成功!");
          FrameRequest.requestMap(this.TO_AUDIT_URL, { id: this.applyInfo.id }, (map) => {
            this.courseList = map['courseList'];
          });
      
      });
  });
}

}
