/*
 * Author: 黎永顺
 * name: 报税信息
 * Description:
 * Date: 2022/12/1
 */
import React, { Component } from "react";
import { WeaBrowser, WeaFormItem, WeaInput, WeaLocaleProvider, WeaSearchGroup, WeaSelect } from "ecCom";
import { fieldList } from "./constants";
import { taxReturnGetForm } from "../../../apis/taxAgent";

const { getLabel } = WeaLocaleProvider;

class TaxDeclarationInfo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      fieldForm: {
        name: "",
        taxCode: "",
        city: "",
        cityVal: [],
        areaCode: null,
        passwordType: "TAX_NET_PASSWORD",
        account: "",
        realNamePassword: "",
        netPassword: null,
        taxRegistrationNumber: "",
        departmentCode: "",
        checkStatus: ""
      },
      fieldItem: []
    };
  }

  componentDidMount() {
    this.setState({
      fieldItem: _.filter(_.map(fieldList, item => ({
        ...item,
        label: getLabel(item.lanId, item.label),
        viewAttr: this.props.isChief ? item.viewAttr : 1
      })), it => it.key !== "account" && it.key !== "realNamePassword")
    }, () => {
      this.taxReturnGetForm();
    });
  }

  taxReturnGetForm = () => {
    const { taxAgentId } = this.props;
    taxReturnGetForm({ taxAgentId }).then(({ status, data }) => {
      if (status) {
        const { fieldForm } = this.state;
        this.setState({
          fieldForm: {
            ...fieldForm,
            ..._.reduce(_.keys(fieldForm), (pre, cur) => {
              if (cur !== "city") {
                if (cur === "checkStatus") {
                  const checkStatusMap = {
                    "NOT_COMMIT": getLabel(111, "未验证"),
                    "SUCCESS": getLabel(111, "成功"),
                    "FAIL": getLabel(111, "失败")
                  };
                  return { ...pre, [cur]: checkStatusMap[data[cur]] };
                }
                return { ...pre, [cur]: data[cur] };
              }
              return {
                ...pre,
                [cur]: `${data["nation"]}-${data["province"]}-${data[cur]}`
              };
            }, {}),
            cityVal: (data["city"] && data["cityName"]) ? [{ id: data["city"], name: data["cityName"] }] : []
          }
        });
      }
    });
  };
  handleChangeValue = (key, value, cityVal = []) => {
    const { fieldForm } = this.state;
    this.setState({
      fieldForm: !_.isEmpty(cityVal) ? {
        ...fieldForm, [key]: value, cityVal
      } : { ...fieldForm, [key]: value }
    }, () => {
      if (key === "passwordType" && this.state.fieldForm.passwordType === "REAL_NAME_PASSWORD") {
        this.setState({
          fieldItem: _.filter(fieldList, it => it.key !== "netPassword"),
          fieldForm: { ...this.state.fieldForm, account: null, realNamePassword: null, netPassword: null }
        });
      } else if (key === "passwordType" && this.state.fieldForm.passwordType === "TAX_NET_PASSWORD") {
        this.setState({
          fieldItem: _.filter(fieldList, it => it.key !== "account" && it.key !== "realNamePassword"),
          fieldForm: { ...this.state.fieldForm, netPassword: null, account: null, realNamePassword: null }
        });
      }
    });
  };

  render() {
    const { fieldItem, fieldForm } = this.state;
    return (
      <div className="taxDeclarationInfoWrapper form-dialog-layout">
        <WeaSearchGroup col={1} needTigger showGroup>
          {
            _.map(fieldItem, item => {
              const { key, label, type, viewAttr, options = [] } = item;
              return <WeaFormItem label={label} labelCol={{ span: 6 }} wrapperCol={{ span: 14 }} key={key}>
                {
                  (type === "TEXT" || type === "PASSWORD") &&
                  <WeaInput autocomplete="off" value={fieldForm[key]} type={_.lowerCase(type)} viewAttr={viewAttr}
                            onChange={(v) => this.handleChangeValue(key, v)}/>
                }
                {
                  type === "SELECT" &&
                  <WeaBrowser replaceDatas={fieldForm["cityVal"]} type={58} viewAttr={viewAttr}
                              helpfulTip={getLabel(111, "请展开选择报税所属区域")}
                              onChange={(__, ___, datas) => {
                                if (!_.isEmpty(datas)) {
                                  this.handleChangeValue(key, `1-${datas[0].pid}-${datas[0].id}`, _.map(datas, it => ({
                                    id: it.id,
                                    name: it.name
                                  })));
                                } else {
                                  this.handleChangeValue(key, "", []);
                                }
                              }}
                  />
                }
                {
                  type === "RADIO" &&
                  <WeaSelect detailtype={3} value={fieldForm[key]}
                             options={_.map(options, it => ({ ...it, showname: getLabel(it.lanId, it.showname) }))}
                             viewAttr={viewAttr}
                             onChange={(v) => this.handleChangeValue(key, v)}/>
                }
              </WeaFormItem>;
            })
          }
        </WeaSearchGroup>
      </div>
    );
  }
}

export default TaxDeclarationInfo;
