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

class Main {
  @observable form = new WeaForm();
  @observable condition = [];
  @observable singleForm = new WeaForm();
  @observable singleCondition = [];
  @observable hasRight = true;

  @observable
  status = {
    loading: false,
    formLoading: true
  }

  @observable buttons = [];

  @action
  setStatus = (key, value) => {
    this.status[key] = value;
  }

  @action
  doInit = () => {
    this.getForm();
    this.getRightMenu();
  }

  @action
  singleInit = id => {
    this.setStatus("formLoading", true);
    this.getRightMenu();
    API.getSysid(id).then(res => {
      if(res.noright == "1") {
        this.hasRight = false;
        return;
      }
      this.hasRight = true;
      this.setStatus("formLoading", false);
      this.singleForm.initFormFields(res.condition);
      this.singleCondition = res.condition;
    })
  }

  @action
  getForm = params => {
    this.setStatus("formLoading", true);
    API.getForm(params).then(res => {
      if(res.noright == "1") {
        this.hasRight = false;
        return;
      }
      this.hasRight = true;
      runInAction(() => {
        this.setStatus("formLoading", false);
        this.form.initFormFields(res.condition);
        this.condition = res.condition;
      })
    })
  }

  @action
  getRightMenu = params => {
    this.setStatus("formLoading", true);
    API.getRightMenu(params).then(res => {
      runInAction(() => {
        this.setStatus("formLoading", false);
        this.buttons = res.rightMenus;
      })
    })
  }

  @action
  doSave = (params, callback) => {
    this.setStatus("loading", true);
    API.doSave(params).then(res => {
      const {
        msg,
        redirect
      } = res;
      if (msg) {
        message.info(msg);
      }

      if(res.status == "1") {
        message.success(getLabel('22619', '保存成功'));
        isFunction(callback) && callback();
      }

      redirect && window.open(`${window.ecologyContentPath || ""}/${redirect}`, '_self');

      runInAction(() => {
        this.setStatus("loading", false);
      })
    })
  }
}

export default Main;