/*
 * Author: 黎永顺
 * name: 新建考勤自定义字段
 * Description:
 * Date: 2023/3/1
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog } from "ecCom";
import { Button, message } from "antd";
import { conditions } from "../columns";
import { getSearchs } from "../../../../util";
import { saveAttendanceField } from "../../../../apis/attendance";
import "./index.less";

@inject("attendanceStore")
@observer
class AttendanceCustomFieldsModal extends Component {
  componentDidMount() {
    const { attendanceStore: { form } } = this.props;
    form.initFormFields(conditions);
  }

  componentWillReceiveProps(nextProps, nextContext) {
    const { attendanceStore: { initForm } } = nextProps;
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.handleResetForm();
    if (nextProps.visible !== this.props.visible && !nextProps.visible) initForm();
  }


  /*
   * Author: 黎永顺
   * Description: 保存考勤字段
   * Params:
   * Date: 2023/3/1
   */
  handleSubmitFields = () => {
    const { attendanceStore: { form }, onRefresh, onCancel } = this.props;
    form.validateForm().then(f => {
      if (f.isValid) {
        const payload = form.getFormParams();
        saveAttendanceField(payload).then(({ status, errormsg }) => {
          if (status) {
            message.success("新增成功");
            onCancel();
            onRefresh();
          } else {
            message.error(errormsg || "新增失败");
          }
        });
      } else {
        f.showErrors();
      }
    });
  };
  handleResetForm = () => {
    const { attendanceStore: { form } } = this.props;
    form.updateFields({ fieldName: "", fieldType: "NUMBER", enableStatus: "0", description: "" });
  };

  render() {
    const { attendanceStore: { form } } = this.props;
    const buttons = [
      <Button type="primary" onClick={this.handleSubmitFields}>保存</Button>
    ];
    return (
      <WeaDialog {...this.props} style={{ width: 480, height: 202 }} buttons={buttons} initLoadCss>
        <div className="form-dialog-layout"> {getSearchs(form, conditions, 1)}</div>
      </WeaDialog>
    );
  }
}

export default AttendanceCustomFieldsModal;
