import {Component, ViewChild,OnInit} 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 { FramePagerComponent } from "../../../../core/pager/pager.component";



@Component({
  selector: 'app-std-refund-final',
  templateUrl: './std-refund-final.component.html',
  styleUrls: ['./std-refund-final.component.scss']
})
export class StdRefundFinalComponent extends FlexTableComponent implements OnInit {
  
  @ViewChild('kwLayoutDetailPager')
  kwLayoutDetailPager:FramePagerComponent;
  @ViewChild('detailPager')
  detailPager:FramePagerComponent;
  @ViewChild('codePager')
  codePager:FramePagerComponent;

  public query = {
    year_code:"",
    std_name: "",
    cert_no: "",
    ticket_no: "",
    type_name: "",
    status:"",
    bill_no:"",
    audit_optr:"",
    audit_optr_b:"",
    status_a:"",
    status_b:"",
  };

  applyList=null;
  audit_type=null;
  description=null;
  

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

  CODE_QUERY_URL = "std/healthcode/page.htm";
  QUERY_DETAIL_URL = "kj/kjUnifiedScoreDetail/page.htm";
  QUERY_KWDETAIL_URL = "kw/kwLayoutDetail/queryKwLayoutDetailByStd.htm";

  

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
    
      audit_type: [null, Validators.compose([Validators.required])],
      description: [null, Validators.compose([Validators.required])],
    });
  }

  ngOnInit() {
    

  }

  async aftGoEditEvent(entity?){
    this.kwLayoutDetailPager.reload();
    this.detailPager.reload();
    this.codePager.reload();
    
  }

  showEditWin(batchEditTemplateModal){
    
    this.applyList = this.framePager.getCheckedArray();
      if (this.applyList.length == 0) {
        AppComponent.alert({title:"信息提示",text:"请选择退款申请!"});
        return;
      }
      let ticket_no = '';
      let index = 0;
      let is_show = true;
      this.applyList.forEach((item) => {
        if(index == 0){
          ticket_no = item.ticket_no;
        }

        if(ticket_no != item.ticket_no){
          is_show = false;
          AppComponent.alert({title:"信息提示",text:"请选择同一考生!"});
          return;
        }

        

        index++;
     });

     if(is_show){
       this.item = this.applyList[0];
       this.editShowType = 'only';

       this.kwLayoutDetailPager.reload();
       this.detailPager.reload();
       this.codePager.reload();
     
     // this.bsModalWin = this.bsModalService.show(batchEditTemplateModal, { class: 'modal-lg' });
     }

  }

  audit (){
    

    if(!this.item.audit_type){
      AppComponent.alert({title:"信息提示",text:"请选择审核结果!"});
      return;
    }

    if(!this.item.description){
      AppComponent.alert({title:"信息提示",text:"请填写审核意见!"});
      return;
    }

    let info = "";
    if(this.item.audit_type == '2'){
      info = "确定审核通过退款申请单吗?";
    }else{
      info = "确定审核不通过退款申请单吗?";
    }

    AppComponent.confirm(info,()=>{
      this.item.status = this.item.audit_type;
      //审核A
      this.item.audit = 'B';
      FrameRequest.request("std/refund/finalAudit.htm", this.item, (ret) => {

        if (ret.success) {
          //提示
          AppComponent.GobalComponent.successMsg('审核成功');
          //重新加载列表
          this.framePager.reload();
          this.hideModal();
      }
    
  });

     
  });
}

batchAudit (){
  if(!this.audit_type){
    AppComponent.alert({title:"信息提示",text:"请选择审核结果!"});
    return;
  }

  if(!this.description){
    AppComponent.alert({title:"信息提示",text:"请填写审核意见!"});
    return;
  }

  let info = "";
  if(this.audit_type == '2'){
    info = "确定审核通过退款申请单吗?";
  }else{
    info = "确定审核不通过退款申请单吗?";
  }

    AppComponent.confirm(info,()=>{
      
        this.applyList.forEach((item) => {
          
          if(item && item.id){
           
            item.status = this.audit_type;
            item.description = this.description;
            //审核A
            item.audit = 'B';
           
            FrameRequest.request("std/refund/finalAudit.htm", item, (ret) => {

                  if (ret.success) {
                    //提示
                    AppComponent.GobalComponent.successMsg('批量审核成功');
                    //重新加载列表
                    this.framePager.reload();
                    this.hideModal();
                }
              
            });

          }

          
      });

  });
}


codeSearch(param) {
  param.ticket_no = this.item.ticket_no;
  param.year_code = this.item.year_code;
}

searchKjUnifiedScore(param) {
  param.ticketNo = this.item.ticket_no;
  param.yearCode = this.item.year_code;
}

searchKwLayoutDetail(param) {
  param.ticket_no = this.item.ticket_no;
  param.year_code = this.item.year_code;
}

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

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

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

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

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

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

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

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

    if (!this.query.audit_optr_b) {
      this.query.audit_optr_b = "";
    }
    
    param.bill_no = this.query.bill_no;
    param.std_name = this.query.std_name;
    param.cert_no = this.query.cert_no;
    param.ticket_no = this.query.ticket_no;
    param.year_code = this.query.year_code;
    param.status = this.query.status;
    param.status_a = this.query.status_a;
    param.status_b = this.query.status_b;
    param.audit_optr = this.query.audit_optr;
    param.audit_optr_b = this.query.audit_optr_b;

  }

  exportExcel(){
    let reqParam = this.query;

    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("std/refund/export.htm", reqParam, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("导出成功");
    });

  }

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

    let param = {
      year_code:this.query.year_code
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("std/refund/exportExcelForRefund.htm", param, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("财务退款信息下载成功!");
    });
  }

  carouselContentFilter(str) {
    
    if(str){
        var carContent ='';
        if(str.length >= 20){
           
            carContent = str.substring(0,20) + '...';
        }
        else {
            carContent = str
        }
        return carContent;
    }else{
      return str;
    }
}

carouselContentFilter2(str) {
    
  if(str){
      var carContent ='';
      if(str.length >= 20){
         
          carContent = str.substring(0,20) + '...';
      }
      else {
          carContent = str
      }
      return carContent;
  }else{
    return str;
  }
}


changeRemark(description){
  this.item.description=description;
}

batchChangeRemark(description){
  this.description=description;
}

}
