import React, {
    useState, useEffect, useContext
} from 'react';
import { Form, Col, DatePicker, Button, message, Space, Input } from 'antd';
import apiObj from '@api/index';
import BlockContext from './problem-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 { problemDetail, type } = useContext(BlockContext);
    const initialValues = problemDetail;
    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) {
            dataCheck(values)
        } else {
            editProblem(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 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 editProblem(values) {
        let params = {
            id: problemDetail.id,
            description: values.description,
        }
        post(api.updateColumnResult, params).then((res) => {
            if (res.data.code === 200) {
                props.userChange()
                message.success(res.data.message);
                form.resetFields();
            } else {
                message.error(res.data.message);
            }
        })
    }
    //数据对比
    function dataCheck(values) {
        let params = {
            behospitalCode: values.behospitalCode,
            dateEnd: values.endDate,
            dateStart: values.startDate,
            isPlacefile: localStorage.getItem('isPlacefile')
        }
        post(api.dataCheck, 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 cancel() {
        props.cancel()
    }
    return (
        <>
            <Form
                labelCol={type == 3 ? { span: 16 } : { span: 4 }}
                wrapperCol={type == 3 ? { span: 12 } : { span: 16 }}
                form={form}
                name="register"
                layout={type == 3 ? 'inline' : 'horizontal'}
                onFinish={onFinish}
                initialValues={initialValues}
                className='problem'
            >
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="behospitalCode"
                        label="住院序号"
                        labelAlign="right"
                    >
                        <span>{initialValues.behospitalCode}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="hosptialDatatmpCode"
                        label="文书编号"
                    >
                        <span>{initialValues.hosptialDatatmpCode}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="hosptialDatatmpName"
                        label="文书标题"
                    >
                        <span>{initialValues.hosptialDatatmpName}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="modeName"
                        label="质控模块名称"
                    >
                        <span>{initialValues.modeName}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="tableCname"
                        label="表名称（中文）"
                    >
                        <span>{initialValues.tableCname}</span>

                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="tableEname"
                        label="表名称（英文）"
                    >
                        <span>{initialValues.tableEname}</span>

                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="columnCname"
                        label="字段名称（中文）"
                    >
                        <span>{initialValues.columnCname}</span>

                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="columnEname"
                        label="字段名称（英文）"
                    >
                        <span>{initialValues.columnEname}</span>

                    </Form.Item>
                </Col>
                <Col span={24} hidden={type != 3}>
                    <Form.Item
                        name="tableVal"
                        label="上传字段值"
                    >
                        <span>{initialValues.tableVal ? initialValues.tableVal.length > 15 ? <span title={initialValues.tableVal}>{initialValues.tableVal.substring(0, 15) + '...'}</span> : initialValues.tableVal : '-'}</span>
                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="type"
                        label="问题类型"
                    >
                        <span>{initialValues.type == 1 ? "数据缺失" : initialValues.type == 2 ? "非标准值" : "正则校验失败"}</span>

                    </Form.Item>
                </Col>
                <Col span={12} hidden={type != 3}>
                    <Form.Item
                        name="status"
                        label="状态"
                    >
                        <span>{initialValues.isSolved == 1 ? "已处理" : "未处理"}</span>

                    </Form.Item>
                </Col>
                <Col span={24} hidden={type != 3}>
                    <Form.Item
                        name="description"
                        label="备注"
                        wrapperCol={{ span: 18 }}
                        rules={[{ max: 200, message: '备注不能超过200个字符' }]}
                    >
                        <TextArea
                            autoSize={{ minRows: 5, maxRows: 5 }}
                        />
                    </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>
                        : ''
                }
                <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 == 1}>
                            {type == 1 ? '确定' : '保存'}
                        </Button>
                    </Space>
                </Form.Item>

            </Form>
        </>
    );
}

export default EditBlock;