import React, { useReducer, useEffect } from 'react';
import { MyContext } from '../../utils/contextManage';
import { ConfigProvider } from 'antd';
import ErrorBoundary from "../../components/ErrorBoundary";
import enUS from 'antd/es/locale/en_US';
import zhCN from 'antd/es/locale/zh_CN';
import PageContent from "./PageContent";
// @ts-ignore
import sqlUtil from '../../utils/sqlUtil'
import { ActiveTableProps } from '../../index.d'


const initState = {
    lang: 'zh-CN',
    getEntity: null
};
const reducer = (state: any, action: { type: string, value: any }) => {
    console.log('action', action)
    switch (action.type) {
        case 'setEntityModel':
            return Object.assign({}, state, { entityModel: action.value });
        case 'setRefTableEntityModel':
            return Object.assign({}, state, { refTableEntityModel: action.value });
        case 'setLang':
            return Object.assign({}, state, { lang: action.value });
        case 'setCudEntity':
            return Object.assign({}, state, { cudEntity: action.value });
        case 'setGetEntity':
            return Object.assign({}, state, { getEntity: action.value });
        case 'setGetEntityList':
            return Object.assign({}, state, { getEntityList: action.value });
        case 'setAdreflist':
            return Object.assign({}, state, { adreflist: action.value });
        case 'setAdureflist':
            return Object.assign({}, state, { adureflist: action.value });
        case 'setGetEntityListAsPage':
            return Object.assign({}, state, { getEntityListAsPage: action.value });
        case 'setCustomerFields':
            return Object.assign({}, state, { customerFields: action.value });
        default:
            return state;
    }
}


const OpreateTable: React.FC<ActiveTableProps> = (props) => {
    const {
        lang,
        entityModel = 'com.glory.framework.activeentity.model.ADTable',
        refTableEntityModel = 'com.glory.framework.activeentity.model.ADRefTable',
        cudEntity,
        getEntity,
        getEntityCount,
        getEntityList,
        adreflist,
        adureflist,
        customerFields = {}
    } = props
    const [state, dispatch] = useReducer(reducer, initState);
    useEffect(() => {
        dispatch({
            type: 'setCudEntity',
            value: cudEntity
        })
        dispatch({
            type: 'setGetEntity',
            value: getEntity
        })
        dispatch({
            type: 'setGetEntityList',
            value: getEntityList
        })
        dispatch({
            type: 'setAdreflist',
            value: adreflist
        })
        dispatch({
            type: 'setAdureflist',
            value: adureflist
        })
        dispatch({
            type: 'setLang',
            value: lang
        })
        dispatch({
            type: 'setEntityModel',
            value: entityModel
        })
        dispatch({
            type: 'setRefTableEntityModel',
            value: refTableEntityModel
        })
        dispatch({
            type: 'setCustomerFields',
            value: customerFields
        })
        const getEntityListAsPage = function (x: any) {
            const page = x.page || 1;
            const pageSize = x.pageSize || 20;
            const params = {
                ENTITYMODEL: x.ENTITYMODEL,
                FIELDS: x.FIELDS,
                WHERECLAUSE: x.WHERECLAUSE,
                ORDERBYCLAUSE: x.ORDERBYCLAUSE,
                FIRST: ((page || 1) - 1) * pageSize,
                MAX: pageSize || 20
            }
            const countParams = {
                ENTITYMODEL: x.ENTITYMODEL,
                FIELDS: x.FIELDS,
                WHERECLAUSE: x.WHERECLAUSE
            }
            if (x.queryParams) {
                const sqlwhere = sqlUtil.convertSqlWhere(x.queryParams)
                if (sqlwhere) {
                    params.WHERECLAUSE = params.WHERECLAUSE ? params.WHERECLAUSE + ' and ' + sqlwhere : sqlwhere
                    countParams.WHERECLAUSE = countParams.WHERECLAUSE ? countParams.WHERECLAUSE + ' and ' + sqlwhere : sqlwhere
                }
            }
            return new Promise(resolve => {
                getEntityCount(countParams)
                    .then((res: any) => {
                        if (res.Header.RESULT === 'SUCCESS') {
                            getEntityList(params)
                                .then((res1: any) => {
                                    if (res1.Header.RESULT === 'SUCCESS') {
                                        const dataPage = {
                                            total: res.Body.DATACOUNT,
                                            list: [...res1.Body.DATALIST]
                                        }
                                        resolve(dataPage)
                                    } else {
                                        resolve({
                                            total: 0,
                                            list: []
                                        })
                                    }
                                })
                        } else {
                            resolve({
                                total: 0,
                                list: []
                            })
                        }
                    })
            })
        }

        dispatch({
            type: 'setGetEntityListAsPage',
            value: getEntityListAsPage
        })
    }, [])
    return (
        <MyContext.Provider value={{ state: state }}>
            <ConfigProvider locale={lang === 'zh-CN' ? zhCN : enUS}>
                <ErrorBoundary>
                    <PageContent {...props} />
                </ErrorBoundary>
            </ConfigProvider>
        </MyContext.Provider>
    )
}

export default OpreateTable;