import { Component, OnInit, ViewChild, TemplateRef, SimpleChanges, IterableDiffer, IterableDiffers } from "@angular/core";
import { BsModalService } from "ngx-bootstrap/modal";
import { StdRegInfoModal } from "../stdRegInfo/flex-std-reg-info";
import * as _ from 'lodash';

@Component({
  selector: "demo-modal",
  templateUrl: "./demo.html",
  styleUrls: ["./demo.scss"]
})
export class Demo implements OnInit {

  ngOnInit(): void {
    
  }

  @ViewChild('stdRegInfoModal')
  stdRegInfoModal: StdRegInfoModal
  area
  sex
  time:string
  date:string
  optr
  stdRegInfo: string
  clickedNode: string
  subItem=[]

  leftArray=[]
  rightArray=[]

  leftSelected = {};
  rightSelected = {};
  totalArray = [];

  query = {
    keyword:""
  }

  optr1: string
  total: string
  totalRight: string
  val: string

  // total
  optrValDiffer: IterableDiffer<any>;
  constructor(
    public bsModalService: BsModalService,
    iterableDiffers: IterableDiffers,
  ) {
    this.optrValDiffer = iterableDiffers.find([]).create<any>(null);

    for (let index = 0; index < 20; index++) {
      this.leftArray.push({name: (index + 1) +"_left"});
      this.rightArray.push({name: (index + 1) +"_right"});
    }

    for (let index = 1; index <= 20; index++) {
      this.totalArray.push({id:index,text:index});
    }
    
  }

  stdRegInfoModalConfirm(this){
    let checkArray = this.stdRegInfoModal.getCheckData();
    this.stdRegInfoModal.hideModal();
    this.stdRegInfo = JSON.stringify(checkArray);
  }
  clickedNodeConfirm(t){
    this.clickedNode = JSON.stringify(t.clickedTreeNode);
  }

  public optrChange(a1,a2){
    if(a1 == null){
      this.subItem = [];;
      return;
    }
    let arr = []
      for (let i = 0; i < 5; i++) {
        arr.push({
          optr_id: 1,
          optr_name: a1 + "_"+ i
        })
      }
      this.subItem  = arr;
  }

  public totalChange(a1,a2){
    if(a1 == null){
      return;
    }
    this.init();
    for (let i = 0; i < a1; i++) {
      this.leftArray[i].selected = true;
    }
  }
  public totalRightChange(a1,a2){
    if(a1 == null){
      return;
    }
    this.init();
    for (let i = 0; i < a1; i++) {
      this.rightArray[i].selected = true;
    }
  }

  init(){
    for (let i = 0; i < this.leftArray.length; i++) {
      if(this.leftArray[i])this.leftArray[i].selected = false;
      if(this.rightArray[i])this.rightArray[i].selected = false;
    }
  }
  moveAllRight(){
    this.rightArray.concat(_.cloneDeep(this.leftArray));
    this.leftArray = [];
  }
  moveAllLeft(){
    this.leftArray.concat(_.cloneDeep(this.rightArray));
    this.rightArray = [];
  }
  moveRight(){

    /*
    this.rightArray.push(_.cloneDeep(this.leftSelected));
    for (let index = 0; index < this.leftArray.length; index++) {
      let item = this.leftArray[index];
      if(this.leftSelected && item.name == this.leftSelected['name']){
        this.leftArray = this.leftArray.slice(0,index).concat(this.leftArray.splice((index+1),(this.leftArray.length)))
        this.leftSelected = null;
      }
      
    }
    */
    let selectedArray = [];
    let unselectedArray = [];
    for (let index = 0; index < this.leftArray.length; index++) {
      let item = this.leftArray[index];
      if(item.selected === true){
        item.selected = false;
        selectedArray.push(item);
      }else{
        unselectedArray.push(item);
      }
    }
    this.leftArray = unselectedArray;
    this.rightArray = this.rightArray.concat(selectedArray);
  }
  moveLeft(){
    /*
    this.leftArray.push(this.rightSelected);
    for (let index = 0; index < this.rightArray.length; index++) {
      let item = this.rightArray[index];
      if(this.rightSelected && item.name == this.rightSelected['name']){
        this.rightArray = this.rightArray.slice(0,index).concat(this.rightArray.splice((index+1),(this.rightArray.length)))
        this.rightSelected = null;
      }
      
    }
    */
    let selectedArray = [];
    let unselectedArray = [];
    for (let index = 0; index < this.rightArray.length; index++) {
      let item = this.rightArray[index];
      if(item.selected === true){
        item.selected = false;
        selectedArray.push(item);
      }else{
        unselectedArray.push(item);
      }
    }
    this.rightArray = unselectedArray;
    this.leftArray = this.leftArray.concat(selectedArray);
  }
}
