
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 '../../../..';

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

    public query = {
        "cert_code": "",
        "cert_name": "",
        "cert_level": "",
        "cert_type": "",
    };

    QUERY_URL = "mk/mkCertType/page.htm";
    DELETE_URL = "mk/mkCertType/delete.htm";
    GET_URL = "mk/mkCertType/get.htm";
    ADD_URL = "mk/mkCertType/add.htm";
    EDIT_URL = "mk/mkCertType/edit.htm";
    STATUS_OPEN_URL = "mk/mkCertType/statusOpen.htm";
    STATUS_CLOSe_URL = "mk/mkCertType/statusClose.htm";

    QUERY_COURSE_URL = "mk/mkCertCourse/listByCertId.htm";
    QUERY_MAJOR_URL = "pl/plMajor/listByCurrentYear.htm";
    QUERY_MAJOR_COURSE_URL = "pl/plMajor/find.htm";
    ADD_COURSE_URL = "mk/mkCertCourse/add.htm";
    DELET_COURSE_URL = "mk/mkCertCourse/delete.htm";


    selectedCertType = null;
    selectedCourseId = null;
    certTypeCourseList = null;
    selectedMajor = null;

    subItem = null;

    constructor(
        public bsModalService: BsModalService,
        fb: FormBuilder
    ) {
        super();
        this.formModel = fb.group({
            id: [null],
            'cert_code': [null, Validators.compose([Validators.required])],
            'cert_name': [null, Validators.compose([Validators.required])],
            'cert_level': [null, Validators.compose([Validators.required])],
            'cert_type': [null, Validators.compose([Validators.required])],
            'is_std': [null, Validators.compose([Validators.required])],
            'is_score': [null, Validators.compose([Validators.required])],
            'is_edu': [null, Validators.compose([Validators.required])],
        });
    }

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

        let url = (val ==1?this.STATUS_OPEN_URL:this.STATUS_CLOSe_URL);
        FrameRequest.requestEntity(url, {id: checkedArray[0].id}, (entity) => {
            AppComponent.alert({title:"信息提示",text:"状态更新成功!"});
           this.framePager.reload();
         })
    }

    majorChange(a1,a2){
        if(a1 == null){
          this.subItem = [];
          return;
        }
        FrameRequest.requestMap(this.QUERY_MAJOR_COURSE_URL,
            { id: a1 }, (map) => {
                map['courseList'].forEach((item)=>{
                    item.course_full_name = "("+item.course_code +")"+item.course_name;
                })
                this.subItem = map['courseList'];
            });
    }

    transtIsStatus(val){
        if(val === 1){ return "是";}
        if(val === 0){ return "否";}
    }

    frameStatus(val){
        if(val === 1){ return "启用";}
        if(val === 0){ return "禁用";}
    }

    refeshCertTypeCourse(func?) {
        FrameRequest.requestArray(this.QUERY_COURSE_URL, { cert_id: this.selectedCertType.id }, (array) => {
            this.certTypeCourseList = array;
            if(func)
             func.apply(this,null)
        });
    }
    
    toCouirseManage(item,tpl){
        this.selectedCertType = item;
        //初始化参数
        this.selectedMajor = null;
        this.subItem = null;
        this.selectedCourseId = null;
        this.refeshCertTypeCourse(function(){
            this.bsModalWin = this.bsModalService.show(tpl, { class: "modal-lg" });
        });
    }

    addeCertTypeCourse(){
        FrameRequest.requestEntity(this.ADD_COURSE_URL, { course_id:this.selectedCourseId,major_id:this.selectedMajor, cert_id: this.selectedCertType.id }, (entity) => {
            // AppComponent.alert({ title: "信息提示", text: "添加成功!" });
            AppComponent.GobalComponent.successMsg('添加成功!');
            this.refeshCertTypeCourse();
            
        });
    }
    deleteCertTypeCourse(item){
        FrameRequest.requestEntity(this.DELET_COURSE_URL, {ids:item.id}, (entity) => {
            // AppComponent.alert({ title: "信息提示", text: "删除成功!" });
            AppComponent.GobalComponent.successMsg('删除成功!');
            this.refeshCertTypeCourse();
            
        });
    }



}


