// 消费记录查询
import { connect } from "umi";
import type { CurrentUser } from "umi";
import type { ConnectState } from "@/models/connect";
import React, { useRef, useState } from "react";
import ProCard from "@ant-design/pro-card";
import { MenuFoldOutlined } from "@ant-design/icons";
import type { FormInstance } from "antd";
import { Button, message, Space, Spin, Tree } from "antd";
import useRequest from "@ahooksjs/use-request";
import { getServerpartTree } from "@/services/options";
import type { ActionType } from "@ant-design/pro-table";
import ProTable from "@ant-design/pro-table";
import ReactHTMLTableToExcel from "react-html-table-to-excel";
import LeftSelectTree from "@/pages/reports/settlementAccount/component/leftSelectTree";
import PageTitleBox from "@/components/PageTitleBox";
import { handleGetCONSUMPTIONRECORDList } from "../service";
import moment from 'moment'
import session from "@/utils/session";


const ConsumptionRecordSearch: React.FC<{ currentUser: CurrentUser }> = (props) => {
    const { currentUser } = props
    const downloadBtnRef = useRef<any>()
    const actionRef = useRef<ActionType>();
    const formRef = useRef<FormInstance>();
    const [reqDetailList, setReqDetailList] = useState<any>(); // 合计项数据源
    const [printOut, setPrintOut] = useState<any>(); // 打印数据的内容
    const [collapsible, setCollapsible] = useState<boolean>(false)
    const [treeView, setTreeView] = useState<any>()
    const [printIndex, setPrintIndex] = useState<number>(new Date().getTime())

    let CONSUMPTIONRECORDTYPEObj = session.get('CONSUMPTIONRECORDTYPEObj')

    // 树相关的属性和方法
    const [selectedId, setSelectedId] = useState<string>()
    // 导出的加载效果
    const [showLoading, setShowLoading] = useState<boolean>(false)
    // 是否显示打印的表格
    const [showExportTable, setShowExportTable] = useState<boolean>(false)
    // 查询的条件
    const [searchParams, setSearchParams] = useState<any>()

    const columns: any = [
        {
            title: '查询时间',
            dataIndex: 'search_date',
            valueType: 'dateRange',
            hideInTable: true,
            hideInDescriptions: true,
            search: {
                transform: (value) => {
                    return {
                        CHECK_STARTDATE: value[0],
                        CHECK_ENDDATE: value[1],
                    };
                },
            },
            initialValue: [moment().format('YYYY-MM-DD'), moment().subtract(1, 'M').format('YYYY-MM-DD')],
        },
        {
            title: "服务区名称",
            width: 150,
            dataIndex: "SERVERPART_NAME",
            hideInSearch: true,
            align: 'center',
            ellipsis: true,
        },
        {
            title: "门店名称",
            width: 150,
            dataIndex: "SHOPNAME",
            hideInSearch: true,
            align: 'center',
            ellipsis: true,
        },
        {
            title: "消费类型",
            width: 120,
            dataIndex: "CONSUMPTIONRECORD_TYPE",
            align: 'center',
            ellipsis: true,
            valueType: 'select',
            valueEnum: {
                "0": "全部",
                ...CONSUMPTIONRECORDTYPEObj
            },
            initialValue: "0"
        },
        {
            title: "订单金额",
            width: 120,
            dataIndex: "CONSUME_AMOUNT",
            hideInSearch: true,
            align: 'center',
            valueType: 'digit',
            ellipsis: true,
        },
        {
            title: "消费金额",
            width: 120,
            dataIndex: "CONSUME_PRICE",
            hideInSearch: true,
            valueType: 'digit',
            align: 'center',
            ellipsis: true,
        },
        {
            title: "消费积分",
            width: 120,
            dataIndex: "CONSUME_SCORE",
            hideInSearch: true,
            align: 'center',
            valueType: 'digit',
            ellipsis: true,
        },
        {
            title: "实付金额",
            width: 120,
            dataIndex: "PAY_AMOUNT",
            hideInSearch: true,
            valueType: 'digit',
            align: 'center',
            ellipsis: true,
        },
        {
            title: "优惠金额",
            width: 120,
            dataIndex: "COUPON_AMOUNT",
            hideInSearch: true,
            align: 'center',
            ellipsis: true,
        },
        {
            title: "消费时间 ",
            width: 150,
            align: 'center',
            dataIndex: "CONSUMPTIONRECORD_DATE",
            hideInSearch: true,
            render: (_, record) => {
                return record?.CONSUMPTIONRECORD_DATE ? moment(record?.CONSUMPTIONRECORD_DATE).format('YYYY-MM-DD HH:mm:ss') : '-'
            }
        },
    ]


    return (
        <div ref={(el) => {
            // 打印报表
            if (!reqDetailList || reqDetailList.length === 0) return;
            setPrintOut(el);
        }} >

            <div style={{ backgroundColor: '#fff', display: 'flex' }}>
                <div style={{
                    width: '100%',
                    paddingTop: 0,
                    paddingBottom: 0,
                    paddingRight: 0
                }}>
                    <ProTable
                        actionRef={actionRef}
                        formRef={formRef}
                        columns={columns}
                        bordered
                        expandable={{
                            expandRowByClick: true
                        }}
                        headerTitle={<PageTitleBox props={props} />}
                        scroll={{ x: '100%', y: 'calc(100vh - 450px)' }}
                        search={{ span: 6 }}
                        request={async (params) => {
                            const req: any = {
                                SearchParameter: {
                                    PROVINCE_CODE: currentUser?.ProvinceCode || "",
                                    OWNERUNIT_ID: 911,
                                    CONSUMPTIONRECORD_TYPE: params?.CONSUMPTIONRECORD_TYPE === '0' ? '' : params?.CONSUMPTIONRECORD_TYPE
                                },
                                PageIndex: params?.current,
                                PageSize: 20
                            }
                            const data = await handleGetCONSUMPTIONRECORDList(req)
                            if (data.List && data.List.length > 0) {
                                return { data: data.List, success: true, total: data.TotalCount }
                            }
                            return { data: [], success: true }
                        }}
                        toolbar={{
                            actions: [

                            ]
                        }}
                    />
                </div>
            </div>
        </div>
    )
}

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