/*
 * Author: 黎永顺
 * name: 社保福利方案-信息表单
 * Description:
 * Date: 2023/2/13
 */
import React, { Component } from "react";
import { WeaFormItem, WeaInput, WeaSearchGroup, WeaSelect, WeaTextarea } from "ecCom";
import { schemeFields } from "./enum";

class SchemeInfoForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      schemeFieldsItem: schemeFields
    };
  }

  componentDidMount() {
    const { schemeFieldsItem } = this.state;
    const { requestParams, salaryFileStore: { userStatusList }, taxAgentStore: { taxAgentAdminOption } } = this.props;
    const { sharedType } = requestParams;
    this.setState({
      schemeFieldsItem: _.map(schemeFieldsItem, item => {
        const { key } = item;
        switch (key) {
          case "sharedType":
            return {
              ...item,
              options: userStatusList
            };
          case "taxAgentIds":
            return {
              ...item,
              multiple: true,
              display: !_.isNil(sharedType) && sharedType.toString() === "1",
              options: taxAgentAdminOption
            };
          default:
            break;
        }
        return { ...item };
      })
    });
  }

  handleChangeSchemeFiledItems = (key, v) => {
    const { onChangeFieldsItem } = this.props;
    const { schemeFieldsItem } = this.state;
    this.setState({
      schemeFieldsItem: _.map(schemeFieldsItem, item => {
        if (key === "sharedType" && item.key === "taxAgentIds") {
          return { ...item, display: v === "1" };
        }
        return { ...item };
      })
    }, () => {
      onChangeFieldsItem({ [key]: (key === "useDefault" || key === "useInEmployeeSalary") ? Number(v) : v });
    });
  };

  render() {
    const { requestParams } = this.props;
    const { schemeFieldsItem } = this.state;
    return (
      <WeaSearchGroup showGroup needTigger={false}>
        {
          _.map(schemeFieldsItem, item => {
            const { key, label, type, viewAttr, options, display = true, multiple = false } = item;
            const value = !_.isNil(requestParams[key]) ? requestParams[key].toString() : "";
            return <React.Fragment>
              {
                (type === "INPUT" && display) ?
                  <WeaFormItem label={label} labelCol={{ span: 6 }} wrapperCol={{ span: 12 }}>
                    <WeaInput viewAttr={viewAttr} value={value}
                              onChange={v => this.handleChangeSchemeFiledItems(key, v)}/></WeaFormItem> :
                  (type === "SELECT" && display) ?
                    <WeaFormItem label={label} labelCol={{ span: 6 }} wrapperCol={{ span: 12 }}>
                      <WeaSelect value={value} options={options}
                                 multiple={multiple} viewAttr={viewAttr}
                                 onChange={v => this.handleChangeSchemeFiledItems(key, v)}/>
                    </WeaFormItem> :
                    (type === "TEXTAREA" && display) ?
                      <WeaFormItem label={label} labelCol={{ span: 6 }} wrapperCol={{ span: 12 }}>
                        <WeaTextarea value={value} viewAttr={viewAttr} minRows={3}
                                     onChange={v => this.handleChangeSchemeFiledItems(key, v)}/>
                      </WeaFormItem> : null
              }
            </React.Fragment>;
          })
        }
      </WeaSearchGroup>
    );
  }
}

export default SchemeInfoForm;
