import { Modal,Input, Form,Button,Select,message,Switch,Divider,Space,progress,percent} from 'antd';
import { BetaSchemaForm, ProFormSelect,ProFormDependency } from '@ant-design/pro-form';
import React, { useState,useEffect } from 'react';
import { PlusOutlined } from '@ant-design/icons';
const CreateAction = (props) => {
  const [form] = Form.useForm();
  const formRef = React.useRef(null);
  const [valueType, setValueType] = useState('text');
  const [layoutType, setLayoutType] = useState('Form');
  const [optionitem,setoptionitem]=useState([])
  const [jsontext,setjsontext]=useState('')
  
  const [modalVisit, setModalVisit] = useState(false);
  const [shuUpdate, setshuUpdate] = useState(false);
  const { TextArea } = Input;
  useEffect(() => {
    formRef.current?.setFieldsValue(props.values);
    if(props.createModalVisible){
      if(props.values.actionType==null){
        formRef.current?.setFieldsValue({
          actionType: 'text',
        });
        props.values.actionType="text";
        setValueType("text");
        setLayoutType('Form');
        setoptionitem([]);
      }else{
        formRef.current?.setFieldsValue({
          actionType: props.values.actionType,
        });
        setValueType(props.values.actionType)
        if(props.values.actionType=="select"){
          setLayoutType('ModalForm');
          let ary=[];
          let op=JSON.parse(props.values.actionValue)
          for (let t = 0; t < op.length; t++) {
            ary.push({actionValueName:op[t].actionValueName})
          }
          setoptionitem(ary);
        }if(props.values.actionType=="textareaary"){
          setjsontext(props.values.actionValue)
        }else{
          setValueType(props.values.actionType);
          setLayoutType('Form');
          setoptionitem([]);
        }
      }
      
    }
}, [props.createModalVisible]);
const handleChange=(value)=>{
  console.log(JSON.stringify(value))
  if(value){
    formRef.current?.setFieldsValue({
      actionValue:JSON.stringify(value)
    })
  }
  setModalVisit(false);
  message.success("保存成功")
}
  const options = [
      {value:'text',label: `text(字符串)`},
      {value:'textareaary',label: `textarea(字符串数组)`},
      { value: `select`, label: `enum(枚举)`},
        // { value: `money`, label: `金额输入`},
      { value: `textarea`, label: `text(文本域)`},
        // { value: `date`, label: `日期`},
        // { value: `dateTime`, label: `日期时间`},
        // { value: `radio`, label: `单选框`},
        // { value: `radioButton`, label: `按钮单选框`},
      { value: `digit`, label: `int32(整型)`},
      { value: `switch`, label: `bool(布尔型)`},
      ]
  const columns=[
      {
        dataIndex: 'actionValue',
        valueType:'dependency',
        name: ['valueType'],
        columns: ({ actionType }) => {
          console.log(valueType)
          switch(valueType)
          {
            case 'select':
              return [
                {
                  dataIndex: 'list',
                  valueType: 'formList',
                  initialValue:optionitem,
                  style:{paddingTop:'20px'},
                  columns: [
                    {
                      valueType: 'group',
                      columns: [
                        {
                          title: '枚举名称',
                          dataIndex: 'actionValueName',
                          width: 'md',
                          formItemProps: {
                            rules: [
                              {
                                required: true,
                                message: '此项为必填项',
                              },
                            ],
                            
                          },
                        }
                      ]
                    }
                  ],
                },
                {
                  renderFormItem: (schema,config,form) => {
                    return [
                      <div style={{float:'right'}}>
                      <Button
                      type="primary"
                      style={{'margin-left':'5px'}}
                      onClick={() => {
                        handleChange(form?.formRef?.current?.getFieldValue("list"));
                        return true; 
                      }}>
                        保存
                      </Button>
                      </div>
                    ];
                  },
                }
              ];
            case 'text':
              return [
                {
                  dataIndex: 'actionValue',
                  valueType: 'text',
                  renderFormItem: () => <Input placeholder="输入类型值" disabled />
                },
              ];
              case 'textareaary':
              return [
                {
                  dataIndex: 'actionValue',
                  valueType: 'text',
                  initialValue:jsontext,
                  renderFormItem: () => <TextArea
                  placeholder="数据格式：[data1:{value1:'1'},{value2:'2'},data2:{value1:'1'},{value2:'2'}]"
                  autoSize={{minRows: 10,maxRows: 100}}/>
                },
              ];
            default:
              return [{
                dataIndex: 'actionValue',
                valueType: valueType,
                fieldProps:{
                  disabled:true
                }
              }];
          }
        }
      },
    ]
    return (
        <>
    <Modal
      title={'新增参数'}
      width={640}
      ref={formRef}
      visible={props?.createModalVisible}
      bodyStyle={{ padding: '32px 40px 1px 48px' }}
      footer={null}
      onCancel={() => { 
        props?.onCancel();
      }}
      destroyOnClose
      maskClosable={false}
    >
         <Form
        layout="vertical"
        preserve={false}
        // form={form}
        initialValues={props?.values}
        onFinish={props?.onFinish}
        ref={formRef}
      >
         <Form.Item name="id" hidden={true}>
          <Input />
        </Form.Item>
       
        <Form.Item>
        <Form.Item name="actionName" label="参数名称" rules={[{ required: true, max: 64, whitespace: true }]}>
          <Input placeholder="请输入参数名称" />
        </Form.Item>
        <Form.Item name="actionType" label="数据类型" rules={[{ required: true, max: 64, whitespace: true }]}>
            <ProFormSelect options={options}  fieldProps={{
            value: valueType,
            onChange: (e) => {
              if(e=='select'){
                setLayoutType('ModalForm');
              }else{
                setLayoutType('Form')
                
              }
              setValueType(e);
            },
          }}/>
        </Form.Item>
        <Form.Item name="actionValue" label="输入类型控件">
          <BetaSchemaForm 
           trigger={<a onClick={()=>setModalVisit(true)}>添加枚举</a>}
           columns={columns}
           layoutType={layoutType}
           visible={modalVisit}
           modalProps={{
            forceRender: true,//切换显示模式后强制渲染其他模式表单
          
            onCancel:()=>setModalVisit(false)
           }}
           onVisibleChange={(vis)=>{
            setshuUpdate(vis)
           }}
           width={420}
           shouldUpdate={shuUpdate}
           submitter={false}
           style={{padding:'40px 10px 1px 10px;' }}
          />
        </Form.Item>
        
        {/* <Form.Item name="unit" label="单位">
          <Input placeholder="请输入单位" />
        </Form.Item>
        <Form.Item name="isBatch" label="是否绑定物料" valuePropName="checked">
            <Switch checkedChildren="是" unCheckedChildren="否"/>
        </Form.Item> */}
        {/* <Form.Item name="sort" label="排序">
          <Input placeholder="请输入排序" />
        </Form.Item> */}
        <Button  htmlType="button" style={{float:'right',left:10}} onClick={props.onCancel} >
                        取消
                    </Button>
                    <Button type="primary" htmlType="submit" style={{float:'right'}} >
                        保存
                    </Button>
        </Form.Item>
      </Form>
    </Modal>
    </>
    )
}
export default CreateAction;