import { Component, OnInit, ViewChild, ElementRef, TemplateRef } from '@angular/core';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormControl, Validators, FormBuilder } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { FrameFunction } from '../../../../../shared/frame/frame_function';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { AppComponent } from '../../../../../app.component';
import { deprecate } from 'util';
import { FramePagerComponent } from '../../../../../core/pager/pager.component';

@Component({
    selector: 'app-layout-query',
    templateUrl: './layout-query.component.html',
    styleUrls: ['./layout-query.component.scss']
})

export class LayoutQueryComponent extends FlexTableComponent implements OnInit {
    
    examPointList = [];//考点
    examUnitList = [];//考点
    examDateList = [];//具体考试日期,根据
    lyRoomDetailList = [];//具体考场对应的学生编排详情列表
    is_zw:any;

    interval = null;
    progress_value = -1;

    time_interval = null;
    time_value = -1;
    total_time_value = -1;

    @ViewChild('exportModal')
    exportModal:TemplateRef<any>;
    @ViewChild('exportSeaModal')
    exportSeaModal:TemplateRef<any>;
    @ViewChild('exportSeaThreeModal')
    exportSeaThreeModal:TemplateRef<any>;

    QUERY_URL = "kw/kwLayoutDetail/page.htm";


    query = {
        exPointId: "",
        faceSortType: "",
        examTimeId: "",
        exUnitId: "", //具体选择的考点
        examDate: "", //具体选择的考试日期
        orgType: "",
        examTimes: "",//时段
        alone_flag:"",
        exam_time:"",
        isOneUnitOneClassroom: false
    }

    constructor(public bsModalService: BsModalService) { 
        super(bsModalService);
    }
  
    ngOnInit() {
        let this_ = this;
        setTimeout(() => {
            FrameRequest.requestMap("kw/kwLayout/initdata.htm", {}, (map)=>{
                this_.examUnitList = map["examUnitList"];
                this_.query.isOneUnitOneClassroom = map["isOneUnitOneClassroom"];
                if (map["exam_time_id"]) {
                    this_.query.examTimeId = map["exam_time_id"];
                    this_.examTimeChange(this_.query.examTimeId);
                    this_.query.orgType = map["org"].org_type;
                    
                }
            });
        }, 1000);
    }

    resetData(){
        this.query.examDate = "";
        this.query.exam_time = "";
    }

    //编排界面-分页查询条件
    search(param) {
        if (!this.query.examTimeId) {
            this.query.examTimeId = "";
        }
        if (!this.query.exPointId) {
            this.query.exPointId = "";
        }

        

        if(!this.query.examDate && this.query.exam_time){
            AppComponent.alert({ title: "信息提示", text: "选择考试时间时，请选择考试日期!" });
            return;
        } 

        if(this.query.examDate && !this.query.exam_time){
            AppComponent.alert({ title: "信息提示", text: "选择考试日期时，请选择考试时间!" });
            return;
        } 

        param.exam_point_id=this.query.exPointId;
        param.exam_time_id=this.query.examTimeId;
        param.examDate=this.query.examDate;
        param.exam_time=this.query.exam_time;
    }

  //切换考试批次查询------------------------------------------------------------------------------------------------------
  examTimeChange(a1) {
    if (a1 == null) {
        this.examDateList=[];
        return;
    }
    FrameRequest.requestArray("pl/plExamTime/getdatelist.htm", {exam_time_id:a1,RequestLoading:true},(arr)=>{
        this.examDateList = arr;
    });
    let param = {
        start: 0,
        limit: 100,
        exam_time_id: a1,
        RequestLoading: true
    }
    FrameRequest.requestPage("kw/kwExamPoint/pageForQuery.htm", param, (page)=>{
        
        this.examPointList = page.records;
        //默认选中第一个考点
        if (this.examPointList.length > 0) {
            this.query.exPointId = this.examPointList[0].id;
            //刷新该考点下面的考点
            this.freshExamUit(this.query.exPointId);
        }
    });
    //查询考点
    /*let param2 = {
        start: 0,
        limit: 100,
        exam_time_id: a1,
        exam_point_id: "",
        RequestLoading: true
    }
    FrameRequest.requestPage("kw/kwExamUnit/page.htm", param2, (page)=>{
        this.examUnitList = page.records;
    });*/
  }

  //选择考点刷新考点--注意这里刷新所有的考点单元（特殊和非特殊的）
  freshExamUit(exPointId) {
    if (!exPointId) {
        return;
    }

    var param = {
        exam_time_id: this.query.examTimeId,
        exam_point_id: exPointId
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.requestArray("kw/kwExamUnit/listunitlist.htm", param, (arr)=>{
        AppComponent.GobalComponent.setLoading(false);
        this.examUnitList = arr;
    });
  }

  freshExamUit2(alone_flag) {
    if (!this.query.exPointId) {
        return;
    }

    if (!alone_flag) {
        return;
    }
    
    var param = {
        exam_time_id: this.query.examTimeId,
        exam_point_id: this.query.exPointId,
        alone_flag:alone_flag
    }
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.requestArray("kw/kwExamUnit/listunitlist.htm", param, (arr)=>{
        AppComponent.GobalComponent.setLoading(false);
        this.examUnitList = arr;
    });
  }

  //编排查询
  amQuerysearch(param) {
    if (!this.query.examTimeId) {
        this.query.examTimeId = "";
    }

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

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

    param.exam_time_id=this.query.examTimeId;
    param.exam_unit_id=this.query.exUnitId;
    param.exam_date=this.query.examDate;
    param.am_pm="AM";
  }
  pmQuerysearch(param) {
    if (!this.query.examTimeId) {
        this.query.examTimeId = "";
    }

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

    if (!this.query.examDate) {
        this.query.examDate = "";
    }
    
    param.exam_time_id=this.query.examTimeId;
    param.exam_unit_id=this.query.exUnitId;
    param.exam_date=this.query.examDate;
    param.am_pm="PM";
  }
  //查询指定考场的学生编排明细
  lyRoomDetailQuery(room) {
    let param = {
        room_id:room.id,
        //course_id:room.course_id,
        RequestLoading:true
    };
    FrameRequest.requestArray("kw/kwLayoutDetail/querydetaillist.htm", param, (arr)=>{
        this.lyRoomDetailList = arr;
    });
  }

  //打印头像单
  exportFunc() {
      //不是编排到考场的，一定要选考点
    if (!this.query.isOneUnitOneClassroom) {
        // 特殊考点选择是，则可以不选择考点
        if(this.query.alone_flag != '1'){
            if (!this.query.exUnitId) {
                AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
                return;
            }
        }   
    }

    if (!this.query.faceSortType) {
        AppComponent.alert({ title: "信息提示", text: "请选择排序方式!" });
        return;
    }
   
    AppComponent.confirm("确定下载图像单吗?",()=>{
        if(!this.query.examTimes){
            this.query.examTimes = "";
        }

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

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

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

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

        if(!this.query.alone_flag){
            this.query.alone_flag = "";
        }
    
        let param = {
            exam_point_id: this.query.exPointId,
            exam_unit_id: this.query.exUnitId,
            exam_time_id: this.query.examTimeId,
            face_sort_type: this.query.faceSortType,
            exam_times: this.query.examTimes,
            alone_flag:this.query.alone_flag,
            is_zw:this.is_zw
        };
    
        let progress_this = this;
        let page = 0;
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.requestEntity('kw/kwLayoutDetail/printlayoutphotopage.htm',param, (entity) => {
            AppComponent.GobalComponent.setLoading(false);
            if(entity){
                page = entity;
            }else{
                page = 10;
            }
    
            // 预计用时
            progress_this.total_time_value = Math.ceil(page*4/60);
            
            // 设置多少秒刷新一次：总页数page*每页需要多少秒完成/100
            let index = page*4000/100;
    
            // 如果刷新频率小于一秒，则设置为一秒
            if(index<1000){
                index = 1000;
            }else if(index>10000){
                // 大于30秒，则30秒刷新一次
                index = 10000;
            }
    
            progress_this.progress_value = 0;
            progress_this.time_value = 0;
    
            progress_this.interval = setInterval(function() { 
                if(progress_this.progress_value <99){
                progress_this.progress_value += 1;
                }
                
            }, index);
    
            progress_this.time_interval = setInterval(function() { 
                progress_this.time_value += 1;
            }, 1000);
    
            FrameRequest.download("kw/kwLayoutDetail/printlayoutphoto.htm",param,()=>{
               // this.hideModal();
                window.clearInterval(progress_this.interval);
                progress_this.progress_value = 100;
                window.clearInterval(progress_this.time_interval);
               // progress_this.time_value = -1;
    
                AppComponent.GobalComponent.successMsg("图像单下载成功");
            });
            
          });
    }); 

    
  }

  //打印头像单-按考试时间+考场 分页显示
  printPortraitBill(is_zw) {
    if (!this.query.examTimeId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
        return;
    }

    if (!this.query.exPointId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考区!" });
        return;
    }

    this.is_zw = is_zw;

    //不是编排到考场的，一定要选考点
    //if (!this.query.isOneUnitOneClassroom) {
        // 特殊考点选择是，则可以不选择考点
    //    if(this.query.alone_flag != '1'){
    //        if (!this.query.exUnitId) {
    //            AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
    //            return;
   //         }
   //     }   
  //  }

   
  this.shouModalWin(this.exportModal,'modal-md');

    
  }

  showPrintSeatBill(){
    if (!this.query.examTimeId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
        return;
    }

    if (!this.query.exPointId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考区!" });
        return;
    }


    if(!this.query.examDate && this.query.exam_time){
        AppComponent.alert({ title: "信息提示", text: "选择考试时间时，请选择考试日期!" });
        return;
    } 

    this.shouModalWin(this.exportSeaModal,'modal-md');
  }

  //打印座位单-按考试时间+考场+课程 分页显示
  printSeatBill() {
    
     //不是编排到考场的，一定要选考点
     if (!this.query.isOneUnitOneClassroom) {
        
        // 特殊考点选择是，则可以不选择考点
        if(this.query.alone_flag != '1'){
            if (!this.query.exUnitId) {
                AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
                return;
            }
        }   
    }

    if(this.query.alone_flag != '1'){
        if (!this.query.exUnitId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
            return;
        }
    }

    let param = {
        exam_point_id: this.query.exPointId,
        exam_unit_id:this.query.exUnitId,
        exam_time_id:this.query.examTimeId,
        is_room_course:1,
        alone_flag:this.query.alone_flag,
        exam_date:this.query.examDate,
        exam_time:this.query.exam_time,
        RequestLoading:true
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/kwLayoutDetail/printlayoutseatno.htm",param,()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("座位单导出成功");
    });
  }



  showPrintSeatThreeBill(){
    if (!this.query.examTimeId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
        return;
    }

    if (!this.query.exPointId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考区!" });
        return;
    }


    if(!this.query.examDate && this.query.exam_time){
        AppComponent.alert({ title: "信息提示", text: "选择考试时间时，请选择考试日期!" });
        return;
    } 

    this.shouModalWin(this.exportSeaThreeModal,'modal-md');
  }

  //打印座位单-按考试时间+考场+课程 分页显示
  printSeatBillThree() {
 
    //不是编排到考场的，一定要选考点
    if (!this.query.isOneUnitOneClassroom) {
        
        // 特殊考点选择是，则可以不选择考点
        if(this.query.alone_flag != '1'){
            if (!this.query.exUnitId) {
                AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
                return;
            }
        }   
    }

    if(this.query.alone_flag != '1'){
        if (!this.query.exUnitId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
            return;
        }
    }   

    let param = {
        exam_point_id: this.query.exPointId,
        exam_unit_id:this.query.exUnitId,
        exam_time_id:this.query.examTimeId,
        is_room_course:1,
        alone_flag:this.query.alone_flag,
        exam_date:this.query.examDate,
        exam_time:this.query.exam_time,
        RequestLoading:true
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/kwLayoutDetail/printlayoutseatnoThree.htm",param,()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("座位单导出成功");
    });
  }

  //打印座位单（合并）-按考试时间+考场 分页显示
  printSeatMergeBill() {
    if (!this.query.exUnitId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
        return;
    }
    let param = {
        exam_unit_id: this.query.exUnitId,
        exam_time_id: this.query.examTimeId,
        is_room_course: 0
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/kwLayoutDetail/printlayoutseatno.htm", param,()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("打印座位单（合并）导出成功");
    });
  }

    //导出编排明细
    exportLyDetail() {
        if (!this.query.examTimeId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
            return;
        }
        let param = {
            exam_point_id: "",
            exam_unit_id: "",
            alone_flag: "",
            exam_time_id: this.query.examTimeId
        };
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("kw/kwLayoutDetail/exportlayout.htm",param, ()=>{
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("编排明细导出成功");
        });
    }
    
    //导出编排汇总--包括普通统考和独立编排的
    exportLySummary() {
        if (!this.query.examTimeId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
            return;
        }
        let param = {
            exam_point_id: "",
            exam_unit_id: "",
            alone_flag: "",
            exam_time_id: this.query.examTimeId
        };
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("kw/kwLayoutVolumeCourse/exportsummery.htm",param,()=>{
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("编排汇总导出成功!");
        });
    }
    
    //下载考场安排表一
    expRoomArr1() {
        if (!this.query.examTimeId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
            return;
        }
        let param = {
            mode: "1",
            org_id: "",
            exam_time_id: this.query.examTimeId
        };
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("kw/kwLayoutVolumeCourse/examroomarrange.htm",param,()=>{
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("导出成功!");
        });
    }

    //下载考场安排表二
    expRoomArr2() {
        if (!this.query.examTimeId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
            return;
        }
        let param = {
            mode: "2",
            org_id: "",
            exam_time_id: this.query.examTimeId
        };
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("kw/kwLayoutVolumeCourse/examroomarrange.htm",param,()=>{
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("导出成功!");
        });
    }

     //考场号标识
     expRoomArr3() {
        if (!this.query.examTimeId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
            return;
        }
        let param = {
            mode: "3",
            org_id: "",
            exam_time_id: this.query.examTimeId
        };
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("kw/kwLayoutVolumeCourse/examroomarrange.htm",param,()=>{
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("导出成功!");
        });
    }

    //导出编排DBF
    exportLyDetailDbf() {
        if (!this.query.examTimeId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
            return;
        }
        let param = {
            exam_point_id: "",
            exam_unit_id: "",
            alone_flag: "",
            exam_time_id: this.query.examTimeId
        };
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("kw/kwLayoutDetail/exportlayoutDbf.htm",param, ()=>{
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("编排明细DBF导出成功");
        });
    }

    exportLyDetailDbfForGl() {
        if (!this.query.examTimeId) {
            AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
            return;
        }
        let param = {
            exam_point_id: "",
            exam_unit_id: "",
            alone_flag: "",
            exam_time_id: this.query.examTimeId
        };
        AppComponent.GobalComponent.setLoading(true);
        FrameRequest.download("kw/kwLayoutDetail/exportlayoutDbfGl.htm",param, ()=>{
            AppComponent.GobalComponent.setLoading(false);
            AppComponent.GobalComponent.successMsg("编排明细DBF导出成功");
        });
    }

    
  //编排确认
  layoutConfirm() {
    if (!this.query.examTimeId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
        return;
    }
    if (!this.query.exPointId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考区!" });
        return;
    }
    AppComponent.confirm("确定编排完成?",()=>{
        let param = {
            exam_point_id:this.query.exPointId,
            exam_time_id:this.query.examTimeId,
            //exam_area_id:
            RequestLoading:true
        };
        FrameRequest.request("kw/kwLayoutMonitor/confirm.htm", param, (ret) => {
            if (ret.errorCode == 0) {
                AppComponent.GobalComponent.successMsg("确定编排完成成功!");
                return;
            }
        });
      });     
  }

  expDbfData() {
    if (!this.query.examTimeId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
        return;
    }
    let param = {
        exam_time_id: this.query.examTimeId
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/kwLayoutDetail/expprintdbf.htm",param,()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("导出成功!");
    });
  }

}
  