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';

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

  majorArray = [];
  rightArray = [];
  currMajorIds : any;
  query = {
    school_id : '',
    batch_id : ''
  };
  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({

    });
  }

  ngOnInit() {
    const param = {
      material_rule_id: this.ResponseEntity.material_rule_id,
      material_def_id: this.ResponseEntity.material_def_id,
      rule_type: 'aspect'
    };
    AjaxService.requestArray('cf/material/rule/detail/list.htm',param, (array)=>{
      this.rightArray = array;
    });
    AjaxService.requestArray('cf/major/list.htm',{school_id: this.ResponseEntity.school_id,batch_id: this.ResponseEntity.batch_id}, (array)=>{
      this.majorArray = array;
    });
  }

  initDefautEntity() {
    if (this.detailMode === 'add') {

    }
  }

  selectMajor() {

    console.log(this.currMajorIds);
    // this.aspectPager.pagerParams.major_ids = this.currMajorIds;
    this.aspectPager.reload({school_id: this.ResponseEntity.school_id, batch_id: this.ResponseEntity.batch_id,major_ids:this.currMajorIds});
  }

  // 左边加到右边
  addToRight() {

    const leftCheckedArray = this.aspectPager.getCheckedArray();
    for (const asp of leftCheckedArray) {
      let exist = false;
      for (const m of this.rightArray) {
        if (parseInt(m.rule_value, 10) === asp.aspect_id) {
          exist = true;
          break;
        }
      }
      if (!exist) {
        const mObj = {
          rule_value: asp.aspect_id + '',
          major_code: asp.major_code,
          major_name: asp.major_name,
          aspect_code: asp.aspect_code,
          aspect_name: asp.aspect_name,
        };
        this.rightArray.push(mObj);
      }
    }

  }
  // 从右边删除
  delFromRight(v) {

    for (const m of this.rightArray) {
      if (parseInt(v.rule_value,10) === parseInt(m.rule_value,10)) {
        const idx = this.rightArray.indexOf(m);
        this.rightArray.splice(idx, 1);
        break;
      }
    }
  }


  getRightIdArray() {
    const checkedArray = [];
    this.rightArray.forEach((item) => {
      checkedArray.push(parseInt(item.rule_value,10));
    });
    return checkedArray;
  }

  save() {
		if (this.rightArray.length === 0) {
      MsgService.errorToastr('请至少选择一条记录!');
      return ;
    }
    const _this = this;
    const checkedArray = this.getRightIdArray();
    const param = {
      ids: checkedArray,
      material_rule_id: this.ResponseEntity.material_rule_id,
      material_def_id: this.ResponseEntity.material_def_id,
      rule_type: 'aspect'
    };
    MsgService.showLoading();
    AjaxService.request('cf/material/rule/aspect/set.htm', param, (entity) => {
      MsgService.hideLoading();
      if(entity.success === true){
        MsgService.successToastr('设置成功!');
        _this.closeWindow();
      } else {
        MsgService.errorToastr(entity.errorMsg);
      }
    });
  }

  deleteAll() {
    this.rightArray = [];
  }

  saveAll() {
    const _this = this;
    const param = {
      school_id: this.ResponseEntity.school_id,
      batch_id: this.ResponseEntity.batch_id,
      major_ids:this.currMajorIds,
      material_rule_id: this.ResponseEntity.material_rule_id,
      material_def_id: this.ResponseEntity.material_def_id,
      rule_type: 'aspect'
    };
    MsgService.showLoading();
    AjaxService.request('cf/material/rule/aspect/setAll.htm', param, (entity) => {
      MsgService.hideLoading();
      if(entity.success === true){
        MsgService.successToastr('设置成功!');
        _this.closeWindow();
      } else {
        MsgService.errorToastr(entity.errorMsg);
      }
    });
  }
}
