/*
  *  角色详情设置弹窗
  *   成员、数据明细查询
  * @Author:    黎永顺
  * @Date:      2024/9/27
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaDialog, WeaInputSearch, WeaLocaleProvider, WeaTable } from "ecCom";
import * as API from "../../../../apis/taxAgent";
import "../index.less";

const getLabel = WeaLocaleProvider.getLabel;
const APIFOX = {
  "auth.MemberTargetTypeEnum": API.authMemberDetail,
  "auth.DataTargetTypeEnum": API.authDataDetail
};

class DetailDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false, pageInfo: { current: 1, pageSize: 10, total: 0 },
      query: { username: "" }, dataSource: [], columns: []
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getData(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) this.setState({
      query: { username: "" }, pageInfo: { current: 1, pageSize: 10, total: 0 }
    });
  }

  getData = (props) => {
    const { pageInfo, query } = this.state, { roleId, selectedKey } = props || this.props;
    let payload = { ...pageInfo, ...query, roleId };
    this.setState({ loading: true });
    APIFOX[selectedKey](payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({
          columns, dataSource, pageInfo: { ...pageInfo, current, pageSize, total }
        });
      }
    });
  };
  renderTitle = () => {
    const { selectedKey } = this.props, { query } = this.state;
    const title = selectedKey === "auth.MemberTargetTypeEnum" ? getLabel(111, "成员明细") : getLabel(111, "数据明细");
    return (<div className="authDetail-dialog-title">
      <span>{title}</span>
      <WeaInputSearch value={query.username} onChange={v => this.setState({ query: { username: v } })}
                      onSearch={v => this.setState({ pageInfo: { current: 1 } }, () => this.getData())}/>
    </div>);
  };

  render() {
    const { loading, dataSource, pageInfo, columns } = this.state;
    const sheight = this.dialog ? this.dialog.state.height - 120 : 260;

    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.getData());
      },
      onChange: current => {
        this.setState({ pageInfo: { ...pageInfo, current } }, () => this.getData());
      }
    };

    return (
      <WeaDialog
        {...this.props} initLoadCss ref={dom => this.dialog = dom} title={this.renderTitle()}
        className="authDetail_dialog" style={{
        width: 784, height: 460, minHeight: 200, minWidth: 380,
        maxHeight: "90%", maxWidth: "90%", overflow: "hidden", transform: "translate(0px, 0px)"
      }} buttons={[]}>
        <div className="authDetail-table">
          <WeaTable dataSource={dataSource} loading={loading} pagination={pagination} scroll={{ y: sheight }}
                    rowKey="id" columns={columns}/>
        </div>
      </WeaDialog>
    );
  }
}

export default DetailDialog;
