import React, { useContext} from 'react';
import { Form, Input, Button, message, Space } from 'antd';
import apiObj from '@api/index';
import { getCookie,setCookie } from '@utils/index';
import SurgContext from './surg-context';
const { post, api } = apiObj;

function AddSurg(props) {
  const [form] = Form.useForm();
  const { type, formData} = useContext(SurgContext);
  const initialValues = formData;

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

  };
  function addSurg(param) {
    const hisId = getCookie("hospitalId");
	const params ={
		hospitalId:hisId,
		...param
	}
	post(api.addOperation, params).then((res) => {
      if (res.data.code === 200) {
        props.SurgChange()
        message.success(res.data.message);
      } else {
        message.error(res.data.message);
      }
    })
  }
  function upSurgById(param) {
	  const hisId = getCookie("hospitalId");
	  const params ={
	  	hospitalId:hisId,
	  	...param
	  }
    post(api.upOperationById, params).then((res) => {
      if (res.data.code === 200) {
        props.SurgChange()
        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: 7 }}
        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="手术和操作代码"
		  rules={[
		    {
		      required: true,
		      message: '请输入手术和操作代码',
		    },
		  ]}
		>
		  {type == 3 ?
		    <Input  autoComplete='off'/>
		    :
		    <Input placeholder="请输入" 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 AddSurg;