import {observable, action } from "mobx";
import {WeaForm} from "comsMobx";
import {message} from "antd";
import { WeaLocaleProvider } from 'ecCom';
import * as API from "../apis/LDAPInit";
const {getLabel} = WeaLocaleProvider;

export default class LDAPInit {
  @observable newForm = new WeaForm(); 
  @observable status = {
    condition: [],
    loading: false
  }

  @action setState(params={}) {
    let status = {...this.status};
    Object.keys(params).forEach(key => {
      status[key] = params[key];
    })
    this.status = status;
  }

  @action getForm() {
    this.setState({loading: true});
    API.getForm().then(result => {
      this.setState({loading: false});
      if(result.status) {
        this.setState({condition: result.condition});
        this.newForm = new WeaForm();
        this.newForm.initFormFields(result.condition);
      }
    })
  }

  @action save = () => {
    let params = this.newForm.getFormParams()  || {};
    API.save(params).then(result => {
      if(result.status) {
        message.success(result.error);
      }else {
        message.error(result.error);
      }
    })
  }
}