/*
  *
  * 个税字段对应
  * @Author:    黎永顺
  * @Date:      2024/6/17
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaLocaleProvider, WeaSearchGroup, WeaTab } from "ecCom";
import LedgerFieldsItemPopver from "./ledgerFieldsItemPopver";
import { taxruleGetForm } from "../../../apis/ledger";
import LedgerFieldsTable from "./ledgerFieldsTable";
import uuidV4 from "uuid/v4";

const { getLabel } = WeaLocaleProvider;

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

  componentDidMount() {
    this.taxruleGetForm();
  }

  taxruleGetForm = () => {
    const { editId, saveSalarySobId } = this.props;
    taxruleGetForm({ 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,
          incomeTaxFields: _.map(data, it => ({
            ...it, taxReportRules: _.map(it.taxRules, o => ({
              ...o, canEdit: o.canEdit, id: uuidV4(), reportColumnDataIndex: o.taxIndex, reportColumnName: o.name,
              salaryItem: [{ id: o.salaryItemId, name: o.salaryItemName }]
            }))
          }))
        }, () => this.props.onSetTaxreportrule(this.state.incomeTaxFields, this.state.selectedKey));
      }
    });
  };
  handleChangeSwitch = (visible, id) => {
    const { incomeTaxFields, selectedKey } = this.state;
    this.setState({
      incomeTaxFields: _.map(incomeTaxFields, it => {
        if (it.incomeCategoryId === selectedKey) {
          return {
            ...it,
            taxReportRules: _.map(it.taxReportRules, child => {
              if (child.id === id) {
                return { ...child, visible };
              }
              return { ...child, visible: false };
            })
          };
        }
        return { ...it };
      })
    }, () => this.props.onSetTaxreportrule(this.state.incomeTaxFields, this.state.selectedKey));
  };
  handleChangeIncomeFieldsItem = (salaryItem, recordRuleId) => {
    const { incomeTaxFields, selectedKey } = this.state;
    this.setState({
      incomeTaxFields: _.map(incomeTaxFields, it => {
        if (it.incomeCategoryId === selectedKey) {
          return {
            ...it,
            taxReportRules: _.map(it.taxReportRules, child => {
              if (child.id === recordRuleId) {
                return { ...child, visible: false, salaryItem };
              }
              return { ...child, visible: false };
            })
          };
        }
        return { ...it };
      })
    }, () => {
      this.props.onSetTaxreportrule(this.state.incomeTaxFields, this.state.selectedKey);
    });
  };

  render() {
    const { editId, saveSalarySobId } = this.props;
    const { selectedKey, tabs, incomeTaxFields, keywords } = this.state;
    const list = _.map(incomeTaxFields, it => {
      if (it.incomeCategoryId === selectedKey) {
        return {
          ...it,
          taxReportRules: _.filter(it.taxReportRules, item => item.reportColumnName.indexOf(keywords) !== -1)
        };
      }
      return { ...it };
    });
    const dataSource = _.takeWhile(list, it => it.incomeCategoryId === selectedKey);
    return (
      <WeaSearchGroup
        className="incomeWrapper" showGroup needTigger={false}
        title={
          <div className="incomeTitleContail">
            <WeaTab
              datas={tabs} keyParam="viewcondition" selectedKey={selectedKey}
              searchType={["base"]} searchsBasePlaceHolder={getLabel(111, "请输入对象")}
              onSearch={val => this.setState({ keywords: val })}
            />
          </div>
        }
      >
        <LedgerFieldsTable
          columns={[
            {
              title: <span>
                <span style={{ marginRight: 8 }}>{getLabel(111, "个税申报表字段")}</span>
                {/*<WeaHelpfulTip*/}
                {/*  title={getLabel(111, "若【个税申报表字段】与【对应本账套薪资项目】对应，则申报表内数据为当前账套核算数据，若【个税申报表字段】未与【对应本账套薪资项目】对应，则申报表内数据默认显示为0")}*/}
                {/*  placement="top" width={250}*/}
                {/*/>*/}
              </span>,
              width: "50%",
              dataIndex: "reportColumnName"
            },
            {
              title: getLabel(111, "对应本账套薪资项目"),
              width: "50%",
              dataIndex: "salaryItem",
              render: (_, record) => (
                <LedgerFieldsItemPopver salarySobId={editId || saveSalarySobId} record={record}
                                        onChangeSwitch={this.handleChangeSwitch}
                                        onChange={this.handleChangeIncomeFieldsItem}
                />
              )
            }
          ]}
          dataSource={dataSource}
        />
      </WeaSearchGroup>
    );
  }
}

export default IncomeTaxFields;
