/*
 * Author: 黎永顺
 * name: 薪酬日志查看
 * Description:
 * Date: 2024/1/24
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTable } from "ecCom";
import DetailLogView from "./components/detailLogView";
import { logConditions, renderLogSearchsForm } from "./config";
import * as API from "../../apis/index";
import "./index.less";

const getLabel = WeaLocaleProvider.getLabel;

@inject("baseFormStore")
@observer
class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataSource: [], columns: [], pageInfo: { current: 1, pageSize: 10, total: 0 },
      loading: false, conditions: [], logDetailDialog: { visible: false, title: "", logFunction: "", mainid: "" }
    };
  }

  componentDidMount() {
    const { baseFormStore: { form } } = this.props;
    this.setState({
      conditions: _.map(logConditions, it => ({
        ...it, items: _.map(it.items, o => ({ ...o, label: getLabel(o.lanId, o.label) }))
      }))
    }, () => form.initFormFields(this.state.conditions));
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getLogs({}, nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      this.setState({
        dataSource: [], columns: [], pageInfo: { current: 0, pageSize: 10, total: 0 },
        loading: false
      }, () => this.props.baseFormStore.form.resetForm());
    }
  }

  getLogs = (extra = {}, props) => {
    const { pageInfo } = this.state;
    const { baseFormStore: { form }, logFunction, filterConditions } = props;
    const payload = {
      ...pageInfo, ...extra, searchMap: { ...form.getFormParams() },
      function: logFunction, showColums: "", filterConditions
    };
    this.setState({ loading: true });
    API.getLogs(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize, total },
          dataSource, columns: _.map(columns, it => {
            if (it.dataIndex === "operatedesc") {
              return {
                ...it, render: (__, record) => (<a href="javascript:void(0);" onClick={() => {
                  const { operatedesc: title, uuid: mainid } = record;
                  this.setState({ logDetailDialog: { visible: true, title, logFunction, mainid } });
                }}>{getLabel(33564, "查看")}</a>)
              };
            }
            return { ...it };
          })
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };

  render() {
    const { loading, dataSource, columns, pageInfo, conditions, logDetailDialog } = this.state;
    const { baseFormStore: { form } } = this.props;
    const pagination = {
      ...pageInfo,
      showTotal: total => `${getLabel(18609, "共")} ${total} ${getLabel(18256, "条")}`,
      showQuickJumper: true,
      showSizeChanger: true,
      pageSizeOptions: ["10", "20", "50", "100"],
      onShowSizeChange: (current, pageSize) => {
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize }
        }, () => this.getLogs({}, this.props));
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getLogs({}, this.props));
      }
    };
    const scrollHeight = this.logRef ? this.logRef.state.height - 254 : 606.6;
    return (
      <WeaDialog
        {...this.props} title={getLabel(111, "操作日志")}
        ref={dom => this.logRef = dom} className="logDialog" initLoadCss
        style={{
          width: 1150,
          height: 606.6,
          minHeight: 200,
          minWidth: 380,
          maxHeight: "90%",
          maxWidth: "90%",
          overflow: "hidden",
          transform: "translate(0px, 0px)"
        }}
      >
        <div className="logDialogContent">
          {renderLogSearchsForm(form, conditions, () => this.getLogs({ current: 1 }, this.props), () => {
            form.resetForm();
            this.getLogs({ current: 1 }, this.props);
          })}
          <WeaTable
            columns={columns} dataSource={dataSource} loading={loading} className="logTable"
            pagination={pagination} scroll={{ y: `${scrollHeight}px` }}
          />
          <DetailLogView {...logDetailDialog}
                         onCancel={() => this.setState({ logDetailDialog: { ...logDetailDialog, visible: false } })}/>
        </div>
      </WeaDialog>
    );
  }
}

export default Index;
