
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//归档
};

@Component({
    selector: 'cert_edu_apply_local',
    templateUrl: './cert_edu_apply.component.local.html',
    styleUrls: ['./cert_edu_apply.component.local.scss']
})
export class CertEduApplyComponentLocal extends FlexTableComponent implements OnInit {

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

    APPLY_VERIFY_URL = "mk/mkCertEduApply/verify.htm";
    CERT_TYPE_List_URL= "mk/mkCertType/listAll.htm";
    VERIFY_ALL_URL= "mk/mkCertEduApply/verifyAll.htm";
    APPLY_EDIT_URL = "mk/mkCertEduApply/editApply.htm";

    bsModalWin_verify : any;
    certTypeList = [];
    orgList:any;
    bsModalWin2 = null;
    file_path = null;
    curr_id = null;
    current = 0;

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

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

    resetData(){
        this.query.ticket_no = "";
        this.query.std_name = "";
        this.query.std_cert_no = "";
        this.query.cert_type_id = "";
        this.query.time_config_id = "";
        this.query.isdo = "";
        this.query.is_atg = "";
        this.query.status = "";
        this.query.org_id = "";
    }

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

        FrameRequest.requestArray(this.CERT_TYPE_List_URL, {type: 1}, (array) => {
            this.certTypeList = 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/mkCertEduApply/export.htm", {
        ticket_no : this.query.ticket_no,
        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,
        org_id: this.query.org_id
    }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("导出成功");
    });
  }

   


    goVerify(tpl,item){
       // let checkedArray =  this.framePager.getCheckedArray();
     //   if(checkedArray.length == 0){
     //       AppComponent.alert({title:"信息提示",text:"请选择数据!"});
      //      return;
     //   }
    //    if(checkedArray.length > 1){
     //       AppComponent.alert({title:"信息提示",text:"只允许操作一条数据!"});
     //       return;
     //   }

        
        FrameRequest.requestEntity(this.GO_VERIFT_URL, {id: item.id}, (entity) => {
            this.item = entity;
            if(this.item.status !="1"){
                AppComponent.alert({title:"信息提示",text:"该状态无法审批"});
                return;
            }
            this.item.status = "1";
            this.editShowType = 'only';
            this.item.verify1_remark = '同意上报';
            // this.shouModalWin_verify(tpl);
            
         });


        //  FrameRequest.requestArray("mk/mkCertEduApply/certTypeList.htm", {}, (array) => {
        //     this.certTypeList = array;
        //  });

        

    }

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

    //现场审批
    saveVerify(){

        if(!(this.item.status+"")){
            AppComponent.alert({title:"信息提示",text:"请填写审批状态!"});
            return
        }
        if(!this.item.verify1_remark || this.item.verify1_remark.lengh == 0){
            AppComponent.alert({title:"信息提示",text:"请填写审批意见!"});
            return
        }
        if(!String(this.item.cert_type_id)){
            AppComponent.alert({title:"信息提示",text:"请选择学历证书!"});
            return
        }
        if(!this.item.done_course_name){
            AppComponent.alert({title:"信息提示",text:"请填写已考课程!"});
            return
        }
        if(!this.item.done_course_score){
            AppComponent.alert({title:"信息提示",text:"请填写已考课程成绩!"});
            return
        }
        let verfiyStatus;
        switch (this.item.status) {
            case "0":
                verfiyStatus = APPLY_STATUS.NEW
                break;
            case "1":
                verfiyStatus = APPLY_STATUS.PROV_VERIFY
                break;
            default:
                break;
        }

        AppComponent.confirm("确定提交吗?",()=>{
            FrameRequest.requestMap(this.APPLY_VERIFY_URL, 
                { apply_id: this.item.id, 
                    status: verfiyStatus,
                    verify1_remark:this.item.verify1_remark,
                    cert_type_id:this.item.cert_type_id,
                    done_course_name:this.item.done_course_name,
                    done_course_score:this.item.done_course_score,
                }, (map) => {
                    AppComponent.alert({title:"信息提示",text:"审批提交成功!"});
                    this.hideModal();
                   this.framePager.reload();
             });
        });

       
    }

    editApply(){
        if(!this.item.done_course_name){
            AppComponent.alert({title:"信息提示",text:"请填写已考课程!"});
            return
        }
        if(!this.item.done_course_score){
            AppComponent.alert({title:"信息提示",text:"请填写已考课程成绩!"});
            return
        }
        if(!this.item.cert_type_id){
            AppComponent.alert({title:"信息提示",text:"请选择学历证书!"});
            return
        }
        
        FrameRequest.requestMap(this.APPLY_EDIT_URL, 
            { apply_id: this.item.id, 
                cert_type_id:this.item.cert_type_id,
                done_course_name:this.item.done_course_name,
                done_course_score:this.item.done_course_score,
            }, (map) => {
                AppComponent.alert({title:"信息提示",text:"保存成功!"});
                this.hideModal();
               this.framePager.reload();
         });
    }

    doVerifyAll(){
       let repParam =  _.cloneDeep(this.query);
        repParam['fromStatus'] = APPLY_STATUS.LOCAL_VERIFY;
        repParam['toStatus'] = APPLY_STATUS.PROV_VERIFY;
        FrameRequest.requestMap(this.VERIFY_ALL_URL, 
            repParam, (map) => {
                AppComponent.alert({title:"信息提示",text:"全部审批通过提交成功!"});
                this.hideModal();
               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.requestEntity("mk/mkCertEduApply/checkDownApplyPDF.htm", {std_id:checkArray[0].std_reg_id,time_config_id:checkArray[0].time_config_id}, (entity) => {
        this.item = entity;
        if(entity){
            AppComponent.confirm(entity,()=>{
                FrameRequest.download('mk/mkCertEduApply/downApplyPDF.htm', {std_id:checkArray[0].std_reg_id,time_config_id:checkArray[0].time_config_id});
            });     
        }else{
            FrameRequest.download('mk/mkCertEduApply/downApplyPDF.htm', {std_id:checkArray[0].std_reg_id,time_config_id:checkArray[0].time_config_id});
        }
       
     });
    
      
    }

    isPakage(path){
        if(path.indexOf('zip') != -1 || path.indexOf('rar') != -1){
            return true;
        }
        return false;
    }

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

    
showImg(editFormTemplateModal,path) {
    this.file_path = path;
    this.curr_id = path;
    this.current =0;
   
    let css = "modal-bg";
    this.bsModalWin2 = this.bsModalService.show(editFormTemplateModal, { class: css });
  
  }
  
  rotate(i){
  
    var imge = document.getElementById(this.curr_id);
    if(i ==0){
      this.current = (this.current-90)%360
    }else if(i ==1){
      this.current = (this.current+90)%360
    }
  
    
  
  imge.style.transform='rotate('+this.current+'deg)';
  
  if(imge.offsetWidth>imge.offsetHeight){
    imge.style.marginTop = (imge.offsetWidth-imge.offsetHeight)/2+"px";
  
  }
    
  }

}


