/*
 * Author: 黎永顺
 * name: 自定义导出-导出模板管理
 * Description:
 * Date: 2024/3/28
 */
import React, { Component } from "react";
import { WeaButtonIcon, WeaDialog, WeaInputSearch, WeaLocaleProvider, WeaTab, WeaTable } from "ecCom";
import { message, Modal } from "antd";
import ExpFieldsSetDialog from "../expFieldsSetDialog";
import * as API from "../../../../../apis/calculate";

const getLabel = WeaLocaleProvider.getLabel;

class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      pageInfo: { current: 1, pageSize: 10, total: 0 }, dataSource: [], columns: [], loading: false,
      fieldsSetDialog: { visible: false, salaryAcctRecordId: "", tempId: "", templateName: "", checkItems: [] },
      query: { templateName: "" }, selectedRowKeys: []
    };
    this.dialogRef = null;
  }

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

  getExportTemplateList = (props) => {
    const { pageInfo, query } = this.state;
    const { salaryAcctRecordId } = props;
    const payload = { ...pageInfo, ...query, salaryAcctRecordId };
    this.setState({ loading: true });
    API.getExportTemplateList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { pageInfo: { columns, list: dataSource, pageNum: current, pageSize, total } } = data;
        this.setState({
          dataSource, pageInfo: { ...pageInfo, current, pageSize, total },
          columns: [...columns, {
            dataIndex: "operate", title: getLabel(111, "操作"), width: 120,
            render: (__, record) => (<React.Fragment>
              <a href="javascript:void(0);"
                 onClick={() => this.getExportTemplateForm(record)}>{getLabel(111, "编辑")}</a>
              <a href="javascript:void(0);" style={{ marginLeft: 10 }}
                 onClick={() => this.deleteExportTemplate([record.id])}>{getLabel(111, "删除")}</a>
            </React.Fragment>)
          }]
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };
  getExportTemplateForm = (record) => {
    const { id, templateName } = record;
    const { fieldsSetDialog } = this.state;
    const { salaryAcctRecordId } = this.props;
    API.getExportTemplateForm({ id }).then(({ status, data }) => {
      if (status) {
        const { checkItems } = data;
        this.setState({
          fieldsSetDialog: {
            ...fieldsSetDialog, tempId: id, templateName, visible: true, salaryAcctRecordId, checkItems
          }
        });
      } else {
        this.setState({
          fieldsSetDialog: {
            ...fieldsSetDialog, tempId: id, templateName, visible: true, salaryAcctRecordId, checkItems: []
          }
        });
      }
    });
  };
  deleteExportTemplate = (ids) => {
    Modal.confirm({
      title: getLabel(131329, "信息确认"),
      content: getLabel(111, "确定要删除吗？"),
      onOk: () => {
        API.deleteExportTemplate({ ids }).then(({ status, errormsg }) => {
          if (status) {
            message.success(getLabel(111, "操作成功！"));
            this.setState({ selectedRowKeys: [] }, () => this.getExportTemplateList(this.props));
          } else {
            message.error(errormsg);
          }
        });
      }
    });
  };

  render() {
    const { fieldsSetDialog, query, pageInfo, selectedRowKeys, dataSource, columns } = this.state;
    const { salaryAcctRecordId } = this.props;
    const dialogBodyHeight = this.dialogRef ? this.dialogRef.state.height : 600;
    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.getExportTemplateList(this.props));
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getExportTemplateList(this.props));
      }
    };
    const rowSelection = {
      selectedRowKeys, onChange: selectedRowKeys => this.setState({ selectedRowKeys })
    };
    return (
      <WeaDialog
        {...this.props} hasScroll scalable title={getLabel(111, "导出模板管理")}
        ref={dom => this.dialogRef = dom} initLoadCss className="tempManageDialogLayout"
        style={{
          width: 700, height: 606.6, minHeight: 200, minWidth: 380, maxHeight: "70%",
          maxWidth: "90%", overflow: "hidden", transform: "translate(0px, 0px)"
        }}
      >
        <WeaTab datas={[]} buttons={[
          <WeaButtonIcon buttonType="add" type="primary" title={getLabel(111, "添加")}
                         onClick={() => this.setState({
                           fieldsSetDialog: {
                             ...fieldsSetDialog, visible: true, tempId: "", templateName: "",
                             salaryAcctRecordId, checkItems: []
                           }
                         })}/>,
          <WeaButtonIcon buttonType="del" type="primary" title={getLabel(111, "删除")}
                         disabled={_.isEmpty(selectedRowKeys)}
                         onClick={() => this.deleteExportTemplate(selectedRowKeys)}/>,
          <WeaInputSearch value={query.templateName}
                          onChange={val => this.setState({ query: { ...query, templateName: val } })}
                          onSearch={() => this.setState({
                            pageInfo: { ...pageInfo, current: 1 }
                          }, () => this.getExportTemplateList(this.props))}/>
        ]}/>
        <WeaTable dataSource={dataSource} columns={columns} pagination={pagination} rowSelection={rowSelection}
                  scroll={{ y: `calc(${dialogBodyHeight}px - 148px)` }}/>
        <ExpFieldsSetDialog {...fieldsSetDialog}
                            onCancel={(isRefresh) => this.setState({
                              fieldsSetDialog: { ...fieldsSetDialog, visible: false, checkItems: [] }
                            }, () => isRefresh && this.getExportTemplateList(this.props))}/>
      </WeaDialog>
    );
  }
}

export default Index;
