/** @format */

import * as React from 'react';
import {ColumnProps} from 'antd/es/table';
import Result from '../../../../components/result';
import Table from '../../../../components/table';
import { useMerchantDataApiPriceList } from '../../../../service/accouting';
import { decodeBase64 } from '../../../../utils/base64';
import MerchantPriceEditModal from './merchantPriceEditModal';
import { numberFormats } from '../../../../utils/numberFormat';
interface RecordProps {
    index: number;
    id: number;
    status_code: number;
    data_api_name: string;
    query_type: string;
    combo_type: string;
    price: string;
    add_order_time: string;
}

const Index: React.FC = props => {
    const search = decodeBase64(location.search.slice(1));
    const merchantId = new URLSearchParams(search).get('merchant_id');
    const [state, requestData] = useMerchantDataApiPriceList({merchant_id:merchantId});
    const tableColumns = React.useMemo(() => {
        const Columns: ColumnProps<RecordProps>[] = [
            {
                title: '产品',
                dataIndex: 'merchant_data_api_name',
                key: 'merchant_data_api_name',
            },
            {
                title: '￥价格',
                dataIndex: 'unit_price',
                align:'right',
                render:(_,record:any)=>{
                    const { unit_price } =record
                    return <div>￥{numberFormats(unit_price)}</div>
                }
            },
            {
                title: '操作',
                render:(_,record:any)=>{
                    return <MerchantPriceEditModal record={record} refresh={refresh}/>
                }
            },
        ];
        return Columns;
    }, []);

    const refresh = () =>{
        requestData({merchant_id:merchantId})
    }
    return (
        <Result error={state.error} onRetry={() => requestData({merchant_id:merchantId})}>
            <Table
                pagination={false}
                onChange={()=>{}}
                rowKey="merchant_data_api_id"
                columns={tableColumns}
                loading={state.loading}
                dataSource={state.dataSource}
            />
        </Result>
    );
};
export default Index;
