import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { FormBuilder } from '@angular/forms';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import { AppComponent } from '../../../../app.component';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePagerComponent } from '../../../../core/pager/pager.component';
import * as _ from 'lodash';
@Component({
  selector: 'app-pl-major-course-pre',
  templateUrl: './pl-major-course-pre.component.html',
  styleUrls: ['./pl-major-course-pre.component.scss']
})
export class PlMajorCoursePreComponent extends FlexTableComponent implements OnInit {

  public query = {
    query: "",
    major_level: "",
    nation_exam: "",
    status: ""
  };
  keyword;
  content;
  major = [];
  courseList = [];
  ctList = [];
  courseModalWin: any;
  @ViewChild('courseFramePager')
  public courseFramePager: FramePagerComponent;

  directRemark: any; //选修方向

  QUERY_URL = "pl/plMajorPre/page.htm";
  MAJOR_COURSE_URL = "pl/plMajorCoursePre/list.htm";
  COURSE_URL = "pl/plCoursePre/page.htm";
  ADD_COURSE_URL = "pl/plMajorCoursePre/add.htm";
  DELETE_URL = "pl/plMajorCoursePre/delete.htm";
  DETAIL_URL = "pl/plMajorCoursePre/detail.htm";
  COURSE_NATURE_URL = "pl/plMajorCoursePre/editType.htm";
  UPDATE_DIRECT_REMARK_URL = "pl/plMajorCoursePre/updateMcDirect.htm";
  UPDATE_COURSE_SCORE_URL = "pl/plMajorCoursePre/updateCourseScore.htm";
  UPDATE_COURSE_ISCOMPOSE_URL = "pl/plMajorCoursePre/updateCourseIscompose.htm";

  

  constructor(public bsModalService: BsModalService) {
    super();
    const fb = new FormBuilder();
    this.formModel = fb.group({});
  }

  ngOnInit() {
    //this.query.status='1';
  }

  /**
     * 专业课程界面
     */
  goCourseDetail(show_type: string) {
    if (!this.preParamHandler()) return;
    this.editShowType = show_type;
  }

  /**
   * 课程维护详情-数据加载
   */
  preParamHandler() {
    let checkedArray = this.framePager.getCheckedArray((item) => item.id);
    if (checkedArray.length === 0 || checkedArray.length > 1) {
      AppComponent.alert({ title: '提示', text: '请选择一条专业!' });
      return false;
    }
    this.reloadMajorCourse(checkedArray[0]);
    return true;
  }

  goCourseDetailOne(item,show_type: string) {
   
    this.reloadMajorCourse(item.id);
    this.editShowType = show_type;
   
  }

  /**
   * 增加课程界面
   */
  goCourseAdd(template: TemplateRef<any>, modalcss?) {
    let css = modalcss ? modalcss : "modal-lg";
    this.courseModalWin = this.bsModalService.show(template, { class: css,backdrop: 'static' });
    
  }

  /**
   * @param major_id: 专业ID
    * 增加专业课程
    */
  addCourse(major_id: number) {
    let checkedArray = this.courseFramePager.getCheckedArray((item) => item.id);
    if (checkedArray.length === 0) {
      AppComponent.alert({ title: '提示', text: '请至少选择一门课程！' });
      return;
    }
    FrameRequest.request(this.ADD_COURSE_URL, { major_id: major_id, courseIds: checkedArray.join() }, () => {
      AppComponent.GobalComponent.successMsg("增加课程成功");
      this.hideModalWin();
      this.reloadMajorCourse(major_id);
    });
  }

  /**
    * @param major_id: 专业ID
     * 重新加载专业课程数据
     */
  reloadMajorCourse(major_id: number) {
    FrameRequest.requestMap(this.MAJOR_COURSE_URL, { major_id: major_id }, (map) => {
      this.major = map['major'];
      this.courseList = map['courseList'];
    });
  }

  /**
       * 删除专业下的课程
       */
  removeCourse(major_id: number,item) {
   
    AppComponent.confirm("确定移除课程吗！",()=>{
      FrameRequest.request(this.DELETE_URL, { ids: item.id }, () => {
        AppComponent.alert({ title: '提示', text: '移除课程成功！' });
        this.reloadMajorCourse(major_id);
      });
    });
  
   
  }

  removeAllCourse(major_id: number) {
   
    let checkedArray = []
    _.forEach(this.courseList, (item) => {
      if (item['isChecked'] === true){
        checkedArray.push(item.id);
      }
        
    });

    if (checkedArray.length ==0) {
      AppComponent.alert({ "title": "提示", "text": "请选择课程" })
      return;
    }

    AppComponent.confirm("确定移除所选中的课程吗！",()=>{
      
      FrameRequest.request(this.DELETE_URL, { ids: checkedArray }, () => {
        AppComponent.alert({ title: '提示', text: '移除课程成功！' });
        this.reloadMajorCourse(major_id);
      });
    });
  
   
  }

  detail(show_type: string, major_id: number) {
    this.editShowType = show_type;
    FrameRequest.requestMap(this.DETAIL_URL, { major_id: major_id }, (map) => {
      this.major = map['major'];
      this.ctList = map['ctList'];
    });
  }

  //保存课程的性质
  updateMajorCourse(major_id: number) {
    let course_ids = [];
    let course_natures = [];
    course_ids = this.courseList.map(item => item.course_id);
    course_natures = this.courseList.map(item => item.course_nature);
    if (!this.judgeNature(course_natures)) {
      AppComponent.alert({ "title": "提示", "text": "请选择课程性质" })
      return;
    }

    FrameRequest.request(this.COURSE_NATURE_URL,
      { major_id: major_id, course_ids: course_ids, course_natures: course_natures }, () => {
        AppComponent.GobalComponent.successMsg("课程属性设置成功");
        this.hideModal();
        this.framePager.reload();
        this.courseList = [];
      });
  }

  //判断是否选择了课程的性质
  judgeNature(course_natures: any) {
    if (!course_natures) {
      return false;
    } else {
      for (let i = 0; i < course_natures.length; i++) {
        if (!course_natures[i]) return false;
      }
    }
    return true;
  }

  //全选复选切换
  public checkedMasterToggle(event) {
    this.setCheckedAll(event.currentTarget.checked)
  }

  //设置全选 反选状态
  public setCheckedAll(checkStatus) {
    _.forEach(this.courseList, (item) => {
      item['isChecked'] = checkStatus;
    });
  }

  //获取checked数组
  public getCheckedArray(func?) {
    let checkedArray = []
    _.forEach(this.courseList, (item) => {
      if (item['isChecked'] === true)
        if (func && _.isFunction(func)) {
          checkedArray.push(func.call(this, item));
        } else {
          checkedArray.push(item);
        }
    });
    return checkedArray;
  }

  /**
      * 模态窗口隐藏
      */
  hideModalWin() {
    if (this.courseModalWin) {
      this.courseModalWin.hide();
    }
  }

  courseSearch(major_id: any) {
    FrameRequest.requestMap(this.MAJOR_COURSE_URL, {major_id: major_id, keyword: this.keyword}, (map) => {
      this.courseList = map['courseList'];
    });
  }

  courseContentSearch(major_id: any) {
    FrameRequest.requestMap(this.DETAIL_URL, { major_id: major_id, content: this.content}, (map) => {
      this.major = map['major'];
      this.ctList = map['ctList'];
    });
  }


  //更新专业选修方向
  updateMcDirect(major_id: any, course_id: any,direct_remark: any,course_nature: any) {
    if(!direct_remark){
      direct_remark = "";
    }
    FrameRequest.request(this.UPDATE_DIRECT_REMARK_URL, {
      major_id: major_id,
      course_id: course_id,
      direct_remark: direct_remark,
      course_nature: course_nature
    }, () => {
      AppComponent.GobalComponent.successMsg("保存成功!");

    });
  }

  //更新专业课程学分
  updateMcCourseScore(major_id: any, course_id: any,course_score: any) {
    AppComponent.confirm("修改该专业课程学分，其他专业下的这门课程学分也同时更新，确定更新学分吗", () => {
        FrameRequest.request(this.UPDATE_COURSE_SCORE_URL, {
          major_id: major_id,
          course_id: course_id,
          course_score: course_score
        }, () => {
          AppComponent.GobalComponent.successMsg("课程学分更新成功，请及时更新相关专业的毕业总学分!");
        });
    });
    
  }

  //更新专业课程是否百分百合成
  updateMcCourseIscompose(major_id: any, course_id: any,iscompose: any) {
    AppComponent.confirm("确定更新是否百分百合成吗", () => {
        FrameRequest.request(this.UPDATE_COURSE_ISCOMPOSE_URL, {
          major_id: major_id,
          course_id: course_id,
          iscompose: iscompose
        }, () => {
          AppComponent.GobalComponent.successMsg("是否百分百合成更新成功!");
        });
    });
    
  }

  export(major_id) {
    
    
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("pl/plMajorCoursePre/majorPlanExport.htm", { major_id: major_id }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("导出成功");
    });
  }

}
