/*
 * Author: 黎永顺
 * name: 社保福利方案重构-复制
 * Description:
 * Date: 2024/2/4
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider } from "ecCom";
import { Button, message } from "antd";
import * as API from "../../../../../apis/welfareScheme";
import { getSearchs } from "../../../../../util";

const getLabel = WeaLocaleProvider.getLabel;

@inject("programmeStore")
@observer
class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && !nextProps.visible) nextProps.programmeStore.initPlanCopyForm();
  }


  save = () => {
    const { programmeStore: { planCopyForm }, copyId: id, selectedKey } = this.props;
    planCopyForm.validateForm().then(f => {
      if (f.isValid) {
        this.setState({ loading: true });
        API.copyScheme({ id, schemeName: planCopyForm.getFormDatas()[`${selectedKey}_schemeName`].value })
          .then(({ status, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(30700, "操作成功！"));
              this.props.onCancel(true);
            } else {
              message.error(errormsg);
            }
          });
      } else {
        f.showErrors();
      }
    }).catch(() => this.setState({ loading: false }));
  };

  render() {
    const { loading } = this.state;
    const { programmeStore: { planCopyForm }, conditions } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: 80 }} initLoadCss
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(537558, "保存")}</Button>
        ]}
      >
        <div className="form-dialog-layout">{getSearchs(planCopyForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default Index;
