/*
  *  个税扣缴义务人-角色设置
  *
  * @Author:    黎永顺
  * @Date:      2024/8/5
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { WeaBrowser, WeaLocaleProvider } from "ecCom";
import { message, Modal } from "antd";
import AddRoleDialog from "../addRoleDialog";
import RoleDetailSetDialog from "../roleDetailSetDialog";
import * as API from "../../../../apis/taxAgent";
import "../index.less";

const getLabel = WeaLocaleProvider.getLabel;

class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      addRoleDialog: { visible: false, taxAgentId: "" },
      roleSetDialog: { visible: false, roleId: "", name: "" }
    };
  }

  showRoleSetDialog = (role) => this.setState({ roleSetDialog: { visible: true, roleId: role.id, name: role.name } });
  deleteAuthRole = (role) => {
    Modal.confirm({
      title: getLabel(111, "信息确认"),
      content: getLabel(111, "确认要删除吗?"),
      onOk: () => {
        API.deleteAuthRole([role.id]).then(({ status, errormsg }) => {
          if (status) {
            message.success(getLabel(111, "操作成功！"));
            this.props.onSearch();
          } else {
            message.error(errormsg);
          }
        });
      }
    });

  };

  render() {
    const { addRoleDialog, roleSetDialog } = this.state;
    const { taxAgent, onSearch } = this.props;
    const { role } = taxAgent;
    return (
      <React.Fragment>
        <WeaBrowser type={-99991} isSingle={false} hasAddBtn hasBorder whiteBackground viewAttr={1}
                    className="tax_role_setting_browser"
                    addOnClick={() => this.setState({ addRoleDialog: { taxAgentId: taxAgent.id, visible: true } })}
                    replaceDatas={_.map(role, o => ({
                      id: o.id,
                      name: <span className="only-operate">
                        <a href="javascript:void(0);" onClick={() => this.showRoleSetDialog(o)}>{o.name}</a>
                        <span className="ant-select-selection__choice__remove"
                              onClick={() => this.deleteAuthRole(o)}/>
                      </span>
                    }))}/>
        {/*添加角色*/}
        <AddRoleDialog {...addRoleDialog} onSearch={onSearch}
                       showRoleSetDialog={this.showRoleSetDialog}
                       onCancel={callback => this.setState({
                         addRoleDialog: { ...addRoleDialog, visible: false }
                       }, () => callback && callback())}/>
        {/*角色详情设置*/}
        <RoleDetailSetDialog {...roleSetDialog} onSearch={onSearch}
                             onCancel={callback => this.setState({
                               roleSetDialog: { ...roleSetDialog, visible: false }
                             }, () => callback && callback())}/>
      </React.Fragment>
    );
  }
}

export default Index;
