import * as React from 'react';
import { Dropdown, Menu, } from 'kts-components-antd-x4';
import { message } from 'kts-xui';
// import SellerImport from './sellerimport';
// import BuyerImport from './buyerimport';
import TaskImport from './taskimport';
import InvoiceImport from '../../CommonComponents/InvoiceImport';
import agent from '../../Server';
import { DigitalAssetVerification } from '../../..';
/** Props接口 */
interface IProps {
    onSuccess?: () => void;
    taskUuid: string;
    value?: string;
}
//发票采集功能
export default class UIComponents extends React.Component<IProps, any> {

    constructor(props) {
        super(props);
        this.state = {
            menuKey: '',
            visible: false,
            taskUuid: this.props.taskUuid,
        };
    }
    componentDidMount() {
        if (this.props.value) {
            this.handleMenuClick({ key: this.props.value })
        }
    }

    handleMenuClick = e => {
        this.setState({
            visible: true,
            menuKey: e.key
        });
    };
    render() {
        const menu = (
            <Menu onClick={this.handleMenuClick}>
                <Menu.Item key="scangun">
                    扫码枪
                </Menu.Item>
                <Menu.Item key="excel">
                    Excel上传
                </Menu.Item>
                <Menu.Item key="manual">
                    手工录入
                </Menu.Item>
                <Menu.Item key="task">
                    关联任务导入
                </Menu.Item>
            </Menu>
        );
        return (
            <div>
                <Dropdown overlay={menu}>
                    {this.props.children}
                </Dropdown>
                <this.renderCollect />
            </div>
        );
    }
    onClose = () => {
        this.setState({
            visible: false,
        });
    }
    onSuccess = async (list: any[]) => {
        const data = {
            relationTaskList: list,
            taskUuid: this.props.taskUuid
        }
        const res: any = await agent.post('/collector/task/collectByRelationTask', data, this);
        if (res.er) {
            return;
        } else {
            message.success('操作成功');
            this.onClose();
            this.onRefresh();
        }
    }
    onRefresh = () => {
        this.props.onSuccess && this.props.onSuccess();
    }
    uploadInvoice = async (values) => {
        values = {
            ...values,
            collectionMode: 1,
            taskUuid: this.props.taskUuid
        }
        const res: any = await agent.post('/collector/asset/collect', values, this);
        return res;
    }
    uploadInvoiceByExcel = async (values) => {
        const res: any = await DigitalAssetVerification.server
            .post(`${DigitalAssetVerification.prefix_path}/collector/asset/uploadInvoice`)
            .reqFs(DigitalAssetVerification.server.reqFs.filter(v => v !== DigitalAssetVerification.serviceTools.addContentType))
            .field('taskUuid', this.props.taskUuid)
            .field('fileName', values.name)
            .attach('file', values, values.name).end(this);

        return res;
    }
    uploadInvoiceByScan = async (values) => {
        values = {
            ...values,
            collectionMode: 2,
            taskUuid: this.props.taskUuid
        }
        const res: any = await agent.post('/collector/asset/collect', values, this);
        return res;
    }
    private renderCollect = () => {
        const { menuKey, visible } = this.state;
        const urlData = {
            url: this.uploadInvoiceByExcel,
            key4: this.uploadInvoice,
            scankey4: this.uploadInvoiceByScan,
            downloadurl: '/invoiceexcel.xls',
        }
        if (menuKey && visible) {
            switch (menuKey) {
                case 'task':
                    return <TaskImport taskUuid={this.props.taskUuid} onClose={this.onClose} importInvoice={this.onSuccess} />
                // case 'seller':
                //     return <SellerImport onClose={this.onClose} importInvoice={this.onSuccess} />;
                // case 'buyer':
                //     return <BuyerImport onClose={this.onClose} importInvoice={this.onSuccess} />;
                default:
                    return <InvoiceImport currentActive={menuKey} onClose={this.onClose} refreshInvoice={this.onRefresh} urlData={urlData} />;
            }
        }
        return <></>;
    }
}