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

const FormItem = Form.Item;

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

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

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

    const items = [{
      title: '新密码',
      dataIndex: 'p',
      options: {
        rules: [{
          required: true,
          message: '必填',
        }, {
          min: 8,
          message: '不能少于8位',
        }],
      },
      tag: <Input type="password"/>
    }, {
      title: '再次输入新密码',
      dataIndex: 'pn',
      options: {
        rules: [{
          required: true,
          message: '必填',
        }, {
          validator: (rule, value, callback) => {
            if (value && value !== getFieldValue('p')) {
              callback('两次输入不一致！');
            }
            callback();
          }
        }, {
          min: 8,
          message: '不能少于8位',
        }],
      },
      tag: <Input type="password"/>
    }
    ];

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

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

    const modalOpts = {
      ...modalProps,
      onCancel: handleCancel,
      footer: [
        <Button key="back" onClick={handleCancel}
                disabled={loading.effects[`${domain}/updatePW`]}
        >取消</Button>,
        mode !== 'view' &&
        <Button
          key="submit"
          type="primary"
          loading={loading.effects[`${domain}/updatePW`]}
          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}>
        <Alert message="密码需8位以上数字和字母的组合" type="warning" showIcon/>
        <Row type="flex" justify="start">
          {items.map(item => renderItem(item))}
        </Row>
      </Modal>
    );
  };

export default Form.create()(modal);
