import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter } from 'react-native'; import FastImage from 'react-native-fast-image'; import { TextInput } from 'react-native-gesture-handler'; import Animated, { useAnimatedStyle, useSharedValue, withSpring, withTiming } from 'react-native-reanimated'; import { BjInstanceActualMeasurementResearchTableRow } from '../../database/Instance'; import { colors, deepCopy, handleDismissKeyboard, isEmpty, onePx, px2dp, toastShort } from '../../utils'; import { BjTypeActualMeasurementResearchTableRow } from '../../utils/types'; import NormalActionSheet from './../kits/NormalActionSheet'; interface Props { } interface State { row: BjTypeActualMeasurementResearchTableRow } interface TypeResearch { name: string type: string } const NumberOfPlantsDialog = (props: Props) => { const containerRef = React.useRef(null); const delayTimer = React.useRef(null); const delayTimer1 = React.useRef(null); const overlayOpacity = useSharedValue(0.1); const layoutScaleValue = useSharedValue(0); const [layerType, setLayerType] = React.useState(""); const [autoCheckLoading, setAutoCheckLoading] = React.useState(false); const [tempRow, setTempRow] = React.useState(deepCopy(BjInstanceActualMeasurementResearchTableRow)); const showToastListener = React.useRef(null); const hideToastListener = React.useRef(null); const tempKey = React.useRef(null); const tempPropName = React.useRef(null); const actionSheetRef = React.useRef(null); React.useEffect(() => { overlayOpacity.value = withTiming(0.2); layoutScaleValue.value = withSpring(0.6); showToastListener.current = DeviceEventEmitter.addListener('show-number-of-plants-dialog', (a) => { tempKey.current = a.key; console.log("a.row", a.row) setLayerType(a.layerType); setTempRow(deepCopy(a.row)); delayTimer.current = setTimeout(() => { handleShowToast() }, 0); }); hideToastListener.current = DeviceEventEmitter.addListener('hide-number-of-plants-dialog', (a) => { handleHideToast() }); return () => { delayTimer.current && clearTimeout(delayTimer.current); delayTimer1.current && clearTimeout(delayTimer1.current); showToastListener.current && showToastListener.current?.remove(); hideToastListener.current && hideToastListener.current?.remove(); } }, []); const handleShowToast = () => { requestAnimationFrame(() => { containerRef.current.setNativeProps({ style: { width: '100%', height: '100%' } }) delayTimer1.current = setTimeout(() => { overlayOpacity.value = 0.52; layoutScaleValue.value = 1; }, 0); }) }; const handleHideToast = () => { delayTimer.current = setTimeout(() => { containerRef.current.setNativeProps({ style: { width: 0, height: 0 } }) }, 240); overlayOpacity.value = 0.1; layoutScaleValue.value = 0; handleDismissKeyboard(); } const handleComfirmToast = () => { const { numberOfPlants, checkPass } = handleCheckTotalNumberOfPlants(); let _tempRow = tempRow; // if(numberOfPlants == 0){ // return toastShort("请输入数值") // } _tempRow.numberOfPlants = numberOfPlants + ""; if (checkPass) { DeviceEventEmitter.emit('on-number-of-plant-dialog-finish', { key: tempKey.current, tempRow: _tempRow }) handleHideToast(); } } const overlayAnimatedStyle = useAnimatedStyle(() => { return { opacity: withTiming(overlayOpacity.value, { duration: 240 }) } }); const toastBodyAnimatedStyle = useAnimatedStyle(() => { return { transform: [ { scale: withSpring(layoutScaleValue.value, { stiffness: 96 }), } ], opacity: withTiming(layoutScaleValue.value, { duration: 120 }), } }) const _onTextChange = (propName, text) => { let _tempRow = tempRow; var patrn = /^[0-9]*$/; if (text == "0") { return toastShort("数量不可为0") } if (!patrn.test(Number(text) + "") || text.includes('.')) { return toastShort("请输入正整数"); } console.log("_onTextChange", _tempRow) _tempRow[propName] = text; let _numberOfPlants = 0; console.log("layerType", layerType) setTempRow(deepCopy(_tempRow)); setAutoCheckLoading(true); delayTimer.current = setTimeout(() => { autoCheckTotalNumberOfPlants(); }, 230); }; const autoCheckTotalNumberOfPlants = () => { let _numberOfPlants = 0; let _tempRow = tempRow; switch (layerType) { case 'arbor': const totalSmallLiveEnvNumber0 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface); const totalHealthNumber0 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber0); if (totalSmallLiveEnvNumber0 > totalHealthNumber0) { _numberOfPlants = totalSmallLiveEnvNumber0 } else { _numberOfPlants = totalHealthNumber0 } break; case 'shrub': const totalSmallLiveEnvNumber1 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface); const totalHealthNumber1 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber1); if (totalSmallLiveEnvNumber1 > totalHealthNumber1) { _numberOfPlants = totalSmallLiveEnvNumber1 } else { _numberOfPlants = totalHealthNumber1 } break; case 'plant': const totalSmallLiveEnvNumber2 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface) + Number(tempRow.plantNumberRockEpiphyte) + Number(tempRow.plantNumberBranchEpiphyte); const totalHealthNumber2 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber2); if (totalSmallLiveEnvNumber2 > totalHealthNumber2) { _numberOfPlants = totalSmallLiveEnvNumber2 } else { _numberOfPlants = totalHealthNumber2 } break; case 'moss': const totalSmallLiveEnvNumber3 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface) + Number(tempRow.plantNumberRockEpiphyte) + Number(tempRow.plantNumberBranchEpiphyte); // const totalHealthNumber3 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); // console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber3); // if (totalSmallLiveEnvNumber3 > totalHealthNumber3) { // _numberOfPlants = totalSmallLiveEnvNumber3 // } else { // _numberOfPlants = totalHealthNumber3 // } _numberOfPlants = totalSmallLiveEnvNumber3 break; default: break; }; _tempRow.numberOfPlants = _numberOfPlants + ''; setTempRow(deepCopy(_tempRow)); setAutoCheckLoading(false); } //乔木: 合计数值 = 各类小生境之和 = 各类健康状况之和相同 //灌木: 合计数值 = 各类小生境之和 = 各类健康状况之和相同 >= 开花(且>=结实) //草本: 合计数值 = 各类小生境之和 = 各类健康状况之和相同 >= 开花(且>= 结实) //苔藓: 合计数值 = 各类小生境之和 = 各类健康状况之和相同 >= 具孢蒴或孢子囊植株数 const handleCheckTotalNumberOfPlants = () => { let _numberOfPlants = 0; let checkPass = false; switch (layerType) { case 'arbor': const totalSmallLiveEnvNumber0 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface); const totalHealthNumber0 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber0); if (totalSmallLiveEnvNumber0 == totalHealthNumber0) { _numberOfPlants = totalSmallLiveEnvNumber0 checkPass = true; } else { toastShort("各类小生境之和应等于各类健康状况之和,请检查后重试!") } break; case 'shrub': const totalSmallLiveEnvNumber1 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface); const totalHealthNumber1 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); // const totalLastNumber1 = Number(tempRow.plantNumberIsFlower) + Number(tempRow.plantNumberIsFruit) console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber1); if (totalSmallLiveEnvNumber1 == totalHealthNumber1) { if (totalSmallLiveEnvNumber1 >= Number(tempRow.plantNumberIsFlower) && totalSmallLiveEnvNumber1 >= Number(tempRow.plantNumberIsFruit)) { _numberOfPlants = totalSmallLiveEnvNumber1; checkPass = true; } else { toastShort("合计株数应大于等于开花数或结实数,请检查") } } else { toastShort("各类小生境之和应等于各类健康状况之和,请检查后重试!") } break; case 'plant': const totalSmallLiveEnvNumber2 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface) + Number(tempRow.plantNumberRockEpiphyte) + Number(tempRow.plantNumberBranchEpiphyte); const totalHealthNumber2 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); const totalLastNumber2 = Number(tempRow.plantNumberIsFlower) + Number(tempRow.plantNumberIsFruit) console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber2); if (totalSmallLiveEnvNumber2 == totalHealthNumber2) { if (totalSmallLiveEnvNumber2 >= Number(tempRow.plantNumberIsFlower) && totalSmallLiveEnvNumber2 >= Number(tempRow.plantNumberIsFruit)) { _numberOfPlants = totalSmallLiveEnvNumber2; checkPass = true; } else { toastShort("合计株数应大于等于开花数或结实数,请检查") } } else { toastShort("各类小生境之和应等于各类健康状况之和,请检查后重试!") } break; case 'moss': checkPass = true; const totalSmallLiveEnvNumber3 = Number(tempRow.plantNumberSoilSurface) + Number(tempRow.plantNumberStoneDitch) + Number(tempRow.plantNumberStoneSurface) + Number(tempRow.plantNumberRockEpiphyte) + Number(tempRow.plantNumberBranchEpiphyte); const totalHealthNumber3 = Number(tempRow.plantNumberHealth) + Number(tempRow.plantNumberSubHealth) + Number(tempRow.plantNumberMediumHealth) + Number(tempRow.plantNumberNotHealth); console.log("totalSmallLiveEnvNumber", totalSmallLiveEnvNumber3); _numberOfPlants = totalSmallLiveEnvNumber3; checkPass = true; // if (totalSmallLiveEnvNumber3 == totalHealthNumber3) { // if (totalSmallLiveEnvNumber3 >= Number(tempRow.plantNumberOtherType)) { // _numberOfPlants = totalSmallLiveEnvNumber3; // checkPass = true; // } else { // toastShort("【各类小生境之和及各类健康状况之和】都应大于等于具孢蒴或孢子囊植株数,请检查后重试!") // } // } else { // toastShort("各类小生境之和应等于各类健康状况之和,请检查后重试!") // } break; default: break; }; return { numberOfPlants: _numberOfPlants, checkPass }; } const onPressGroupItem = (propName) => { if (layerType !== 'moss') { return false; } tempPropName.current = propName; actionSheetRef.current.show(); }; const _onSheetChange = (e) => { console.log("_onSheetChange--->", e) _onTextChange(tempPropName.current, e.value + '') } return ( 株数统计 合计: { autoCheckLoading ? 计算中... : <> { tempRow.numberOfPlants ? {tempRow.numberOfPlants} : 自动计算 } } 根部小生境(株) onPressGroupItem('plantNumberSoilSurface')} > 土面 _onTextChange('plantNumberSoilSurface', text)} /> { layerType == 'moss' ? : null } onPressGroupItem('plantNumberStoneDitch')} > 石沟 _onTextChange('plantNumberStoneDitch', text)} /> { layerType == 'moss' ? : null } onPressGroupItem('plantNumberStoneSurface')} > 石面 _onTextChange('plantNumberStoneSurface', text)} /> { layerType == 'moss' ? : null } { layerType == 'plant' || layerType == 'moss' ? onPressGroupItem('plantNumberRockEpiphyte')} > 岩石附生 _onTextChange('plantNumberRockEpiphyte', text)} /> { layerType == 'moss' ? : null } : null } { layerType == 'plant' || layerType == 'moss' ? onPressGroupItem('plantNumberBranchEpiphyte')} > 枝干附生 _onTextChange('plantNumberBranchEpiphyte', text)} /> { layerType == 'moss' ? : null } : null } 健康情况(株) onPressGroupItem('plantNumberHealth')} > 健康 _onTextChange('plantNumberHealth', text)} /> { layerType == 'moss' ? : null } onPressGroupItem('plantNumberSubHealth')} > 亚健康 _onTextChange('plantNumberSubHealth', text)} /> { layerType == 'moss' ? : null } onPressGroupItem('plantNumberMediumHealth')} > 中等健康 _onTextChange('plantNumberMediumHealth', text)} /> { layerType == 'moss' ? : null } onPressGroupItem('plantNumberNotHealth')} > 不健康 _onTextChange('plantNumberNotHealth', text)} /> { layerType == 'moss' ? : null } <> { layerType !== 'smallTreeResearch' && layerType !== 'smallSeedlingResearch' ? <> { layerType == 'moss' ? 具孢蒴或孢子囊植株数 onPressGroupItem('plantNumberOtherType')} > _onTextChange('plantNumberOtherType', text)} /> { layerType == 'moss' ? : null } : 开花结实情况(株) 开花 _onTextChange('plantNumberIsFlower', text)} /> 结果 _onTextChange('plantNumberIsFruit', text)} /> } : null } 取消 确定 ) }; const styles = StyleSheet.create({ overlay: { position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, width: '100%', height: '100%', backgroundColor: '#000', zIndex: -1 }, container: { position: 'absolute', top: 0, bottom: 0, left: 0, right: 0, zIndex: 999, width: 0, height: 0, overflow: 'hidden' }, content: { position: 'relative', width: '100%', height: '100%', flex: 1, justifyContent: 'center', alignItems: 'center', // backgroundColor: 'red' }, toastBody: { width: px2dp(300), minHeight: px2dp(200), backgroundColor: '#fff', borderRadius: px2dp(8), elevation: 12, justifyContent: 'space-between', alignItems: 'center', paddingVertical: px2dp(12) }, dialogHeader: { width: px2dp(268), height: px2dp(32), borderBottomWidth: 1, borderBottomColor: '#909399', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: px2dp(2) }, dialogHeaderRight: { flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center' }, footer: { width: px2dp(272), flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginTop: px2dp(24) }, footerLeftBtn: { width: px2dp(133), height: px2dp(36), borderRadius: px2dp(18), justifyContent: 'center', alignItems: 'center', backgroundColor: '#00D38F58', borderTopRightRadius: px2dp(3), borderBottomRightRadius: px2dp(3), }, footerRightBtn: { width: px2dp(133), height: px2dp(36), borderRadius: px2dp(18), justifyContent: 'center', alignItems: 'center', backgroundColor: colors.themeColor, borderTopLeftRadius: px2dp(3), borderBottomLeftRadius: px2dp(3) }, selectItem: { flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', borderBottomWidth: 1, borderBottomColor: '#303133', paddingTop: px2dp(16) }, selectItemName: { fontSize: 16, color: '#303133', marginLeft: px2dp(3) }, group: { width: px2dp(268), backgroundColor: '#f2f5f8', paddingHorizontal: px2dp(8), borderRadius: px2dp(3), marginTop: px2dp(12), paddingVertical: px2dp(6), }, groupRow: { width: '100%', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: px2dp(8) }, groupTitle: { fontSize: 15, color: '#303133', marginBottom: px2dp(5) }, groupHeader: { }, item: { position: 'relative', height: px2dp(42), flex: 1, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }, itemContent: { height: '100%', flex: 1, justifyContent: 'center', alignItems: 'flex-start' }, itemTitle: { fontSize: 14, color: '#303133' }, itemInput: { width: '100%', maxWidth: px2dp(128), height: px2dp(24), lineHeight: px2dp(24), justifyContent: 'center', alignItems: 'center' }, rightIcon: { width: px2dp(13), height: px2dp(13), tintColor: '#909399', marginRight: px2dp(8) } }) export default NumberOfPlantsDialog;