/*
  * 数据推送
  * 推送明细新增编辑
  * @Author:    黎永顺
  * @Date:      2024/11/20
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTools } from "ecCom";
import { commonEnumList } from "../../../../apis/ruleconfig";
import * as API from "../../../../apis/datapush";
import { PDConditions } from "../../conditions";
import { Button, message } from "antd";
import { formRender } from "../../formRender";


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

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

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.initForm(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) nextProps.baseFormStore.initFormExtra();
  }

  initForm = async (props) => {
    const { detail = {} } = props;
    const { data: fieldType } = await commonEnumList({ enumClass: "com.engine.salary.enums.push.PushItemFieldEnum" });
    this.setState({
      conditions: _.map(PDConditions, item => ({
        ...item, items: _.map(item.items, o => {
          o = { ...o, label: getLabel(o.lanId, o.label), value: detail[getKey(o)] || "" };
          if (getKey(o) === "fieldType") {
            return {
              ...o, value: detail[getKey(o)] ? String(detail[getKey(o)]) : "",
              options: _.map(fieldType, o => ({ key: o.enum, showname: o.defaultLabel }))
            };
          }
          return { ...o };
        })
      }))
    }, () => {
      props.baseFormStore.formExtra.initFormFields(this.state.conditions);
    });
  };
  save = () => {
    const { baseFormStore: { formExtra }, detail: { id }, settingId } = this.props;
    formExtra.validateForm().then(f => {
      if (f.isValid) {
        const payload = formExtra.getFormParams();
        this.setState({ loading: true });
        API.savePushItemList({ ...payload, settingId, id })
          .then(({ status, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(30700, "操作成功"));
              this.props.onCancel(this.props.onSearch());
            } else {
              message.error(errormsg);
            }
          }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { baseFormStore: { formExtra }, detail } = this.props, { loading, conditions } = this.state;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: 174 }} initLoadCss className="Pdetail_dialog"
        buttons={[
          <Button onClick={this.props.onCancel}>{getLabel(111, "取消")}</Button>,
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(111, "保存")}</Button>
        ]}
      >
        <div className="form-dialog-layout">{formRender(formExtra, conditions, detail)}</div>
      </WeaDialog>
    );
  }
}

export default Index;
