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

const FormItem = Form.Item;

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

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

const modal =
  ({
     mode,
     domain,
     readOnly,
     record,
     onOk,
     onCancel,
     dispatch,
     loading,
     form: {
       getFieldDecorator,
       validateFields,
       getFieldsValue,
     },
     ...modalProps
   }) => {

    const items = [{
      title: '编号',
      dataIndex: 'ouCode',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '名称',
      dataIndex: 'ouName',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '简称',
      dataIndex: 'ouAbbr',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '类型',
      dataIndex: 'ouType',
      tag: <UdcSelect domain="EDP" code="OU_TYPE" disabled={readOnly}/>,
    }, {
      title: '状态',
      dataIndex: 'ouStatus',
      tag: <UdcSelect domain="EDP" code="ON_OFF_FLAG" disabled={readOnly}/>,
    }, {
      title: '本位币',
      dataIndex: 'ouCurr',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '地址号',
      dataIndex: 'addrNo',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '区域',
      dataIndex: 'region',
      tag: <Input disabled={readOnly} />,
    }, {
      title: '是否删除',
      dataIndex: 'deleteFlag',
      tag: <UdcSelect domain="EDP" code="YES_NO_FLAG" disabled={readOnly}/>,
    }, {
      title: '备注',
      dataIndex: 'remark',
      tag: <Input disabled={readOnly} type="textarea"/>,
      colProps: {
        xs: 24,
        sm: 24,
        md: 24,
        xl: 24,
      },
      formItemLayout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 20 },
      },
    }];

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

    const modalOpts = {
      ...modalProps,
      onCancel: onCancel,
      footer: [
        <Button key="back" onClick={onCancel}>取消</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);
