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 { FrameUploaderComponent } from '../../../../../core/uploader/uploader.component';
import { FrameRequest } from "../../../../../shared/frame/frame-request";
import { FramePagerComponent } from "../../../../../core/pager/pager.component";
import { FrameFunction } from "../../../../../shared/frame/frame_function";
import * as _ from 'lodash';

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

  public query = {
    query: "",
    year_code: "",
    school_id: ""
  };
  
  public queryDetail = {
    school_id:"",
    course_id: "",
    major_id: "",
    query: ""
  }
 
  public impParam = {
    year_code: ""
  }

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;
  @ViewChild('framePager')
  framePager:FramePagerComponent;
  @ViewChild('detailPager')
  detailPager:FramePagerComponent;
  @ViewChild('rejectModal')
  rejectModal: TemplateRef<any>;
  
  filePath : String;
  yearCodeList = [];
  courseList = [];
  GLOBAL_SCORE_TYPE = 1;//网络助学
  //TODO
  QUERY_URL = "kj/kjCollegeScoreMain/page.htm";
  DELETE_URL = "kj/kjCollegeScoreMain/delete.htm";
  GET_URL = "kj/kjPracticalScore/get.htm";
  ADD_URL = "kj/kjCollegeScoreMain/add.htm";
  EDIT_URL = "kj/kjCollegeScoreMain/edit.htm";

  UPLOAD_URL = "pl/kjCollegeScoreMain/practical/upload.htm";
  ORG_LIST_URL = "cf/cfOrganization/list.htm";
  QUERY_DETAIL_URL = "kj/kjCollegeScoreDetail/page.htm";

  AUTH_PASS_URL = "kj/kjCollegeScoreMain/auditpass.htm";
  AUTH_REJECT_URL = "kj/kjCollegeScoreMain/auditreject.htm";

  school_id="";
  year_code="";
  pageLevel = "main";//标识 主列表 和 计划详情列表切换
  majorList = [];
  masterSchoolList = [];
  defaultBatchName = "";

  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      'year_code' : [null, Validators.compose([Validators.required])],
      'batch_name' : [null, Validators.compose([Validators.required])],
      'remark' : [null],
    });
  }

  ngOnInit() {
    this.editShowType = "showMainList";
     //课程--course_exam_type=2为实践课
     FrameRequest.requestArray("pl/plCourse/listCourseByType.htm", {course_exam_type:2,special_flag:""}, (arrary)=>{
       this.courseList = arrary;
     });
    //专业
    var parm = {ids:""};
    FrameRequest.requestArray("pl/plMajor/list.htm", parm, (arrary) => {
      this.majorList = arrary;
    });
    //主考学校
    FrameRequest.requestArray(this.ORG_LIST_URL, {org_type: 1}, (arrary) => {
      this.masterSchoolList = arrary;
    });
    //年度码
    FrameRequest.requestArray("pl/plYearCode/list.htm", {}, (array) => {
      this.yearCodeList = array;
    });
  }

  search(param) {
    param.query = this.query.query;
    param.yearCode = this.query.year_code;
    param.schoolId = this.query.school_id;
    param.scoreType = this.GLOBAL_SCORE_TYPE;//网络助学
  }

  searchDetail(paramDetail) {
    if (!this.queryDetail.course_id) {
      this.queryDetail.course_id = "";
    }
    if (!this.queryDetail.major_id) {
      this.queryDetail.major_id = "";
    }
    paramDetail.query = this.queryDetail.query;
    paramDetail.courseId = this.queryDetail.course_id;
    paramDetail.majorId = this.queryDetail.major_id;
    paramDetail.schoolId = this.school_id;
    paramDetail.yearCode = this.year_code;
    paramDetail.scoreType = this.GLOBAL_SCORE_TYPE;//网络助学
  }
  public rejectObject = {
    reject_remark:""
  }
  //年度码选择
  yearCodeChange(year_code) {

  }

  //审核通过
  doAudit() {
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray.length == 0) {
      AppComponent.alert({title:"信息提示",text:"请选待审核记录!"});
      return;
    }
    for (let obj of checkedArray) {
      if (obj.status != 2) {
        AppComponent.alert({title:"信息提示",text:"只能审核待审批记录!"});
        break;
      }
    }
    AppComponent.confirm("确定通过",()=>{
      let idArr = checkedArray.map((obj) => obj.id);
      FrameRequest.request(this.AUTH_PASS_URL, {ids: idArr,toStatus: 2}, (ret)=>{
        if (ret.errorCode == 0) {
          this.framePager.reload();
          AppComponent.GobalComponent.successMsg('成功审核通过!');
        }
      });
    });
  }

  popUpRejectWind() {
    let exist = false;
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray.length == 0) {
      AppComponent.alert({title:"信息提示",text:"请选待审核记录!"});
      return;
    }
    for (let obj of checkedArray) {
      if (obj.status != 2) {
        AppComponent.alert({title:"信息提示",text:"只能驳回待审批记录!"});
        exist = true;
        break;
      }
    }
    if (!exist) {
      this.shouModalWin(this.rejectModal,'modal-md');
    }
  }

  doReject() {debugger
    let checkedArray = this.framePager.getCheckedArray();
    AppComponent.confirm("确定驳回",()=>{
      let idArr = checkedArray.map((obj) => obj.id);
      let rejectParam = {
        id: idArr, 
        toStatus: 0, 
        scoreType: this.GLOBAL_SCORE_TYPE, //实践课成绩
        msg: this.rejectObject.reject_remark
      }
      FrameRequest.request(this.AUTH_REJECT_URL, rejectParam, (ret)=>{
        if (ret.errorCode == 0) {
          this.hideModal();
          this.framePager.reload();
          this.editShowType = "showMainList";
          AppComponent.GobalComponent.successMsg('成功驳回!');
        }
      });
    });

  }

  //打开批次对应的计划详情
  goDetailList(year_code, school_id) {
    this.editShowType = "practicalDetailList";
    this.pageLevel = "detail";
    this.year_code = year_code;
    this.school_id = school_id;
    this.detailPager.reload();
  }

  //返回主列表
  backMain() {
    this.editShowType = "showMainList";
    this.pageLevel = "main";
  }

  deletePlanDetail(item) {
    let checkedArray = this.detailPager.getCheckedArray();
    if(checkedArray.length === 0 && !item) {
      AppComponent.alert({title:"信息提示",text:"请选择数据!"})
      return;
    }
    AppComponent.confirm("确定删除",()=>{
      let checkedArray = [];
      item ? checkedArray[0] = item.id: 
             checkedArray = this.detailPager.getCheckedArray((item)=>item.id)
      
      FrameRequest.requestMap("kj/kjCollegeScoreDetail/delete.htm", {ids:checkedArray.join()}, () => {
        AppComponent.GobalComponent.successMsg('数据删除成功');
        this.aftDeleteEvent();
        this.detailPager.reload();
      });
    });
  }
}