import React, { useState, useEffect } from 'react';
import { Modal, Form, Input, Button, Select, InputNumber } from 'antd';
import { getGoods } from '../services';

const CreateForm = (props) => {
  const { Option, OptGroup } = Select;
  const [goods, setGoods] = useState([]);
  //初始化数据
  useEffect(() => {
    const initGoods = () => {
      getGoods().then((data) => {
        let list = [];
        data.data.data.forEach((item) => {
          list.push({
            text: item.name,
            id: item.id,
          });
        });
        setGoods(list);
      });
    };
    initGoods();
  }, []);

  return (
    <Modal
      title={props.values.id ? '编辑' : '新建'}
      width={640}
      visible={props.createModalVisible}
      bodyStyle={{ padding: '32px 40px 48px' }}
      footer={null}
      onCancel={() => {
        props.onCancel();
      }}
      destroyOnClose
    >
      <Form
        layout="vertical"
        preserve={false}
        initialValues={props.values}
        onFinish={props.onFinish}
      >
        <Form.Item name="id" hidden={true}>
          <Input />
        </Form.Item>
        <Form.Item label={'商品'} name="goods_Id" rules={[{ required: true }]}>
          <Select>
            {goods.map((item, index) => {
              return (
                <Select.Option index={index} value={item.id} key={item.id}>
                  {item.text}
                </Select.Option>
              );
            })}
          </Select>
        </Form.Item>
        <Form.Item label={'价格'} name="price" rules={[{ required: true }]}>
          <InputNumber placeholder="价格" min={0} />
        </Form.Item>
        <Form.Item label={'状态'} name="goods_Enable" rules={[{ required: true }]}>
          <Select placeholder="状态">
            <Option value={1}>正常</Option>
            <Option value={0}>停用</Option>
          </Select>
        </Form.Item>
        <Form.Item>
          <Button type="primary" htmlType="submit">
            保存
          </Button>
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default CreateForm;
