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-receipt',
  templateUrl: './by-turn-out-receipt.component.html',
  styleUrls: ['./by-turn-out-receipt.component.scss']
})
export class ByTurnOutReceiptComponent 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"; //登录用户所属机构类型
 // 回执处理
 RECEIPT_URL = "by/ByCoutryTurn/receipt.htm";


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

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

  ngOnInit() {
    FrameRequest.requestEntity(this.GET_DEPT_URL, {}, (entity) => {
      this.logOrgType = entity;
      if(this.logOrgType ==0){
        this.query.status="5";
      }
      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) {
    if (!this.loadData()) return;
    
    setTimeout(() => {
      this.editShowType = editShowType;
      this.appr_type = "";
      this.reject_advice = "";
    }, 400);
  }

  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() {

   // 省级账号只允许 
    if (this.logOrgType != 0) {
      AppComponent.alert({ title: '提示', text: '只允许省级账号处理。' });
      return ;
    }

      // 只允许处理转至省外的申请单
     if (this.logOrgType == 0 && this.applyInfo.status !='5') {
      AppComponent.alert({ title: '提示', text: '只允许处理状态是转至省外的申请单。' });
      return ;
    }

    if (!this.check()) return;
    AppComponent.confirm("确定处理转至外省回执吗?",()=>{
      FrameRequest.request(this.RECEIPT_URL, {
        receive_status: this.receive_status,
        back_reason: this.back_reason,
        id: this.applyInfo.id
      }, () => {
        AppComponent.GobalComponent.successMsg("处理转至外省回执成功");
        this.hideModal();
        this.framePager.reload();
      });
    });

    
  }

  //检查
  check() {

    if (!FrameFunction.validForm(this.formModel)) {
      return;
    }
    if (this.receive_status === undefined) this.receive_status = "";
    if (this.back_reason === undefined) this.back_reason = "";
    if (this.receive_status === '') return true;
    if (Number(this.receive_status) != 1 && Number(this.receive_status) != 2 && !this.back_reason) {
      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 ()=>{
            //刷新成绩显示
            this.loadData();
            AppComponent.GobalComponent.successMsg("删除转出成绩成功!");
        });
  }

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

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

}
