import {
  observable,
  action,
} from "mobx";
import {
  WeaForm,
} from "comsMobx";
import {
  message
} from "antd";
import { 
  WeaLocaleProvider 
} from 'ecCom';
import * as API from "../apis/delegateClient";
import {setModalInfo} from '../util/IntegrationUtil';

const {getLabel} = WeaLocaleProvider;
  
export default class DelegateClient {
  @observable
  status = {
    loading: true,
    formLoading: true,
    hasRight: false,
    condition: [],
    buttons: [],
  }
  @observable baseForm = new WeaForm();
  @observable hiddenFields = {};

  @action
  setState = (params={}) => {
    let status = {...this.status};

    Object.keys(params) && Object.keys(params).forEach(key => {
      status[key] = params[key];
    })
    this.status = {...status};
  }

  @action('清空数据')
  cleanStore = () => {
    this.baseForm = new WeaForm();
    this.hiddenFields = {};
    this.setState({
      loading: true,
      formLoading: true,
      hasRight: false,
      condition: [],
    })
  }

  
  @action
  doInit = () => {
    this.cleanStore();
    this.getBaseInfoForm();
  }
  
  @action('获取form表单')
  getBaseInfoForm = (params={}) => {
    this.setState({formLoading: true});
    this.baseForm = new WeaForm();
    API.getBaseInfoForm(params).then(res => {
      if(res.ret == "noright") {
        this.setState({
          hasRight: false,
          loading: false,
          formLoading: false,
        });
        return;
      }

      this.hiddenFields = res.hiddenFields || {};
      this.setState({
        condition: res.condition || [],
        hasRight: true,
        formLoading: false,
        loading: false,
      });
      this.baseForm.initFormFields(res.condition || []);
    })
  }
  
  @action('对form表单的操作')
  getBaseInfoFormOperation = (params={}) => {
    this.setState({formLoading: true});
    API.getBaseInfoOperation(params).then(res => {
      if(res.status == "1") {
        if('1' == params.isdeploy) {
          let data = {};
          if(res.syncResult) {
            try {
              if(typeof(res.syncResult) === "string") {
                data = JSON.parse(res.syncResult) || {};
              } else if (typeof(res.syncResult) === "object") {
                data = res.syncResult;
              }
            } catch(er) {}
          }
          if(data.status === "failure") {
            if(data.errorNode && data.noValNode) {
              setModalInfo(<div>
                {getLabel('522113','集群部署失败， 有节点异常导致不能进行部署， 请检查异常节点， 并重新进行部署。')}
                <div>{getLabel('522114','配置失败的节点为')}: {data.errorNode}</div>
                <div>{getLabel('522115','验证失败的节点为')}: {data.noValNode}</div>
              </div>);
            } else if(data.errorNode) {
              setModalInfo(<div>
                {getLabel('522113','集群部署失败， 有节点异常导致不能进行部署， 请检查异常节点， 并重新进行部署。')}
                <div>{getLabel('522114','配置失败的节点为')}: {data.errorNode}</div>
              </div>);
            } else if(data.noValNode) {
              setModalInfo(<div>
                {getLabel('522113','集群部署失败， 有节点异常导致不能进行部署， 请检查异常节点， 并重新进行部署。')}
                <div>{getLabel('522115','验证失败的节点为')}: {data.noValNode}</div>
              </div>);
            }
          } else {
            message.success(getLabel('522155','部署成功'));
          }
        } else {
          message.success(getLabel('22619', '保存成功'));
        }
        this.getBaseInfoForm();
      }
      this.setState({formLoading: false});
    })
  }
}