import {Component, OnInit} from '@angular/core';
import {SystemService} from '../../system.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'smart-separation',
    templateUrl: './separation.component.html',
    styles: []
})
export class SeparationComponent implements OnInit {

    isVisible = false;
    title;
    list = [];
    value = 0;
    first = {
        id:"",
        scValue:null,
        check:null
    };

    constructor(private systemSer: SystemService,private message:NzMessageService) {
    }

    ngOnInit() {}

    //查询三员分立
    separationPowerStatus() {
        this.systemSer.getSeparationConfig().subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    this.first = res.data[0];
                    if(this.first.scValue == 1){
                        this.first.check = true;
                    }else{
                        this.first.check = false;
                    }
                    res.data.splice(0, 1);
                    this.list = res.data;
                    this.list.forEach(e=>{
                        if(e.scValue == 1){
                            e.check = true;
                        }else{
                            e.check = false;
                        }
                    })
                }
            }
        );
    }

    showModal(title) {
        this.title = title;
        this.isVisible = true;
        this.separationPowerStatus();
    }

    handleCancel() {
        this.isVisible = false;
    }

    changeValue(type) {
        let arr = {
            check:"",
            scValue:null,
            id:""
        };
        if (type == "1") {
            arr = this.first;
        }
        if (type == "2") {
            arr = this.list[0];
        }
        if (type == "3") {
            arr = this.list[1];
        }

        if (arr.check) {
            arr.scValue = 1;
        } else {
            arr.scValue = 0;
        }
        const data = {
            id: arr.id,
            scValue: arr.scValue,
        };

        this.systemSer.updateValue(data).subscribe(
            (res) => {
                if (res.errCode == 10000) {
                    if (this.first.check) {
                        this.message.success("已开启三员分立功能");
                    } else {
                        this.message.success("已关闭三员分立功能");
                    }
                } else {
                    this.message.error(res.errMsg);
                }
            }
        )
    }

    //保存
    handleOk() {
        let arr = [];
        arr.push(this.first);
        arr = arr.concat(this.list);
      let arr1 = arr.map(res=>{
           const data = {
               id:res.id,
               scValue:null,
           };
           if(res.check){
               data.scValue = 0;
           }else{
               data.scValue = 1;
           }
           return data;
       });
      this.systemSer.updateValue(arr1).subscribe(
          (res)=>{
              if(res.errCode == 10000){
                  if(this.first.check){
                      this.message.success("已开启三员分立功能");
                  }else{
                      this.message.success("已关闭三员分立功能");
                  }
                  this.isVisible = false;
              }else{
                  this.message.error(res.errMsg);
              }
          }
      )

    }

}
