import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
import { FormBuilder, Validators } from '@angular/forms';
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";

@Component({
  selector: "app-pl-diplomadist",
  templateUrl: "./diploma-dist.component.html",
  styleUrls: ["./diploma-dist.component.scss"]
})
export class DiplomaDistComponent extends FlexTableComponent implements OnInit {
  public query = {
    query: "",
    major_id: "",
    school_id: "",
    exam_time_id: "",
    is_dist: ""

  };

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;
  @ViewChild('framePager')
  framePager:FramePagerComponent;

  courseList=[];
  //url
  QUERY_URL = "by/byDiaplma/page.htm";
  ORG_LIST_URL = "cf/cfOrganization/list.htm";
  EXPORT_URL = "by/byDiaplma/export.htm";
  EXPORT_DBF_URL = "by/byDiaplma/exportdbf.htm";
  BATCH_BUILD_URL = "by/byDiaplma/batchbuild.htm";//批量生成毕业证号
  DIST_URL = "by/byDiaplma/dist.htm";//发放毕业证
  DIST_ALL_URL = "by/byDiaplma/distAll.htm";//发放毕业证

  orgList = [];
  authList = [];
  filePath : String;
  radioSelect : any;
  scoreList = [];//考生的毕业成绩Map
  majorList = [];
  masterSchoolList = [];

  applyStatus = {
    NEW_0: 0,
    TO_FIRST_AUDIT_1: 1,
    TO_FINAL_AUDIT_2: 2,
    TO_DISTRIBUTE_3: 3,
    ARCHIVED_4: 4
  };

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

    });
  }

  ngOnInit() {
    //专业
    
    FrameRequest.requestMap("by/byApply/initdata.htm", {}, (map)=>{
      this.majorList  = map.majorList;//专业
      this.orgList = map.cityOrgList;//市考办
      this.masterSchoolList = map.schoolOrgList;//主考学校
      this.query.exam_time_id=map.examTimeId;
      this.framePager.reload();
    });
  
  }

  search(param) {
    if (!this.query.exam_time_id) {
      this.query.exam_time_id="";
    }
    if (!this.query.school_id) {
      this.query.school_id="";
    }
    if (!this.query.major_id) {
      this.query.major_id="";
    }
    if (!this.query.is_dist || '请选择'==this.query.is_dist) {
      this.query.is_dist="";
    }
    param.exam_time_id=this.query.exam_time_id;
    param.school_id=this.query.school_id;
    param.major_id=this.query.major_id;
    param.is_dist=this.query.is_dist;
    param.query=this.query.query;
  }

  //批量生成毕业证书号
  batchBuildDiploma() {
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray.length == 0) {
      AppComponent.alert({title:"信息提示",text:"请选择考生记录!"});
      return;
    } else {
      if (checkedArray[0].by_status != this.applyStatus.TO_DISTRIBUTE_3) {
        AppComponent.alert({title:"信息提示",text:"只能选择待发放状态记录!"});
        return;
      }
    }
    let idArr = checkedArray.map((apply) => apply.id);
    AppComponent.confirm("确定批量生成证书号?",()=>{
      FrameRequest.request(this.BATCH_BUILD_URL, {ids: idArr}, (ret)=>{
        if (ret.errorCode == 0) {
          this.hideModal();
          this.framePager.reload();
          AppComponent.GobalComponent.successMsg("批量成功");
        }
      });
    });

  }

  //导出报国家毕业生数据
  exportNationStu() {
    if (!this.query.exam_time_id) {
      AppComponent.alert({title:"信息提示",text:"选择时间批次!"});
      return;
    }
    var parm = {
      exam_time_id: this.query.exam_time_id
    };
    //下载
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.EXPORT_URL, parm, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载成功");
    });

   
  }

  //处理历史数据
  exportNationStuBak() {
    var parm = {
      exam_time_id: this.query.exam_time_id,
      diaplma_no:this.query.query
    };
    //下载
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("by/byDiaplma/getExportFileBak.htm", parm, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("下载成功");
    });

   
  }

  //导出报国家毕业生数据DBF
  exportNationStuDBF() {
    if (!this.query.exam_time_id) {
      AppComponent.alert({title:"信息提示",text:"选择时间批次!"});
      return;
    }
    var parm = {
      exam_time_id: this.query.exam_time_id,
      diaplma_no:""
    };
    //下载
    FrameRequest.download(this.EXPORT_DBF_URL, parm);
  }

  //发放毕业证
  distDiploma() {
    let checkedArray = this.framePager.getCheckedArray();
    if (checkedArray.length == 0) {
      if(!this.query.exam_time_id){
        AppComponent.alert({title:"信息提示",text:"请选择时间批次!"});
        return;
      }

      AppComponent.confirm("确定对时间批次内待发放的考生发放毕业证?",()=>{
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.request(this.DIST_ALL_URL, {exam_time_id: this.query.exam_time_id}, (ret)=>{
          if (ret.errorCode == 0) {
            AppComponent.GobalComponent.setLoading(false);
            this.hideModal();
            this.framePager.reload();
            AppComponent.GobalComponent.successMsg("发放成功");
          }
        });
      });

      
    } else {
      for (let obj of checkedArray) {
        if (obj.by_status != this.applyStatus.TO_DISTRIBUTE_3) {
          AppComponent.alert({title:"信息提示",text:"只能选择待发放状态记录!"});
          return;
        }
        if (!obj.diaplma_no) {
          AppComponent.alert({title:"信息提示",text:"请生成证书号后再发放!"});
          return;
        }
        if (obj.is_dist == 1) {
          AppComponent.alert({title:"信息提示",text:"不能重复发放毕业证!"});
          return;
        }
      }

      let idArr = this.framePager.getCheckedArray((item)=>item.id);
      AppComponent.confirm("确定发放毕业证?",()=>{
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.request(this.DIST_URL, {ids: idArr}, (ret)=>{
          if (ret.errorCode == 0) {
            AppComponent.GobalComponent.setLoading(false);
            this.hideModal();
            this.framePager.reload();
            AppComponent.GobalComponent.successMsg("发放成功");
          }
        });
      });

    }
    
  }
}