import { connect } from "umi";
import type { ConnectState } from "@/models/connect";
import { FormInstance, Modal } from "antd";
import ProTable from "@ant-design/pro-table";
import { useRef } from "react";
import { handleGetCommodityList, handleGetServerpartShopTrade } from "../../commodityInfo/service";
import { getFieldEnum } from "@/services/options";

type DetailProps = {
    onShow: any // 显示悬浮框
    setShowAddShopModal: any // 关闭悬浮框
    setShopData: any // 改变当前商品
    selectRowList: any // 当前选择的商品行
    currentUser: any // 公参信息
    CommodityData: any // 枚举值
    selectedOrderRowKeys: any // 选择的行值
    setSelectRowList: any // 改变选择的行
    setSelectedOrderRowKeys: any // 改变选择的行号
    formRef?: any //  申请的表单数据
}
const SelectShopModal = ({ onShow, setShowAddShopModal, setShopData, selectRowList, currentUser, CommodityData, selectedOrderRowKeys, setSelectRowList, setSelectedOrderRowKeys, formRef }: DetailProps) => {
    // 选择商品的表格结构
    const selectShopColumns: any = [
        {
            title: "商品查询",
            dataIndex: "searchText",
            hideInTable: true,
            fieldProps: {
                placeholder: "请输入商品名称"
            }
        },
        {
            title: <div style={{ textAlign: 'center' }}>序号</div>,
            width: 70,
            dataIndex: 'index',
            hideInSearch: true,
            valueType: 'index'
        },
        // {
        //     title: <div style={{ textAlign: 'center' }}>服务区</div>,
        //     width: 150,
        //     ellipsis: true,
        //     align: 'center',
        //     dataIndex: 'SERVERPART_NAME',
        //     hideInSearch: true,
        //     valueType: "select",
        //     valueEnum: ServerpartIdsList
        // },
        {
            title: <div style={{ textAlign: 'center' }}>商品业态</div>,
            width: 120,
            ellipsis: true,
            dataIndex: 'BUSINESSTYPE',
            align: 'center',
            valueType: 'select',
            request: async () => {
                // const BUSINESSTYPERes = await getFieldEnum({ FieldExplainField: 'BUSINESSTYPE', sessionName: 'SHOPTRADE', notformate: true })
                // return BUSINESSTYPERes
                let formRes = formRef.current.modalFormRef.current?.getFieldsValue()
                const req: any = {
                    ProvinceCode: currentUser?.ProvinceCode,
                    ServerpartId: formRes?.ServerpartId,
                    ShopTrade: "",
                    BusinessType: "",
                    ExcludeShopTrade: "",
                    ExcludeBusinessType: ""
                }
                const data = await handleGetServerpartShopTrade(req)
                console.log('data321312', data)
                return data
            },
            fieldProps: {
                showSearch: true,
                filterOption: (input, option) =>
                    (option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
            }
        },
        {
            title: <div style={{ textAlign: 'center' }}>商品类型</div>,
            width: 120,
            ellipsis: true,
            dataIndex: 'COMMODITY_TYPE',
            align: 'center',
            hideInSearch: true,
            valueEnum: CommodityData
        },
        {
            title: <div style={{ textAlign: 'center' }}>商品名称</div>,
            width: 200,
            ellipsis: true,
            align: 'left',
            dataIndex: 'COMMODITY_NAME',
            hideInSearch: true,
            // render: (_, record) => {
            //     return record?.COMMODITY_NAME ? <a onClick={() => {
            //         setCurrentShopRow(record)
            //         setShowEditShopModal(true)
            //     }}>
            //         {record?.COMMODITY_NAME}
            //     </a> : "-"
            // }
        },
        {
            title: <div style={{ textAlign: 'center' }}>商品条码</div>,
            width: 150,
            ellipsis: true,
            align: 'left',
            dataIndex: 'COMMODITY_BARCODE',
            hideInSearch: true,
        },
        {
            title: <div style={{ textAlign: 'center' }}>零售价</div>,
            width: 70,
            ellipsis: true,
            align: 'right',
            dataIndex: 'COMMODITY_RETAILPRICE',
            hideInSearch: true,
        },
        {
            title: <div style={{ textAlign: 'center' }}>进货价</div>,
            width: 70,
            ellipsis: true,
            align: 'right',
            dataIndex: 'COMMODITY_PURCHASEPRICE',
            hideInSearch: true,
        },
        {
            title: <div style={{ textAlign: 'center' }}>商品单位</div>,
            width: 80,
            ellipsis: true,
            align: 'right',
            dataIndex: 'COMMODITY_UNIT',
            hideInSearch: true,
        },
        {
            title: <div style={{ textAlign: 'center' }}>状态</div>,
            width: 100,
            ellipsis: true,
            dataIndex: 'COMMODITY_STATE',
            align: 'center',
            valueType: 'select',
            valueEnum: { 0: { text: '无效', status: 'error' }, 1: { text: '有效', status: 'success' }, 2: { text: '不可选', status: 'default' } },
            initialValue: '1',
        },
        {
            title: <div style={{ textAlign: 'center' }}>审核时间</div>,
            width: 150,
            align: 'center',
            ellipsis: true,
            dataIndex: 'OPERATE_DATE',
            hideInSearch: true,
        }
    ]

    return (
        <div>
            <Modal
                open={onShow}
                width={1200}
                wrapClassName={'shopModal'}
                destroyOnClose
                title={"选择商品"}
                onOk={() => {
                    setShowAddShopModal(false)
                    setShopData(selectRowList)
                }}
                onCancel={() => {
                    setShowAddShopModal(false)
                }}
                bodyStyle={{
                    height: '700px', // 你可以根据需要调整高度
                    overflowY: 'auto',
                }}
            >
                <ProTable
                    options={false}
                    scroll={{ x: "100%", y: "420px" }}
                    bordered
                    search={{ span: 6 }}
                    rowKey={"COMMODITY_ID"}
                    request={async (params) => {
                        console.log('formRefformRefformRef', formRef.current);
                        let formRes = formRef.current.modalFormRef.current?.getFieldsValue()
                        const req: any = {
                            SearchType: 3,
                            ShopTrade: params?.BUSINESSTYPE || "",
                            ProvinceCode: currentUser?.ProvinceCode,
                            // CommodityTypeId: currenMenu,
                            CommodityState: params?.COMMODITY_STATE || "",
                            ServerpartID: formRes?.ServerpartId,
                            PageIndex: 1,
                            PageSize: 999999,
                            SortStr: "OPERATE_DATE desc",
                            SearchKey: "COMMODITY_NAME",
                            SearchValue: params?.searchText || ""
                            // PageSize: 20
                        }
                        const data = await handleGetCommodityList(req)
                        console.log('shdasidh', data);
                        if (data.List && data.List.length > 0) {
                            return { data: data.List, success: true, total: data.TotalCount }
                        }
                        return { data: [], success: true }
                    }}
                    columns={selectShopColumns}
                    rowSelection={{
                        type: 'checkbox',
                        selectedRowKeys: selectedOrderRowKeys,
                        onChange: (selectedRowKeys: any, selectedRows: any) => {
                            console.log('selectedRowKeysselectedRowKeys', selectedRowKeys);
                            console.log('selectedRowsselectedRowsselectedRows', selectedRows);

                            setSelectRowList(selectedRows)
                            setSelectedOrderRowKeys(selectedRowKeys)
                        },
                    }}
                />
            </Modal>
        </div>
    )
}

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