import { Button, Col, Divider, Drawer, message, Row, Image } from "antd";
import { useEffect, useRef, useState } from "react";
import { connect } from "umi";
import { getFieldEnumTreeNoSession, handleDeletePicture, handleGetBatchPROPERTYSHOP, handleGetBusinessProjectList, handleGetPictureList, handleGetPROPERTYASSETSDetail, handleGetProvinceVehicleDetail, handleGetServerpartInfo, handleGetServerpartTree, handleGetSynchroPROPERTYSHOP, handleSynchroPROPERTYASSETS, uploadAHYDPicture } from "../service";
import moment from "moment";
import { ProForm, ProFormSelect, ProFormText, ProFormTreeSelect, ProFormUploadButton } from "@ant-design/pro-components";
import RelatedShop from "./relatedShop";
import AssetsDetail from "./assetsDetail";
import HistoryTable from "./historyTable";


type DetailProps = {
    onShow: any // 控制组件是否显示
    setOnShow: any // 关闭抽屉
    handleClose?: any // 组件的关闭事件
    currentUser?: any
    id: any
    parentRef?: any // 上一级 可能需要刷新表格的
    onlyRead?: boolean // 抽屉底部不显示任何关于编辑新增的按钮
    serverPartShopId?: any // 有id  或者门店id和服务区id同时存在
    SERVERPARTID?: any
}

const LeftSelectTree = ({ onShow, handleClose, setOnShow, currentUser, id, parentRef, onlyRead, serverPartShopId, SERVERPARTID }: DetailProps) => {
    const formAssetsRef = useRef<any>();
    const relatedShopRef = useRef<any>()
    const historyTableRef = useRef<any>()
    // 点击的资产详情
    const [assetsDetail, setAssetsDetail] = useState<any>()
    // 点击行的数据
    const [currentRow, setCurrentRow] = useState<any>()
    // 是否正在提交
    const [isSubmit, setIsSubmit] = useState<boolean>(false)
    // 关联门店的id
    const [selectShopId, setSelectShopId] = useState<any>()
    // 资产详情抽屉
    const [assetsDetailDrawer, setAssetsDetailDrawer] = useState<boolean>(false)
    // 关联历史门店的抽屉
    const [showHistory, setShowHistory] = useState<boolean>(false)
    // 默认选中门店的行号列表
    const [defaultSelectList, setDefaultSelectList] = useState<any>()
    // 上传平面图的图片列表
    const [fileList, setFileList] = useState<any>()
    // 服务区方位根据服务区动态变化的选择数据
    const [selectRegionList, setSelectRegionList] = useState<any>()
    // 判断资产编码是否必填 资产类型是商铺的时候必填
    const [assetsCodeRequired, setAssetsCodeRequired] = useState<boolean>(false)
    // 资产类型
    const [PROPERTYASSETSTYPE, setPROPERTYASSETSTYPE] = useState<any>()
    // 预览图片
    const [imagePreviewVisible, setImagePreviewVisible] = useState<boolean>(false)
    // 关联门店抽屉
    const [relatedShopModal, setRelatedShopModal] = useState<boolean>(false)

    // 校验图片大小
    const beforeUpload = (file: any) => {
        formAssetsRef.current?.validateFields(['SERVERPART_ID', 'PROPERTYASSETS_REGION']).then(async (res) => {
            console.log('file', file);

            const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
            if (!isJpgOrPng) {
                message.error('你只能上传 JPG或PNG 格式的图片文件!');
                setFileList([])
                return isJpgOrPng;
            }
            console.log('2');

            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isLt2M) {
                message.error('图片大小不超过 2MB!');
            }
            return isJpgOrPng && isLt2M;
        }).catch(() => {
            setFileList([])
            return false
        })
    }

    // 预览上传后的图片
    const handlePreview = async () => {

        setFileList(fileList)
        setImagePreviewVisible(true)

    };

    // 保存和新增的方法
    const handleCreateAssets = async (type: string) => {
        // type add的时候是新增  type 是save的时候是保存
        // 新增不会关闭抽屉 表格会刷新 只会清空资产类型、资产编码、资产名称、面积。。。。 即不是方位和服务区的内容清空掉
        // 保存是会关闭整个抽屉 跟老的走一样的
        // 新增在编辑的时候不会出现
        console.log('assetsDetail', assetsDetail);

        // if (!selectShopId?.SERVERPARTSHOP_ID && !assetsDetail?.PropertyShop?.SERVERPARTSHOP_ID) {
        //   message.error('请先选择关联门店！')
        //   return
        // }
        formAssetsRef.current?.validateFields().then(async (res) => {
            // 因为现在有连续新增 所以在每一次的新增前都要去判断一下 这一次新增的编码 在这个服务区的这个方位是否已经有了
            // 编辑不用判断
            if (!currentRow?.PROPERTYASSETS_ID) {
                const req: any = {
                    SearchParameter: {
                        SERVERPART_IDS: res?.SERVERPART_ID,
                        // PROPERTYASSETS_REGION: res?.PROPERTYASSETS_REGION,
                        PROPERTYASSETS_STATE: 1
                    },
                    PageIndex: 1,
                    PageSize: 999999
                }
                const data = await handleGetProvinceVehicleDetail(req)
                console.log('32131231', data);
                // 默认设置为没有一样的 有一样的就变true
                let isSame: boolean = false
                let sameObj: any = {}
                if (data && data.length > 0) {
                    data.forEach((item: any) => {
                        if (item.PROPERTYASSETS_CODE === res.PROPERTYASSETS_CODE) {
                            isSame = true
                            sameObj = item
                        }
                    })
                }
                console.log('sameObj', sameObj);

                if (isSame) {
                    message.error(`${sameObj?.PROPERTYASSETS_CODE}已与${sameObj?.SHOPNAME}关联，如需修改，请先删除关联！`)
                    return
                }
            }

            console.log('res', res);
            let req: any = {}
            setIsSubmit(true)
            // if (type === 'save') {
            //   setIsSubmit(true)
            // }
            if (currentRow?.PROPERTYASSETS_ID) {
                // 编辑
                req = {
                    ...assetsDetail,
                    ...res,
                    OPERATOR_ID: currentUser?.ID,
                    OPERATOR_NAME: currentUser?.Name,
                    OPERATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss')
                }
            } else {
                // 新增
                req = {
                    ...res,
                    STAFF_ID: currentUser?.ID,
                    STAFF_NAME: currentUser?.Name,
                    CREATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
                    OPERATOR_ID: currentUser?.ID,
                    OPERATOR_NAME: currentUser?.Name,
                    OPERATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss')
                }
            }
            console.log('req', req);

            const data = await handleSynchroPROPERTYASSETS(req)
            console.log('data', data);
            if (data.Result_Code === 100) {
                console.log('relatedShopRef', relatedShopRef);
                console.log('currentRow', currentRow);

                message.success(data.Result_Desc)

                // 拿到关联门店的id
                let shopIdList: any = relatedShopRef.current?.selectModalRowList
                let shopIdObj: any = {}
                if (shopIdList && shopIdList.length > 0) {
                    shopIdObj = shopIdList[0]
                    console.log('shopIdObj', shopIdObj);
                }
                setSelectShopId(shopIdObj)
                if (shopIdList && shopIdList.length > 0 && PROPERTYASSETSTYPE !== 1) {
                    await handleGetShopRelateAssets(data.Result_Data, shopIdObj)
                }
                if (parentRef) {
                    parentRef.current?.reload()
                }
            } else {
                message.error(data.Result_Desc)
            }

            console.log('typetype', type);

            if (type === 'save') {
                // 现在要求保存的时候也不关闭 抽屉 但是点了保存 整个抽屉的内容要重新刷新
                // setAssetsDrawer(false);
                // setCurrentRow(undefined);
                // setIsSearchDetail(false)
                // setAssetsCodeRequired(false)
                // setSelectShopId(undefined)
                // setSelectRegionList([])
                // setFileList([])

                setIsSubmit(false)
                setAssetsDetail(undefined);
                setDefaultSelectList([])
                // 重新拿一下详情
                if (id) {
                    const req: any = {
                        PROPERTYASSETSId: id
                    }
                    const detailData = await handleGetPROPERTYASSETSDetail(req)
                    console.log('detail', detailData);
                    setDefaultSelectList([`${detailData?.PROPERTYASSETS_CODE}-${detailData?.PropertyShop?.SERVERPARTSHOP_ID}`])
                    setAssetsDetail(detailData)
                    // 详情表单刷新
                    // formAssetsRef.current?.reload()
                    // 选择门店的列表刷新
                    relatedShopRef.current?.setSelectedModalOrderRowKeys([`${detailData?.PROPERTYASSETS_CODE}-${detailData?.PropertyShop?.SERVERPARTSHOP_ID}`])
                }
                relatedShopRef.current?.handleReloadTable()
            } else {
                // 老表单保存的内容
                let oldObj: any = {
                    SERVERPART_ID: res.SERVERPART_ID,
                    PROPERTYASSETS_REGION: res.PROPERTYASSETS_REGION
                }
                // 清空关联门店
                setSelectShopId(undefined)
                formAssetsRef.current?.resetFields()
                formAssetsRef.current?.setFieldsValue(oldObj)
                if (data.Result_Data && data) {
                    // 重新拿一下详情
                    const req: any = {
                        PROPERTYASSETSId: data.Result_Data.PROPERTYASSETS_ID
                    }
                    const detailData = await handleGetPROPERTYASSETSDetail(req)
                    console.log('detail', detailData);
                    setDefaultSelectList([`${detailData?.PROPERTYASSETS_CODE}-${detailData?.PropertyShop?.SERVERPARTSHOP_ID}`])
                    console.log('[`${detailData?.PROPERTYASSETS_CODE}-${detailData?.PropertyShop?.SERVERPARTSHOP_ID}`]', [`${detailData?.PROPERTYASSETS_CODE}-${detailData?.PropertyShop?.SERVERPARTSHOP_ID}`]);

                    setAssetsDetail(detailData)
                    setIsSubmit(false)
                }
                relatedShopRef.current?.setSelectedModalOrderRowKeys([])
                relatedShopRef.current?.handleReloadTable()
            }
        })
    }

    // 将资产跟门店关联
    const handleGetShopRelateAssets = async (obj: any, shopId: any) => {
        console.log('obj', obj);
        console.log('详情', assetsDetail);
        console.log('选择的', shopId);
        if (assetsDetail) {
            if (assetsDetail?.PropertyShop?.PROPERTYSHOP_ID === shopId) {
                return
            }
        }

        // 没有选择的话就不调了
        if (!shopId) {
            return
        }

        // PROPERTYSHOP_ID 表示已经关联过了 这一次是修改 所以只能从详情里面拿到
        // SERVERPART_ID  PROPERTYASSETS_ID 虽然详情里面也有 但是如果是新增的话 详情是没有值的 所以只能取接口返回的回参
        // SERVERPARTSHOP_ID 进到这一步 说明要么是第一次关联 要么是修改过的 所以用选择的数据 合适
        // 要判断是不是新增 新增要传创建人 编辑就操作人
        let req: any = {}
        let reqList: any = []

        // 去拿当前项目的开始结束时间
        let projectReq: any = {
            SearchParameter: {
                SERVERPARTSHOP_ID: shopId?.SERVERPARTSHOP_ID,
                PROJECT_VALID: 1
            },
            PageIndex: 1,
            PageSize: 999999
        }
        const projectData = await handleGetBusinessProjectList(projectReq)
        console.log('projectData', projectData);

        let projectStart: any = ''
        let projectEnd: any = ''
        if (projectData && projectData.length > 0) {
            projectStart = projectData[0].PROJECT_STARTDATE
            projectEnd = projectData[0].PROJECT_ENDDATE
        }

        if (assetsDetail?.PropertyShop?.PROPERTYSHOP_ID) {
            // 编辑
            req = {
                PROPERTYSHOP_ID: assetsDetail?.PropertyShop?.PROPERTYSHOP_ID,
                SERVERPART_ID: obj.SERVERPART_ID,
                PROPERTYASSETS_ID: obj.PROPERTYASSETS_ID,
                SERVERPARTSHOP_ID: shopId.SERVERPARTSHOP_ID,
                OPERATOR_ID: currentUser?.ID,
                PROPERTYSHOP_STATE: 1,
                OPERATOR_NAME: currentUser?.Name,
                OPERATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
            }
        } else {
            // 新增
            req = {
                SERVERPART_ID: obj.SERVERPART_ID,
                PROPERTYASSETS_ID: obj.PROPERTYASSETS_ID,
                SERVERPARTSHOP_ID: shopId.SERVERPARTSHOP_ID,
                STAFF_ID: currentUser?.ID,
                STAFF_NAME: currentUser?.Name,
                PROPERTYSHOP_STATE: 1,
                STARTDATE_Start: projectStart ? moment(projectStart).format('YYYY-MM-DD') : '',
                STARTDATE_End: projectEnd ? moment(projectEnd).format('YYYY-MM-DD') : '',
                CREATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
                OPERATOR_ID: currentUser?.ID,
                OPERATOR_NAME: currentUser?.Name,
                OPERATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
            }
        }
        console.log('req', req);
        const data = await handleGetSynchroPROPERTYSHOP(req)
        // handleGetSynchroPROPERTYSHOP 单个
        // handleGetBatchPROPERTYSHOP 批量
        console.log('data222222', data);
        if (data.Result_Code === 100) {
            // message.success(data.Result_Desc)
        } else {
            message.error(data.Result_Desc)
        }
    }

    // 历史表格的提交
    const handleSubHistoryTableData = async () => {
        let list: any = historyTableRef.current?.selectModalRowList
        console.log('list', list);
        let reqList: any = []
        list.forEach((item: any) => {
            reqList.push({
                SERVERPART_ID: currentRow.SERVERPART_ID,
                PROPERTYASSETS_ID: currentRow.PROPERTYASSETS_ID,
                SERVERPARTSHOP_ID: item.SERVERPARTSHOP_ID,
                STAFF_ID: currentUser?.ID,
                STAFF_NAME: currentUser?.Name,
                PROPERTYSHOP_STATE: 1,
                CREATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
                OPERATOR_ID: currentUser?.ID,
                OPERATOR_NAME: currentUser?.Name,
                OPERATE_DATE: moment().format('YYYY-MM-DD HH:mm:ss'),
            })
        })
        const data = await handleGetBatchPROPERTYSHOP(reqList)
        if (data.Result_Code === 100) {
            message.success(data.Result_Desc)
            setShowHistory(false);
        } else {
            message.error(data.Result_Desc)
        }
    }

    // 判断服务区和方位是不是已经都选择了 去拿他们服务区的平面图
    const handleGetServicePic = async () => {
        let res: any = formAssetsRef.current?.getFieldsValue()
        console.log('res', res);
        if (res?.PROPERTYASSETS_REGION && res?.SERVERPART_ID) {
            const imgReq: any = {
                TableId: res?.SERVERPART_ID,
                TableType: 1201,
                ImageIndex: res?.PROPERTYASSETS_REGION
            }
            const imgData = await handleGetPictureList(imgReq)
            console.log('imgData', imgData);
            let imgList: any = imgData.List
            let realImgList: any = []
            if (imgList && imgList.length > 0) {
                imgList.forEach((item: any) => {
                    realImgList.push({
                        uid: item.ImageId,
                        name: item.ImageName,
                        status: 'done',
                        url: item.ImageUrl,
                        imgUrl: item.ImagePath,
                    })
                })
            }
            setFileList(realImgList)
            relatedShopRef.current?.handleReloadTable()

            if (PROPERTYASSETSTYPE === 1) {
                formAssetsRef.current?.setFieldsValue({ PROPERTYASSETS_CODE: `${res?.SERVERPART_ID}-${res?.PROPERTYASSETS_REGION}` })
            }
        } else {
            setFileList([])
        }
    }


    return (
        <div>
            {/* 编辑资产和新增资产的抽屉 */}
            <Drawer
                width={'80%'}
                visible={onShow}
                destroyOnClose
                title={false}
                closeIcon={false}
                onClose={() => {
                    setOnShow(false)
                    setCurrentRow(undefined);
                    setAssetsDetail(undefined);
                    setDefaultSelectList([])
                    setAssetsCodeRequired(false)
                    setSelectShopId(undefined)
                    setIsSubmit(false)
                    setPROPERTYASSETSTYPE(undefined)
                    setFileList([])
                    setSelectRegionList([])
                    if (handleClose) {
                        handleClose()
                    }
                }}
                bodyStyle={{ backgroundColor: "#fff", padding: 16, paddingTop: 0 }}
                footer={
                    <div style={{ width: '100%' }}>
                        {
                            <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', boxSizing: 'border-box', paddingLeft: '16px' }}>
                                {/* <Button type={'primary'} onClick={() => {
                    formAssetsRef.current?.validateFields(['SERVERPART_ID', 'PROPERTYASSETS_REGION']).then(async (res) => {
                      setRelatedShopModal(true)
                      if (relatedShopRef.current?.handleReloadTable) {
                        relatedShopRef.current?.handleReloadTable()
                      }
                    })
                  }}>
                    关联门店
                  </Button> */}
                                {/* 占位的div 如果上面的Button重新要显示 div就注释 */}
                                <div>
                                    {
                                        currentRow?.PROPERTYASSETS_ID ? <Button type={"primary"} onClick={() => {
                                            setAssetsDetailDrawer(true)
                                        }}>资产详情</Button> : ''
                                    }
                                </div>
                                <div>
                                    {/* {
                      currentRow?.PROJECT_ENDDATE && new Date(moment(currentRow?.PROJECT_ENDDATE).format('YYYY-MM-DD')).getTime() > new Date('2024-01-01').getTime() ?
                        <Button type={"primary"} style={{ marginRight: '8px' }} onClick={() => {
                          setShowHistory(true)
                        }}>历史关联门店</Button> : ''
                    } */}
                                    {
                                        onlyRead ? '' :
                                            <>
                                                {
                                                    currentRow?.PROPERTYASSETS_ID ? <Button type={"primary"} style={{ marginRight: '8px' }} onClick={() => {
                                                        setShowHistory(true)
                                                    }}>历史关联门店</Button> : ""
                                                }


                                                {
                                                    currentRow?.PROPERTYASSETS_ID ? '' :
                                                        <Button loading={isSubmit} type={"primary"} style={{ marginRight: '8px' }} onClick={() => {
                                                            handleCreateAssets('add')
                                                        }}>新增</Button>
                                                }

                                                <Button loading={isSubmit} type={"primary"} onClick={() => {
                                                    handleCreateAssets('save')
                                                }}>
                                                    保存
                                                </Button>
                                            </>
                                    }
                                </div>
                            </div>
                        }
                    </div>
                }
            >
                <Divider orientation="left">{currentRow?.PROPERTYASSETS_ID ? '编辑资产' : '新增资产'}</Divider>
                <ProForm
                    layout={'horizontal'}
                    formRef={formAssetsRef}
                    labelCol={{ span: 5 }}
                    submitter={{
                        render: (_, record) => {
                            return []
                        }
                    }}
                    request={async () => {
                        console.log('serverPartShopId', serverPartShopId);
                        console.log('idid', id);

                        // 有 说明是编辑 默认去请求数据
                        if (id) {
                            const req: any = {
                                PROPERTYASSETSId: id
                            }
                            const data = await handleGetPROPERTYASSETSDetail(req)
                            console.log('detail', data);
                            setDefaultSelectList([`${data?.PROPERTYASSETS_CODE}-${data?.PropertyShop?.SERVERPARTSHOP_ID}`])
                            setAssetsDetail(data)
                            setCurrentRow(data)
                            // 去拿图片列表
                            const imgReq: any = {
                                TableId: data.SERVERPART_ID,
                                TableType: 1201,
                                ImageIndex: data.PROPERTYASSETS_REGION
                            }
                            const imgData = await handleGetPictureList(imgReq)
                            console.log('imgData', imgData);
                            let imgList: any = imgData.List
                            let realImgList: any = []
                            if (imgList && imgList.length > 0) {
                                imgList.forEach((item: any) => {
                                    realImgList.push({
                                        uid: item.ImageId,
                                        name: item.ImageName,
                                        status: 'done',
                                        url: item.ImageUrl,
                                        imgUrl: item.ImagePath,
                                    })
                                })
                            }
                            setFileList(realImgList)
                            // 拿服务区方位列表
                            const regReq: any = {
                                ServerpartId: data.SERVERPART_ID
                            }
                            const regData = await handleGetServerpartInfo(regReq)
                            let list: any = regData.RegionInfo
                            if (list && list.length > 0) {
                                let res: any = []
                                list.forEach((item: any) => {
                                    res.push({ label: item.SERVERPART_REGIONNAME, value: item.SERVERPART_REGION })
                                })
                                console.log('res', res);
                                setSelectRegionList(res)
                                // formAssetsRef.current?.setFieldsValue({ PROPERTYASSETS_REGION: res[0].value })
                            }


                            if (data.PROPERTYASSETS_TYPE === 1000) {
                                setAssetsCodeRequired(true)
                            }

                            setPROPERTYASSETSTYPE(data.PROPERTYASSETS_TYPE)
                            console.log('dajkdjasd', data);

                            return data
                        } else if (serverPartShopId) {
                            // 有门店但是没资产的点编辑进来
                            // 去拿图片列表
                            const imgReq: any = {
                                TableId: SERVERPARTID,
                                TableType: 1201,
                                ImageIndex: currentRow?.PROPERTYASSETS_REGION
                            }
                            const imgData = await handleGetPictureList(imgReq)
                            console.log('imgData', imgData);
                            let imgList: any = imgData.List
                            let realImgList: any = []
                            if (imgList && imgList.length > 0) {
                                imgList.forEach((item: any) => {
                                    realImgList.push({
                                        uid: item.ImageId,
                                        name: item.ImageName,
                                        status: 'done',
                                        url: item.ImageUrl,
                                        imgUrl: item.ImagePath,
                                    })
                                })
                            }
                            setFileList(realImgList)
                            // 拿服务区方位列表
                            const regReq: any = {
                                ServerpartId: currentRow?.SERVERPART_ID || SERVERPARTID
                            }
                            const regData = await handleGetServerpartInfo(regReq)
                            let list: any = regData.RegionInfo
                            if (list && list.length > 0) {
                                let res: any = []
                                list.forEach((item: any) => {
                                    res.push({ label: item.SERVERPART_REGIONNAME, value: item.SERVERPART_REGION })
                                })
                                console.log('res', res);
                                setSelectRegionList(res)
                                formAssetsRef.current?.setFieldsValue({
                                    SERVERPART_ID: currentRow?.SERVERPART_ID || Number(SERVERPARTID),
                                    PROPERTYASSETS_REGION: currentRow?.PROPERTYASSETS_REGION

                                })
                            }
                            return {
                                SERVERPART_ID: currentRow?.SERVERPART_ID || Number(SERVERPARTID),
                                PROPERTYASSETS_REGION: currentRow?.PROPERTYASSETS_REGION
                            }
                        }
                        return {}
                    }}
                >
                    <Row gutter={16}>
                        <Col span={8}>
                            <ProFormSelect
                                label={'服务区名称'}
                                name={"SERVERPART_ID"}
                                disabled={id || (serverPartShopId && SERVERPARTID)}
                                request={async () => {
                                    const req: any = {
                                        ProvinceCode: currentUser?.ProvinceCode,
                                        ServerpartType: 1000,
                                        StatisticsType: 1000,
                                        ShowWholePower: false,
                                        ShowSPRegion: false,
                                        ShowRoyalty: false,
                                        ShowCompactCount: false,
                                    }
                                    const data = await handleGetServerpartTree(req)
                                    // 去除安徽驿达
                                    let list: any = []
                                    if (data && data.length > 0) {
                                        data.forEach((item: any) => {
                                            if (item.value !== 424) {
                                                list.push(item)
                                            }
                                        })
                                    }
                                    console.log('listlistlistlist', list);
                                    return list
                                }}
                                rules={[
                                    {
                                        required: true,
                                        message: '请选择服务区名称！',
                                    },
                                ]}
                                labelAlign={'right'}
                                fieldProps={{
                                    showSearch: true,
                                    onChange: async (e: any) => {
                                        if (e) {
                                            // 根据服务区去请求服务区方位列表 然后给方位列表显示
                                            // 默认帮忙选第一个
                                            setSelectRegionList([])
                                            console.log('e', e);
                                            const req: any = {
                                                ServerpartId: e
                                            }
                                            const data = await handleGetServerpartInfo(req)
                                            let list: any = data.RegionInfo
                                            if (list && list.length > 0) {
                                                let res: any = []
                                                list.forEach((item: any) => {
                                                    res.push({ label: item.SERVERPART_REGIONNAME, value: item.SERVERPART_REGION })
                                                })
                                                console.log('res', res);
                                                setSelectRegionList(res)
                                                formAssetsRef.current?.setFieldsValue({ PROPERTYASSETS_REGION: res[0].value })
                                            }
                                            handleGetServicePic()
                                        } else {
                                            setSelectRegionList([])
                                            relatedShopRef.current?.handleReloadTable()
                                        }

                                    }
                                }}
                            />
                        </Col>

                        <Col span={8}>
                            <ProFormSelect
                                label={"服务区方位"}
                                labelAlign={'right'}
                                name={"PROPERTYASSETS_REGION"}
                                disabled={id}
                                rules={[
                                    {
                                        required: true,
                                        message: '请选择服务区方位！',
                                    },
                                ]}
                                fieldProps={{
                                    options: selectRegionList,
                                    onChange: (e: any) => {
                                        handleGetServicePic()
                                    }
                                }}
                            />
                        </Col>

                        <Col span={8}>
                            <ProFormTreeSelect
                                label={"资产类型"}
                                labelAlign={'right'}
                                name={"PROPERTYASSETS_TYPE"}
                                rules={[
                                    {
                                        required: true,
                                        message: '请选择资产类型！',
                                    },
                                ]}
                                request={async () => {
                                    const list = await getFieldEnumTreeNoSession({ FieldExplainField: 'PROPERTYASSETS_TYPE' })

                                    if (list && list.length > 0 && !currentRow?.PROPERTYASSETS_ID) {
                                        formAssetsRef.current?.setFieldsValue({ PROPERTYASSETS_TYPE: list[0].children[0].value })
                                    }
                                    return list
                                }}
                                fieldProps={{
                                    onChange: (e: any) => {
                                        console.log('e', e);
                                        setPROPERTYASSETSTYPE(e)
                                        if (e === 1000 || e === 1) {
                                            if (e === 1) {
                                                let res: any = formAssetsRef.current?.getFieldsValue()
                                                if (res?.PROPERTYASSETS_REGION && res?.SERVERPART_ID) {
                                                    formAssetsRef.current?.setFieldsValue({ PROPERTYASSETS_CODE: `${res?.SERVERPART_ID}-${res?.PROPERTYASSETS_REGION}` })
                                                }
                                            }
                                            setAssetsCodeRequired(true)
                                        } else {
                                            setAssetsCodeRequired(false)
                                        }
                                    },
                                    treeDefaultExpandAll: true
                                }}
                            />
                        </Col>
                    </Row>
                    <Row gutter={16}>
                        <Col span={16}>
                            <Row>
                                <Col span={12}>
                                    <ProFormText
                                        label={"资产编码"}
                                        labelAlign={'right'}
                                        name={"PROPERTYASSETS_CODE"}
                                        rules={[
                                            {
                                                required: true,
                                                message: '请输入资产编码！',
                                            },
                                        ]}
                                    />
                                </Col>
                                <Col span={12}>
                                    <ProFormText
                                        label={"面积"}
                                        labelAlign={'right'}
                                        name={"PROPERTYASSETS_AREA"}
                                        fieldProps={{
                                            addonAfter: "m²"
                                        }}
                                        rules={[
                                            {
                                                required: true,
                                                message: '请输入资产面积！',
                                            },
                                        ]}
                                    />
                                </Col>
                                <Col span={12}>
                                    <ProFormText
                                        label={"备注说明"}
                                        labelAlign={'right'}
                                        name={"PROPERTYASSETS_DESC"}
                                    />
                                </Col>
                                <Col span={12}>
                                    <ProFormSelect
                                        label={"有效状态"}
                                        labelAlign={'right'}
                                        name={"PROPERTYASSETS_STATE"}
                                        rules={[
                                            {
                                                required: true,
                                                message: '请选择有效状态！',
                                            },
                                        ]}
                                        initialValue={1}
                                        request={() => {
                                            // let list: any = [{ label: '有效', value: 1 }, { label: '无效', value: 0 }]
                                            let list: any = [{ label: '正常', value: 1 }, { label: '关闭', value: 0 }]
                                            return list
                                        }}
                                    />
                                </Col>
                            </Row>
                        </Col>
                        <Col span={8}>
                            <ProFormUploadButton
                                label={"上传平面图"}
                                fieldProps={{
                                    name: 'files',
                                    maxCount: 1,
                                    fileList: fileList,
                                    beforeUpload: beforeUpload,
                                    onPreview: handlePreview,
                                    customRequest: async (info) => {
                                        const isOk: any = await formAssetsRef.current?.validateFields(['SERVERPART_ID', 'PROPERTYASSETS_REGION'])
                                        console.log('isOk', isOk);
                                        if (isOk?.PROPERTYASSETS_REGION && isOk?.SERVERPART_ID) {
                                            let res: any = formAssetsRef.current?.getFieldsValue()
                                            console.log('res', res);
                                            console.log('info', info);
                                            const formData = new FormData();
                                            formData.append('files', info.file);
                                            formData.append('TableId', res?.SERVERPART_ID);
                                            formData.append('TableType', '1201');
                                            formData.append('ImageIndex', res?.PROPERTYASSETS_REGION);
                                            formData.append('ImageName', typeof info.file !== 'string' ? info.file?.name : '');

                                            console.log('formData', formData);
                                            console.log('info.fileinfo.file', info.file);

                                            if (info.filename) {
                                                const success = await uploadAHYDPicture(formData)
                                                console.log('success', success);

                                                if (success.Result_Code === 100) {

                                                    const list = [{
                                                        uid: `${success.Result_Data.ImageId}`, // 注意，这个uid一定不能少，否则上传失败
                                                        name: success.Result_Data.ImageName,
                                                        status: 'done',
                                                        url: success.Result_Data.ImageUrl, // url 是展示在页面上的绝对链接
                                                        imgUrl: success.Result_Data.ImagePath  // + success.ImageUrl,
                                                    }]
                                                    setFileList(list)
                                                    message.success("上传成功！")
                                                    console.log('listlistlist', list);

                                                }
                                            } else {

                                                message.error("您上传的图片不存在.")
                                            }
                                        }
                                    },
                                    onRemove: async (info: any) => {
                                        console.log('info', info);
                                        // ImageId === uid   imgUrl === ImagePath
                                        const req: any = {
                                            imageid: info.uid,
                                            ImagePath: info.imgUrl,
                                            TableName: 'SERVERPART',
                                            TableType: '1010',
                                        }
                                        const data = await handleDeletePicture(req)
                                        if (data) {
                                            message.success('删除成功！')
                                            setFileList([])
                                        }
                                    }
                                }}
                            />
                        </Col>
                    </Row>

                    <Row gutter={16}>
                    </Row>
                    <Row>
                        <Col span={8}>
                            {
                                PROPERTYASSETSTYPE === 1 ? '' :
                                    <RelatedShop currentUser={currentUser} parentRef={parentRef} currentRow={currentRow} defaultId={defaultSelectList} detailFormRef={formAssetsRef} onRef={relatedShopRef} show={relatedShopModal} />
                            }
                        </Col>
                        <Col span={16}>
                            {fileList && fileList.length > 0 ? <div>
                                {
                                    fileList.map((n) =>
                                        <Image src={n.url} key={n.url} />
                                    )
                                }
                            </div> : ''}
                        </Col>
                    </Row>
                </ProForm>

            </Drawer>



            {/* 资产详情抽屉 */}
            <Drawer
                width={'80%'}
                visible={assetsDetailDrawer}
                className={"assetsDetailDrawer"}
                onClose={() => {
                    setAssetsDetailDrawer(false);
                }}
                bodyStyle={{ backgroundColor: "#fff", padding: 16 }}
                closable={false}
                destroyOnClose
            >
                <AssetsDetail currentRow={currentRow} currentUser={currentUser} />
            </Drawer>

            <Drawer
                width={'60%'}
                visible={showHistory}
                className={"assetsDetailDrawer"}
                onClose={() => {
                    setShowHistory(false);
                }}
                bodyStyle={{ backgroundColor: "#fff", padding: 16 }}
                closable={false}
                destroyOnClose
                footer={
                    <div style={{ width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'flex-end' }}>
                        <Button type={"primary"} onClick={() => {
                            handleSubHistoryTableData()
                        }}>
                            关联历史门店
                        </Button>
                    </div>
                }
            >
                <HistoryTable onRef={historyTableRef} currentRow={currentRow} defaultId={defaultSelectList} />
            </Drawer>
        </div>
    )
}



export default connect(({ user }: ConnectState) => ({
}))(LeftSelectTree);