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

@Component({
  selector: 'app-kw-special-course-layout',
  templateUrl: './kw-special-course-layout.component.html',
  styleUrls: ['./kw-special-course-layout.component.scss']
})
export class KwSpecialCourseLayoutComponent extends FlexTableComponent implements OnInit {

	@ViewChild('uploader')
		uploader: FrameUploaderComponent;
		@ViewChild('uploaderForRoomName')
    uploaderForRoomName: FrameUploaderComponent;
    @ViewChild('donePager')
		donePager:FramePagerComponent;
		@ViewChild('toLayoutPager')
		toLayoutPager:FramePagerComponent;

		@ViewChild('ticketnoModal')
		ticketnoModal: TemplateRef<any>
		
		
	filePath : String;
	filePathForRoomName : String;
	ticket_no="";

  query = {
    exPointId: "",
    examTimeId: "",
    exUnitId: "", //具体选择的考点
    examDate: "" //具体选择的考试日期
  }

  TO_LAYOUT_URL = "kw/kwSpecialLayout/toLayout.htm";
  LAYOUT_URL = "kw/kwSpecialLayout/doLayout.htm"; //编排
  CANCEL_URL = "kw/kwSpecialLayout/cancelLayout.htm"; //取消编排
	LATEST_TIME_URL = "cf/cfOperateTime/getLatest.htm";
	
	UPLOAD_URL="kw/kwLayout/updateaddress.htm";
	UPLOAD_URL_FORROOMNAME="kw/kwLayout/updateaddressForRoomName.htm";


  toLayoutList = []; //待编排
  layoutList = []; //已编排
  alone_flag = 1; //独立编排标志

	constructor(public bsModalService: BsModalService) { 
		super(bsModalService);
		
	}


  ngOnInit() {
    FrameRequest.requestArray(this.LATEST_TIME_URL, { oper_type: 16 }, (array) => {
      if (array && array.length > 0) {
        this.query.examTimeId = array[0].exam_time_id;
				
				this.search();
      }
    });
    
  }

  //搜索
  search() {
    FrameRequest.requestMap(this.TO_LAYOUT_URL, { exam_time_id: this.query.examTimeId, exam_point_id: this.query.exPointId }, (map) => {
      if (map) {
        this.toLayoutList = map['noList'];
        this.layoutList = map['haveList'];
      }
    });
  }

  //编排
  layout() {
    if (this.toLayoutList && this.toLayoutList.length === 0) {
      AppComponent.alert({ title: '提示', text: '没有待编排的考生' });
      return;
    }
    let param = {
      exam_point_id: this.query.exPointId,
      exam_time_id: this.query.examTimeId,
      RequestLoading: true
	};
	AppComponent.GobalComponent.setLoading(true);
    FrameRequest.request(this.LAYOUT_URL, param, () => {
	  AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg('编排成功');
      this.search();
    });
  }

  //取消编排
  cancelLayout() {
    if (this.layoutList && this.layoutList.length === 0) {
      AppComponent.alert({ title: '提示', text: '没有已编排的考生' });
      return;
    }
    AppComponent.confirm("确定取消编排吗?", () => {
      let param = {
        exam_point_id: this.query.exPointId,
        exam_time_id: this.query.examTimeId
	  };
	 // AppComponent.GobalComponent.setLoading(true);
      FrameRequest.request(this.CANCEL_URL, param, (ret) => {
		//AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("取消成功!");
        this.search();
      });
    });
  }

  //导出编排明细
  exportLyDetail() {
    let param = {
        exam_point_id:this.query.exPointId,
        exam_unit_id:this.query.exUnitId,
        exam_time_id:this.query.examTimeId,
        alone_flag: this.alone_flag,
        RequestLoading:true
    };
    FrameRequest.download("kw/kwLayoutDetail/exportlayout.htm",param);
  }

//   //导出编排汇总
//   exportLySummary() {
//     let param = {
//         exam_point_id:this.query.exPointId,
//         exam_unit_id:this.query.exUnitId,
//         exam_time_id:this.query.examTimeId,
//         alone_flag: this.alone_flag,
//         RequestLoading:true
//     };
//     FrameRequest.download("kw/kwLayoutVolumeCourse/exportsummery.htm",param);
//   }

  //导出编排汇总
  exportLySummary() {
		if (!this.query.examTimeId) {
			AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
			return;
		}
		// if (!this.query.exPointId) {
		// 	AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
		// 	return;
		// }
		let param = {
			exam_point_id: this.query.exPointId,
			exam_unit_id: "",
			exam_time_id: this.query.examTimeId,
			alone_flag: this.alone_flag
		};
		AppComponent.GobalComponent.setLoading(true);
		FrameRequest.download("kw/kwLayout/exportsummery.htm", param, ()=>{
			AppComponent.GobalComponent.setLoading(false);
			AppComponent.GobalComponent.successMsg("考场地址导出成功");
		});
		}
		
		//导出编排汇总
  exportLySummaryForRoomName() {
    if (!this.query.examTimeId) {
        AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
        return;
    }
   
    let param = {
        exam_point_id: this.query.exPointId,
        exam_unit_id: "",
				exam_time_id: this.query.examTimeId,
				alone_flag: this.alone_flag
    };
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/kwLayout/exportsummeryForRoomName.htm", param, ()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("考场地址导出成功");
    });
	}
	
	  //更新考场地址
	  uploadExamAddr() {
		if (!this.query.examTimeId) {
			AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
			return;
		}
		// if (!this.query.exPointId) {
		// 	AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
		// 	return;
		// }
		let param = {
			title_rows: 1
		  };
		  this.uploader.choooseFileAndUpload(param, ((ret) => {
			
			if (ret.errorCode > 0) {
			  AppComponent.alert({title:"信息提示", text: ret.errorMsg});
			}
			else if (ret.errorCode == -1) {
			  this.filePath = ret.url;
			  AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，再上传' });
			} else if (ret.errorCode == -2) {
				AppComponent.alert({ 'title': '提示', 'text': ret.errorMsg });
		  } else if (ret.errorCode == 0) {
			  AppComponent.alert({title:"信息提示", text:"更新成功!"});
			}
		  }));
		}
		
		//更新考场地址
		uploadExamAddrForRoomName() {
			if (!this.query.examTimeId) {
					AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
					return;
			}
		
			let param = {
					title_rows: 1
				};
				this.uploaderForRoomName.choooseFileAndUpload(param, ((ret) => {
					if (ret.errorCode > 0) {
						AppComponent.alert({title:"信息提示", text: ret.errorMsg});
					}
					else if (ret.errorCode == -1) {
						this.filePathForRoomName = ret.url;
						AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，再上传' });
					} else if (ret.errorCode == -2) {
						AppComponent.alert({ 'title': '提示', 'text': ret.errorMsg });
					} else if (ret.errorCode == 0) {
						AppComponent.alert({title:"信息提示", text:"更新成功!"});
					}
				}));
		}

		downloadErrFileForRoomName() {
			AppComponent.GobalComponent.setLoading(true);
			FrameRequest.download("pl/kwLayout/error/download.htm",  { filePath: this.filePathForRoomName }, ()=>{
					AppComponent.GobalComponent.setLoading(false);
			});
		}
	
	   //错误数据下载
	   downloadErrFile() {
	 	AppComponent.GobalComponent.setLoading(true);
	 	FrameRequest.download("pl/kwLayout/error/download.htm",  { filePath: this.filePath }, ()=>{
	 		AppComponent.GobalComponent.setLoading(false);
	 	});
	   }
	
	//   //导出编排数据
	//   expLyData() {
	// 	if (!this.query.examTimeId) {
	// 		AppComponent.alert({ title: "信息提示", text: "请选择考试批次!" });
	// 		return;
	// 	}
	// 	if (!this.query.exPointId) {
	// 		AppComponent.alert({ title: "信息提示", text: "请选择考点!" });
	// 		return;
	// 	}
	// 	debugger;
	// 	if (!this.donePager.getRecords() || this.donePager.getRecords().length == 0) {
	// 		AppComponent.alert({ title: "信息提示", text: "没有可以导出的数据!" });
	// 		return;
	// 	}
	// 	let param = {
	// 		exam_point_id:this.query.exPointId,
	// 		exam_time_id:this.query.examTimeId
	// 	};
	// 	AppComponent.GobalComponent.setLoading(true);
	// 	FrameRequest.download("kw/kwLayout/expdonexls.htm", param, ()=>{
	// 		AppComponent.GobalComponent.setLoading(false);
	// 		AppComponent.GobalComponent.successMsg("导出成功!");
	// 	});
	//   }


	
  previewTicketno() {
    this.shouModalWin(this.ticketnoModal,'modal-lg');
  }

  //预览准考证
  downTicket() {
    if(!this.ticket_no){
      AppComponent.alert({ 'title': '提示', 'text': '请输入准考证号' });
      return;
	}
	
    FrameRequest.requestEntity("std/stdRegInfo/queryStdByTicketNo.htm",{ticket_no: this.ticket_no},  (entity) => {
        
        if(entity) {
            AppComponent.GobalComponent.setLoading(true);
            FrameRequest.download("std/stdRegInfo/downTicket.htm",  {ticket_no: this.ticket_no,is_layout:1}, ()=>{
								AppComponent.GobalComponent.setLoading(false);
								this.hideModal();
                AppComponent.GobalComponent.successMsg("下载成功!");
            });
    
        }else{
            AppComponent.alert({ 'title': '提示', 'text': '准考证'+this.ticket_no+'在系统中不存在!' });
        }
        
    })
    
    }

}
