import Taro, { Component } from '@tarojs/taro'
import { View, Switch, Input, Textarea, Picker } from '@tarojs/components'
import { strTrim, arrToObj } from '@utils'
import './index.scss'
import MultiSelect from './MultiSelect'
import RadioSelect from './RadioSelect'
import LCanton from './LCanton'
import TimeTwo from './TimeTwo'
const HLKEY = '654mca0l38b489d9'
const CJ = require('crypto-js')
export default class FormGroup extends Component {
constructor (props) {
super(props)
const { val, typeStr, labelShow } = this.props
const { valTrue = '1', valFalse = '2' } = this.props
this.state = {
switchValTrue: valTrue,
switchValFalse: valFalse,
curRegionName: '',
curLabelShow: labelShow === 'no' ? false : true,
curTypeStr: typeStr || 'input',
curVal: val || '',
curCheckedArr: [],
curRadioObj: {},
isMSShow: false,
isRSShow: false,
curImgUrl: val,
isCShow: false,
cantonNameObj: {},
}
}
componentDidUpdate (prevProps, prevState) {
const { val, typeStr } = this.props
if (prevProps.val !== val) {
this.setState({
curVal: val || ''
})
if (typeStr === 'img') {
this.setState({
curImgUrl: val || ''
})
}
}
}
// 后期有空来拆分,暂时放一块
changeInput (e) {
const { valStr, bc } = this.props
bc(valStr, strTrim(e.target.value))
}
selectChange (curArr, e) {
const { valStr, bc } = this.props
bc(valStr, curArr[Number(e.detail.value)].val, curArr)
}
dateChange (e) {
const { valStr, bc } = this.props
bc(valStr, strTrim(e.target.value))
}
regionChange (e) {
const { valStr, bc } = this.props
bc(valStr, e.target.value, e.target.code)
}
openMSPopup (e) {
this.setState({
isMSShow: true
})
}
dealMSPopup (curVal, curCheckedArr) {
this.closeMSPopup()
const { valStr, bc } = this.props
if (curVal && curCheckedArr) {
this.setState({
curVal,
curCheckedArr,
})
bc(valStr, curVal, curCheckedArr)
} else {
bc(valStr, '')
}
}
closeMSPopup () {
this.setState({
isMSShow: false,
})
}
openRSPopup (e) {
this.setState({
isRSShow: true
})
}
dealRSPopup (curVal, curRadioObj) {
this.closeRSPopup()
const { valStr, bc } = this.props
if (curVal && curRadioObj) {
this.setState({
curVal,
curRadioObj,
})
bc(valStr, curVal, curRadioObj)
} else {
bc(valStr, '')
}
}
closeRSPopup () {
this.setState({
isRSShow: false
})
}
openCPopup (e) {
this.setState({
isCShow: true
})
}
dealCPopup (curVal, cantonNameObj) {
this.closeCPopup()
const { valStr, bc } = this.props
if (curVal && cantonNameObj) {
this.setState({
curVal,
cantonNameObj,
})
bc(valStr, curVal, cantonNameObj)
} else {
bc(valStr, '')
}
}
closeCPopup () {
this.setState({
isCShow: false
})
}
renderTimeTwo () {
const { curVal } = this.state
const {
disabled,
label1,
label2,
} = this.props
return (
)
}
timeTwoChange (arr) {
const { valStr, bc } = this.props
bc(valStr, arr)
}
switchClickHandle () {
const { valStr, bc } = this.props
const { switchValTrue, switchValFalse } = this.state
const { curVal } = this.state
bc(valStr, curVal === switchValTrue ? switchValFalse : switchValTrue)
}
uploadImgHandle () {
const { valStr, bc } = this.props
const that = this
Taro.chooseImage({
count: 1, // 默认9
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
const tempFilePaths = res.tempFilePaths
Taro.uploadFile({
url: `${BASE_URL}/fileupload/uploadfile`,
filePath: tempFilePaths[0],
name: 'file',
formData: {
'filename': 'xcxOperate'
},
success (res){
const msg = res.data || ''
const key = CJ.enc.Utf8.parse(HLKEY)
const bytes = CJ.AES.decrypt(msg, key, {
mode: CJ.mode.ECB,
padding: CJ.pad.Pkcs7
})
const originalText = bytes.toString(CJ.enc.Utf8)
const cData = JSON.parse(originalText)
that.setState({
curImgUrl: cData.data || ''
}, () => {
bc(valStr, cData.data || '')
})
}
})
}
})
}
render () {
const {
curTypeStr,
curLabelShow,
curVal,
isMSShow,
isRSShow,
curCheckedArr,
curRadioObj,
curImgUrl,
isCShow,
cantonNameObj,
inputDiyType,
} = this.state
const {
keyStr,
keyStr2,
moreOptions,
labelWidth,
labelAlign = 'left',
disabled,
pbShow,
placeholderSingle = false,
moreRows = false,
cantonLevel = 3,
cantonLabelObj,
cantonVerify,
dateFields
} = this.props
const { hasFooterNav = false } = this.props
const curArr = moreOptions && moreOptions.arr ? moreOptions.arr : []
const curObj = arrToObj(curArr)
const curCheckedLabelArr = curCheckedArr.map(item => {
return item.label
})
const curCheckedLabel = curCheckedLabelArr.join(',')
const curRadioLabel = curRadioObj.label ? curRadioObj.label : curObj[curVal]
const { hasText, textTrue, textFalse, rightAlign='right' } = this.props
const { switchValTrue } = this.state
const switchHasText = hasText || false
const switchTextTrue = textTrue || '开启'
const switchTextFalse = textFalse || '关闭'
const switchChecked = curVal === switchValTrue
let boxClassName = 'l-form-group'
if (pbShow === 'no') boxClassName += ' nopb'
if (moreRows) boxClassName += ' rows2'
const {
defineBoxClassName
} = this.props
if (defineBoxClassName) {
if (defineBoxClassName === 'noborder') {
boxClassName += ' noborder'
} else {
boxClassName += ' ' + defineBoxClassName
}
}
let { inputType = 'text' } = this.state
if (inputDiyType) inputType = inputDiyType
const bg = require('@img/icon_upload_img.png')
const signRight = require('@img/icon_sign_right.png')
const imageSrc = curImgUrl || bg
const {
initUpdate,
} = this.props
const { inputFont } = this.props
const { startDate } = this.props
const { defineContentClassName } = this.props
let formContentStr = 'l-form-content'
if (defineContentClassName) {
formContentStr = 'l-form-content ' + defineContentClassName
}
return (
{
curLabelShow
&&
{keyStr}
}
{
curTypeStr === 'input'
&&
}
{
curTypeStr === 'inputFont'
&&
{inputFont}
}
{
curTypeStr === 'textarea'
&&
}
{
curTypeStr === 'select'
&&
}
{
curTypeStr === 'radio'
&&
{
curVal
? {curRadioLabel}
: {`${placeholderSingle ? '' : keyStr2 ? keyStr2 : '请选择' + keyStr}`}
}
}
{
curTypeStr === 'radio'
&&
}
{
curTypeStr === 'multiSelect'
&&
{
curVal && curVal.length > 0
? {curCheckedLabel}
: {`${placeholderSingle ? '' : keyStr2 ? keyStr2 : '请选择' + keyStr}`}
}
}
{
curTypeStr === 'multiSelect'
&&
}
{
curTypeStr === 'date'
&&
}
{
curTypeStr === 'time'
&&
}
{
curTypeStr === 'timeTwo'
&&
this.renderTimeTwo()
}
{
curTypeStr === 'canton'
&&
{
cantonNameObj && cantonNameObj.canton1
? {`${cantonNameObj.canton1 ? cantonNameObj.canton1 : ''}${cantonNameObj.canton2 ? '-' + cantonNameObj.canton2 : ''}${cantonNameObj.canton3 ? '-' + cantonNameObj.canton3 : ''}`}
: {`${placeholderSingle ? '' : keyStr2 ? keyStr2 : '请选择' + keyStr}`}
}
}
{
curTypeStr === 'canton'
&&
}
{
curTypeStr === 'region'
&&
{
curVal && curVal.length > 0
?
{curVal[0]}-{curVal[1]}-{curVal[2]}
:
请选择{keyStr}
}
}
{
curTypeStr === 'switch'
&&
{switchHasText ? switchChecked ? switchTextTrue : switchTextFalse : ''}
}
{
curTypeStr === 'img'
&&
}
)
}
}