/*
 * Author: 黎永顺
 * name: 薪资核算
 * Description:
 * Date: 2023/9/14
 */
import React, { Component } from "react";
import { WeaHelpfulTip, WeaLocaleProvider } from "ecCom";
import { getColumnDesc, getSalarySobCycle } from "../../../../../apis/calculate";
import { sysConfCodeRule } from "../../../../../apis/ruleconfig";
import EditCalcAdvanceSearchPannel from "./editCalcAdvanceSearchPannel";
import EditCalcTable from "./editCalcTable";
import SalaryMonthTip from "../salaryMonthTip";
import SalaryCalcLayout from "./salaryCalcLayout";
import cs from "classnames";
import "./index.less";

const getLabel = WeaLocaleProvider.getLabel;

class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      salarySobCycle: {}, showSearchAd: false,
      columnDesc: {}, formulaTd: "", showTotalCell: false
    };
  }

  openAdvanceSearch = () => this.setState({ showSearchAd: !this.state.showSearchAd });
  onAdSearch = (bool = true) => {
    this.calcTableRef.wrappedInstance.handleQuery();
    bool && this.openAdvanceSearch();
  };
  init = async () => {
    const { routeParams: { salaryAcctRecordId } } = this.props;
    const [salarySobCycle, columnDesc, confCode] = await Promise.all([
      getSalarySobCycle({ salaryAcctRecordId }), getColumnDesc({ salaryAcctRecordId }),
      sysConfCodeRule({ code: "OPEN_ACCT_RESULT_SUM" })
    ]);
    if (salarySobCycle.status && columnDesc.status && confCode.status) {
      this.setState({
        salarySobCycle: salarySobCycle.data,
        columnDesc: columnDesc.data, showTotalCell: confCode.data === "1"
      });
    }
  };
  handleShowFormulaTa = (dataIndex) => this.setState({ formulaTd: dataIndex });
  handleBatchEditing = (editable) => {
    this.calcTableRef.wrappedInstance.handleBatchEditing(editable);
  };

  render() {
    const { salarySobCycle, showSearchAd, formulaTd, columnDesc, showTotalCell } = this.state;
    const { routeParams: { salaryAcctRecordId } } = this.props;
    const formulaObj = _.get(columnDesc, [formulaTd]) || {};
    return (<SalaryCalcLayout {...this.props} init={this.init} onConfirm={() => this.onAdSearch(false)}>
      <div className="salary-edit-calc-content">
        <div className="salary-flex-between weapp-salary-tb-tip">
          <div>
            <span>{getLabel(542604, "薪资所属月")}：</span>
            <span>{salarySobCycle.salaryMonth}</span>
            <WeaHelpfulTip
              width={200} placement="topLeft"
              title={<SalaryMonthTip {...salarySobCycle}/>}
              style={{ marginLeft: 10 }}
            />
          </div>
          <div></div>
        </div>
        <div className="salary-flex-between formula-detail-area">
          <div className="formula-detail">
            <span>{getLabel(18125, "公式")}=</span>
            <span>{formulaObj.formulaContent}</span>
          </div>
          <div style={{ paddingRight: 8 }}>
            {/*<Button type="primary"*/}
            {/*        onClick={() => this.handleBatchEditing(true)}>{getLabel(111, "批量保存")}</Button>*/}
          </div>
        </div>
        <div className={cs("searchAdvanced-condition-container", { "searchAdvanced-condition-hide": !showSearchAd })}>
          <EditCalcAdvanceSearchPannel
            salaryAcctRecordId={salaryAcctRecordId}
            onToggleSwitch={this.openAdvanceSearch}
            onAdSearch={this.onAdSearch}
          />
        </div>
        <EditCalcTable ref={dom => this.calcTableRef = dom} salarySobId={salarySobCycle.salarySobId}
                       {...this.props} showTotalCell={showTotalCell}
                       onShowFormulaTd={this.handleShowFormulaTa}/>
      </div>
    </SalaryCalcLayout>);
  }
}

export default Index;
