import React, { useState, useEffect } from 'react';
import {Modal,Form,Switch,InputNumber,Input,Button,TreeSelect} from 'antd'
import { gettree } from '../service';
const CreateForm = (props) => {
    const [OrgList, setOrgList] = useState([]);
      //初始化数据
  useEffect(() => {
    initOrgList();
  }, []);
  function initOrgList() {
    gettree().then((r) => {
      inittree(r.data);
      function inittree(datas) {
        for (var i in datas) {
          if(  datas[i]['type']==2||  datas[i]['type']==3){
            datas[i]['disabled']=false;
          }else{
            datas[i]['disabled']=true;
          }

          if (datas[i].children) {
            inittree(datas[i].children);
          }
        }
      }
      setOrgList(r.data);
    });
  }
  return <>
  <Modal
      title={props.values.id ? '编辑充值活动' : '新建充值活动'}
      width={500}
      visible={props.createModalVisible}
      footer={null} //底部内容
      onCancel={() => {
        props.onCancel();
      }}
      //关闭时销毁 Modal 里的子元素
      destroyOnClose={true}
    >
        <Form
        preserve={false}
        initialValues={props.values}
        labelCol={{ span: 4 }}
        layout="Horizontal"
        // initialValues={props.values}
        onFinish={props.onFinish}
      >
          <Form.Item name="id" hidden={true}>
            <Input />
          </Form.Item>
          <Form.Item name="storeId" label="选择店铺" rules={[{ required: true, max: 64 }]}>
          <TreeSelect
            style={{ width: '100%' }}
            dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
            treeData={OrgList}
            placeholder="归属门店"
            treeDefaultExpandAll
          />
            </Form.Item>
          <Form.Item name="rechargeMoney" label="充值金额" rules={[{ required: true, max: 64,type:'money' }]}>
          <InputNumber
            formatter={(value) => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            parser={(value) => value.replace(/\$\s?|(,*)/g, '')}
            value={props.values.rechargeMoney}
            stringMode
            style={{width: '100%'}}
            />
          </Form.Item>
         
          <Form.Item name="giveMoney" label="赠送金额" rules={[{ required: true, max: 64,type:'money' }]}>
          <InputNumber
            formatter={(value) => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
            parser={(value) => value.replace(/\$\s?|(,*)/g, '')}
            style={{width: '100%'}}
            value={props.values.giveMoney}
            stringMode
            />
          </Form.Item>
          <Form.Item name="recommend" label="是否推荐">
            <Switch checked={props.values.recommend}/>
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit" style={{ float: 'right' }}>
              保存
            </Button>
          </Form.Item>
      </Form >
    </Modal>
  </>
}
export default CreateForm;