/*
 * Author: 黎永顺
 * Description: 核算弹框
 * Date: 2022-04-21 15:16:18
 * LastEditTime: 2022-06-28 15:52:31
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { Button } from "antd";
import { WeaDialog, WeaLocaleProvider, WeaTools } from "ecCom";
import { socialAccountConditions } from "../columns";
import { getSearchs } from "../../../../util";
import "./index.less";

const getLabel = WeaLocaleProvider.getLabel;
const getKey = WeaTools.getKey;

@inject("standingBookStore")
@observer
class Accountdialog extends Component {
  constructor(props) {
    super(props);
    this.state = { conditions: [] };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.init(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) nextProps.standingBookStore.initAccountForm();
  }

  init = (props) => {
    this.setState({
      conditions: _.map(socialAccountConditions, item => ({
        ...item, items: _.map(item.items, o => {
          if (getKey(o) === "paymentOrganization") {
            return {
              ...o, label: getLabel(o.lanId, o.label),
              options: this.props.options
            };
          }
          return { ...o, label: getLabel(o.lanId, o.label) };
        })
      }))
    }, () => props.standingBookStore.accountForm.initFormFields(this.state.conditions));
  };

  handleSubmit = () => {
    const { onOk, standingBookStore: { accountForm } } = this.props;
    accountForm.validateForm().then(f => {
      if (f.isValid) {
        onOk(accountForm.getFormParams());
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { conditions } = this.state;
    const { standingBookStore: { accountForm }, loading } = this.props;
    return (
      <WeaDialog
        {...this.props}
        style={{ width: 520, height: 156 }}
        initLoadCss className="accountDialogWrapper"
        buttons={[
          <Button type="primary" onClick={this.handleSubmit} loading={loading}>{getLabel(111, "确认")}</Button>
        ]}>
        {getSearchs(accountForm, conditions, 1, false)}
      </WeaDialog>
    );
  }
}

export default Accountdialog;
