/*
  * 薪酬报表-薪酬明细
  * 模板管理新增编辑弹框
  * @Author:    黎永顺
  * @Date:      2024/12/4
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaDialog, WeaLocaleProvider, WeaTransfer } from "ecCom";
import { postFetch } from "../../../util/request";
import { Button } from "antd";

const getLabel = WeaLocaleProvider.getLabel;

class SalaryTempAdminDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false, selectedKeys: [], dataSource: []
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { dataParams = {} } = nextProps;
      this.setState({ loading: true });
      postFetch("/api/bs/hrmsalary/common/pageList/template/get", { ...dataParams })
        .then(({ status, data }) => {
          if (status) {
            this.setState({
              dataSource: _.map(data.setting, o => ({ id: o.column, name: o.text })),
              selectedKeys: _.map(data.checked, o => o.column)
            });
          }
        });
    }
  }

  render() {
    const { dataSource, selectedKeys } = this.state, { dataParams } = this.props;
    const heads = _.reduce(selectedKeys, (pre, cur) => {
      const item = dataSource.find(data => data.id === cur);
      if (item) pre.push(item.name);
      return pre;
    }, []);
    return (<WeaDialog
      {...this.props} initLoadCss ref={dom => this.dialog = dom} title={getLabel(111, "模板管理")}
      className="temp_admin_dialog" style={{
      width: 784, height: 460, minHeight: 200, minWidth: 380,
      maxHeight: "90%", maxWidth: "90%", overflow: "hidden", transform: "translate(0px, 0px)"
    }} buttons={[
      <Button type="primary"
              onClick={() => this.props.onAddTemp(dataParams.id, selectedKeys, heads)}>{getLabel(111, "确 定")}</Button>,
      <Button type="ghost" onClick={this.props.onCancel}>{getLabel(111, "取 消")}</Button>
    ]}>
      <WeaTransfer data={dataSource} selectedKeys={selectedKeys} onChange={v => this.setState({ selectedKeys: v })}
                   height={this.dialog ? this.dialog.state.height - 10 : 260}/>
    </WeaDialog>);
  }
}

export default SalaryTempAdminDialog;
