import { Component, OnInit, Input, ViewChild, Output, EventEmitter } from "@angular/core";
import { FlexTableComponent } from "../../../../shared/common/FlexTable.component";
import { FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { BsModalService } from "ngx-bootstrap/modal";
import { FramePagerComponent } from "../../../../core/pager/pager.component";
import { FrameRequest } from "../../../../shared/frame/frame-request";
import * as _ from 'lodash';
import { AppComponent } from "../../../..";

@Component({
  selector: "std_payinvoicing",
  templateUrl: "./std_payinvoicing.component.html",
  styleUrls: ["./std_payinvoicing.component.scss"]
})
export class StdPayinvoicingComponent extends FlexTableComponent implements OnInit {
  public query = {
    "exam_time_id":"",
    "query":"",
    "status":""
  };

  QUERY_URL = "pay/payInvoicing/page.htm";
  DELETE_URL = "std/stdEnrol/delete.htm";
  GET_URL = "std/stdEnrol/get.htm";
  ADD_URL = "std/stdEnrol/add.htm";
  EDIT_URL = "std/stdEnrol/edit.htm";
  

  examAgentArray = [];
  courseList = [];
  flag: boolean = true;



  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
  }

  ngOnInit() {
    FrameRequest.requestEntity("pl/plExamTime/getCurrentExamTimeId.htm", {}, (entity) => {
      if(entity) {
        this.query.exam_time_id = entity.id;
        this.framePager.reload();
      }

    
      
    });

  }

  search(param){
    if(!this.query.exam_time_id){
      AppComponent.alert({ 'title': '提示', 'text': '请选择批次' });
      return;
    }

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

    let searchCondition = this.query;
    //参数合并
    _.assignIn(param,searchCondition);
    for (const key in param) {
      if (param.hasOwnProperty(key)) {
        const element = param[key];
        if(element == null) param[key] = "";
        
      }
    }
  }

  

 
  //下载电子发票
  downTicket(item){
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("pay/payInvoicing/downInvoicing.htm",  {bill_no: item.bill_no}, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载电子发票成功!");
    });

   //window.open('ses-upload/'+item.ticket_file);
   
  }

  //批量申请电子发票
  buildPayinvoicing() {
   
	if(!this.query.exam_time_id){
		AppComponent.alert({ 'title': '提示', 'text': '请选择批次' });
		return;
  }

    AppComponent.confirm("确定对受理中的申请单进行批量电子发票申请吗?",()=>{
      AppComponent.GobalComponent.setLoading(true);
      FrameRequest.requestMap("pay/payInvoicing/build.htm",  {exam_time_id:this.query.exam_time_id,again:false}, ()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("批量电子发票申请提交成功，后台进程正在处理中，请稍后查看发票申请结果!");
      });
   });
   
    
    }

    canclePayinvoicing(){
      

      if(!this.query.query){
        AppComponent.alert({ 'title': '提示', 'text': '请在关键字中输入订单编号。' });
        return;
      }
    
        AppComponent.confirm("确定对订单编号为"+this.query.query+"的订单进行发票冲红吗?",(ret)=>{
          AppComponent.GobalComponent.setLoading(true);
          
          FrameRequest.request("pay/payInvoicing/canclePayinvoicing.htm",  {bill_no:this.query.query}, (ret)=>{
           
            if (ret.errorCode == 0) {
              AppComponent.GobalComponent.successMsg("冲红发票成功!");
              this.framePager.reload();
            }else{
              AppComponent.GobalComponent.errorMsg(ret.errorMsg);
            }
            
            AppComponent.GobalComponent.setLoading(false);
          });
       });
    }



}
