import React, { useEffect, useMemo, useRef, useState } from "react";
import {
    Dropdown,
    Menu,
    Button,
    message,
} from "antd";
import AutoTable from "@/components/AutoTable";
import defaultFields from "./fields";
import { doFetch } from "@/utils/doFetch";
import DrawInitForm from "@/components/DrawInitForm";
import getPrem from '@/utils/getPrem';//权限判断fn
import { useReactToPrint } from 'react-to-print';


const keytoval = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4
}, items = [
    {
        key: 'one', 
        label: (
            <a>
                生产领料出库
            </a>
        ),
    },
    {
        key: 'two',
        label: (
            <a>
                销售出库
            </a>
        ),
    },
    {
        key: 'three',
        label: (
            <a>
                报废出库
            </a>
        ),
    },
    {
        key: 'four',
        label: (
            <a>
                其他出库
            </a>
        ),
    },
]

function Instore(props) {
    const [activeTabKey, onTabChange] = useState("1");
    let [drawprops, setdrawprops] = useState({
        title: "",
        visible: false,
        onClose: () => {
            setdrawprops(s => ({
                ...s,
                visible: false,
            }))
        },
        fields: {},
        width:1200
    }),
        actionRef = useRef(),
        ChildRef = null,
        printRef = useRef();

    //操作完成后刷新    
    function reload() {
        actionRef.current.reload();
        ChildRef?.onRefresh();
        message.success("操作成功");
        setdrawprops(s => ({
            ...s,
            visible: false
        }))
    }

    const handlePrint = useReactToPrint({
        content: () => printRef.current,
    });


    const columns = useMemo(() => {
        if (activeTabKey == "1") {
            return [
                {
                    "title": "出库单号",
                    "dataIndex": "materieOutstoreNo",
                    "key": "materieOutstoreNo",
                    "render": (dom, record) => {
                        return <a
                            onClick={() => {
                                setdrawprops(s => ({
                                    ...s,
                                    visible: true,
                                    //查看详情 props
                                    val: "detail",
                                    title: `查看详情`,
                                    ...defaultFields?.detail,
                                    totalPath: "/ngic-workmanship/wmsMaterieOutstore/getOutStoreInfoById",
                                    totalParams: { id: record.id },
                                    printRef: printRef,
                                    extra: (<Button
                                        onClick={async () => {
                                            handlePrint()
                                        }}
                                    >
                                        打印
                                    </Button>)
                                }))
                            }}
                        >{dom}</a>
                    }
                },
                {
                    "title": "出库类型",
                    "dataIndex": "outstoreTypeName",
                    "key": "outstoreType",
                    "valueType": "select",
                    "options": [
                        {
                            "label": "采购出库",
                            "value": "1"
                        },
                        {
                            "label": "生产出库",
                            "value": "2"
                        },
                        {
                            "label": "退料出库",
                            "value": "3"
                        },
                        {
                            "label": "其他出库",
                            "value": "4"
                        }
                    ]
                },
                {
                    "title": "出库仓库",
                    "dataIndex": "storeName",
                    "key": "storeId",
                    options: {
                        database: () => doFetch({ url: "/ngic-auth/sysStore/selectionBox", params: { factoryIdList: [] } }),
                        params: {}
                    },
                    valueType: "select",
                },
                {
                    "title": "相关单号",
                    "dataIndex": "businessNo",
                    "key": "businessNo"
                },
                {
                    "title": "备注",
                    "dataIndex": "remark",
                    "key": "remark",
                    "search": false
                },
                {
                    "title": "创建人",
                    "dataIndex": "createUserName",
                    "key": "createUserName"
                },
                {
                    "title": "创建时间",
                    "dataIndex": "createTime",
                    "key": "createTime",
                    "search": false
                },
                {
                    "title": "创建时间开始时间",
                    "dataIndex": "createTimeStart",
                    "key": "createTimeStart",
                    "valueType": "date",
                    "hideInTable": true
                },
                {
                    "title": "创建时间结束时间",
                    "dataIndex": "createTimeEnd",
                    "key": "createTimeEnd",
                    "valueType": "date",
                    "hideInTable": true
                },
                {
                    "title": "状态",
                    "dataIndex": "statusName",
                    "key": "status",
                    "valueType": "select",
                    "options": [
                        {
                            "label": "待上架",
                            "value": "0"
                        },
                        {
                            "label": "上架中",
                            "value": "1"
                        }
                    ]
                },
                {
                    "title": "操作",
                    "valueType": "option",
                    "width": 240,
                    "render": (text, record, _, action) => {
                        return [
                            getPrem("equipmentCustomer_save", action, '下架采集', async () => {
                                let extra = defaultFields.dooutside(record.id,record.storeId);
                                setdrawprops(s => ({
                                    ...s,
                                    visible: true,
                                    //查看详情 props
                                    val: "detail",
                                    title: `下架采集`,
                                    ...extra,
                                    totalPath: "/ngic-workmanship/wmsMaterieOutstore/getOutStoreInfoById",
                                    totalParams: { id: record.id }
                                }))

                            }),
                            getPrem("equipmentCustomer_deleteById", action, '关单', null, {
                                title: "确认关单？",
                                onConfirm: () => {
                                    doFetch({ url: "/ngic-workmanship/wmsMaterieOutstore/close", params: { id: record.id } }).then(res => {
                                        if (res.code == "0000") {
                                            reload()
                                        }
                                    })
                                }
                            }),
                            getPrem("equipmentCustomer_deleteById", action, '删除', null, {
                                title: "确认删除？",
                                onConfirm: () => {
                                    doFetch({ url: "/ngic-workmanship/wmsMaterieOutstore/deleteById", params: { id: record.id } }).then(res => {
                                        if (res.code == "0000") {
                                            reload()
                                        }
                                    })
                                }
                            })
                        ];
                    }
                }
            ]
        } else {
            return [
                {
                    "title": "出库单号",
                    "dataIndex": "materieOutstoreNo",
                    "key": "materieOutstoreNo",
                    "render": (dom, record) => {
                        return <a
                            onClick={() => {
                                setdrawprops(s => ({
                                    ...s,
                                    visible: true,
                                    //查看详情 props
                                    val: "detail",
                                    title: `查看详情`,
                                    ...defaultFields?.detail,
                                    totalPath: "/ngic-workmanship/wmsMaterieOutstoreHis/getOutStoreInfoById",
                                    totalParams: { id: record.id }
                                }))
                            }}
                        >{dom}</a>
                    }
                },
                {
                    "title": "出库类型",
                    "dataIndex": "outstoreTypeName",
                    "key": "outstoreType",
                    "valueType": "select",
                    "options": [
                        {
                            "label": "生产领料出库",
                            "value": "1"
                        },
                        {
                            "label": "销售出库",
                            "value": "2"
                        },
                        {
                            "label": "报废出库",
                            "value": "3"
                        },
                        {
                            "label": "其他出库",
                            "value": "4"
                        }
                    ]
                },
                {
                    "title": "出库仓库",
                    "dataIndex": "storeName",
                    "key": "storeId",
                    options: {
                        database: () => doFetch({ url: "/ngic-auth/sysStore/selectionBox", params: { factoryIdList: [] } }),
                        params: {}
                    },
                    valueType: "select",
                },
                {
                    "title": "相关单号",
                    "dataIndex": "businessNo",
                    "key": "businessNo"
                },
                {
                    "title": "备注",
                    "dataIndex": "remark",
                    "key": "remark",
                    "search": false
                },
                {
                    "title": "创建人",
                    "dataIndex": "createUserName",
                    "key": "createUserName"
                },
                {
                    "title": "创建时间",
                    "dataIndex": "createTime",
                    "key": "createTime",
                    "search": false
                },
                {
                    "title": "创建时间开始时间",
                    "dataIndex": "createTimeStart",
                    "key": "createTimeStart",
                    "valueType": "date",
                    "hideInTable": true
                },
                {
                    "title": "创建时间结束时间",
                    "dataIndex": "createTimeEnd",
                    "key": "createTimeEnd",
                    "valueType": "date",
                    "hideInTable": true
                },
                {
                    "title": "关单时间",
                    "dataIndex": "closeTime",
                    "key": "closeTime",
                    "search": false
                },
                {
                    "title": "关单时间开始时间",
                    "dataIndex": "closeTimeStart",
                    "key": "closeTimeStart",
                    "valueType": "date",
                    "hideInTable": true
                },
                {
                    "title": "关单时间结束时间",
                    "dataIndex": "closeTimeEnd",
                    "key": "closeTimeEnd",
                    "valueType": "date",
                    "hideInTable": true
                },
                {
                    "title": "状态",
                    "dataIndex": "statusName",
                    "key": "status",
                    "valueType": "select",
                    "options": [
                        {
                            "label": "已上架",
                            "value": "2"
                        },
                        {
                            "label": "已关单",
                            "value": "4"
                        }
                    ]
                }
            ]
        }
    }, [activeTabKey])

    const tableprops = {
        ...props,
        pageextra: activeTabKey == "1" ? <Dropdown placement="bottomRight" overlay={<Menu
            onClick={(e) => {
                setdrawprops(s => ({
                    ...s,
                    visible: true,
                    title: "新增",
                    fields: defaultFields[e.key],
                    outstoreType: keytoval[e.key],
                    val: "add" //类型
                }))
            }}
            items={items}
        />}>
            <Button type="primary">新增</Button>
        </Dropdown> : "none",
        tabList: [
            {
                tab: "未完成",
                key: "1"
            }, {
                tab: "已完成",
                key: "2"
            }
        ],
        activeTabKey,
        onTabChange,
        columns,
        path: activeTabKey == "1" ? "/ngic-workmanship/wmsMaterieOutstore/queryList" : "/ngic-workmanship/wmsMaterieOutstoreHis/queryList"
    }


    return (
        <div>
            <AutoTable
                {
                ...tableprops
                }
                actionRef={actionRef}
                onRef={(node) => (ChildRef = node)}
            ></AutoTable>
            <DrawInitForm
                {...drawprops}
                submitData={async (value) => {
                    if (drawprops.val == "add") {
                        value.materialList = value.materialList?.map(it=>{
                            delete it.usableNum;
                            return it
                        })//usableNum
                        let res = await doFetch({ url: "/ngic-workmanship/wmsMaterieOutstore/saveOutStore", params: { ...value, outstoreType: drawprops.outstoreType } })
                        if (res.code == "0000") {
                            reload();
                        }
                    }
                }}
            />
        </div>
    );
}

export default Instore;