
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: 'major_course',
    templateUrl: './major_course.component.html',
    styleUrls: ['./major_course.component.scss']
})
export class MajorCourseComponent extends FlexTableComponent implements OnInit {

    public query = {
        "course_code": "",
        "course_name": "",
        "major_code": "",
        "major_name": "",
    };

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

    QUERY_MAJOR_URL = "pl/plMajor/listByCurrentYear.htm";
    QUERY_MAJOR_COURSE_URL = "pl/plMajor/find.htm";

    selectedMajorId = null;
    selectedCourseId = null;
    subItem = null;

    constructor(
        public bsModalService: BsModalService,
        fb: FormBuilder
    ) {
        super();
        this.formModel = fb.group({
            id: [null],
            'major_code': [null],
            'course_code': [null],
        });

    }

    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'];
            });
    }

    async preGoEditEvent(){
        //初始化页面参数
        this.selectedMajorId = null;
        this.selectedCourseId = null;
        this.subItem = null;

        setTimeout(() => {
            this.selectedMajorId = this.item.major_id;
            this.majorChange(this.selectedMajorId,"");
            this.selectedCourseId = this.item.course_id;
        }, 500);

        return true;
    }
    saveData(){
        if(!this.selectedMajorId){
            AppComponent.alert({ title: "信息提示", text: "请选择专业!" });
            return;
        }
        if(!this.selectedCourseId){
            AppComponent.alert({ title: "信息提示", text: "情选择免考课程!" });
            return;
        }

        let url = this.item.id ? this.EDIT_URL: this.ADD_URL;
        FrameRequest.requestMap(url,
            { major_id:this.selectedMajorId,course_id:this.selectedCourseId,id:this.item.id?this.item.id:''}, (map) => {
                AppComponent.GobalComponent.successMsg('信息保存成功');
                if(this.bsModalWin)this.bsModalWin.hide();
               this.framePager.reload();
            });
    }
}


