import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
import { FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { BsModalService } from "ngx-bootstrap/modal";
import { FlexTableComponent } from "../../../../../shared/common/FlexTable.component";
import { AppComponent } from "../../../../../app.component";
import { FrameRequest } from "../../../../../shared/frame/frame-request";
import { FramePagerComponent } from "../../../../../core/pager/pager.component";
import { isNumber } from "util";

@Component({
  selector: "app-pl-examplan-query",
  templateUrl: "./examplan-query.component.html",
  styleUrls: ["./examplan-query.component.scss"]
})
export class ExamPlanQueryComponent extends FlexTableComponent implements OnInit {

  @ViewChild('framePager')
  framePager:FramePagerComponent;
  @ViewChild('detailPager')
  detailPager:FramePagerComponent;

  //TODO
  QUERY_URL = "pl/mergePublishPlan/examYearMonthPage.htm";
  QUERY_COURSE_DETAIL_URL = "pl/mergePublishPlan/courseDetailPage.htm";//指定批次对应的国考 省考课程结合集

  yearList = [];
  majorList = [];
  examTime4 = {};//时间配置对象4月
  examTime7 = {};//时间配置对象7月
  examTime10 = {};//时间配置对象10月
  mainExamYear : number;//主记录选择的年度
  mainExamMonth : number;//主记录选择的月份,默认-1 显示 4,7,10三个月
  pageLevel = "main";//标识 主列表 和 计划详情列表切换


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

  ngOnInit() {
     let dateNow = new Date();
     this.yearList.push({key:dateNow.getFullYear() + 1,val:dateNow.getFullYear() + 1});
     this.yearList.push({key:dateNow.getFullYear(),val:dateNow.getFullYear()});
     for (let i=1;i<=8;i++) {
       let yearN = dateNow.getFullYear() - i;
       this.yearList.push({key:yearN,val:yearN});
     }
     this.queryMajor.status=1;
     this.editShowType = "showMainList";
  }

  public query = {
    examYear: "",
    examMonth: "",
    query: ""
  };

  public queryDetail = {
    examYear: "",
    examMonth: "",
    isNationExam: "",
    query: "",
    exam_time:""
  }

  public queryMajor = {
    examYear: "",
    examMonth: "",
    isNationExam: "",
    query: "",
    status:1,
    is_new_major:"",
    majortype:""
  }

  search(param) {
    if (this.query.examYear == null) {
      this.query.examYear = "";
    }
    param.examYear=this.query.examYear;
    param.examMonth=this.query.examMonth;
  }

  //发布明细记录分页
  searchDetail(param) {
    param.examYear=this.queryDetail.examYear;
    param.examMonth=this.queryDetail.examMonth;
    param.isNationExam=this.queryDetail.isNationExam;
    param.query=this.queryDetail.query;
    param.exam_time=this.queryDetail.exam_time;
  }

  backMain() {
    this.editShowType = "showMainList";
    this.pageLevel = "main";
    //this.pageStart = 1;
  }

  //课程模式查询
  courseDetail(exam_year, exam_month) {
    this.queryDetail.examYear = exam_year;
    if (exam_month == -1) {
      this.queryDetail.examMonth = "";
    } else {
      this.queryDetail.examMonth = exam_month;
    }
    
    this.editShowType = "courseDetail";
    this.pageLevel = "courseDetail";
    this.detailPager.reload();
  }

  //专业模式查询
  majorDetail(exam_year_arg, exam_month_arg) {
    let param = {};
    this.mainExamYear = exam_year_arg;
    this.mainExamMonth = exam_month_arg;
    this.editShowType = "majorDetail";
    this.pageLevel = "majorDetail";
    if (exam_month_arg == "-1") {
      param = {
        exam_year:exam_year_arg,
        exam_month:"",
        is_nation_exam:"",
        query: ""
      };
    } else {
      param = {
        exam_year:exam_year_arg,
        exam_month:exam_month_arg,
        is_nation_exam:"",
        query: ""
      };
    }
    FrameRequest.requestMap("pl/mergePublishPlan/majorModeQuery.htm", param, (map)=>{
      this.majorList = map.majorList;
      if (map.examTime4) {
        this.examTime4 = map.examTime4;
      }
      if (map.examTime7) {
        this.examTime7 = map.examTime7;
      }
      if (map.examTime10) {
        this.examTime10 = map.examTime10;
      }
    });
    
  }

  queryMajorDetail() {
    let param = {
      exam_year: this.mainExamYear,  //年度是死的，外面主列表带进来 不能改.
      exam_month: this.queryMajor.examMonth,
      is_nation_exam: this.queryMajor.isNationExam,
      query: this.queryMajor.query,
      status:this.queryMajor.status,
      is_new_major:this.queryMajor.is_new_major,
      majortype:this.queryMajor.majortype
    }
    FrameRequest.requestMap("pl/mergePublishPlan/majorModeQuery.htm", param, (map)=>{
      this.majorList = map.majorList;
      if (map.examTime4) {
        this.examTime4 = map.examTime4;
      }
      if (map.examTime7) {
        this.examTime7 = map.examTime7;
      }
      if (map.examTime10) {
        this.examTime10 = map.examTime10;
      }
    });
  }

  //下载pdf
  exportPdf() {
    let param = {
      query: this.queryMajor.query,
      exam_year: this.mainExamYear, 
      exam_month: this.mainExamMonth,
      status:this.queryMajor.status,
      is_new_major:this.queryMajor.is_new_major,
      majortype:this.queryMajor.majortype
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("pl/mergePublishPlan/exportpdf.htm", param, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载成功!");
    });
  }

  exportExcel() {
    let param = {
      query: this.queryMajor.query,
      exam_year: this.mainExamYear, 
      exam_month: this.mainExamMonth,
      status:this.queryMajor.status,
      is_new_major:this.queryMajor.is_new_major,
      majortype:this.queryMajor.majortype
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("pl/mergePublishPlan/exportexcel.htm", param, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载成功!");
    });
  }


  //下载pdf
  courseDetailExport() {
    let param = {
      examYear: this.queryDetail.examYear,
      examMonth: this.queryDetail.examMonth, 
      isNationExam: this.queryDetail.isNationExam,
      query:this.queryDetail.query,
      exam_time:this.queryDetail.exam_time
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("pl/mergePublishPlan/courseDetailExport.htm", param, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载成功!");
    });
  }

  courseTextbookExport() {
    let param = {
      examYear: this.queryDetail.examYear,
      examMonth: this.queryDetail.examMonth, 
      isNationExam: this.queryDetail.isNationExam,
      query:this.queryDetail.query,
      exam_time:this.queryDetail.exam_time
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("pl/mergePublishPlan/courseTextbookExport.htm", param, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载成功!");
    });
  }


  //导出命题
  courseDetailExport2() {
    let param = {
      examYear: this.queryDetail.examYear,
      examMonth: this.queryDetail.examMonth, 
      isNationExam: this.queryDetail.isNationExam,
      query:this.queryDetail.query,
      exam_time:this.queryDetail.exam_time
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("pl/mergePublishPlan/courseDetailExportPdf.htm", param, ()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载成功!");
    });
  }

}