import React from 'react';
import { Button, Col, Form, Modal, Row, Radio } from 'antd';
import { SuggestSelect } from '@components';

const FormItem = Form.Item;
const RadioGroup = Radio.Group;

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

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

const udcItemLayout = {
  labelCol: {
    span: 10,
  },
  wrapperCol: {
    span: 14,
  },
};

const udcProps = {
  xs: 20,
  sm: 20,
  md: 20,
  xl: 20,
};


const modal =
  ({
    mode,
    domain,
    readOnly,
    record,
    onOk,
    onCancel,
    dispatch,
    loading,
    form: {
      getFieldDecorator,
      validateFields,
      getFieldsValue,
    },
    ...modalProps
  }) => {
    const items = [{
      title: '选择公司：',
      dataIndex: 'anncClass',
      options: {
        rules: [{
          required: true,
          message: '必填',
        }],
      },
      tag: <SuggestSelect apiName="suppOu" disabled={readOnly} params={{ suppId: record }} onChange={(id) => handleChange(id)} />,
      // tag: <UdcSelect domain="EDP" code="ANNC_CLASS" disabled={readOnly} onChange={(text) => handleChange(text)} />,
      formItemLayout: udcItemLayout,
      colProps: udcProps,
    }, {
      title: '对账类型：',
      dataIndex: 'accountType',
      options: {
        rules: [{
          required: true,
          message: '必填',
        }],
      },
      tag: <RadioGroup onChange={e=>{handleRadioChange(e.target.value)}}> 
        <Radio value={'1'}>公司对账</Radio>
        <Radio value={'2'}>门店对账</Radio>
      </RadioGroup>,
      formItemLayout: udcItemLayout,
      colProps: udcProps,
    }];

    const handleChange = (id) => {
      dispatch({
        type: `${domain}/updateState`,
        payload: {
          ouId: id,
        },
      });
    };

    const handleRadioChange = (value) => {
      dispatch({
        type: `${domain}/updateState`,
        payload: {
          accountType: value,
        },
      });
    };

    const handleOk = () => {

      validateFields((errors) => {
        if (errors) {
          return;
        }
        // const data = {
        //   ...getFieldsValue(),
        // };
        // onOk(data);
        dispatch({ type: `${domain}/hideModal`, });
        dispatch({
          type: `${domain}/getSingle`,
          payload: {
            mode: 'create',
          },
        });
      });
    };

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

    const modalOpts = {
      ...modalProps,
      // onOk: handleOk,
      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 (
        !item.hidden && <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,
            })(item.tag)}
          </FormItem>
        </Col>
      );
    };

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

export default Form.create()(modal);
