import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import Authority from "../../mySalary/authority";
import { salaryacctAcctresultCheckAuth } from "../../../apis/calculate";

@inject("taxAgentStore")
@observer
class Layout extends Component {
  constructor(props) {
    super(props);
    this.state = {
      store: { loading: false, hasRight: false }
    };
  }

  componentDidMount() {
    this.salaryacctAcctresultCheckAuth();
  }

  salaryacctAcctresultCheckAuth = () => {
    const { taxAgentStore: { PageAndOptAuth } } = this.props;
    const { isOpenDevolution } = PageAndOptAuth;
    if (isOpenDevolution) {
      const { routeParams: { salaryAcctRecordId } } = this.props;
      this.setState({ store: { ...this.state.store, loading: true } });
      salaryacctAcctresultCheckAuth({ salaryAcctRecordId }).then(({ status, data }) => {
        this.setState({ store: { ...this.state.store, loading: false, hasRight: status && data } }, () => {
          this.state.store.hasRight && this.props.init && this.props.init();
        });
      });
    } else {
      this.setState({ store: { ...this.state.store, loading: false, hasRight: true } }, () => {
        this.props.init && this.props.init();
      });
    }
  };

  render() {
    return (
      <Authority ecId={`${this && this.props && this.props.ecId || ""}_Authority@lulowc`}
                 store={{ ...this.state.store }}>
        {this.props.children}
      </Authority>
    );
  }
}

export default Layout;
