import React from 'react';
import { Button, Col, Form, Input, Modal, Row } from 'antd';

const FormItem = Form.Item;

const formItemLayout = {
  colon: false,
  labelCol: {
    span: 6,
  },
  wrapperCol: {
    span: 16,
  },
  style: {
    // marginBottom: 20,
  },
};

const ColProps = {
  xs: 24,
  sm: 24,
  md: 12,
  xl: 12,
  style: {},
};

const modal =
  ({
     mode,
     domain,
     readOnly,
     record,
     reminders,
     onOk,
     onCancel,
     dispatch,
     loading,
     form: {
       getFieldDecorator,
       validateFields,
       getFieldsValue,
     },
     ...modalProps
   }) => {
    const items = [{
      title: '提醒种别',
      dataIndex: 'remindClass',
      options: {
        initialValue: '',
        rules: [{
            required: true,
            message: '必填',
        }],
      },
      tag: <Input disabled={readOnly} />,
    }, {
      title: '提醒类型',
      dataIndex: 'remindType',
      options: {
        initialValue: '',
        rules: [{
            required: true,
            message: '必填',
        }],
      },
      tag: <Input disabled={mode != 'create'} />,
    }, {
      title: '提醒天数1',
      dataIndex: 'remindDays1',
      options: {
        rules: [{
            required: true,
            message: '必填',
        }],
      },
      tag: <Input disabled={readOnly} />,
    }, {
      title: '提醒天数2',
      dataIndex: 'remindDays2',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '提醒天数3',
      dataIndex: 'remindDays3',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '提醒天数4',
      dataIndex: 'remindDays4',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '提醒天数5',
      dataIndex: 'remindDays5',
      tag: <Input disabled={readOnly} />,
    }];

    const handleOk = () => {
      validateFields((errors) => {
        if (errors) {
          return;
        }
        const data = {
          ...getFieldsValue(),
        };
        onOk(data);
      });
    };

    const handleCancel = () => {
      onCancel();
    };

    const modalOpts = {
      ...modalProps,
      onCancel: handleCancel,
      footer: [
        <Button key="back" onClick={handleCancel}>取消</Button>,
        mode !== 'view' &&
        <Button
          key="submit"
          type="primary"
          loading={mode === 'create' ? loading.effects[`${domain}/create`] : loading.effects[`${domain}/update`]}
          onClick={handleOk}
        >保存</Button>,
      ],
    };

    const renderItem = (item) => {
      return (
        <Col key={item.dataIndex} {...ColProps} {...item.colProps}>
          <FormItem
            label={item.title}
            hasFeedback={!readOnly && !item.tag.props.disabled && !!(item.options) && !!(item.options.rules)}
            {...formItemLayout}
            {...item.formItemLayout}
          >
            {getFieldDecorator(item.dataIndex, {
              ...item.options,
              initialValue: record[`${item.dataIndex}`] || (item.options && item.options.initialValue),
            })(item.tag)}
          </FormItem>
        </Col>
      );
    };

    return (
      <Modal {...modalOpts}>
        <Row type="flex" justify="start">
          {items.map(item => renderItem(item))}
        </Row>
      </Modal>
    );
  };

export default Form.create()(modal);
