king
2023-10-18 81d0d7721bb14a34b1eef99fd9506c3eda4bda99
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import React from 'react'
import { useDrag, useDrop } from 'react-dnd'
import { Select, DatePicker, Input, InputNumber, Button, Popover, Switch, Radio, Checkbox, Form, Rate } from 'antd'
import { QuestionCircleOutlined, UploadOutlined, EditOutlined, CopyOutlined, CloseOutlined, StarFilled, SearchOutlined, FontColorsOutlined } from '@ant-design/icons'
import moment from 'moment'
 
import asyncComponent from '@/utils/asyncComponent'
import MkIcon from '@/components/mk-icon'
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
const { MonthPicker } = DatePicker
const { TextArea } = Input
 
const Editor = asyncComponent(() => import('@/components/editor'))
const ColorSketch = asyncComponent(() => import('@/mob/colorsketch'))
const CheckCard = asyncComponent(() => import('../checkCard'))
 
const Card = ({ id, card, moveCard, findCard, editCard, closeCard, copyCard, showField, setStyle }) => {
  const originalIndex = findCard(id).index
  const [{ isDragging }, drag] = useDrag({
    item: { type: 'form', id, originalIndex },
    collect: monitor => ({
      isDragging: monitor.isDragging(),
    }),
  })
  const [, drop] = useDrop({
    accept: 'form',
    canDrop: () => true,
    drop: (item) => {
      const { id: draggedId, originalIndex } = item
 
      if (originalIndex === undefined) {
        item.dropTargetId = id
      } else if (draggedId && draggedId !== id) {
        const { index: overIndex } = findCard(id)
        moveCard(draggedId, overIndex)
      }
    }
  })
  const opacity = isDragging ? 0.5 : 1
 
  const edit = () => {
    editCard(id)
  }
 
  const close = () => {
    closeCard(id)
  }
 
  const copy = () => {
    copyCard(id)
  }
 
  const changeStyle = () => {
    let options = ['font1']
 
    MKEmitter.emit('changeStyle', options, card.style || {}, (s) => {setStyle(s, id)})
  }
 
  let selectval = ''
  if (card.type === 'multiselect' || card.type === 'select' || card.type === 'link') {
    if (card.initval) {
      let _option = card.options.filter(option => option.Value === card.initval)[0]
      if (_option) {
        selectval = _option.Text || ''
      } else {
        selectval = ''
      }
    } else if (card.setAll === 'true') {
      selectval = card.emptyText || '空'
    }
  }
 
  let formItem = null
  let className = 'ant-form-item'
  if (card.type === 'text') {
    formItem = (<Input style={{marginTop: '4px'}} placeholder={card.placeholder || ''} value={card.initval} />)
  } else if (card.type === 'number') {
    formItem = (<InputNumber value={card.initval} precision={card.decimal} />)
  } else if (card.type === 'multiselect' || card.type === 'select' || card.type === 'link' || card.type === 'cascader') {
    formItem = (<Select value={selectval}></Select>)
  } else if (card.type === 'popSelect') {
    formItem = (<Select value={card.initval} suffixIcon={<SearchOutlined />}></Select>)
  } else if (card.type === 'color') {
    formItem = (<ColorSketch value={card.initval || 'transparent'}/>)
  } else if (card.type === 'date') {
    let format = 'YYYY-MM-DD'
    if (card.precision === 'hour') {
      format = 'YYYY-MM-DD HH'
    } else if (card.precision === 'minute') {
      format = 'YYYY-MM-DD HH:mm'
    } else if (card.precision === 'second') {
      format = 'YYYY-MM-DD HH:mm:ss'
    }
    formItem = (<DatePicker format={format} value={card.initval ? moment().subtract(card.initval, 'days') : null} />)
  } else if (card.type === 'datemonth') {
    formItem = (<MonthPicker value={card.initval ? moment().subtract(card.initval, 'month') : null} />)
  } else if (card.type === 'datetime') {
    formItem = (<DatePicker format='YYYY-MM-DD HH:mm:ss' value={card.initval ? moment().subtract(card.initval, 'days') : null} />)
  } else if (card.type === 'textarea') {
    formItem = (<TextArea value={card.initval} placeholder={card.placeholder || ''} autoSize={{ minRows: 2, maxRows: 6 }} />)
  } else if (card.type === 'brafteditor') {
    formItem = (<Editor />)
  } else if (card.type === 'fileupload') {
    formItem = (<Button style={{marginTop: '3px'}}><UploadOutlined /> 点击上传 </Button>)
  } else if (card.type === 'funcvar') {
    formItem = (<Input style={{marginTop: '4px'}} value={card.linkfield} />)
  } else if (card.type === 'linkMain') {
    formItem = (<Input style={{marginTop: '4px'}} />)
  } else if (card.type === 'rate') {
    formItem = (<Rate value={card.initval || 0} count={card.rateCount || 5} character={card.character ? <MkIcon type={card.character}/> : <StarFilled />} allowHalf={card.allowHalf === 'true'} />)
  } else if (card.type === 'switch') {
    formItem = (<Switch checkedChildren={card.openText || ''} unCheckedChildren={card.closeText || ''} style={{marginTop: '8px'}} checked={card.initval}/>)
  } else if (card.type === 'radio') {
    formItem = card.options && card.options.length > 0 ? (<Radio.Group value={card.initval}>
      {card.options.map(cell => <Radio key={cell.key} value={cell.Value}>{cell.Text}</Radio>)}
    </Radio.Group>) : (<Radio.Group value={1}>
      <Radio value={1}>A</Radio>
      <Radio value={2}>B</Radio>
      <Radio value={3}>C</Radio>
      <Radio value={4}>D</Radio>
    </Radio.Group>)
  } else if (card.type === 'checkbox') {
    let _val = card.initval ? card.initval.split(',') : []
    formItem = card.options && card.options.length > 0 ? (<Checkbox.Group value={_val}>
      {card.options.map(cell => <Checkbox key={cell.key} value={cell.Value}>{cell.Text}</Checkbox>)}
    </Checkbox.Group>) : (<Checkbox.Group value={['A', 'C']}>
      <Checkbox value="A">A</Checkbox>
      <Checkbox value="B">B</Checkbox>
      <Checkbox value="C">C</Checkbox>
      <Checkbox value="D">D</Checkbox>
    </Checkbox.Group>)
  } else if (card.type === 'check') {
    formItem = <Checkbox checked={card.initval}>{card.checkTip || ''}</Checkbox>
  } else if (card.type === 'hint') {
    formItem = <div style={{marginTop: '8px', color: 'rgba(0, 0, 0, 0.85)', lineHeight: '1.5', ...card.style}}>{card.message}</div>
  } else if (card.type === 'formula') {
    formItem = <div style={{marginTop: '8px', color: 'rgba(0, 0, 0, 0.85)', lineHeight: '1.5', ...card.style}}>{card.formula}{card.postfix || ''}</div>
  } else if (card.type === 'split') {
    formItem = <div className="split-line" style={card.style}>{card.label}</div>
  } else if (card.type === 'vercode') {
    formItem = <Input style={{marginTop: '4px'}} placeholder={card.placeholder || ''} value={card.initval} addonAfter={
      <Button type="link" style={{padding: 0}} size="small">
        获取验证码
      </Button>
    }/>
  } else if (card.type === 'checkcard') {
    className += ' checkcard'
    formItem = <CheckCard config={card} />
  }
 
  let _label = <span className={'mk-form-label ' + (/^\s+$/.test(card.label) ? 'no-colon' : '')} style={card.style}>{card.label}</span>
 
  if (card.tooltip) {
    _label = <><QuestionCircleOutlined className="mk-form-tip" /><span className="mk-form-label" style={card.style}>{card.label}</span></>
  }
  if (card.type === 'brafteditor' && card.hidelabel === 'true') {
    _label = null
  } else if (card.type === 'hint' && !card.label) {
    className += ' no-label'
    _label = ' '
  }
 
  if (window.GLOB.formId === card.uuid) {
    className += ' actived'
  }
 
  return (
    <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
      <div className="mk-popover-control">
        <EditOutlined className="edit" onClick={edit} />
        <CopyOutlined className="copy" onClick={copy} />
        <FontColorsOutlined className="style" onClick={changeStyle} />
        <CloseOutlined className="close" onClick={close} />
      </div>
    } trigger="hover">
      <div className="page-card" style={{ opacity: opacity}}>
        <div ref={node => drag(drop(node))} onDoubleClick={edit}>
          {card.type === 'split' ? formItem : <Form.Item
            className={className}
            colon={false}
            label={_label}
            required={card.required === 'true'}
            extra={card.extra || null}
            labelCol={card.labelwidth ? {style: {width: card.labelwidth + '%'}} : null}
            wrapperCol={card.labelwidth ? {style: {width: (100 - card.labelwidth) + '%'}} : null}
          >
            {formItem}
            <div></div>
            {showField ? <div className="field-name" style={card.writein === 'false' ? {color: 'orange'} : {}}>
              {card.field}{card.hidden === 'true' || card.type === 'funcvar' ? '(隐藏)' : ''}{card.readonly === 'true' ? '(只读)' : ''}{card.readin === 'false' ? '(未填充)' : ''}{card.linkField ? <span style={{color: '#1890ff'}}>{`(关联${card.linkField})`}</span> : ''}{card.supField ? <span style={{color: '#8E44AD'}}>{`(上级${card.supField})`}</span> : ''}
            </div> : ''}
          </Form.Item>}
        </div>
      </div>
    </Popover>
  )
}
export default Card