import React, {
   useContext
} from 'react';
import {  Form, Input, Button, Switch, message, Space } from 'antd';
import apiObj from '@api/index';
import InpaContext from './inpa-context';
const { post, api } = apiObj;
function AddUser(props) {
  const [form] = Form.useForm();
  const { id, type, formData } = useContext(InpaContext);
  
  console.log(formData)
  const initialValues = formData
  function swichChange(val) {
    form.setFieldsValue({ status: val ? 1 : 0 })
  }
  const onFinish = values => {
    let params = values
    if (type == 2) {
      params.id = id
      editRegion(params)
    } else {
      addRegion(params)
    }

  };
  function addRegion(param) {
    post(api.addRegion, param).then((res) => {
      if (res.data.code === 200) {
        props.userChange()
        message.success(res.data.message);
        form.resetFields();
      } else {
        message.error(res.data.message);
      }
    })
  }
  function editRegion(param) {
    post(api.updateRegion, param).then((res) => {
      if (res.data.code === 200) {
        props.userChange()
        message.success(res.data.message);
        form.resetFields();
      } else {
        message.error(res.data.message);
      }
    })
  }
  function cancel() {
    props.userChange()
  }
  return (
    <>
      <Form
        labelCol={{ span: 7 }}
        wrapperCol={{ span: 12 }}
        form={form}
        name="register"
        onFinish={onFinish}
        initialValues={initialValues}
      >
        <Form.Item
          name="hospitalName"
          label="上级组织"
          required
        >
          <Input disabled />
        </Form.Item>
        <Form.Item
          name="name"
          label="病区名称"
          rules={[
            {
              required: true,
              message: '请输入病区名称',
            },
          ]}
        >
          <Input placeholder="请输入病区名称" autoComplete="off"/>
        </Form.Item>
        <Form.Item
          name="code"
          label="病区编码"
        >
          <Input placeholder="请输入病区编码" autoComplete="off"/>
        </Form.Item>
        <Form.Item
          name="liaison"
          label="联系人"
        >
          <Input placeholder="请输入姓名" autoComplete="off"/>
        </Form.Item>
        <Form.Item
          name="phoneNumber"
          label="手机号码"
        >
          <Input placeholder="请输入手机号码" autoComplete="off"/>
        </Form.Item>
        <Form.Item
          name="station"
          label="类型"
          required
        >
          <Input disabled/>
        </Form.Item>
       <Form.Item
          name="orderNo"
          label="排序"
        >
          <Input placeholder="大于0的整数" autoComplete="off"/>
        </Form.Item>
        <Form.Item
          name="status"
          valuePropName="checked"
          label="当前状态"
          required
        >
          <Switch onChange={swichChange} />
        </Form.Item>
        {type == 3 ?
          <Form.Item wrapperCol={{ offset: 10, span: 16 }}>
            <Space size="middle">
              <Button type="primary" htmlType="submit">
                返回
            </Button>
            </Space>
          </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 AddUser;