/*
 * Author: 黎永顺
 * name: 调差
 * Description:
 * Date: 2022/12/5
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaSlideModal } from "ecCom";
import { Button, message, Modal } from "antd";
import SlideModalTitle from "../../../../components/slideModalTitle";
import AdjustTable from "./adjustTable";
import { getQueryString } from "../../../../util/url";
import { compensationSave } from "../../../../apis/standingBook";
import AdjustmentDefaultSlide from "./adjustmentDefaultSlide";
import "./index.less";


@inject("taxAgentStore")
@observer
class AdjustmentSlide extends Component {
  constructor(props) {
    super(props);
    this.state = {
      current: 0,
      loading: false,
      taxAgentId: "",
      adjustDefSlide: {
        visible: false,
        dataSource: []
      }
    };
    this.adjustTableRef = null;
  }

  handleSave = () => {
    const requireKeys = ["adjustTo", "adjustmentTotal", "categoryType", "companyTotal", "countryTotal", "target", "welfareType"];
    let { dataSource, targetOptions } = this.adjustTableRef.state;
    dataSource = _.filter(dataSource, it => (!it.id || !it.status));
    const billMonth = getQueryString("billMonth");
    const paymentOrganization = getQueryString("paymentOrganization");
    let bool = true;
    _.map(dataSource, item => {
      bool = _.every(requireKeys, child => !!item[child]);
    });
    if (_.isEmpty(dataSource)) {
      Modal.warning({
        title: "信息确认",
        content: "请添加调差项！"
      });
      return;
    } else if (!bool) {
      Modal.warning({
        title: "信息确认",
        content: "必要信息不完整，红色*为必填项！"
      });
      return;
    }
    const payload = _.map(dataSource, item => {
      const { adjustToOptions, categoryTypeOptions, target, targetOptions: targetOpts, ...extraParams } = item;
      return {
        ...extraParams,
        employeeId: target,
        target: _.find(targetOptions, child => child.employeeId == target).target,
        billMonth,
        paymentOrganization
      };
    });
    this.setState({ loading: true });
    compensationSave(payload).then(({ status, data, errormsg }) => {
      this.setState({ loading: false });
      if (status) {
        const { data: dataMsg, errorMessage = [] } = data;
        const msg = dataMsg + errorMessage.join(",");
        !_.isEmpty(errorMessage) ? message.error(msg) : message.success(msg || "保存成功");
        // _.isEmpty(errorMessage) &&
        this.adjustTableRef.getCompensationList().then(r => {
        });
      } else {
        message.error(errormsg || "保存失败");
      }
    }).catch(() => this.setState({ loading: false }));
  };
  handleSetasDefault = () => {
    const { adjustDefSlide } = this.state;
    this.setState({
      adjustDefSlide: {
        ...adjustDefSlide,
        visible: true,
        dataSource: _.filter(this.adjustTableRef.state.dataSource, it => it.status)
      }
    })
    ;
  };
  renderCustomOperate = () => {
    return [
      <Button type="primary" onClick={this.handleSave}>保存全部</Button>,
      <Button type="ghost" onClick={this.handleSetasDefault}>设为默认</Button>
    ];
  };

  render() {
    const { title, visible, onCancel, taxAgentStore: { showOperateBtn } } = this.props;
    const { loading, adjustDefSlide } = this.state;
    return (
      <div>
        <WeaSlideModal
          className="adjustmentWrapper"
          visible={visible}
          top={0}
          width={100}
          height={100}
          direction="right"
          measure="%"
          title={
            <SlideModalTitle
              subtitle={title}
              tabs={[]}
              loading={loading}
              showOperateBtn={showOperateBtn}
              editable={false}
              customOperate={this.renderCustomOperate()}
            />
          }
          content={<AdjustTable ref={dom => this.adjustTableRef = dom} visible={visible}/>}
          onClose={onCancel}
        />
        <AdjustmentDefaultSlide
          {...adjustDefSlide}
          onCancel={() => {
            this.setState({
              adjustDefSlide: {
                ...adjustDefSlide,
                visible: false,
                dataSource: []
              }
            });
          }}
        />
      </div>
    );
  }
}

export default AdjustmentSlide;

