
import { FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { BsModalService } from "ngx-bootstrap/modal";

import * as _ from 'lodash';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { OnInit, Component } from '@angular/core';

import { FrameRequest } from '../../../../shared/frame/frame-request';
import { AppComponent } from '../../../..';

//审批状态
const APPLY_STATUS = {
    NEW: 0,//草稿
    LOCAL_VERIFY: 1,//待现场验证
    PROV_VERIFY:2,//待省级验证
    ARCHIVE:3,//归档
    REFUSE:4,
};

@Component({
    selector: 'cert_nedu_apply_prov',
    templateUrl: './cert_nedu_apply.component.prov.html',
    styleUrls: ['./cert_nedu_apply.component.prov.scss']
})
export class CertNeduApplyComponentProv extends FlexTableComponent implements OnInit {

    public query = {
        "std_name": "",
        "std_cert_no": "",
        "cert_type_id": "",
        "time_config_id": "",
        "ticket_no":"",
        "isdo":"",
        "is_atg":"",
        "status":"",
        "org_id":""
    };

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

    CERT_TYPE_List_URL= "mk/mkCertType/listAll.htm";
    CERT_TYPE_COURSE_URL= "mk/mkCertNeduApply/getCourse.htm"
    APPLY_VERIFY_URL = "mk/mkCertNeduApply/verify.htm";
    APPLY_BATCH_VERIFY_URL = "mk/mkCertNeduApply/batchVerify.htm";

    certTypeList = null;
    courseList = null;
    orgList = null;

    bsModalWin_verify : any;
    status:any;

    constructor(
        public bsModalService: BsModalService,
        fb: FormBuilder
    ) {
        super();
        // CustomValidators.rangeLength([1, 6])
        this.formModel = fb.group({
            id: [null],
            'std_name': [null],
            'std_cert_no': [null],
            'cert_no': [null],
            'cert_type_id': [null],
            'status': [null],
            'exam_date': [null],
            'file_path': [null],
            'verify_remark2': [null],
            'verify_remark3': [null],
            'verify_remark1': [null],
            "ticket_no":[null],
        });

        FrameRequest.requestArray(this.CERT_TYPE_List_URL, {}, (array) => {
            this.certTypeList = array;
          });

          FrameRequest.requestArray('cf/cfOrganization/list.htm', {org_type: [1,4], status: 1}, (array) => {
            this.orgList = array;
          });   

    }

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

        let searchCondition = this.query;
        //参数合并
        _.assignIn(param, searchCondition);
    }


    //导出
  export() {
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("mk/mkCertNeduApply/export.htm", {
        std_name : this.query.std_name,
        std_cert_no : this.query.std_cert_no,
        cert_type_id : this.query.cert_type_id,
        time_config_id: this.query.time_config_id,
        isdo: this.query.isdo,
        is_atg:this.query.is_atg,
        status: this.query.status,
        ticket_no:this.query.ticket_no,
        org_id:this.query.org_id
    }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("导出成功");
    });
  }


    goVerify(tpl,item){
       

        FrameRequest.requestMap(this.CERT_TYPE_COURSE_URL, {cert_type_id: item.id}, (map) => {
            this.item = map['apply'];
            this.courseList = map['courseList'];
            this.status = this.item.status;

            if(this.item.status =="2"){
                this.item.verify_remark2 = '同意';
            }

            this.item.status = "1";

            this.shouModalWin_verify(tpl);
            
         });
    }

    goBatchVerify(tpl){
        let checkedArray =  this.framePager.getCheckedArray();
        if(checkedArray.length == 0){
            AppComponent.alert({title:"信息提示",text:"请选择数据!"});
            return;
        }
        this.item = {};
        this.status = "2";

        this.item.verify_remark2 = '同意';

            this.item.status = "1";

            this.shouModalWin_verify(tpl);
    }

    async shouModalWin_verify(editFormTemplateModal, modalcss?) {
        if(!this.item) this.item = {};
        let css = modalcss ? modalcss : "modal-lg";
        this.bsModalWin= this.bsModalService.show(editFormTemplateModal, { class: css });
    }

    //现场审批
    async saveVerify(){
        if(this.item.status_text != "待省级验证"){
            AppComponent.alert({title:"信息提示",text:"申请单状态不是待省级验证，不允许审批!"});
            return
        }

        if(!(this.item.status+"")){
            AppComponent.alert({title:"信息提示",text:"请填写审批状态!"});
            return
        }
        if(!this.item.verify_remark2 || this.item.verify_remark2.lengh == 0){
            AppComponent.alert({title:"信息提示",text:"请填写审批意见!"});
            return
        }
        let verfiyStatus;
        switch (this.item.status) {
            case "0":
                verfiyStatus = APPLY_STATUS.REFUSE
                break;
            case "1":
                verfiyStatus = APPLY_STATUS.ARCHIVE
                break;
            default:
                break;
        }
        
        AppComponent.confirm("确定提交吗?",()=>{
            FrameRequest.requestMap(this.APPLY_VERIFY_URL, 
                { apply_id: this.item.id, status: verfiyStatus,verify_remark2:this.item.verify_remark2},async (ret) => {
                    
                    this.hideModal();
                   // AppComponent.alert({title:"信息提示",text:"审批提交成功!"});
                    AppComponent.GobalComponent.successMsg("审批提交成功!");
                    this.framePager.reload();
                   
             });
        });

        
    }

     //批量审批
     async saveBatchVerify(){
        let checkedArray =  this.framePager.getCheckedArray();
        if(checkedArray.length == 0){
            AppComponent.alert({title:"信息提示",text:"请选择申请单!"});
            return;
        }

        if(!(this.item.status+"")){
            AppComponent.alert({title:"信息提示",text:"请填写审批状态!"});
            return
        }
        if(!this.item.verify_remark2 || this.item.verify_remark2.lengh == 0){
            AppComponent.alert({title:"信息提示",text:"请填写审批意见!"});
            return
        }
        let verfiyStatus;
        switch (this.item.status) {
            case "0":
                verfiyStatus = APPLY_STATUS.REFUSE
                break;
            case "1":
                verfiyStatus = APPLY_STATUS.ARCHIVE
                break;
            default:
                break;
        }

        let checkIds = this.framePager.getCheckedArray((item) => item.id);
        
        AppComponent.confirm("确定批量提交吗?",()=>{
            FrameRequest.requestMap(this.APPLY_BATCH_VERIFY_URL, 
                { ids: checkIds.join(), status: verfiyStatus,verify_remark2:this.item.verify_remark2},async (ret) => {
                    
                    this.hideModal();
                   // AppComponent.alert({title:"信息提示",text:"审批提交成功!"});
                    AppComponent.GobalComponent.successMsg("批量审批提交成功!");
                    this.framePager.reload();
                   
             });
        });

        
    }
    
    downloadTemplate() {
        FrameRequest.download('/std/stdRegInfo/downloadTpl.htm', {fileName: 'mk_apply.doc'});
    }

    downApplyPDF(){
        let checkArray = this.framePager.getCheckedArray();
  
        if(checkArray.length != 1){
          AppComponent.alert({title:"信息提示",text:"请单选一条申请数据!"});
          return
        }
        FrameRequest.download('mk/mkCertEduApply/downApplyPDF.htm', {std_id:checkArray[0].std_reg_id,time_config_id:checkArray[0].time_config_id,status:2});
    }

    changeRemark(remark){
        this.item.verify_remark2=remark;
    }

    callBack(){
        let checkedArray =  this.framePager.getCheckedArray();
        if(checkedArray.length == 0 ||checkedArray.length>1){
            AppComponent.alert({title:"信息提示",text:"请选择一条数据!"});
            return;
        }
    
        AppComponent.confirm("确定撤回吗?",()=>{
            FrameRequest.request("mk/mkCertNeduApply/callBack.htm", {apply_id:checkedArray[0].id}, async ()=>{
                //刷新
                this.framePager.reload();
                AppComponent.GobalComponent.successMsg("撤回成功!");
            
            });
        });
    
      }

}


