AssociationModel.jsx 4.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/* eslint-disable prefer-template */
/* eslint-disable indent */
/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-05-17 10:26:35
 * @LastEditTime: 2022-05-20 09:38:44
 * @LastEditors: leizhe
 */
import React, { useEffect, useState } from 'react';
import { Modal, Form, Input, notification, Button, Select, Space, Image } from 'antd';
import { UpdateParentId, modelManageListAll } from '@/services/drawBoardManage/api';

const { Option } = Select;

const AssociationModel = props => {
  const { visible, onCancel, obj, callBackSubmit = () => {} } = props;
  const { Item } = Form;
  const [form] = Form.useForm();
  const [data, setData] = useState([]);
  const [imageData, setImgData] = useState('');

  useEffect(() => {
    console.log(obj);
    if (visible) {
      modelManageListAll({
        modelName: '',
        modelType: '',
      }).then(res => {
        if (res.code === 0) {
          if (res.data.length > 0) {
            let aa = [];
            res.data.map(i => {
              aa.push(i);
            });
            let bb = aa;
            console.log(bb);
            console.log(obj);
            if (bb && bb.length > 0) {
              if (obj.RelModel != 0) {
                bb.splice(bb.findIndex(item => item.ID === obj.RelModel), 1);
              } else {
                bb.splice(bb.findIndex(item => item.ID === obj.ID), 1);
              }

              bb.map(i => {
                if (i.children && i.children.length > 0) {
                  delete i.children;
                }
              });
              console.log(bb);
              setData(bb);
            }
          }
        }
      });
    } else {
      form.resetFields();
      setImgData('');
      setData('');
    }
  }, [visible]);

  const onSubmit = () => {
    form.validateFields().then(validate => {
      if (validate) {
        let obj1 = form.getFieldsValue();
        let aa = [];
        aa.push(obj.ID);
        UpdateParentId({ modelIds: aa, parentId: obj1.Name }).then(res => {
          if (res.code === 0) {
            callBackSubmit();
            onCancel();
            notification.success({
              message: '关联成功',
              duration: 2,
            });
          } else {
            notification.error({
              message: '关联失败',
              description: res.msg,
            });
          }
        });
      }
    });
  };

  const handleChange = e => {
    console.log(e);
    console.log(data);
    let aa = data.find(i => i.ID === e);
    console.log(aa);
    setImgData(aa.Path);
  };

  return (
    <Modal
      title="关联父模型"
      visible={visible}
      onCancel={onCancel}
      destroyOnClose
      width="600px"
      footer={
        <Space>
          <Button onClick={onSubmit} type="primary">
            确定
          </Button>
        </Space>
      }
    >
      <Form form={form} style={{ overflowY: 'scroll' }}>
        <Item
          labelCol={{ span: 8 }}
          name="Name"
          rules={[
            {
              required: true,
              message: '请选择父模型',
            },
          ]}
        >
          <Select
            style={{ width: '100%' }}
            placeholder="选择父模型"
            optionLabelProp="label"
            onChange={handleChange}
            optionFilterProp="label"
            showSearch
          >
            {data.length
              ? data.map((item, index) => (
                  <>
                    <Option value={item.ID} label={item.ModelName}>
                      <div className="demo-option-label-item">
                        {item.ModelName}
                        <span role="img" aria-label={item.ModelTypeName}>
{item.ModelTypeName}
                        </span>
                      </div>
                    </Option>
                  </>
                ))
              : ''}
          </Select>
        </Item>
        <Item>
          {imageData == '' ? (
            <></>
          ) : (
            <Image
              src={
                window.location.origin +
                `/Publish/Web/File/ModelManage/ModelFilePreview/${encodeURIComponent(imageData)}`
              }
              height="60px"
            />
          )}
        </Item>
      </Form>
    </Modal>
  );
};
export default AssociationModel;