/*
  *  角色新增
  *
  * @Author:    黎永顺
  * @Date:      2024/8/5
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider } from "ecCom";
import { Button, message } from "antd";
import { getSearchs } from "../../../../util";
import { roleConditions } from "../conditions";
import * as API from "../../../../apis/taxAgent";
import "../index.less";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      this.setState({
        conditions: _.map(roleConditions, item => ({
          ...item, items: _.map(item.items, o => ({ ...o, label: getLabel(o.lanId, o.label) }))
        }))
      }, () => nextProps.taxAgentStore.roleForm.initFormFields(this.state.conditions));
    }
    if (nextProps.visible !== this.props.visible && !nextProps.visible) nextProps.taxAgentStore.initRoleForm();
  }

  save = (isSetting) => {
    const { taxAgentStore: { roleForm } } = this.props;
    const { formData } = this.state;
    roleForm.validateForm().then(f => {
      if (f.isValid) {
        const payload = roleForm.getFormParams();
        this.setState({ loading: true });
        API.saveAuthRole({ ...payload, ...formData }).then(({ status, data, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            message.success(getLabel(111, "操作成功！"));
            this.props.onCancel(() => this.props.onSearch());
            isSetting && this.props.showRoleSetDialog({
              id: data, name: payload.name, selectedKey: "auth.MemberTargetTypeEnum"
            });
          } else {
            message.error(errormsg);
          }
        });
      } else {
        f.showErrors();
      }
    });
  };
  handleFormChange = (val) => {
    const key = _.keys(val)[0];
    if (key === "taxAgentIds" || key === "sobIds") {
      this.setState({ formData: { ...this.state.formData, ...val } });
    }
  };

  render() {
    const { conditions, loading, roleSetDialog } = this.state;
    const { taxAgentStore: { roleForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 520 }} initLoadCss title={getLabel(111, "添加业务线")} className="role-dialog"
        buttons={[
          <Button type="primary" loading={loading} onClick={() => this.save()}>{getLabel(111, "保存")}</Button>,
          <Button type="primary" loading={loading}
                  onClick={() => this.save(true)}>{getLabel(111, "保存并进入详细设置")}</Button>
        ]}
      >
        <div className="form-dialog-layout">{getSearchs(roleForm, conditions, 1, false, this.handleFormChange)}</div>
      </WeaDialog>
    );
  }
}

export default Index;
