import React, { useState } from 'react';
import { Button, Popconfirm } from 'antd';

const Deletecheck = props => {
    const { name, type, text, clickfn, pop,btnType } = props;
    //点击删除按钮,若需要调取接口进行校验,需要传btnType,且值为deleteCheck

    const [title, ct] = useState(btnType == "deleteCheck" ? "" : pop?.title),
        [visible, cv] = useState(false);
    let { buttonPerm } = { buttonPerm: [] };//单独运行

    let res = buttonPerm && buttonPerm.length > 0 ? buttonPerm.filter((it) => { return it.typeName == name }) : [];
    if (res.length == 1) {
        res = res[0]
    } else {
        res = {}
    }

    let ifs = name === false ? false : true;

    if (type == "ifs") {
        return ifs
    }


    return pop ? <Popconfirm
        disabled={!ifs}
        okText="确认"
        cancelText="取消"
        placement="bottomRight"
        title={title}
        onConfirm={() => {
            if (!ifs) {
                return
            }
            pop.onConfirm();
            cv(false);
        }}
        visible={visible}
        onCancel={() => {
            cv(false)
        }}
    >
        <Button
            size='small'
            type={type?.type || 'default'}
            disabled={!ifs}
            danger
            style={{ userSelect: "none" }}
            onClick={() => {
                if (btnType == "deleteCheck") {
                    clickfn().then(res => {
                        if (res.code == "0000") {
                            ct(res?.data?.msg)
                            cv(true)
                        }
                    })
                } else {
                    clickfn ? clickfn() : null;
                    cv(true)
                }
            }}
        >
            {text}
        </Button>
    </Popconfirm>
        :
        <Button
            size='small'
            type={type?.type || 'primary'}
            disabled={!ifs}
            style={{ userSelect: "none" }}
            onClick={() => {
                clickfn ? clickfn() : null
            }}
        >
            {text}
        </Button>

}

export default Deletecheck;