import React, {
    useState, useEffect, useContext
} from 'react';
import { Form, Col, DatePicker, Button, Radio, message, Space, Input } from 'antd';
import apiObj from '@api/index';
import BlockContext from './block-context';
import moment from "moment";
import "moment/locale/zh-cn"
import { disabledDate, getDaysBetween, getValueFromEvent } from '@utils/index'
const { post, api, xPost } = apiObj;
const { TextArea } = Input;
function EditBlock(props) {
    useEffect(() => {
    }, []);
    const [form] = Form.useForm();
    const [disable, setDisable] = useState(true);
    const [limit, setLimit] = useState(false);
    const { blockDetail, type } = useContext(BlockContext);
    const initialValues = blockDetail;
    const onFinish = values => {
        values.startDate = values.startDate ? moment(values.startDate).format('YYYY-MM-DD 00:00:00') : null;
        values.endDate = values.endDate ? moment(values.endDate).format('YYYY-MM-DD 23:59:59') : null;
        if (values.startDate > values.endDate) {
            message.warning('开始时间不能大于结束时间');
            return
        } else if (!limit && getDaysBetween(values.startDate, values.endDate) > 29) {
            message.warning('开始时间与结束时间相差不能超过30天');
            return
        }
        if (type == 1) {
            dataRepari(values)
        } else if (type == 2) {
            dataCompare(values)
        } else {
            editBlock(values)
        }
    };
    function change(e) {
        const formData = form.getFieldsValue();
        const { value } = e.target;
        if (value) {
            setLimit(true)
        } else {
            setLimit(false)
        }
        if (formData.behospitalCode || (formData.startDate != undefined && formData.endDate != undefined)) {
            setDisable(false)
        } else {
            setDisable(true)
        }
    }
    function editBlock(values) {
        let params = {
            id: blockDetail.id,
            isAudited: values.isAudited,
            lossCause: values.lossCause
        }
        post(api.upBlockLossById, params).then((res) => {
            if (res.data.code === 200) {
                props.userChange(2)
                message.success(res.data.message);
                form.resetFields();
            } else {
                message.error(res.data.message);
            }
        })
    }
    function onStartChange(val) {
        const formData = form.getFieldsValue();
        if (formData.behospitalCode || (formData.startDate != undefined && formData.endDate != undefined)) {
            setDisable(false)
        } else {
            setDisable(true)
        }
    }
    function onEndChange(val) {
        const formData = form.getFieldsValue();
        if (formData.behospitalCode || (formData.startDate != undefined && formData.endDate != undefined)) {
            setDisable(false)
        } else {
            setDisable(true)
        }
    }
    //数据补录
    function dataRepari(values) {
        let params = {
            behospitalCode: values.behospitalCode,
            endDate: values.endDate,
            startDate: values.startDate,
            isPlacefile: localStorage.getItem('isPlacefile')
        }
        post(api.dataRepari, params).then((res) => {
            if (res.data.code === 200) {
                props.userChange(1)
                message.success(res.data.message);
                form.resetFields();
            } else {
                message.error(res.data.message);
            }
        })
    }
    //数据对比
    function dataCompare(values) {
        let params = {
            behospitalCode: values.behospitalCode,
            endDate: values.endDate,
            startDate: values.startDate,
        }
        post(api.dataCompare, params).then((res) => {
            if (res.data.code === 200) {
                props.userChange(2)
                message.success(res.data.message);
                form.resetFields();
            } else {
                message.error(res.data.message);
            }
        })
    }
    function cancel() {
        props.cancel()
    }
    return (
        <>
            <Form
                labelCol={type == 3 ? { span: 8 } : { span: 4 }}
                wrapperCol={{ span: 16 }}
                form={form}
                name="register"
                layout={type == 3 ? 'inline' : 'horizontal'}
                onFinish={onFinish}
                initialValues={initialValues}
                className='block'
            >
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="behospitalCode"
                        label="住院序号"
                    >
                        <span>{initialValues.behospitalCode}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="recId"
                        label="文书编号"
                    >
                        <span>{initialValues.recId}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="recTitle"
                        label="文书标题"
                    >
                        <span>{initialValues.recTitle}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="lossType"
                        label="丢失类型"
                    >
                        <span>{initialValues.lossType == 0 ? '文书丢失' : initialValues.lossType == 1 ? '病案首页丢失' : initialValues.lossType == 2 ? '患者信息丢失' : ''}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="lossWay"
                        label="丢失途径"
                    >
                        <span>{initialValues.lossWay == 0 ? '外部丢失' : initialValues.lossWay == 1 ? '内部丢失' : ''}</span>

                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="status"
                        label="状态"
                    >
                        <span>{initialValues.status == 0 ? '已丢失' : initialValues.status == 1 ? '已恢复' : ''}</span>

                    </Form.Item>
                </Col>
                <Col span={24} hidden={type != 3}>
                    <Form.Item
                        name="isAudited"
                        label="核查结果"
                        wrapperCol={{ span: 18 }}
                    >
                        <Radio.Group>
                            <Space size={70} >
                                <Radio value={'1'}>核查通过</Radio>
                                <Radio value={'0'}>核查未通过</Radio>
                            </Space>
                        </Radio.Group>
                    </Form.Item>
                </Col>
                <Col span={24} hidden={type != 3}>
                    <Form.Item
                        name="lossCause"
                        label="丢失原因"
                        wrapperCol={{ span: 18 }}
                        rules={[{ max: 200, message: '丢失原因不能超过200个字符' }]}
                    >
                        <TextArea
                            autoSize={{ minRows: 5, maxRows: 5 }}
                            placeholder="请输入"
                        />
                    </Form.Item>
                </Col>


                <Col span={24} hidden={type == 3}>
                    {type != 3 ?
                        <Form.Item label="日期" >
                            <Form.Item name="startDate" className='times'>
                                <DatePicker
                                    disabledDate={disabledDate}
                                    placeholder="请选择开始日期"
                                    onChange={onStartChange}
                                />

                            </Form.Item>
                            <span style={{ margin: '0 5px', position: 'relative', top: '2px' }}>-</span>
                            <Form.Item name="endDate" className='times'>
                                <DatePicker
                                    disabledDate={disabledDate}
                                    placeholder="请选择结束日期"
                                    onChange={onEndChange}
                                />
                            </Form.Item>
                        </Form.Item>
                        : ''}
                </Col>
                <Col span={24} hidden={type == 3} style={{ marginTop: 15 }}>
                    <Form.Item label="住院序号" name="behospitalCode" rules={[{ max: 30, message: '住院序号不能超过30个字符' }]} getValueFromEvent={getValueFromEvent}>
                        <Input placeholder="请输入" autoComplete='off' onChange={change} />
                    </Form.Item>
                </Col>
                {
                    type == 1 ?
                        <div style={{ color: '#1690FF' }}>
                            提示：<br />
                            1、数据补录时间较长，请耐心等待。<br />
                            2、当输入住院序号时，补录将不受时间范围限制。<br />
                        </div>
                        :
                        type == 2 ?
                            <div style={{ color: '#1690FF' }}>
                                提示：当输入住院序号时，对比将不受时间范围限制。
                            </div>
                            : ''
                }
                <Form.Item wrapperCol={type == 3 ? { offset: 24, span: 16 } : { offset: 8, span: 16 }} style={{ marginTop: 15 }}>
                    <Space size="middle" >
                        <Button htmlType="button" onClick={e => cancel()}>
                            关闭
                        </Button>
                        <Button type="primary" htmlType="submit" disabled={disable && type != 3}>
                            {type == 3 ? '保存' : '确定'}
                        </Button>
                    </Space>
                </Form.Item>

            </Form>
        </>
    );
}

export default EditBlock;