import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider } from "ecCom";
import { Button, message } from "antd";
import { salaryStatisticsPushAddSharedSendMsg } from "../../../apis/statistics";
import { sharePersonCondition } from "./condition";
import { getSearchs } from "../../../util";
import "./index.less";

const { getLabel } = WeaLocaleProvider;

@inject("attendanceStore")
@observer
class SalaryStatisticsDetailSharePersonDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      conditions: [], loading: false
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    const { attendanceStore: { initSharePerForm, sharePerForm } } = nextProps;
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      sharePerForm.initFormFields(sharePersonCondition);
    } else {
      initSharePerForm();
    }
  }

  save = () => {
    const { attendanceStore: { sharePerForm }, id } = this.props;
    sharePerForm.validateForm().then(f => {
      if (f.isValid) {
        const { sharedBy } = sharePerForm.getFormParams();
        const payload = {
          id, sharedBy: sharedBy.split(",")
        };
        salaryStatisticsPushAddSharedSendMsg(payload)
          .then(({ status, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(30700, "操作成功！"));
              this.props.onCancel();
              this.props.onGetTable();
            } else {
              message.error(errormsg);
            }
          })
          .catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { attendanceStore: { sharePerForm }, loading } = this.props;
    return (
      <WeaDialog
        {...this.props}
        hasScroll initLoadCss title={getLabel(111, "追加被分享人")}
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(111, "保存并分享")}</Button>
        ]}
        style={{ width: 600, height: 100 }}
      >
        {getSearchs(sharePerForm, sharePersonCondition, 1, false)}
      </WeaDialog>
    );
  }
}

export default SalaryStatisticsDetailSharePersonDialog;
