import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { Form } from 'antd';
import { useAntdTable } from 'ahooks';
import { VtxDatagrid, VtxPageLayout, VtxSearch, VtxDatePicker, VtxSelect } from '@vtx/components';
import { getFillPage } from './api';
import VtxGasStaffModal from '../vtx-gas-staff-modal';
import useSettings from '../vm-hooks/useSettings';

const { Option } = VtxSelect;
const { VtxRangePicker } = VtxDatePicker;
const { TableLayout } = VtxPageLayout;

const FillingInfo = props => {
    const { id, bottleOperateTypeList } = props;
    const [form] = Form.useForm();
    const [dataSearch, setDataSearch] = useState({});
    const { refresh, tableProps, search } = useAntdTable(getFillPage(id, dataSearch), {
        defaultPageSize: 10,
        form,
    });
    const { submit } = search;
    // 表格列
    const columns = [
        {
            title: '气瓶条码号',
            dataIndex: 'bottleNo',
        },
        {
            title: '操作类型',
            dataIndex: 'operateTypeValue',
        },
        {
            title: '操作人员',
            dataIndex: 'operateUserName',
            render: (text, record) => {
                const { operateUserName, operateUserId } = record;
                return <VtxGasStaffModal id={operateUserId} label={operateUserName} />;
            },
        },
        {
            title: '操作时间',
            dataIndex: 'operateTime',
        },
        {
            title: '详细内容',
            dataIndex: 'content',
        },
    ];
    const { current, pageSize } = tableProps?.pagination;
    const datagridProps = {
        ...tableProps,
        columns,
        startIndex: (current - 1) * pageSize + 1,
        onRefresh: refresh,
        toolbar: false,
    };
    const { theme } = useSettings();
    return (
        <TableLayout.Page>
            <TableLayout.Search>
                <Form form={form} name="query-form">
                    <VtxSearch
                        titles={['日期', '操作类型']}
                        gridWeight={[2, 1]}
                        isConfirmButton={false}
                        isClearButton={false}
                    >
                        <Form.Item name="data">
                            <VtxRangePicker
                                dropdownClassName={theme}
                                onChange={(data, dataStr) => {
                                    setDataSearch({
                                        ...dataSearch,
                                        startTime: dataStr[0],
                                        endTime: dataStr[1],
                                    });
                                    submit();
                                }}
                            />
                        </Form.Item>
                        <Form.Item name="operateType">
                            <VtxSelect
                                placeholder="请选择"
                                onChange={submit}
                                dropdownClassName={theme}
                            >
                                {bottleOperateTypeList.map(item => (
                                    <Option key={item.id}>{item.name}</Option>
                                ))}
                            </VtxSelect>
                        </Form.Item>
                    </VtxSearch>
                </Form>
            </TableLayout.Search>
            <TableLayout.Content>
                <TableLayout.Table>
                    <VtxDatagrid style={{ paddingTop: '10px' }} {...datagridProps} />
                </TableLayout.Table>
            </TableLayout.Content>
        </TableLayout.Page>
    );
};

FillingInfo.propTypes = {
    id: PropTypes.string,
    bottleOperateTypeList: PropTypes.array,
};

export default FillingInfo;
