import { connect } from "umi";
import type { ConnectState } from "@/models/connect";
import { Button, Col, Divider, FormInstance, Modal, Row } from "antd";
import ProForm, { ProFormDigit, ProFormSelect, ProFormText, ProFormTextArea } from "@ant-design/pro-form";
import { useRef, useState } from "react";
import Draggable from "react-draggable";
import React from "react";
import { handleGetCommodityInSaleList, handleGetGetCOMMODITYDetail, handleGetINSALES_STATISTICDetail } from "../../service";
import session from "@/utils/session";
import COMMODITYINFO from "./COMMODITYINFO";

type DetailProps = {
    onShow: boolean
    parentRow: any
    onCancel: any
    come?: string
}
const InventoryDetailModal = ({ onShow, parentRow, onCancel, come }: DetailProps) => {
    // come  若等于 inventory  那么说明是库存里面来的 那么请求详情的方法就不一样了
    const formRef = useRef<FormInstance>();
    const draggleRef = React.createRef<any>()

    // 显示历史的进销存信息
    const [historyInfo, setHistoryInfo] = useState<any>()
    // 当前商品的明细
    const [commodityDetail, setCommodityDetail] = useState<any>()

    // 弹出框拖动效果
    const [bounds, setBounds] = useState<{ left: number, right: number, top: number, bottom: number }>() // 移动的位置
    const [disabled, setDraggleDisabled] = useState<boolean>() // 是否拖动
    const onDraggaleStart = (event, uiData) => {
        const { clientWidth, clientHeight } = window.document.documentElement;
        const targetRect = draggleRef.current?.getBoundingClientRect();
        if (!targetRect) {
            return;
        }
        setBounds({
            left: -targetRect.left + uiData.x,
            right: clientWidth - (targetRect.right - uiData.x),
            top: -targetRect.top + uiData.y,
            bottom: clientHeight - (targetRect.bottom - uiData.y),
        });
    };

    // 关闭
    const handleCloseHistory = async () => {
        setHistoryInfo(false)
    }

    return (
        <div>
            <Modal
                title={
                    <div
                        className="InventoryDetailModalTitle"
                        style={{
                            width: '100%',
                            cursor: 'move',
                        }}
                        onMouseOver={() => {
                            if (disabled) {
                                setDraggleDisabled(false)
                            }
                        }}
                        onMouseOut={() => {
                            setDraggleDisabled(true)
                        }}

                        onFocus={() => { }}
                        onBlur={() => { }}
                    >
                        {parentRow?.COMMODITY_NAME || ""}
                    </div>
                }
                destroyOnClose={true}
                width={1200}
                bodyStyle={{
                    height: '700px', // 你可以根据需要调整高度
                    overflowY: 'auto',
                }}
                visible={onShow}
                onCancel={() => {
                    if (onCancel) {
                        onCancel()
                    }
                }}
                footer={false}
                modalRender={(modal) => {
                    return <Draggable
                        disabled={disabled}
                        bounds={bounds}
                        onStart={(event, uiData) => onDraggaleStart(event, uiData)}
                        handle=".InventoryDetailModalTitle"
                    >
                        <div ref={draggleRef}>{modal}</div>
                    </Draggable>
                }}
            >
                <ProForm
                    layout={'horizontal'}
                    formRef={formRef}
                    autoFocusFirstInput
                    labelCol={{ style: { width: 80 } }}
                    submitter={false}
                    preserve={false}
                    request={async () => {
                        if (come === 'inventory') {
                            console.log('parentRowparentRowparentRowparentRow', parentRow);
                            const req: any = {
                                ServerpartShopId: parentRow?.ServerpartShop_Id,
                                CommodityId: parentRow?.COMMODITY_ID,
                                CommodityBarcode: parentRow?.COMMODITY_BARCODE,
                            }
                            const data = await handleGetCommodityInSaleList(req)
                            console.log('datadatadatadatadatadata', data);
                            let obj: any = {}
                            if (data && data.length > 0) {
                                /// 1000：入库业务
                                /// 2000：调拨业务
                                /// 3000：退货业务
                                /// 4000：领用业务
                                /// 5000：单品销售
                                /// 6000：盘点结存
                                /// 6001：盘点损溢
                                /// 6002：上期盘点结存
                                /// 6003：上期盘点损溢
                                data.forEach((item: any) => {
                                    if (item.OPERATE_TYPE === 1000) {
                                        obj = {
                                            ...obj,
                                            PURCHASE_COUNT: item.OPERATE_COUNT,// 入库数量
                                            PURCHASE_TAXPRICE: item.PURCHASE_TAXPRICE,// 含税单价
                                            PURCHASE_TOTALTAXPRICE: item.OPERATE_TAXAMOUNT,// 含税金额
                                            PURCHASE_PRICE: item.PURCHASE_PRICE,// 除税单价
                                            PURCHASE_TOTALPRICE: item.OPERATE_AMOUNT,// 除税金额
                                        }
                                    } else if (item.OPERATE_TYPE === 2000) {
                                        obj = {
                                            ...obj,
                                            TRANSFER_COUNT: item.OPERATE_COUNT,// 调拨数量
                                            TRANSFER_AMOUNT: item.OPERATE_TAXAMOUNT,// 调拨金额
                                            TRANSFER_PRICE: item.OPERATE_AMOUNT,// 除税金额
                                        }
                                    } else if (item.OPERATE_TYPE === 3000) {
                                        obj = {
                                            ...obj,
                                            BACK_COUNT: item.OPERATE_COUNT,// 退货数量
                                            BACK_AMOUNT: item.OPERATE_TAXAMOUNT,// 退货金额
                                            BACK_PRICE: item.OPERATE_AMOUNT,// 除税金额
                                        }
                                    } else if (item.OPERATE_TYPE === 4000) {
                                        obj = {
                                            ...obj,
                                            RECEIVE_COUNT: item.OPERATE_COUNT,// 领用数量
                                            RECEIVE_AMOUNT: item.OPERATE_TAXAMOUNT,// 领用金额
                                            RECEIVE_PRICE: item.OPERATE_AMOUNT,// 除税金额
                                        }
                                    } else if (item.OPERATE_TYPE === 5000) {
                                        obj = {
                                            ...obj,
                                            SELL_COUNT: item.OPERATE_COUNT,// 销售数量
                                            SELL_UNITTAXPRICE: item.PURCHASE_TAXPRICE,// 含税单价
                                            SELL_TOTALTAXPRICE: item.OPERATE_TAXAMOUNT,// 含税金额
                                            // SELL_COST: item.OPERATE_AMOUNT,// 含税成本
                                            SELL_UNITPRICE: item.PURCHASE_PRICE,// 除税单价
                                            SELL_TOTALPRICE: item.OPERATE_AMOUNT,// 除税金额
                                            // SELL_COSTPRICE: item.OPERATE_AMOUNT,// 除税成本
                                        }
                                    } else if (item.OPERATE_TYPE === 6000) {
                                        obj = {
                                            ...obj,
                                            OVERPLUSCOUNT: item.OPERATE_COUNT,// 入库数量
                                            PURCHASE_TAXPRICE: item.PURCHASE_TAXPRICE,// 含税单价
                                            OVERPLUS_AMOUNT: item.OPERATE_TAXAMOUNT,// 含税金额
                                            PURCHASE_PRICE: item.PURCHASE_PRICE,// 除税单价
                                            OVERPLUS_PRICE: item.OPERATE_AMOUNT,// 上期库存除税
                                        }
                                    } else if (item.OPERATE_TYPE === 6001) {
                                        obj = {
                                            ...obj,
                                            LOSSPROFIT_COUNT: item.OPERATE_COUNT,// 损溢数量
                                            LOSSPROFIT_AMOUNT: item.OPERATE_TAXAMOUNT,// 损溢金额
                                            LOSSPROFIT_PRICE: item.OPERATE_AMOUNT,// 损溢除税
                                        }
                                    } else if (item.OPERATE_TYPE === 6002) {
                                        obj = {
                                            ...obj,
                                            LAST_OVERPLUSCOUNT: item.OPERATE_COUNT,// 上期库存数量
                                            LAST_OVERPLUSAMOUNT: item.OPERATE_TAXAMOUNT,// 上期库存金额
                                            LAST_OVERPLUSPRICE: item.OPERATE_AMOUNT,// 上期库存除税
                                        }
                                    } else if (item.OPERATE_TYPE === 6003) {
                                        obj = {
                                            ...obj,
                                            LAST_LOSSPROFIT_COUNT: item.OPERATE_COUNT,// 上期损溢数量
                                            LAST_LOSSPROFIT_AMOUNT: item.OPERATE_TAXAMOUNT,// 上期损溢金额
                                            LAST_LOSSPROFIT_PRICE: item.OPERATE_AMOUNT,// 上期损溢除税
                                        }
                                    }
                                })
                            }
                            // 商品详情再调用一下
                            const detail: any = await handleGetGetCOMMODITYDetail({ COMMODITYId: parentRow?.COMMODITY_ID })
                            console.log('detaildetaildetaildetail', detail);
                            obj = {
                                ...obj,
                                ...detail,
                                SERVERPART_NAME: parentRow?.Serverpart_Name, // 服务区名
                                SHOPNAME: parentRow?.ServerpartShop_Name, // 门店名称
                                SERVERPARTSHOP_ID: parentRow?.ServerpartShop_Id, // 门店ID
                                COMMODITY_CODE: detail?.COMMODITY_CODE, // 商品编码
                                BUSINESSTYPE: detail?.BUSINESSTYPE, // 业态
                                COMMODITY_TYPE: detail?.COMMODITY_TYPE, // 商品类型
                                COMMODITY_BARCODE: detail?.COMMODITY_BARCODE, // 商品条码
                                COMMODITY_NAME: detail?.COMMODITY_NAME, // 商品名称
                                COMMODITY_UNIT: detail?.COMMODITY_UNIT, // 商品单位
                                COMMODITY_RULE: detail?.COMMODITY_RULE, // 商品规格
                                CHECKDATE_Start: "",
                                CHECKDATE: "",
                            }
                            setCommodityDetail(obj)
                            return obj
                        } else {
                            if (!parentRow?.INSALES_STATISTIC_ID) {
                                return {}
                            }
                            console.log('parentRowparentRowparentRowparentRow', parentRow);

                            const req: any = {
                                INSALES_STATISTICId: parentRow?.INSALES_STATISTIC_ID
                            }
                            const data = await handleGetINSALES_STATISTICDetail(req)
                            console.log('抽屉的详情', data);
                            setCommodityDetail(data)
                            return data
                        }
                    }}
                >
                    <Divider orientation="left">商品基本信息</Divider>
                    <div style={{ width: '100%' }}>
                        <Row gutter={8}>
                            <Col span={6}>
                                <ProFormText
                                    label={'服务区名'}
                                    name={'SERVERPART_NAME'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'门店名称'}
                                    name={'SHOPNAME'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'商品编码'}
                                    name={'COMMODITY_CODE'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormSelect
                                    label={'业态'}
                                    name={'BUSINESSTYPE'}
                                    request={async () => {
                                        const list = session.get('SHOPTRADEList')
                                        return list
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'商品类型'}
                                    name={'COMMODITY_TYPE'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'商品条码'}
                                    name={'COMMODITY_BARCODE'}
                                />
                            </Col>
                            <Col span={12}>
                                <ProFormText
                                    label={'商品名称'}
                                    name={'COMMODITY_NAME'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'商品单位'}
                                    name={'COMMODITY_UNIT'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'商品规格'}
                                    name={'COMMODITY_RULE'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'盘存区间'}
                                    name={'CHECKDATE_Start'}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormText
                                    label={'至'}
                                    name={'CHECKDATE'}
                                />
                            </Col>
                        </Row>
                    </div>



                    <Divider orientation="left">上期库存</Divider>
                    <div style={{ width: '100%' }}>
                        <Row>
                            <Col span={18}>
                                <Row gutter={8}>
                                    <Col span={8}>
                                        <ProFormDigit
                                            label={'库存数量'}
                                            name={'LAST_OVERPLUSCOUNT'}
                                            fieldProps={{
                                                addonAfter: false
                                            }}
                                        />
                                    </Col>
                                    <Col span={8}>
                                        <ProFormDigit
                                            label={'库存金额'}
                                            name={'LAST_OVERPLUSAMOUNT'}
                                            fieldProps={{
                                                addonAfter: false
                                            }}
                                        />
                                    </Col>
                                    <Col span={8}>
                                        <ProFormDigit
                                            label={'库存除税'}
                                            name={'LAST_OVERPLUSPRICE'}
                                            fieldProps={{
                                                addonAfter: false
                                            }}
                                        />
                                    </Col>
                                    <Col span={8}>
                                        <ProFormDigit
                                            label={'损溢数量'}
                                            name={'LAST_LOSSPROFIT_COUNT'}
                                            fieldProps={{
                                                addonAfter: false
                                            }}
                                        />
                                    </Col>
                                    <Col span={8}>
                                        <ProFormDigit
                                            label={'损溢金额'}
                                            name={'LAST_LOSSPROFIT_AMOUNT'}
                                            fieldProps={{
                                                addonAfter: false
                                            }}
                                        />
                                    </Col>
                                    <Col span={8}>
                                        <ProFormDigit
                                            label={'损溢除税'}
                                            name={'LAST_LOSSPROFIT_PRICE'}
                                            fieldProps={{
                                                addonAfter: false
                                            }}
                                        />
                                    </Col>
                                </Row>
                            </Col>
                            <Col span={6}>
                                <div style={{ width: '100%', height: '100%', display: 'flex', alignItems: 'flex-start', justifyContent: 'center' }}>
                                    <Button type={'primary'} onClick={() => {
                                        setHistoryInfo(true)
                                    }}>历史进销存</Button>
                                </div>
                            </Col>
                        </Row>
                    </div>


                    <Divider orientation="left">入库信息</Divider>
                    <div style={{ width: '100%' }}>
                        <Row gutter={8}>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'入库数量'}
                                    name={'PURCHASE_COUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'含税单价'}
                                    name={'PURCHASE_TAXPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'含税金额'}
                                    name={'PURCHASE_TOTALTAXPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            {/* <Col span={6}>
                                <ProFormDigit
                                    label={'进价税率'}
                                    name={''}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col> */}
                            <Col span={6}>
                                <ProFormDigit
                                    label={'除税单价'}
                                    name={'PURCHASE_PRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'除税金额'}
                                    name={'PURCHASE_TOTALPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                        </Row>
                    </div>

                    <Divider orientation="left">销售信息</Divider>
                    <div style={{ width: '100%' }}>
                        <Row gutter={8}>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'销售数量'}
                                    name={'SELL_COUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            {/* <Col span={6}>
                                <ProFormDigit
                                    label={'售价税率'}
                                    name={''}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col> */}
                            <Col span={6}>
                                <ProFormDigit
                                    label={'含税单价'}
                                    name={'SELL_UNITTAXPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'含税金额'}
                                    name={'SELL_TOTALTAXPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'含税成本'}
                                    name={'SELL_COST'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'除税单价'}
                                    name={'SELL_UNITPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'除税金额'}
                                    name={'SELL_TOTALPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'除税成本'}
                                    name={'SELL_COSTPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                        </Row>
                    </div>

                    <Divider orientation="left">出库信息</Divider>
                    <div style={{ width: '100%' }}>
                        <Row gutter={8}>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'退货数量'}
                                    name={'BACK_COUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'退货金额'}
                                    name={'BACK_AMOUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                {/* 退货 */}
                                <ProFormDigit
                                    label={'除税金额'}
                                    name={'BACK_PRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                            </Col>

                            <Col span={6}>
                                <ProFormDigit
                                    label={'调拨数量'}
                                    name={'TRANSFER_COUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'调拨金额'}
                                    name={'TRANSFER_AMOUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                {/* 调拨 */}
                                <ProFormDigit
                                    label={'除税金额'}
                                    name={'TRANSFER_PRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'领用数量'}
                                    name={'RECEIVE_COUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'领用金额'}
                                    name={'RECEIVE_AMOUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                {/* 领用 */}
                                <ProFormDigit
                                    label={'除税金额'}
                                    name={'RECEIVE_PRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                        </Row>
                    </div>

                    <Divider orientation="left">库存及利润信息</Divider>
                    <div style={{ width: '100%' }}>
                        <Row gutter={8}>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'本期数量'}
                                    name={'OVERPLUSCOUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'本期金额'}
                                    name={'OVERPLUS_AMOUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'本期除税'}
                                    name={'OVERPLUS_PRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}> </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'损溢数量'}
                                    name={'LOSSPROFIT_COUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'损溢金额'}
                                    name={'LOSSPROFIT_AMOUNT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'损溢除税'}
                                    name={'LOSSPROFIT_PRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}></Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'含税毛利'}
                                    name={'GROSS_PROFIT'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>
                            <Col span={6}>
                                <ProFormDigit
                                    label={'除税毛利'}
                                    name={'GROSS_PROFITPRICE'}
                                    fieldProps={{
                                        addonAfter: false
                                    }}
                                />
                            </Col>


                            <Col span={24}>
                                <ProFormTextArea
                                    label={'备注'}
                                    name={'INSALES_STATISTIC_DESC'}
                                />
                            </Col>
                        </Row>
                    </div>
                </ProForm>
            </Modal>



            {/* 单个商品的历史经销存数据 */}
            <COMMODITYINFO onShow={historyInfo} parentRow={commodityDetail} onCancel={handleCloseHistory} />
        </div >
    )
}

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