/*
  * 薪酬报表-薪酬明细
  * 模板管理弹框
  * @Author:    黎永顺
  * @Date:      2024/12/4
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaButtonIcon, WeaLocaleProvider } from "ecCom";
import CustomTransferDialog from "../../../components/CustomBrowser/components/customTransferDialog";
import SalaryTempAdminDialog from "./salaryTempAdminDialog";
import SalaryDetailsTempDialog from "./salaryDetailsTempDialog";

const getLabel = WeaLocaleProvider.getLabel;

class SalaryTempMangerDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedRowKeys: [], tempAdminDialog: { visible: false, dataParams: { page: "salary_details_report" } },
      tempDialog: { visible: false, setting: [], heads: [], id: "", template: {} }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.isQuery !== this.props.isQuery) this.setState({ selectedRowKeys: [] }, () => this.tempManageRef.getData());
  }

  handleTempAdminCols = (params = {}) => this.setState({
    tempAdminDialog: { visible: true, dataParams: { ...this.state.tempAdminDialog.dataParams, ...params } }
  });
  handelAddTemp = (id = "", setting = [], heads = []) => {
    this.setState({
      tempDialog: {
        visible: true, setting, heads, id, template: _.find(this.tempManageRef.state.listDatas, o => o.id === id)
      }
    });
  };

  render() {
    const { selectedRowKeys, tempAdminDialog, tempDialog } = this.state;
    return (<CustomTransferDialog ref={dom => this.tempManageRef = dom} {...this.props} buttons={[]}
                                  title={getLabel(111, "模板管理")}
                                  onSelect={v => this.setState({ selectedRowKeys: v })}
                                  tableOpts={{
                                    title: getLabel(111, "操作"),
                                    dataIndex: "options",
                                    width: 120,
                                    render: (text, record) => (<React.Fragment>
                                      {
                                        record.canEdit &&
                                        <a href="javascript:void(0);"
                                           onClick={() => this.handleTempAdminCols({ id: record.id })}
                                           style={{ marginRight: 10 }}>{getLabel(111, "编辑")}</a>
                                      }
                                      {
                                        record.canEdit &&
                                        <a href="javascript:void(0);"
                                           onClick={() => this.props.onDelete([record.id])}>{getLabel(111, "删除")}</a>
                                      }

                                    </React.Fragment>)
                                  }}>
      <div className="table_opts">
        <WeaButtonIcon buttonType="add" type="primary" title={getLabel(111, "添加")}
                       onClick={() => this.handleTempAdminCols()}/>
        <WeaButtonIcon buttonType="del" type="primary" title={getLabel(111, "删除")}
                       onClick={() => this.props.onDelete(selectedRowKeys)} disabled={_.isEmpty(selectedRowKeys)}/>
      </div>
      {/*模板管理*/}
      <SalaryTempAdminDialog {...tempAdminDialog} onAddTemp={this.handelAddTemp} onCancel={() => this.setState({
        tempAdminDialog: { visible: false, dataParams: { page: "salary_details_report" } }
      })}/>
      {/*薪资明细模板设置*/}
      <SalaryDetailsTempDialog {...tempDialog}
                               onCancel={callback => this.setState({
                                 tempDialog: { ...tempDialog, visible: false, setting: [], heads: [] }
                               }, () => callback && callback())}
                               onSuccess={() => this.setState({
                                 tempAdminDialog: { visible: false, dataParams: { page: "salary_details_report" } }
                               }, () => {
                                 this.props.onSuccess();
                                 this.tempManageRef.getData();
                               })}/>
    </CustomTransferDialog>);
  }
}

export default SalaryTempMangerDialog;
