/*
 * Author: 黎永顺
 * name: 社保福利方案-自定义福利新建编辑
 * Description:
 * Date: 2024/2/4
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTools } from "ecCom";
import { Button, message } from "antd";
import * as API from "../../../../../apis/welfareScheme";
import { getConditionDomkeys, getSearchs } from "../../../../../util";
import { customPlanConditons } from "../../config";

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

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

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

  getCustomCategoryForm = (props) => {
    API.getCustomCategoryForm({ id: props.customId }).then(({ status, data }) => {
      if (status) {
        const { form } = data;
        this.setState({
          conditions: _.map(customPlanConditons, item => ({
            ...item, items: _.map(item.items, o => {
              if (getKey(o) === "welfareType" && props.customId) {
                return { ...o, viewAttr: 1 };
              } else {
                return { ...o, viewAttr: props.showOperateBtn ? o.viewAttr : 1 };
              }
            })
          }))
        }, () => {
          const { programmeStore: { planCustomForm } } = props;
          planCustomForm.initFormFields(this.state.conditions);
          _.forEach(getConditionDomkeys(this.state.conditions), k => {
            if (k === "paymentScope" && props.customId && form[k]) {
              planCustomForm.updateFields({ [k]: { value: form[k].join(",") } });
            } else {
              planCustomForm.updateFields({ [k]: { value: form[k] || "" } });
            }
          });
        });
      }
    });
  };

  save = () => {
    const { programmeStore: { planCustomForm }, customId: id, selectedKey } = this.props;
    planCustomForm.validateForm().then(f => {
      if (f.isValid) {
        this.setState({ loading: true });
        const payload = {
          id, ...planCustomForm.getFormParams(),
          paymentScope: planCustomForm.getFormParams().paymentScope.split(",")
        };
        API[!id ? "createSICategory" : "updateCustomCategory"](payload).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, conditions } = this.state;
    const { programmeStore: { planCustomForm }, showOperateBtn } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: 174 }} initLoadCss
        buttons={showOperateBtn ? [
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(537558, "保存")}</Button>
        ] : []}
      >
        <div className="form-dialog-layout">{getSearchs(planCustomForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default Index;
