import React from "react";
import { WeaDialog, WeaInputSearch, WeaLocaleProvider, WeaTable } from "ecCom";
import { Button, message, Spin } from "antd";
import { getSysItemList, saveSysItem } from "../../apis/item";
import "./index.less";

const { getLabel } = WeaLocaleProvider;
export default class SystemSalaryItemModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      dataSource: [], columns: [], name: "", loading: false, saveLoading: false,
      selectedRowKeys: [], pageInfo: { current: 1, pageSize: 10, total: 0 }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getSysItemList();
    if (nextProps.visible !== this.props.visible && !nextProps.visible) this.setState({
      selectedRowKeys: [], name: ""
    });
  }

  getSysItemList = () => {
    this.setState({ loading: true });
    const { pageInfo, name } = this.state;
    getSysItemList({ ...pageInfo, name }).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 } });
      }
    }).catch(() => this.setState({ loading: false }));
  };
  handleAdd = () => {
    const { selectedRowKeys } = this.state;
    this.setState({ saveLoading: true });
    saveSysItem(selectedRowKeys).then(({ status, errormsg }) => {
      this.setState({ saveLoading: false });
      if (status) {
        message.success(getLabel(111, "添加成功"));
        this.setState({ selectedRowKeys: [] }, () => this.props.onCancel(this.props.onSearch()));
      } else {
        message.error(errormsg);
      }
    }).catch(() => this.setState({ saveLoading: false }));
  };
  renderTitle = () => {
    const { name, pageInfo } = this.state;
    return <div className="sys-item-title">
      <span>{getLabel(111, "添加系统薪资项目")}</span>
      <WeaInputSearch value={name} onChange={val => this.setState({ name: val })} style={{ width: 200 }}
                      placeholder={getLabel(111, "请输入薪资项目名称")} onSearch={() => this.setState({
        pageInfo: { ...pageInfo, current: 1 }
      }, () => this.getSysItemList())}/>
    </div>;
  };

  render() {
    const { selectedRowKeys, pageInfo, loading, columns, dataSource, saveLoading } = this.state;
    const pagination = {
      current: pageInfo.current,
      pageSize: pageInfo.pageSize,
      total: pageInfo.total,
      showTotal: total => `${getLabel(111, "共")} ${total} ${getLabel(111, "条")}`,
      showQuickJumper: true,
      showSizeChanger: true,
      pageSizeOptions: ["10", "20", "50", "100"],
      onShowSizeChange: (current, pageSize) => {
        this.setState({ pageInfo: { ...pageInfo, current, pageSize } }, () => {
          this.getSysItemList();
        });
      },
      onChange: current => {
        this.setState({ pageInfo: { ...pageInfo, current } }, () => {
          this.getSysItemList();
        });
      }
    };
    const rowSelection = {
      selectedRowKeys,
      onChange: (selectedRowKeys) => this.setState({ selectedRowKeys })
    };
    return (
      <WeaDialog {...this.props} initLoadCss className="sys-salary-wrapper" ref={dom => this.sysItemRef = dom}
                 style={{
                   width: "60vw", height: 600, minHeight: 200, minWidth: 380,
                   maxHeight: "90%", maxWidth: "90%", overflow: "hidden", transform: "translate(0px, 0px)"
                 }} title={this.renderTitle()}
                 buttons={[<Button type="primary" loading={saveLoading} disabled={_.isEmpty(selectedRowKeys)}
                                   onClick={this.handleAdd}>{getLabel(111, "添加")}</Button>]}>
        <div className="sys-item-table-box">
          <Spin spinning={loading && pageInfo.total === 0}>
            <WeaTable columns={columns} dataSource={dataSource} pagination={pagination} rowSelection={rowSelection}
                      loading={loading} scroll={{ y: this.sysItemRef ? this.sysItemRef.state.height - 16 : 600 }}
                      rowKey="id"/>
          </Spin>
        </div>
      </WeaDialog>
    );
  }
}
