import { getCorpAllListApi } from "@/API/global";
import { useAjax } from "@/Hook/useAjax";
import { getCorpDuplicateFanOverviewListApi, GetProps } from "@/pages/weComTask/API/heavyPowderMAS";
import { Card, Col, Flex, Row, Space, Statistic, Table, Typography } from "antd";
import { useEffect, useState } from "react";
import SelectCorp from "./SelectCorp";
import { Columns1 } from "./tableConfig";
import RepeatAnalyze from "./repeatAnalyze";
import RepeatOfCorpUser from "./repeatOfCorpUser";
import UserDetails from "./userDetails";

const { Title } = Typography;

/**
 * 多主体重粉数据
 * @returns 
 */
const HeavyPowderMAS: React.FC = () => {

    /*********************************************/
    const [queryParmas, setQueryParmas] = useState<GetProps>()
    const [filtered, setFiltered] = useState<{ label: string, value: string }[]>([]);

    const getCorpDuplicateFanOverviewList = useAjax((params) => getCorpDuplicateFanOverviewListApi(params))
    const getCorpAllList = useAjax((params) => getCorpAllListApi(params))
    /*********************************************/

    useEffect(() => {
        getCorpAllList.run({}).then(res => {
            const corpList = res?.data?.map((item, index) => {
                if (index === 0) {
                    setQueryParmas({ aCorpIds: [item.corpId], bCorpIds: [item.corpId] })
                }
                return { label: item.corpName, value: item.corpId }
            })
            setFiltered(corpList)
        })
    }, [])

    useEffect(() => {
        if (queryParmas?.aCorpIds.length > 0 && queryParmas?.bCorpIds.length > 0) {
            getCorpDuplicateFanOverviewList.run(queryParmas)
        }
    }, [queryParmas])

    return <Flex vertical gap={10}>
        <Card
            variant="borderless"
        >
            <Flex gap={30}>
                <Space>
                    <Title level={5} style={{ margin: 0 }}>查询组主体：</Title>
                    <SelectCorp
                        corpList={filtered}
                        value={queryParmas?.aCorpIds}
                        onChange={(v) => {
                            setQueryParmas({ ...queryParmas, aCorpIds: v })
                        }}
                    />
                </Space>
                <Space>
                    <Title level={5} style={{ margin: 0 }}>对照组主体：</Title>
                    <SelectCorp
                        corpList={filtered}
                        value={queryParmas?.bCorpIds}
                        onChange={(v) => {
                            setQueryParmas({ ...queryParmas, bCorpIds: v })
                        }}
                    />
                </Space>
            </Flex>
        </Card>
        <Card
            variant="borderless"
            title={<strong>主体详细</strong>}
        >
            <Row gutter={16}>
                <Col span={12}>
                    <Title level={5} style={{ margin: 0, marginBottom: 10 }}>查询组数据</Title>
                    <Table
                        columns={Columns1()}
                        dataSource={getCorpDuplicateFanOverviewList?.data?.data?.aData}
                        loading={getCorpDuplicateFanOverviewList.loading}
                        bordered
                        scroll={{ y: 300 }}
                        rowKey={'corp_id'}
                        summary={() => {
                            return (
                                <Table.Summary fixed='top'><Table.Summary.Row>
                                    <Table.Summary.Cell index={0} align="center">
                                        <Title level={5} style={{ margin: 0, marginBottom: 0 }}>总计</Title>
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={1} align="center">--</Table.Summary.Cell>
                                    <Table.Summary.Cell index={2} align="center">
                                        <Statistic value={getCorpDuplicateFanOverviewList?.data?.data?.aTotalData?.total_num || 0} valueStyle={{ fontSize: 12, fontWeight: 'bold' }} />
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={3} align="center">
                                        <Statistic value={getCorpDuplicateFanOverviewList?.data?.data?.aTotalData?.only_num || 0} valueStyle={{ fontSize: 12, fontWeight: 'bold' }} />
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={4} align="center">
                                        <Statistic value={getCorpDuplicateFanOverviewList?.data?.data?.aTotalData?.qc_null_num || 0} valueStyle={{ fontSize: 12, fontWeight: 'bold' }} />
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={5} align="center">
                                        <Statistic
                                            value={getCorpDuplicateFanOverviewList?.data?.data?.aTotalData?.only_rate ? getCorpDuplicateFanOverviewList?.data?.data?.aTotalData?.only_rate * 100 : 0}
                                            valueStyle={getCorpDuplicateFanOverviewList?.data?.data?.aTotalData?.only_rate < 0.8 ? { color: '#cf1322', fontSize: 12, fontWeight: 'bold' } : { color: '#3f8600', fontSize: 12, fontWeight: 'bold' }}
                                            suffix="%"
                                            precision={2}
                                        />
                                    </Table.Summary.Cell>
                                </Table.Summary.Row></Table.Summary>
                            )
                        }}
                    />
                </Col>
                <Col span={12}>
                    <Title level={5} style={{ margin: 0, marginBottom: 10 }}>对照组数据</Title>
                    <Table
                        columns={Columns1()}
                        dataSource={getCorpDuplicateFanOverviewList?.data?.data?.bData}
                        loading={getCorpDuplicateFanOverviewList.loading}
                        bordered
                        scroll={{ y: 300 }}
                        rowKey={'corp_id'}
                        summary={() => {
                            return (
                                <Table.Summary fixed='top'><Table.Summary.Row>
                                    <Table.Summary.Cell index={0} align="center">
                                        <Title level={5} style={{ margin: 0, marginBottom: 0 }}>总计</Title>
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={1} align="center">--</Table.Summary.Cell>
                                    <Table.Summary.Cell index={2} align="center">
                                        <Statistic value={getCorpDuplicateFanOverviewList?.data?.data?.bTotalData?.total_num || 0} valueStyle={{ fontSize: 12, fontWeight: 'bold' }} />
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={3} align="center">
                                        <Statistic value={getCorpDuplicateFanOverviewList?.data?.data?.bTotalData?.only_num || 0} valueStyle={{ fontSize: 12, fontWeight: 'bold' }} />
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={4} align="center">
                                        <Statistic value={getCorpDuplicateFanOverviewList?.data?.data?.bTotalData?.qc_null_num || 0} valueStyle={{ fontSize: 12, fontWeight: 'bold' }} />
                                    </Table.Summary.Cell>
                                    <Table.Summary.Cell index={5} align="center">
                                        <Statistic
                                            value={getCorpDuplicateFanOverviewList?.data?.data?.bTotalData?.only_rate ? getCorpDuplicateFanOverviewList?.data?.data?.bTotalData?.only_rate * 100 : 0}
                                            valueStyle={getCorpDuplicateFanOverviewList?.data?.data?.bTotalData?.only_rate < 0.8 ? { color: '#cf1322', fontSize: 12, fontWeight: 'bold' } : { color: '#3f8600', fontSize: 12, fontWeight: 'bold' }}
                                            suffix="%"
                                            precision={2}
                                        />
                                    </Table.Summary.Cell>
                                </Table.Summary.Row></Table.Summary>
                            )
                        }}
                    />
                </Col>
            </Row>
        </Card>
        <Row gutter={10}>
            <Col span={12}>
                <RepeatAnalyze queryParmas={queryParmas} />
            </Col>
            <Col span={12}>
                <RepeatOfCorpUser queryParmas={queryParmas} />
            </Col>
        </Row>
        <UserDetails queryParmas={queryParmas} />
    </Flex>
}

export default HeavyPowderMAS;