
import { connect } from "umi";
import type { ConnectState } from "@/models/connect";
import { Button, Drawer, message, Popconfirm } from "antd";
import moment from 'moment'
import { handleCreateBusinessMan, handleDeleteBusinessMan } from "../service";
import { useEffect, useRef } from "react";
import SelectShop from "./selectShop";
import AddForm from "./addForm";
import '../index.less'

type DetailProps = {
    showDrawer: boolean
    currentRow?: any
    setCurrentRow?: any
    setShowDrawer: any
    currentUser: any
    parentRef?: any
    pageType?: string
    afterOk?: any
}
const BusinessManDrawer = ({ showDrawer, currentRow, setCurrentRow, setShowDrawer, currentUser, parentRef, pageType, afterOk }: DetailProps) => {

    const selectShopRef = useRef<any>()
    const addFormRef = useRef<any>()

    // 新增客户单位的方法
    const handleGetAddNew = async (obj?: any) => {
        let req: any = {}
        let shopList: any = selectShopRef.current?.selectId
        let detail: any = addFormRef.current?.formDetail
        console.log('shopList', shopList);

        if (currentRow?.OWNERUNIT_ID) {
            req = {
                ...detail,
                ...obj,
                STAFF_ID: currentUser.ID,
                STAFF_NAME: currentUser.Name,
                OPERATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
                ShopList: shopList && shopList.length > 0 ? shopList : []
            }
        } else {
            req = {
                ...obj,
                OWNERUNIT_STATE: 1,
                STAFF_ID: currentUser.ID,
                STAFF_NAME: currentUser.Name,
                OPERATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
                ShopList: shopList && shopList.length > 0 ? shopList : []
            }
        }

        console.log('req', req);
        const data = await handleCreateBusinessMan(req)
        console.log('data', data);
        if (data.Result_Code === 100) {
            message.success(data.Result_Desc)
            if (parentRef) {
                parentRef.current?.reload()
            }
            if (pageType === 'edit') {
                if (afterOk) {
                    afterOk()
                }
            }
        } else {
            message.success(data.Result_Desc)
        }
    }

    const handleGetShopData = async () => {
        selectShopRef.current?.handleGetTreeData()
    }

    useEffect(() => {
        if (showDrawer && !currentRow) {
            selectShopRef.current?.handleGetTreeData()
        }
    }, [showDrawer])

    return (
        <div>
            <Drawer
                width={'60%'}
                visible={showDrawer}
                onClose={() => {
                    if (setCurrentRow) {
                        setCurrentRow(undefined);
                    }
                    setShowDrawer(false);
                }}
                title={<span style={{ fontWeight: 600 }}>{currentRow?.OWNERUNIT_ID ? '编辑客户单位' : '新增客户单位'}</span>}
                className={"drawerBox"}
                bodyStyle={{ backgroundColor: "#fff", padding: 16 }}
                closable={false}
                destroyOnClose
                footer={<div style={{ width: '100%', boxSizing: 'border-box', padding: '0 16px', display: 'flex', justifyContent: 'flex-end' }}>
                    <Button type={"primary"} onClick={() => {
                        let res: any = addFormRef.current?.formRef.current?.getFieldsValue()
                        console.log('res', res);
                        handleGetAddNew(res)

                    }}>保存</Button>
                    {
                        currentRow?.OWNERUNIT_ID ?
                            <Popconfirm
                                title="确认删除该商户单位管理信息吗?"
                                onConfirm={async () => {
                                    const data = await handleDeleteBusinessMan({ BusinessManId: currentRow?.OWNERUNIT_ID })
                                    if (data.Result_Code === 100) {
                                        message.success(data.Result_Desc)
                                        if (parentRef) {
                                            parentRef.current?.reload()
                                        }
                                        if (setCurrentRow) {
                                            setCurrentRow(undefined);
                                        }
                                        setShowDrawer(false);
                                    } else {
                                        message.error(data.Result_Desc)
                                    }
                                }}
                            >
                                <Button style={{ marginLeft: '8px' }}>刪除</Button>
                            </Popconfirm>
                            :
                            ''
                    }
                </div>}
            >
                <div className="drawerBoxContent">
                    {/* 编辑当行信息或新增内容 */}
                    <div className="rightContent">
                        <AddForm onRef={addFormRef} currentRow={currentRow} selectShopRef={selectShopRef} handleGetShopData={handleGetShopData} pageType={pageType} />
                    </div>

                    {/* 选择关联门店 */}
                    <div className="leftContent">
                        <SelectShop onRef={selectShopRef} onShow={showDrawer} addFormRef={addFormRef} />
                    </div>
                </div>
            </Drawer>
        </div>
    )
}

export default connect(({ user, }: ConnectState) => ({
    currentUser: user.currentUser,
}))(BusinessManDrawer);
