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: { getPermission } } = this.props;
    this.setState({ store: { ...this.state.store, loading: true } });
    getPermission().then(({ data }) => {
      const { isOpenDevolution } = data;
      if (isOpenDevolution) {
        const { routeParams: { salaryAcctRecordId } } = this.props;
        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();
        });
      }
    }).catch(() => this.setState({ store: { ...this.state.store, loading: false } }));
  };

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

export default Layout;
