index.jsx 4.79 KB
Newer Older
左玲玲's avatar
左玲玲 committed
1 2 3 4 5 6 7 8 9 10
import React, { useState, useMemo, useRef, useEffect } from 'react';
import DrawerPro from '@/components/DrawerPro';
import AutoTable from '@/components/AutoTable';
import PremButton from '@/components/PremButton';
import getcolumns from './columns';
import { doFetch } from '@/utils/doFetch';
import { useRequest } from "ahooks";
const Custom = () => {
    let actionRef = useRef();
    const [drawer, setdrawer] = useState({
左玲玲's avatar
左玲玲 committed
11
        open: false,
左玲玲's avatar
左玲玲 committed
12 13 14 15 16 17 18 19
    });
    const { run, loading, runAsync } = useRequest(doFetch, {
        manual: true,
        onSuccess: (res, params) => {
            if (res?.code == "0000") {
                actionRef?.current?.reload();
                setdrawer((s) => ({
                    ...s,
左玲玲's avatar
左玲玲 committed
20
                    open: false,
21
                    item: {}
左玲玲's avatar
左玲玲 committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
                }));
            }
        },
    });

    const edit = (text, row, _, action) => {
        return (
            <PremButton
                key='edit'
                btn={{
                    size: 'small',
                    onClick: () => {
                        setdrawer((s) => ({
                            ...s,
左玲玲's avatar
左玲玲 committed
36
                            open: true,
左玲玲's avatar
左玲玲 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
                            item: row,
                            val: 'edit',
                            title: '编辑'
                        }));
                    },
                }}
            >
                编辑
            </PremButton>
        );
    };

    const remove = (text, row, _, action) => {
        return (
            <PremButton
                key='remove'
                pop={{
                    title: '是否删除该客户?',
                    okText: '确认',
                    cancelText: '取消',
                    onConfirm: async () => {
58
                        let res = await doFetch({ url: pathconfig.delete, params: { id: row.id } });
左玲玲's avatar
左玲玲 committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
                        if (res.code === '0000') {
                            actionRef.current.reload();
                        }
                    },
                }}
                btn={{
                    size: 'small',
                    type: 'danger',
                }}
            >
                删除
            </PremButton>
        );
    };
    const columns = useMemo(() => {
        let defcolumn = getcolumns(setdrawer)?.columns ?? [];
75 76 77 78 79 80
        let factory = defcolumn.filter(it => it.key == 'factoryId')?.[0];
        if (drawer.val == 'add') {
            factory.options.params = {}
        } else if (drawer.val == 'edit') {
            factory.options.params = { factoryId: drawer.item.factoryId };
        }
左玲玲's avatar
左玲玲 committed
81 82 83 84 85 86
        return defcolumn.concat({
            title: '操作',
            valueType: 'option',
            width: 150,
            render: (text, row, _, action) => [edit(text, row, _, action), remove(text, row, _, action)],
        });
87
    }, [drawer.val, drawer.item]);
左玲玲's avatar
左玲玲 committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    const pathconfig = useMemo(() => {
        let pathconf = getcolumns(setdrawer)?.pathconfig ?? {};
        return pathconf;
    }, []);
    return <div>
        <AutoTable
            pagetitle={<h3 className="page-title">客户管理</h3>}
            columns={columns}
            path={pathconfig?.list}
            actionRef={actionRef}
            pageextra={'add'}
            resizeable={false}
            addconfig={{
                // access: 'sysDepartment_save',
                btn: {
                    type: 'primary',
                    disabled: false,
                    onClick: () => {
                        // let res = await doFetch({url:})
                        setdrawer((s) => ({
                            ...s,
左玲玲's avatar
左玲玲 committed
109
                            open: true,
左玲玲's avatar
左玲玲 committed
110 111 112 113 114 115 116 117 118 119
                            item: {},
                            params: {},
                            title: '新增',
                            val: 'add'
                        }));
                    },
                },
            }}
        />
        <DrawerPro
120
            {...drawer}
左玲玲's avatar
左玲玲 committed
121
            fields={columns}
122 123
            detailpath={drawer.val == 'add' || !drawer?.item?.id ? '' : pathconfig?.detail}
            params={drawer.val == 'add' ? {} : { id: drawer?.item?.id }}
左玲玲's avatar
左玲玲 committed
124 125 126 127 128
            defaultFormValue={drawer?.item ?? {}}
            placement="right"
            onClose={() => {
                setdrawer((s) => ({
                    ...s,
左玲玲's avatar
左玲玲 committed
129
                    open: false,
左玲玲's avatar
左玲玲 committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
                }));
            }}
            onFinish={async (vals) => {
                if (drawer?.val == "add") {
                    await runAsync({ url: pathconfig?.add, params: { ...vals } });
                } else if (drawer?.val == "edit") {
                    await runAsync({
                        url: pathconfig?.edit,
                        params: { ...vals, id: drawer?.item?.id },
                    });
                }
            }}
        >
        </DrawerPro>
    </div>
}
export default Custom;