import { Component, OnInit, ViewChild } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import {BsModalRef} from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../frame/core/detail/frame.detail';
import { AjaxService } from '../../../../core/service/ajax.service';
import { FuncService } from '../../../../core/service/func.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePager } from 'src/app/core/frame/pager/frame-pager/frame-pager';
import { ThrowStmt } from '@angular/compiler';

@Component({
  selector: 'app-cf-set-optr',
  templateUrl: './set-optr.component.html',
  styleUrls: ['./set-optr.component.scss']
})
export class SetOptrComponent extends FrameDetailComponent implements OnInit {
  @ViewChild('optrPager', {static: false}) optrPager: FramePager;

  intervalId: any;
  rightArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({
    });
  }

  ngOnInit() {

  }

  initDefautEntity() {
    if(this.ResponseEntity.type==='first'){
        AjaxService.requestArray('cf/initial/optr/detail/list.htm', {main_id: this.ResponseEntity.id}, (array) => {

            this.rightArray = array;
        });
    }
    if(this.ResponseEntity.type==='re'){

        AjaxService.requestArray('cf/initial/re/optr/detail/list.htm', {main_id: this.ResponseEntity.id}, (array) => {

            this.rightArray = array;
        });
    }
  }

  save() {

		if (this.rightArray.length === 0) {
      MsgService.errorToastr('请至少选择一条记录!');
      return ;
    }
    const _this = this;
    const checkedArray = this.getRightIdArray();
    const param = {ids: checkedArray, main_id : this.ResponseEntity.id,type:this.ResponseEntity.type};
      MsgService.showLoading();
      AjaxService.request('cf/initial/optr/set.htm', param, (entity) => {
        MsgService.hideLoading();
        if(entity.success === true){
          MsgService.successToastr('设置成功!');
          _this.closeWindow();
          // _this.pager.reload();
        } else {
          MsgService.errorToastr(entity.errorMsg);
        }
      });
  }

  getRightIdArray() {
    const checkedArray = [];
    this.rightArray.forEach((item) => {
      checkedArray.push(item.optr_id);
    });
    return checkedArray;
  }

  // 左边加到右边
  addToRight() {
    const leftCheckedArray = this.optrPager.getCheckedArray();
    for (const optr of leftCheckedArray) {
      let exist = false;
      for (const m of this.rightArray) {
        if (m.optr_id === optr.optr_id) {
          exist = true;
          break;
        }
      }
      if (!exist) {
        const mObj = {
          optr_id: optr.optr_id,
          optr_name: optr.optr_name,
          optr_sex: optr.optr_sex,
          login_name: optr.login_name,
          optr_mobile: optr.optr_mobile,
          optr_desc: optr.optr_desc
        };
        this.rightArray.push(mObj);
      }
    }

  }

  // 从右边删除
  delFromRight(v) {
    for (const m of this.rightArray) {
      if (v.optr_id === m.optr_id) {
        const idx = this.rightArray.indexOf(m);
        this.rightArray.splice(idx, 1);
        break;
      }
    }
  }


}
