import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { Component, OnInit, ViewChild, ElementRef, TemplateRef } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal/bs-modal.service';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import 'ztree';
import * as _ from 'lodash';
import { Validators, FormBuilder } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { AppComponent } from '../../../../../app.component';
declare var $: any;

@Component({
  selector: 'app-kw-receivepaper',
  templateUrl: './receive-paper.component.html',
  styleUrls: ['./receive-paper.component.scss']
})

export class ReceivePaperComponent extends FlexTableComponent implements OnInit {

  setting = {
    data: {
      simpleData: {
        enable: true,
        idKey: 'id',
        pIdKey: 'pid',
        rootPid: -1
      }
    },
    check: {
      enable: false,
      chkStyle: "checkbox",
      chkboxType : { "Y" : "ps", "N" : "ps" }
    },
    key: {
      checked: 'checked'
    },
    callback: {
      onClick: this.onClickTreeNode
    }
  };

  zNodes: any;
  nodeData: any;
  treeObj: any;
  checkAllTreeToggleFlag = true;
  expandAllTreeToggleFlag = true;

  clickTreePId;
  subjectList = [];
  ptreeNode;
  clickedTreeNode;

  @ViewChild('distModal')
  distModal:TemplateRef<any>;

  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) { 
    super();
  }
   
  ngOnInit() {
    this.initQuery();
  }

  query = {
    examTimeId: "",
    isOneUnitOneClassroom: false, //默认不显示
    net_mark: ""
  }

  initQuery(){
    FrameRequest.requestArray('kw/kwLayoutVolumeCourse/orgtree.htm', {}, (array) => {
      this.nodeData = array;
      this.zNodes = $.fn.zTree.init($('#tree'), this.setting, this.nodeData);
      this.treeObj = $.fn.zTree.getZTreeObj("tree");
      window['paperlist_this'] = this;
      //默认展开
      this.treeObj.expandAll(true)
      let rootNode = this.treeObj.getNodeByParam("id",0 );
      this.clickedTreeNode = rootNode;
      this.ptreeNode = rootNode;
      this.treeObj.selectNode(rootNode,true);//将指定ID的节点选中
    });
    let this_ = this;
    setTimeout(() => {
        FrameRequest.requestMap("kw/kwLayout/initdata.htm", {}, (map)=>{
            if (map["exam_time_id"]) {
                this_.query.examTimeId = map["exam_time_id"];
            }
            this_.query.isOneUnitOneClassroom = map["isOneUnitOneClassroom"];
        });
    }, 1000);
  }

  ngOnDestory(){
    delete window['paperlist_this'];
  }

  searchParam(param) {
    let paramTemp = {};
    if(!this.clickedTreeNode) {
      paramTemp['org_id'] = "";
    } else {
      this.ptreeNode = this.clickedTreeNode;
      if (this.clickedTreeNode.isParent) {
        delete param['id']
        paramTemp['org_id'] = this.clickedTreeNode.id;
      } else {
        delete param['pid']
        paramTemp['org_id'] = this.clickedTreeNode.id;
      }
      paramTemp['org_id']=this.clickedTreeNode.id;
    }
    // if (!this.query.examTimeId) {
    //   AppComponent.alert({title:"信息提示",text:"请选择考试日期!"});
    //   return;
    // }
    paramTemp['exam_time_id']=this.query.examTimeId;

    if(!this.query.net_mark){
      this.query.net_mark = "";
    }
    paramTemp['net_mark']=this.query.net_mark;
    
    // if(_.isNumber(this.clickTreePId))
    _.assignIn(param, paramTemp);
  }

  //参数处理
  async preGoEditEvent(item){
    if(!this.clickedTreeNode){
      AppComponent.alert({text:"请选择父科目!"})
      return false;
    }
    let _item = item ? item : this.item = {};
    if(!_item['pid']) 
      _item['pid'] = this.clickedTreeNode.id;
    if(!_item['id']) 
     _item['id'] = "";
    return true;
  }

  async aftSaveEvent(){
    this.initQuery();
  }

  async aftDeleteEvent(){
    this.initQuery();
  }

  //ZTREE 事件
  onClickTreeNode(event, treeId, treeNode){
    let _pthis = window['paperlist_this'];
    _pthis.clickedTreeNode = treeNode;
    _pthis.framePager.reload();
  }

  //全选切换
  public checkAllTreeToggle(){
    this.treeObj.checkAllNodes(this.checkAllTreeToggleFlag = !this.checkAllTreeToggleFlag);
  }

  //展开切换
  public expandAllTreeToggle(){
    this.treeObj.expandAll(this.expandAllTreeToggleFlag = !this.expandAllTreeToggleFlag);
  }

  //领劵pdf
  receivePaperPdf() {
    if (!this.query.examTimeId) {
      AppComponent.alert({title:"信息提示",text:"请选择考试时间!"});
      return;
    }
    let queryOrgId = "";
    if (!this.clickedTreeNode) {
      queryOrgId = "999";
    } else {
      queryOrgId = this.clickedTreeNode.id;
    }

    if(!this.query.net_mark){
      this.query.net_mark = "";
    }

    let param = {
      exam_time_id: this.query.examTimeId,
      org_id: queryOrgId,
      net_mark:this.query.net_mark
    }
    FrameRequest.download("kw/kwLayoutVolumeCourse/receivepaperpdf.htm",param,()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("领劵PDF导出成功!");
    });
  }

  //分卷pdf
  distPaperPdf() {
    if (!this.query.examTimeId) {
      AppComponent.alert({title:"信息提示",text:"请选择考试时间!"});
      return;
    }
    let queryOrgId = "";
    if (this.clickedTreeNode) {
      queryOrgId = this.clickedTreeNode.id;
    }

    if(!this.query.net_mark){
      this.query.net_mark = "";
    }

    let param = {
      exam_time_id:this.query.examTimeId,
      org_id: queryOrgId,
      net_mark:this.query.net_mark
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/nomal/distpaperpdf.htm",param,()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("分卷PDF导出成功!");
    });
  }

  //印卷统计pdf-所有考区汇总.
  printPaperPdf() {
    if(!this.query.net_mark){
      this.query.net_mark = "";
    }

    let param = {
      exam_time_id:this.query.examTimeId,
      net_mark:this.query.net_mark
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/nomal/printpaperpdf.htm",param,()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("印卷统计PDF导出成功!");
    });
  }

  //试卷分发总清单xls-所有考区汇总.
  distTotalXls() {
    if (!this.query.examTimeId) {
      AppComponent.alert({title:"信息提示",text:"请选择考试时间!"});
      return;
    }
    this.shouModalWin(this.distModal,'modal-md');
  }

  //分发清单导出
  exportFunc() {
    if(!this.query.net_mark){
      this.query.net_mark = "";
    }

    let param = {
      exam_time_id: this.query.examTimeId,
      net_mark: this.query.net_mark
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/nomal/disttotalxls.htm",param,()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("分发总清单XLS导出成功!");
    });
  }
  
}
