import * as React from 'react';
import ModalForm from '../../../../components/modalForm';
import { Form, notification, Radio } from 'antd';
import { FormComponentProps } from 'antd/es/form';

import { useUpdateOrderStatus } from '../../../../service/merchantOrder';
const formItemLayout = {
    labelCol: {
        sm: { span: 6 },
    },
    wrapperCol: {
        sm: { span: 18 },
    },
};

interface UpdateStatusProps {
    record: any,
    refresh: () => void
}


const UpdateOrderPriceModalForm: React.FC<FormComponentProps & UpdateStatusProps> = props => {
    const [visible, setVisible] = React.useState(false)
    const { record, refresh, form } = props;
    const { order_no } = record
    const [state, updateStatus] = useUpdateOrderStatus()
    const { getFieldDecorator } = form;


    const handleSubmit = (e: any) => {
        e.preventDefault();
        form.validateFieldsAndScroll((err, values) => {
            const { status_code } = values
            updateStatus({ order_no, is_pass: status_code }).then((error) => {
                if (error) {
                    return notification.error({
                        message: '更新状态失败',
                        description: error,
                    })
                }
                setVisible(false)
                refresh()
            })
        });
    };
    return (
        <>
            <a onClick={() => { setVisible(true) }}>审核</a>
            <ModalForm
                visible={visible}
                onCancel={() => setVisible(false)}
                onOk={handleSubmit}
                loading={state.loading}
            >
                <Form {...formItemLayout}>
                    <Form.Item label='订单状态'>
                        {getFieldDecorator('status_code', {
                        })(<Radio.Group>
                            <Radio value={1}>
                                审核通过
                            </Radio>
                            <Radio value={2}>拒绝订单,审核不通过</Radio>
                        </Radio.Group>)}
                    </Form.Item>
                </Form>
            </ModalForm>
        </>
    );
};
export default Form.create<any>()(UpdateOrderPriceModalForm);
