/*
 * Author: 黎永顺
 * name: 新增数据采集项
 * Description:
 */
import React, { Component } from "react";
import { WeaBrowser, WeaFormItem } from "ecCom";
import { WeaSwitch } from "comsMobx";
import { getDomkes, toDecimal_n } from "../../util";
import FormInfo from "../../components/FormInfo";
import "./index.less";

class AddItems extends Component {
  componentDidMount() {
    const { editId, condition, form } = this.props;
    if (!_.isEmpty(editId)) {
      getDomkes(condition).map(item => {
        if (item === "employeeId") {
          form.updateFields({
            [item]: {
              value: editId[item],
              valueSpan: editId["username"],
              valueObj: [{ id: editId[item], name: editId["username"] }]
            }
          });
        } else if (item === "taxAgentId") {
          form.updateFields({
            [item]: editId[item].toString()
          });
        } else {
          form.updateFields({
            [item]: editId[item] || ""
          });
        }
      });
    }
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.editId !== this.props.editId && !_.isEmpty(nextProps.editId)) {
      getDomkes(nextProps.condition).map(item => {
        if (item === "employeeId") {
          nextProps.form.updateFields({
            [item]: {
              value: nextProps.editId[item],
              valueSpan: nextProps.editId["username"],
              valueObj: [{ id: nextProps.editId[item], name: nextProps.editId["username"] }]
            }
          });
        } else if (item === "taxAgentId") {
          nextProps.form.updateFields({
            [item]: nextProps.editId[item].toString()
          });
        } else {
          nextProps.form.updateFields({
            [item]: nextProps.editId[item] || ""
          });
        }
      });
    }
  }

  render() {
    const { form, condition = [] } = this.props;
    const itemRender = {
      // 累计专项附加扣除
      addUpChildEducation: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpChildEducation: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpContinuingEducation: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpContinuingEducation: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpHousingLoanInterest: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpHousingLoanInterest: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpHousingRent: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpHousingRent: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpSupportElderly: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpSupportElderly: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpIllnessMedical: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpIllnessMedical: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpInfantCare: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpInfantCare: { value: toDecimal_n(v, 2) } })}/>);
      },
      //专项附加扣除
      childrenEducation: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ childrenEducation: { value: toDecimal_n(v, 2) } })}/>);
      },
      continuingEducation: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ continuingEducation: { value: toDecimal_n(v, 2) } })}/>);
      },
      housingLoanInterest: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ housingLoanInterest: { value: toDecimal_n(v, 2) } })}/>);
      },
      housingRent: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ housingRent: { value: toDecimal_n(v, 2) } })}/>);
      },
      supportingElder: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ supportingElder: { value: toDecimal_n(v, 2) } })}/>);
      },
      seriousIllnessTreatment: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ seriousIllnessTreatment: { value: toDecimal_n(v, 2) } })}/>);
      },
      infantCare: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ infantCare: { value: toDecimal_n(v, 2) } })}/>);
      },
      //往期累计情况（工资、薪金）
      addUpIncome: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpIncome: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpSubtraction: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpSubtraction: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpSocialSecurityTotal: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpSocialSecurityTotal: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpAccumulationFundTotal: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpAccumulationFundTotal: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpEnterpriseAndOther: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpEnterpriseAndOther: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpOtherDeduction: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpOtherDeduction: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpTaxExemptIncome: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpTaxExemptIncome: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpAllowedDonation: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpAllowedDonation: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpTaxSavings: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpTaxSavings: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpAdvanceTax: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpAdvanceTax: { value: toDecimal_n(v, 2) } })}/>);
      },
      actualAddUpAdvanceTax: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ actualAddUpAdvanceTax: { value: toDecimal_n(v, 2) } })}/>);
      },
      taxAdjustment: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ taxAdjustment: { value: toDecimal_n(v, 2) } })}/>);
      },
      addUpTaxableIncome: (field, textAreaProps, form, formParams) => {
        return (<WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                           form={form} formParams={formParams}
                           onBlur={(v) => v && form.updateFields({ addUpTaxableIncome: { value: toDecimal_n(v, 2) } })}/>);
      },
      // 其他免税扣除
      freeIncome: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                     form={form} formParams={formParams}
                     onBlur={(v) => v && form.updateFields({ freeIncome: { value: toDecimal_n(v, 2) } })}/>
          {field.extraDom && field.extraDom}
        </React.Fragment>);
      },
      businessHealthyInsurance: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                     form={form} formParams={formParams}
                     onBlur={(v) => v && form.updateFields({ businessHealthyInsurance: { value: toDecimal_n(v, 2) } })}/>
          {field.extraDom && field.extraDom}
        </React.Fragment>);
      },
      taxDelayEndowmentInsurance: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                     form={form} formParams={formParams}
                     onBlur={(v) => v && form.updateFields({ taxDelayEndowmentInsurance: { value: toDecimal_n(v, 2) } })}/>
          {field.extraDom && field.extraDom}
        </React.Fragment>);
      },
      deductionAllowedDonation: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                     form={form} formParams={formParams}
                     onBlur={(v) => v && form.updateFields({ deductionAllowedDonation: { value: toDecimal_n(v, 2) } })}/>
          {field.extraDom && field.extraDom}
        </React.Fragment>);
      },
      derateDeduction: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                     form={form} formParams={formParams}
                     onBlur={(v) => v && form.updateFields({ derateDeduction: { value: toDecimal_n(v, 2) } })}/>
          {field.extraDom && field.extraDom}
        </React.Fragment>);
      },
      otherDeduction: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                     form={form} formParams={formParams}
                     onBlur={(v) => v && form.updateFields({ otherDeduction: { value: toDecimal_n(v, 2) } })}/>
          {field.extraDom && field.extraDom}
        </React.Fragment>);
      },
      privatePension: (field, textAreaProps, form, formParams) => {
        return (<React.Fragment>
          <WeaSwitch fieldConfig={{ ...field, ...textAreaProps }}
                     form={form} formParams={formParams}
                     onBlur={(v) => v && form.updateFields({ privatePension: { value: toDecimal_n(v, 2) } })}/>
          {field.extraDom && field.extraDom}
        </React.Fragment>);
      }
    };
    return (
      <div className="addItemsWrapper form-dialog-layout">
        <FormInfo center={false} form={form} formFields={condition} colCount={2} custLabelCol={8}
                  itemRender={itemRender}/>
        <Tips><span>若此员工数据已存在在同期列表中，则当前数据保存后会覆盖列表数据</span></Tips>
      </div>
    );
  }
}

export default AddItems;

export const Browser = payload => {
  const { label, onChange, viewAttr = 3, value, valueSpan, type = 1, isSingle = true } = payload;
  return (
    <WeaFormItem label={label} labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
      <WeaBrowser
        viewAttr={viewAttr}
        type={type}
        isSingle={isSingle}
        value={value}
        valueSpan={valueSpan}
        onChange={(ids, names) => onChange({ ids, names })}/>
    </WeaFormItem>
  );
};
export const Tips = payload => {
  const { children } = payload;
  return (
    <div className="tipWrapper">
      <div className="title">小提示</div>
      <div className="content">{children}</div>
    </div>
  );
};
