/*
 * Author: 黎永顺
 * name: 分享记录
 * Description:
 * Date: 2023/9/19
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTools } from "ecCom";
import { Button, message } from "antd";
import { shareCondition } from "./condition";
import { getSearchs } from "../../../util";
import { salaryStatisticsPushGetForm, salaryStatisticsPushSendMsg } from "../../../apis/statistics";
import "./index.less";

const getKey = WeaTools.getKey;
const { getLabel } = WeaLocaleProvider;

@inject("attendanceStore")
@observer
class SalaryStatisticsDetailShareDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      conditions: [], loading: false
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    const { attendanceStore: { initShareForm } } = nextProps;
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      this.salaryStatisticsPushGetForm(nextProps);
    } else {
      initShareForm();
    }
  }

  salaryStatisticsPushGetForm = (props) => {
    const { attendanceStore: { shareForm } } = props;
    salaryStatisticsPushGetForm().then(({ status, data }) => {
      if (status) {
        const { reportOptions } = data;
        this.setState({
          conditions: _.map(shareCondition, item => {
            return {
              ...item,
              items: _.map(item.items, o => {
                if (getKey(o) === "sharedBy") {
                  return {
                    ...o,
                    helpfulTitle: getLabel(111, "被分享人收到您分享的报表后，会默认赋予您的数据权限，但还需要有【薪酬统计分析】的菜单权限才可以查看报表。菜单权限设置路径：后台管理中心-【权限管理中心】-【角色设置】，建议创建一个仅查看报表的角色，这个角色只有薪酬管理模块的【薪酬统计分析】菜单权限。\n" +
                      "为了避免所选的人同名导致选错人，可在后台管理中心-【组织架构设置】-【浏览框显示字段定义】中人员浏览框的显示字段多勾选几个能区别人员的字段")
                  };
                } else if (getKey(o) === "startTime__endTime") {
                  return {
                    ...o,
                    helpfulTitle: getLabel(111, "有效时间之外，被分享人无法查看您分享的报表")
                  };
                } else if (getKey(o) === "remind") {
                  return {
                    ...o,
                    helpfulTitle: getLabel(111, "开启后，被分享人查看报表时，系统自动通过系统消息提醒分享人。")
                  };
                } else if (getKey(o) === "reportIds") {
                  return {
                    ...o,
                    options: _.map(reportOptions, it => ({ key: it.id, showname: it.reportName }))
                  };
                }
                return { ...o };
              })
            };
          })
        }, () => {
          shareForm.initFormFields(this.state.conditions);
          shareForm.updateFields({ pushTitle: data["pushTitle"] || "" });
          shareForm.updateFields({ mark: data["mark"] || "" });
        });
      }
    });
  };
  salaryStatisticsPushSendMsg = () => {
    const { attendanceStore: { shareForm } } = this.props;
    shareForm.validateForm().then(f => {
      if (f.isValid) {
        const { sharedBy, reportIds, ...extraFormparams } = shareForm.getFormParams();
        const payload = {
          sharedBy: sharedBy.split(","),
          reportIds: reportIds.split(","),
          ...extraFormparams
        };
        this.setState({ loading: true });
        salaryStatisticsPushSendMsg(payload).then(({ status, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            message.success(getLabel(111, "分享成功"));
            this.props.onCancel();
          } else {
            message.error(errormsg);
          }
        }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { attendanceStore: { shareForm } } = this.props;
    const { conditions, loading } = this.state;
    return (
      <WeaDialog
        {...this.props}
        hasScroll initLoadCss title={getLabel(111, "分享报表")}
        buttons={[
          <Button type="primary" onClick={this.salaryStatisticsPushSendMsg}
                  loading={loading}>{getLabel(111, "确认分享")}</Button>
        ]}
        className="shareDialogWrapper"
        style={{
          width: 750,
          height: 375.6,
          minHeight: 200,
          minWidth: 380,
          maxHeight: "60%",
          maxWidth: "60%",
          overflow: "hidden",
          transform: "translate(0px, 0px)"
        }}
      >
        <div style={{ padding: 16, overflowY: "auto" }}>
          {getSearchs(shareForm, conditions, 1, false)}
        </div>
      </WeaDialog>
    );
  }
}

export default SalaryStatisticsDetailShareDialog;
