/*
 * Author: 黎永顺
 * name: 个税对接-累计字段对应
 * Description:
 * Date: 2023/8/17
 */
import React, { Component } from "react";
import { WeaLocaleProvider, WeaSearchGroup, WeaTab } from "ecCom";
import LedgerFieldsItemPopver from "./ledgerFieldsItemPopver";
import { addupruleGetForm } from "../../../apis/ledger";
import LedgerFieldsTable from "./ledgerFieldsTable";

const { getLabel } = WeaLocaleProvider;

class CumulativeFields extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedKey: "", tabs: [],
      addupruleFields: []
    };
  }

  componentDidMount() {
    this.taxreportruleGetForm();
  }

  taxreportruleGetForm = () => {
    const { editId, saveSalarySobId } = this.props;
    addupruleGetForm({ id: editId || saveSalarySobId }).then(({ status, data }) => {
      if (status && !_.isEmpty(data)) {
        this.setState({
          tabs: _.map(data, it => ({ viewcondition: it.incomeCategoryId, title: it.incomeCategoryName })),
          selectedKey: _.take(data)[0].incomeCategoryId,
          addupruleFields: data
        }, () => this.props.onSetAdduprule(this.state.addupruleFields, this.state.selectedKey));
      }
    });
  };
  handleChangeSwitch = (visible, id) => {
    const { addupruleFields, selectedKey } = this.state;
    this.setState({
      addupruleFields: _.map(addupruleFields, it => {
        if (it.incomeCategoryId === selectedKey) {
          return {
            ...it,
            addUpRules: _.map(it.addUpRules, child => {
              if (child.id === id) {
                return { ...child, visible };
              }
              return { ...child, visible: false };
            })
          };
        }
        return { ...it };
      })
    }, () => this.props.onSetAdduprule(this.state.addupruleFields, this.state.selectedKey));
  };
  handleChangeAddupruleFieldsItem = (salaryItem, recordRuleId) => {
    const { addupruleFields, selectedKey } = this.state;
    this.setState({
      addupruleFields: _.map(addupruleFields, it => {
        if (it.incomeCategoryId === selectedKey) {
          return {
            ...it,
            addUpRules: _.map(it.addUpRules, child => {
              if (child.id === recordRuleId) {
                return { ...child, visible: false, salaryItem };
              }
              return { ...child, visible: false };
            })
          };
        }
        return { ...it };
      })
    }, () => this.props.onSetAdduprule(this.state.addupruleFields, this.state.selectedKey));
  };

  render() {
    const { editId, saveSalarySobId } = this.props;
    const { selectedKey, tabs, addupruleFields } = this.state;
    const dataSource = _.takeWhile(addupruleFields, it => it.incomeCategoryId === selectedKey);
    return (
      <WeaSearchGroup
        className="incomeWrapper" showGroup needTigger={false}
        title={
          <div className="incomeTitleContail">
            <WeaTab
              datas={tabs} keyParam="viewcondition" selectedKey={selectedKey}
            />
          </div>
        }
      >
        <LedgerFieldsTable
          columns={[
            {
              title: getLabel(111, "往期累计情况字段"),
              width: "50%",
              dataIndex: "addUpColumnName"
            },
            {
              title: getLabel(111, "对应本账套薪资项目"),
              width: "50%",
              dataIndex: "salaryItem",
              render: (_, record) => (
                <LedgerFieldsItemPopver salarySobId={editId || saveSalarySobId} record={record}
                                        onChangeSwitch={this.handleChangeSwitch}
                                        onChange={this.handleChangeAddupruleFieldsItem}
                />
              )
            }
          ]}
          dataSource={dataSource}
        />
      </WeaSearchGroup>
    );
  }
}

export default CumulativeFields;
