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';
import * as _ from 'lodash';

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

export class LayoutAdjustComponent extends FlexTableComponent implements OnInit {
    
    examUnitList : any;

    //编排查询
    examDateList = [];//具体考试日期,根据
    lyRoomDetailList = [];//具体考场对应的学生编排详情列表

    //编排调整
    ajustStep = "select";//select,sameAdjust,diffAdjust
    sameChecked = false;//控制调整选radio
    diffChecked = false;//控制调整选radio
    amPmList = [{key:"AM",val:"上午"},{key:"PM",val:"下午"}];
    adjustDetailSourceList = [];//学生编排详情列表
    adjustDetailTargetList = [];//学生编排详情列表
    adjustDiffRoomSourceList = [];//考场信息-不同考场间调整-转出
    adjustDiffRoomTargetList = [];//考场信息-不同考场间调整-转入
    sourceRoomId : any;//考务编排调整用-相同考点单元内调整
    sourceStdNum : number;//转出人数-相同考点单元内调整
    targetStdNum : number;//转入人数-相同考点单元内调整
    targetRoomId : any;//考务编排调整用-相同考点单元内调整
    sourceRoomArr = [];//考务调整-不同考场单元之间
    targetExUnit : any;//目标考点单元-不同考场单元之间

    blnClickDiffMove = false;

    @ViewChild('sameSourcePager')
    sameSourcePager:FramePagerComponent;

    @ViewChild('sameTargetPager')
    sameTargetPager:FramePagerComponent;

    @ViewChild('diffTargetPager')
    diffTargetPager:FramePagerComponent;
    @ViewChild('diffSourcePager')
    diffSourcePager:FramePagerComponent;

    query = {
        exPointId:"",
        maxCourse:"",
        examTimeId:"",
        exUnitId:"", //具体选择的考点单元
        examDate:"", //具体选择的考试日期
        amPm:"", //上午、下午
        outExUnitId:"",//转出考点单元-不同考点单元之间考务编排
        inExUnitId:"" //转入考点单元-不同考点单元之间考务编排
    }

    constructor(public bsModalService: BsModalService) { 
        super(bsModalService);
    }
  
    ngOnInit() {
        var param = {
            start: 0,
            limit: 100,
            exam_time_id: "",
            exam_point_id: "",
            RequestLoading: true
        }
        FrameRequest.requestPage("kw/kwExamUnit/page.htm", param, (page)=>{
            this.examUnitList = page.records;
        });    
    }

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

  //参数处理,父类调用,覆盖了父类该方法
  async preShowModalWin() {
    var param = {
        exam_point_id: this.query.exPointId,
        alone_flag: 0,
        RequestLoading:true
    };
    await FrameRequest.getRequestPromise('kw/kwExamUnit/getunitlist.htm',param)
    .then((resp) => {
      this.examUnitList = resp['array'];
    })
    return true;
  }

  //编排查询------------------------------------------------------------------------------------------------------
  examTimeChange(a1) {
    if (!a1) {
        this.examDateList=[];
        return;
    }
    FrameRequest.requestArray("pl/plExamTime/getdatelist.htm", {exam_time_id:a1},(arr)=>{
        this.examDateList = arr;
    });
    var param = {
        start: 0,
        limit: 100,
        exam_time_id: a1,
        exam_point_id: "",
        RequestLoading: true
    }
    FrameRequest.requestPage("kw/kwExamUnit/page.htm", param, (page)=>{
        this.examUnitList = page.records;
    });    
  }

  //考务编排调整---------------------------------------------------------------------------------------------------------------
  sameAdjustQuery() {
    this.sameSourcePager.reload();
    this.sameTargetPager.reload();
    this.adjustDetailSourceList = [];
    this.adjustDetailTargetList = [];
  }
  
  selectAdjust(type) {
    if (type == "same") {

        this.ajustStep = "same";
        this.sameChecked = true;
        this.diffChecked = false;

    } else if (type == "diff") {

        this.ajustStep = "diff";
        this.diffChecked = true;
        this.sameChecked = false;

    } else if (type == "back") {
        this.ajustStep = "select";
    }
  }
  //相同考点单元内考场间调整-分页回调
  sameSourceSearch(param) {
    param.exam_time_id=this.query.examTimeId;
    param.exam_unit_id=this.query.exUnitId;
    param.exam_date=this.query.examDate;
    param.am_pm=this.query.amPm;
  }
  sameTargetSearch(param) {
    param.exam_time_id=this.query.examTimeId;
    param.exam_unit_id=this.query.exUnitId;
    param.exam_date=this.query.examDate;
    param.am_pm=this.query.amPm;
  }
  //相同考点单元内考场间调整-列表刷新考生明细
  sameDetailQuery(room,from) {
    let param = {
        room_id:room.id,
        //course_id:room.course_id,
        RequestLoading:true
    };
    FrameRequest.requestArray("kw/kwLayoutDetail/querydetaillist.htm", param, (arr)=>{
        if (from == 'source') {
            this.adjustDetailSourceList = arr;
            this.sourceRoomId = room.id;
            this.sourceStdNum = room.stu_num;
        } else if (from == 'target') {
            this.adjustDetailTargetList = arr;
            this.targetRoomId = room.id;
            this.targetStdNum = room.stu_num;
        }
    });
  }

  //移动到转入表
  moveAdjustSame() {
    if (!this.sourceRoomId) {
        AppComponent.alert({title:"信息提示",text:"请选择转出考场!"});
        return;
    }
    if (!this.targetRoomId) {
        AppComponent.alert({title:"信息提示",text:"请选择转入考场!"});
        return;
    }
    if (this.sourceRoomId == this.targetRoomId) {
        AppComponent.alert({title:"信息提示",text:"相同考场之间无法转入!"});
        return;
    }
    if (this.sourceStdNum + this.targetStdNum > 30) {
        AppComponent.alert({title:"信息提示",text:"转出人数>待转入考场剩余人数!"});
        return;  
    }
    this.adjustDetailTargetList.concat(_.cloneDeep(this.adjustDetailSourceList));
    this.adjustDetailSourceList = [];
  }

  //相同考点下-考务编排调整-提交
  submitSame() {
    if (!this.sourceRoomId) {
        AppComponent.alert({title:"信息提示",text:"请选择转出考场!"});
        return;
    }
    if (!this.targetRoomId) {
        AppComponent.alert({title:"信息提示",text:"请选择转入考场!"});
        return;
    }
    if (this.sourceRoomId == this.targetRoomId) {
        AppComponent.alert({title:"信息提示",text:"相同考场之间无法转入!"});
        return;
    }
    if (this.sourceStdNum + this.targetStdNum > 30) {
        AppComponent.alert({title:"信息提示",text:"转出人数>待转入考场剩余人数!"});
        return;  
    }
    let param = {
        fromRoomId:this.sourceRoomId,
        toRoomId:this.targetRoomId,
        RequestLoading:true
    };
    AppComponent.confirm("确定提交调整?",()=>{
        FrameRequest.request("kw/kwLayout/adjustsame.htm", param, (ret) => {
            if (ret.errorCode == 0) {
                this.sameSourcePager.reload();
                this.sameTargetPager.reload();
                AppComponent.GobalComponent.successMsg("编排调整成功!");
                return;
            }
        });
    });

  }

  //---------------------------------------------------------不同考点之间-------------------------------------------------------------------
  //不同考点之间调整-查询考场信息
  diffSourceSearch(param) {
    param.exam_time_id = this.query.examTimeId;
    param.exam_unit_id = this.query.outExUnitId;
    param.exam_date = this.query.examDate;
    param.am_pm = this.query.amPm;
  }
  diffTargeSearch(param) {
    param.exam_time_id = this.query.examTimeId;
    param.exam_unit_id = this.query.inExUnitId;
    param.exam_date = this.query.examDate;
    param.am_pm = this.query.amPm;
  }

  getToExUnitRemains() {
    let param = {
        id:this.query.inExUnitId
    };
    FrameRequest.requestEntity("kw/kwExamUnit/get.htm", param, (entity) => {
        this.targetExUnit = entity;
    });
  }

  diffMovLeft() {
    let arr = this.diffSourcePager.getCheckedArray();
    if (this.query.outExUnitId == this.query.inExUnitId) {
        AppComponent.alert({title:"信息提示",text:"转出转入考点单元不能相同!"});
        return;
    }
    if (arr.length == 0) {
        AppComponent.alert({title:"信息提示",text:"请选择要转出的考场!"});
        return;
    }
    if (this.diffTargetPager.recordTotal + arr.length > this.targetExUnit.room_num) {
        AppComponent.alert({title:"信息提示",text:"剩余考场数小于转入考场!"});
        return;
    }
    let newSourceArr = [];
    for (let i=0;i<arr.length;i++) {
        this.sourceRoomArr.push(arr[i].id);
        arr[i].room_code = this.diffTargetPager.recordTotal + 1;
        this.diffTargetPager.records.push(arr[i]);
        this.diffTargetPager.recordTotal = this.diffTargetPager.recordTotal + 1;
        for (let j=0;j<this.diffSourcePager.records.length;j++) {
            if (this.diffSourcePager.records[j] && this.diffSourcePager.records[j].id == arr[i].id) {
                delete this.diffSourcePager.records[j];//delete 后下标还在，元素为undefined
                break;
            }
        }
    }
    //把没有删除的对象 放到新数组，再赋值回diffSourcePager去
    for (let j=0;j<this.diffSourcePager.records.length;j++) {
        if (this.diffSourcePager.records[j]) {
            newSourceArr.push(this.diffSourcePager.records[j]);
        }
    }
    this.diffSourcePager.records = newSourceArr;

    this.diffSourcePager.recordTotal = this.diffSourcePager.recordTotal - arr.length;
    this.blnClickDiffMove = true;
  }
  //不同考点单元之间调整-提交
  submitDiff() {
    if (!this.blnClickDiffMove) {
        AppComponent.alert({title:"信息提示",text:"请先转入考场!"});
        return;
    }
    let param = {
        roomIds:this.sourceRoomArr,
        fromExUnitId:this.query.outExUnitId,
        toExUnitId:this.query.inExUnitId,
        examDate:this.query.examDate,
        amPm:this.query.amPm,
        RequestLoading:true
    };
    AppComponent.confirm("确定提交调整?",()=>{
        FrameRequest.request("kw/kwLayout/adjustdiff.htm", param, (ret) => {
            if (ret.errorCode == 0) {
                this.diffSourcePager.reload();
                this.diffTargetPager.reload();            
                this.blnClickDiffMove = false;
                AppComponent.GobalComponent.successMsg("编排调整成功!");
                return;
            }
        });
    });
  }

}
  