import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { Component, OnInit, ViewChild } 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-paperlist',
  templateUrl: './subscribe-paper.component.html',
  styleUrls: ['./subscribe-paper.component.scss']
})

export class SubscribePaperComponent 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;

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

  query = {
    examTimeId:""
  }

  totalObj = {
    bag30: "",
    bag15: "",
    bag5: "",
    bags: "",
    stunum: ""
  }

  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"];

                FrameRequest.requestMap("kw/kwLayoutVolumeCourse/subscribetotal.htm", {exam_time_id: this_.query.examTimeId}, (map)=>{
                  this_.totalObj.bag5 = map.bag5;
                  this_.totalObj.bag15 = map.bag15;
                  this_.totalObj.bag30 = map.bag30;
                  this_.totalObj.bags = map.bags;
                  this_.totalObj.stunum = map.stunum;
                });
            }
        });
    }, 500);

  }

  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;
    }
    paramTemp['exam_time_id']=this.query.examTimeId;
    
    // 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;
  }

  formate(row) {
    if(row.exam_times == "A") {
      return row.exam_first_day + "上午";
    } else if(row.exam_times == "B") {
      return row.exam_first_day + "下午";
    } else if(row.exam_times == "C") {
      return row.exam_second_day + "上午";
    } else if(row.exam_times == "D") {
      return row.exam_second_day + "下午";
    } else if(row.exam_times == "X") {
      return row.exam_third_day + "上午";
    } else if(row.exam_times == "Y") {
      return row.exam_third_day + "下午";
    }    
  }

  formateBags(row) {
    //return row.bag5 + row.bag10 + row.bag15 + row.bag20 + row.bag25 + row.bag30;
    return row.bag5 + row.bag15 + row.bag30;
  }

  formateTotal(row) {
    //return row.bag5*5 + row.bag10*10 + row.bag15*15 + row.bag20*20 + row.bag25*25 + row.bag30*30;
    return row.bag5*5 + row.bag15*15 + row.bag30*30;
  }

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

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

  //ZTREE 事件
  onClickTreeNode(event, treeId, treeNode){
    let _pthis = window['paperlist_this'];
    _pthis.clickedTreeNode = treeNode;
    _pthis.framePager.reload();
    FrameRequest.requestMap("kw/kwLayoutVolumeCourse/subscribetotal.htm", {exam_time_id: _pthis.query.examTimeId, org_id: _pthis.clickedTreeNode.id}, (map)=>{
      _pthis.totalObj.bag5 = map.bag5;
      _pthis.totalObj.bag15 = map.bag15;
      _pthis.totalObj.bag30 = map.bag30;
      _pthis.totalObj.bags = map.bags;
      _pthis.totalObj.stunum = map.stunum;
    });
  }

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

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

  //订卷PDF
  /*printPaperPdf() {
    debugger;
    let param = {
      exam_time_id: this.query.examTimeId,
      org_id: (this.clickedTreeNode ? this.clickedTreeNode.id : "")
    }
    FrameRequest.download("kw/kwLayoutVolumeCourse/printpaperlist.htm",param);
  }*/

  //订卷校对PDF
  verifyPaperPdf() {
    let param = {
      exam_time_id: this.query.examTimeId,
      org_id: (this.clickedTreeNode ? this.clickedTreeNode.id : "")
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/kwLayoutVolumeCourse/printVerifyPaper.htm",param,()=>{
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("导出成功!");
    });
  }

  examTimeChange(examTimeId) {
    if (!examTimeId) {
        return;
    }
    let param = {};
    if (this.clickedTreeNode) {
      param = {exam_time_id: this.query.examTimeId, org_id: this.clickedTreeNode.id};
    } else {
      param = {exam_time_id: this.query.examTimeId, org_id: ""};
    }
    
    FrameRequest.requestMap("kw/kwLayoutVolumeCourse/subscribetotal.htm", param, (map)=>{
      this.totalObj.bag5 = map.bag5;
      this.totalObj.bag15 = map.bag15;
      this.totalObj.bag30 = map.bag30;
      this.totalObj.bags = map.bags;
      this.totalObj.stunum = map.stunum;
    });
    this.framePager.reload();
  }
}
