/*
 * Author: 黎永顺
 * name: 统计弹框
 * Description:
 * Date: 2023/4/10
 */
import React, { Component } from "react";
import { Button, message, Modal } from "antd";
import { WeaDialog, WeaLocaleProvider, WeaSlideModal } from "ecCom";
import { reportStatisticsReportSave } from "../../../apis/ruleconfig";
import "../index.less";

const { getLabel } = WeaLocaleProvider;

class StatisticsModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      document.querySelector(".xc_tj_fx_wrapper").classList.add("zIndex0-statistics");
    } else if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      document.querySelector(".xc_tj_fx_wrapper").classList.remove("zIndex0-statistics");
    }
  }

  handleSaveReportList = () => {
    const { form, id, onCancel } = this.props;
    form.validateForm().then(f => {
      if (f.isValid) {
        const { dimensionIds, reportName } = form.getFormParams();
        const payload = { id, reportName, dimensionIds: dimensionIds.split(",") };
        this.setState({ loading: true });
        reportStatisticsReportSave(payload).then(({ status, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            onCancel(true);
            message.success(getLabel(111, "保存成功"));
            form.resetForm();
          } else {
            message.error(errormsg || getLabel(111, "保存失败"));
          }
        }).catch(() => this.setState({ loading: false }));
      } else {
        Modal.warning({
          title: getLabel(111, "信息确认"),
          content: getLabel(111, "必要信息不完整，红色*为必填项！")
        });
      }
    });
  };
  renderTitle = () => {
    return <div className="titleDialog">
      <div className="titleCol titleLeftBox">
        <div className="titleIcon"><i className="icon-coms-fa"/></div>
        <div className="title">{getLabel(543313, "统计维度管理")}</div>
      </div>
      <div className="titleCol titleRightBox">
        <Button type="primary" onClick={() => this.props.onAddDimension()}>{getLabel(543314, "新建统计维度")}</Button>
      </div>
    </div>;
  };

  render() {
    const { loading } = this.state;
    const { typeKey, onCancel } = this.props;
    const buttons = typeKey === "addReport" ? [
      <Button type="primary" onClick={this.handleSaveReportList} loading={loading}>{getLabel(111, "保存")}</Button>
    ] : [];
    return (
      <React.Fragment>
        {
          typeKey === "addReport" ? <WeaDialog
              {...this.props} hasScroll
              style={typeKey === "addReport" ? { width: 600 } : { width: 640, height: 540 }}
              buttons={buttons}
              onCancel={onCancel}
              initLoadCss
              className="dimensionModalWrapper"
            >
              {this.props.children}
            </WeaDialog> :
            <WeaSlideModal
              className="dimensionModalWrapper" {...this.props}
              top={0} width={760} height={100} measureT={"%"} measureY={"%"} measureX={"px"}
              direction={"right"} content={this.props.children} title={this.renderTitle()}
            />
        }
      </React.Fragment>
    );
  }
}

export default StatisticsModal;
