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

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,
     bpmRoles: { tasks },
     onOk,
     onCancel,
     dispatch,
     loading,
     form: {
       getFieldDecorator,
       validateFields,
       getFieldsValue,
     },
     ...modalProps
   }) => {
    const items = [{
      title: '角色领域',
      dataIndex: 'field',
      options: {
        initialValue: 'EO',
        rules: [{
          required: true,
          message: '必填',
        }],
      },
      tag: <UdcSelect domain="EDP" code="AUTH_FIELD"/>,
    }, {
      title: '角色名称',
      dataIndex: 'name',
      options: {
        rules: [{
          required: true,
          message: '必填',
        }],
      },
      tag: <Input disabled={readOnly}/>,
    }, {
      title: '预警提醒',
      dataIndex: 'reminderId',
      tag: <SuggestSelect disabled={readOnly} apiName="reminders"/>,
    }, {
      title: '授权角色',
      dataIndex: 'authRoleId',
      options: {
        rules: [{
          required: true,
          message: '必填',
        }],
      },
      tag: <SuggestSelect disabled={readOnly} apiName="authRoles"/>,
    }, {
      title: '关联任务',
      dataIndex: 'tasks',
      // options: {
      //   rules: [{
      //     required: true,
      //     message: '必填',
      //   }],
      // },
      tag: <SuggestSelect disabled={true} apiName="tasks" mode="multiple"/>,
    }
    ];

    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>
    );
  };

modal.propTypes = {
  readOnly: PropTypes.bool,
  form: PropTypes.object.isRequired,
  type: PropTypes.string,
  record: PropTypes.object,
  onOk: PropTypes.func,
};

export default Form.create()(modal);
