import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { FlexTableComponent } from '../../../../shared/common/FlexTable.component';
import { AppComponent } from '../../../..';
import { FrameRequest } from '../../../../shared/frame/frame-request';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { FrameUploaderComponent } from '../../../../core/uploader/uploader.component';

@Component({
  selector: 'app-kw_ticket_content',
  templateUrl: './kw_ticket_content.component.html',
  styleUrls: ['./kw_ticket_content.component.scss']
})
export class KwTicketContent extends FlexTableComponent implements OnInit {

  query = {
    year_code: ""
  }

  QUERY_URL = "kw/StdTicketContentApply/page.htm";
  ADD_URL = "kw/StdTicketContentApply/add.htm";
  DELETE_URL = "kw/StdTicketContentApply/delete.htm";
  GET_URL = "kw/StdTicketContentApply/get.htm";
  EDIT_URL = "kw/StdTicketContentApply/edit.htm";
  EXAM_TIME_URL = "pl/plExamTime/list.htm";
  
  CURR_YEAR_CODE_URL = "pl/plExamTime/getCurrentYearCode.htm";
  
  YEAR_CODE_URL = "pl/plExamTime/list.htm";

  GET_DEPT_URL = "frame/optr/getDeptType.htm"; //登录用户所属机构类型
  AREA_LIST_URL = "cf/cfOrganization/list.htm?org_type=4&status=1"; //考区列表



  timeList = [];
  currentYearCode = "";
  flag = false;
  modalWin: any;
  yearCode: any;
  course_yearCode: any;

  examAgentArray = [];
  logOrgType = "-1";


  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      id: [null],
      year_code: [null, Validators.required],
      org_code: [null, Validators.required],
      title: [null, Validators.required],
      content: [null, Validators.required],
     

    });
  }

  ngOnInit() {
    FrameRequest.requestArray(this.EXAM_TIME_URL, {}, (array) => {
      this.timeList = array;
    });
    
    FrameRequest.requestEntity(this.GET_DEPT_URL, {}, (entity) => {
      this.logOrgType = entity;
      // 考试院账号，则展示 所有考区
      if(this.logOrgType == '0'){
        FrameRequest.requestArray(this.AREA_LIST_URL,{}, async (array) => {
          this.examAgentArray = array;
        });
      }else{
        // 非考试院账号，只展示当前考区
        FrameRequest.requestEntity("cf/cfOrganization/queryExamArea.htm",{}, async (entity) => {
          this.examAgentArray = [entity];
          
        });
      }

    });

   

    FrameRequest.requestEntity(this.CURR_YEAR_CODE_URL, {}, (entity) => {
      this.currentYearCode = entity;
      this.query.year_code=this.currentYearCode;
    });
  }

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

   
  }



  async preGoEditEvent(entity?) {
    if (!entity) {
      this.flag = false;
      if (!this.item) {
        this.item = {};
      }
      this.item.year_code = this.currentYearCode;
    } else {
      this.flag = true;
    }
    return true;
  }

  
  /**
     * 模态窗口隐藏
     */
  hideModalWin() {
    if (this.modalWin) {
      this.modalWin.hide();
    }
  }
 
  
  carouselContentFilter(str) {
    
    if(str){
        var carContent ='';
        if(str.length >= 40){
           
            carContent = str.substring(0,40) + '...';
        }
        else {
            carContent = str
        }
        return carContent;
    }else{
      return str;
    }
}


apply() {
  AppComponent.confirm("确定提交自治区考试院审核吗？", () => {
    
    FrameRequest.request("kw/StdTicketContentApply/apply.htm", this.item, () => {
      AppComponent.GobalComponent.successMsg("提交自治区考试院审核成功");
      this.editShowType = null;
      this.framePager.reload();
    });
  });
  
}

callback() {
  AppComponent.confirm("确定撤回吗？", () => {
    
    FrameRequest.request("kw/StdTicketContentApply/callback.htm", this.item, () => {
      AppComponent.GobalComponent.successMsg("撤回成功");
      this.editShowType = null;
      this.framePager.reload();
    });
  });
  
}


}

