import React, {
  useState, useEffect, useContext
} from 'react';
import { Form, Input, Select, Button, Switch, TreeSelect, message, Space } from 'antd';
import apiObj from '@api/index';
import { getCookie,setCookie } from '@utils/index';
import DiagContext from './Diag-context';
import { useSelector } from 'react-redux'
import Item from 'antd/lib/list/Item';
const { post, api } = apiObj;
const { TextArea } = Input;

function AddDiag(props) {
  
  const [form] = Form.useForm();
  const { type, formData} = useContext(DiagContext);
  const staticInfo = useSelector(state => {
    return state.staticInfo;
  });
  const initialValues = formData;

  const onFinish = values => {
	  values.status ? values.status=1:values.status=0
    let params = values
    if (type == 3) {
	  params.id=initialValues.id
      upDiseaseById(params)
    } else {
      addDisease(params)
    }

  };
  function addDisease(param) {
    const hisId = getCookie("hospitalId");
	const params ={
		hospitalId:hisId,
		...param
	}
	post(api.addDisease, params).then((res) => {
      if (res.data.code === 200) {
        props.DiagChange()
        message.success(res.data.message);
      } else {
        message.error(res.data.message);
      }
    })
  }
  function upDiseaseById(param) {
	  const hisId = getCookie("hospitalId");
	  const params ={
	  	hospitalId:hisId,
	  	...param
	  }
    post(api.upDiseaseById, params).then((res) => {
      if (res.data.code === 200) {
        props.DiagChange()
        message.success(res.data.message);
      } else {
        message.error(res.data.message);
      }
    })
  }
  function cancel() {
  	props.cancel()
  }
  
  function onValuesChange() {
  	props.isChange(form.isFieldsTouched())
  }

  return (
    <>
      <Form
        labelCol={{ span: 6 }}
        wrapperCol={{ span: 16 }}
        form={form}
        name="register"
        onFinish={onFinish}
        initialValues={initialValues}
		onValuesChange={onValuesChange}
      >

        <Form.Item
          name="name"
          label="医院诊断名称"
          rules={[
            {
              required: true,
              message: '请输入医院诊断名称',
            },
          ]}
        >
          {type == 3 ?
		    <Input  autoComplete='off'/>
            :
            <Input placeholder="请输入医院诊断名称" autoComplete='off'/>
          }

        </Form.Item>
		<Form.Item
		  name="code"
		  label="code"
		  rules={[
		    {
		      required: true,
		      message: '请输入code',
		    },
		  ]}
		>
		  {type == 3 ?
		    <Input  autoComplete='off'/>
		    :
		    <Input placeholder="请输入code" autoComplete='off'/>
		  }
		
		</Form.Item>
        <Form.Item
          name="icd10"
          label="ICD-10编码："
        >
          {type == 3 ?
            <Input  autoComplete='off'/>
            :
            <Input placeholder="请输入ICD-10编码：" autoComplete='off'/>
          }

        </Form.Item>
        
          <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
            <Space size="middle">
              <Button htmlType="button" onClick={e => cancel()}>
                取消
            </Button>
              <Button type="primary" htmlType="submit">
                保存
            </Button>
            </Space>
          </Form.Item>

      </Form>
    </>
  );
}

export default AddDiag;