import React, { useState, useEffect } from 'react';
import { Input, Button, Form, message, Spin } from 'antd';

import { connect, history } from 'umi';

import { RA, ResultClear } from '@/utils/method';
import TitleBack from '@/components/TitleBack/TitleBack';
import SearchOptionsCommnity from '@/components/SearchOptions/SearchOptionsCommnity';
import Line from '@/components/Line/Line';
import TextArea from 'antd/lib/input/TextArea';
import Relationship from '@/components/Relationship/Relationship';
import { validateMessages } from '@/utils/params';
import styles from './css/index.less';

const module = 'CommunityService';
let communityName: string = '';
const CardAdd = (props: any) => {
  const { dispatch, Result, communityInfo } = props;

  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);

  useEffect(() => {
    if (Result != null) {
      if ((Result.error_code = '0000')) {
        message.success('Operation Success!');
        setLoading(false);
        ResultClear(module, dispatch);
        history.push('/CommercialService/AccessCardApplication');
      } else {
        message.error('Operation Fail!');
        setLoading(false);
        ResultClear(module, dispatch);
      }
    }
  }, [Result]);

  const onFinish = (values: any) => {
    if (!communityName) {
      message.warning('Please enter the complete!');
      return;
    }

    values.communityName = communityName;
    values.buildNumber = 'BLK ' + values.buildNumber;
    console.log(values);
    setLoading(true);
    RA(35, values, module, dispatch);
  };

  const extendName = (values: any) => {
    communityName = values;

    RA(32, { communityName: values }, 'User', dispatch);
  };

  useEffect(() => {
    if (communityInfo != null) {
      form.setFieldsValue({
        zipCode:
          communityInfo.data.rows.residentialAddress +
          ' ' +
          communityInfo.data.rows.residentialZipCode,
      });
    }
  }, [communityInfo]);

  useEffect(() => {
    form.resetFields();
  }, []);

  const checkData2: (rule: any, value: string, cb: any) => void = (rule, value, cb) => {
    if (value) {
      if (/^[a-zA-Z0-9]+$/g.test(value)) {
        cb();
      } else {
        cb(new Error('Only numbers and letters can be entered!'));
      }
    }
    cb();
  };

  // 手机号码验证
  const phoneNumber: (rule: any, value: string, cb: any) => void = (rule, value, cb) => {
    if (value) {
      if (/^\d{8}$|^1[3|5|7|8|9]\d{9}$/.test(value)) {
        cb();
      } else {
        cb(new Error('The format is incorrect!'));
      }
    }
    cb();
  };

  return (
    <div style={{ width: '100%', minWidth: 1020, padding: 34, backgroundColor: '#ffffff' }}>
      <TitleBack title={'Apply Card Add'}></TitleBack>

      <Form form={form} name="basic" onFinish={onFinish} validateMessages={validateMessages}>
        <Spin spinning={loading}>
          <Form.Item labelCol={{ span: 3 }} label="Community" style={{ marginBottom: 0 }}>
            <Input.Group compact>
              <Form.Item name="communityName" style={{ marginRight: 20 }}>
                <SearchOptionsCommnity opname={extendName} />
              </Form.Item>
              <Form.Item
                name="buildNumber"
                style={{ marginRight: 16 }}
                className={styles.buildNumber}
                rules={[
                  { required: true, pattern: /^\d+$/, message: 'Only number can be entered' },
                ]}
              >
                <Input style={{ width: 80 }} placeholder={'BLK'} />
              </Form.Item>
              <div style={{ lineHeight: 2, marginRight: 16 }}>#</div>
              <Form.Item
                name="floorNumber"
                style={{ marginRight: 16 }}
                className={styles.floorNumber}
                rules={[
                  { required: true, pattern: /^\d+$/, message: 'Only number can be entered' },
                ]}
              >
                <Input style={{ width: 80 }} placeholder={'Floor'} />
              </Form.Item>
              <div style={{ lineHeight: 2, marginRight: 16 }}>-</div>
              <Form.Item
                name="roomNumber"
                className={styles.roomNumber}
                rules={[{ required: true, validator: checkData2 }]}
              >
                <Input style={{ width: 120 }} placeholder={'Room'} />
              </Form.Item>
            </Input.Group>
          </Form.Item>

          <Form.Item name="zipCode" wrapperCol={{ offset: 3 }} rules={[{ required: true }]}>
            <Input
              style={{ width: 580 }}
              placeholder={'Display the address and postcode automatically according to the'}
              disabled
            ></Input>
          </Form.Item>

          <Form.Item labelCol={{ span: 3 }} label="Owner Name" style={{ marginBottom: -40 }}>
            <Input.Group compact>
              <Form.Item
                name="communityOwner"
                style={{ marginRight: 20 }}
                rules={[{ required: true, max: 30, min: 2 }]}
              >
                <Input placeholder={'Name'} />
              </Form.Item>
              <Form.Item style={{ marginRight: 20 }} label={'Contact Information'}>
                <Input hidden={true} />
              </Form.Item>
              <Form.Item
                name="communityOwnerPhone"
                style={{ marginRight: 20 }}
                rules={[{ validator: phoneNumber }]}
              >
                <Input placeholder={'Phone number'} />
              </Form.Item>
              <Form.Item name="communityOwnerEmail" rules={[{ required: true, type: 'email' }]}>
                <Input placeholder={'Email'} />
              </Form.Item>
            </Input.Group>
          </Form.Item>

          <Line></Line>

          <Form.Item labelCol={{ span: 3 }} label="Applicant" style={{ marginBottom: -40 }}>
            <Input.Group compact>
              <Form.Item
                name="applyName"
                style={{ marginRight: 20 }}
                rules={[{ required: true, max: 30, min: 2 }]}
              >
                <Input placeholder={'Name'} />
              </Form.Item>
              <Form.Item style={{ marginRight: 20 }} label={'Contact Information'}>
                <Input hidden={true} />
              </Form.Item>
              <Form.Item
                name="applyNamePhone"
                style={{ marginRight: 20 }}
                rules={[{ validator: phoneNumber }]}
              >
                <Input placeholder={'Phone number'} />
              </Form.Item>
              <Form.Item name="applyNameEmail" rules={[{ required: true, type: 'email' }]}>
                <Input placeholder={'Email'} />
              </Form.Item>
            </Input.Group>
          </Form.Item>

          {/* label={"Contact Information"} */}
          <Form.Item labelCol={{ span: 3 }} name="applyRelationOwner" label={'Relationship'}>
            <Relationship />
          </Form.Item>
          <Form.Item
            labelCol={{ span: 3 }}
            name="applyReasonContent"
            label={'Reason'}
            rules={[{ required: true, max: 500 }]}
          >
            <TextArea style={{ width: 400 }}></TextArea>
          </Form.Item>
          <Line></Line>
          <Form.Item wrapperCol={{ offset: 3 }}>
            <Button type="primary" htmlType="submit">
              Submit
            </Button>
          </Form.Item>
        </Spin>
      </Form>
    </div>
  );
};

function map(state: any) {
  const { Result } = state[module];
  const { communityInfo } = state.User;
  return { Result, communityInfo };
}
export default connect(map)(CardAdd);
