/** * 添加/编辑 Vehicle页面 * @邠心vbe on 2024/01/30 */ import React, { Component } from 'react'; import { View, Text, StyleSheet, TextInput, Image, Pressable, ScrollView } from 'react-native'; import Dialog from '../../components/Dialog'; import apiUser from '../../api/apiUser'; import TextView from '../../components/TextView'; import Dropdown from '../../components/Dropdown'; import { TypeImageList } from '../chargeV2/Charging'; import ImageCropPicker from 'react-native-image-crop-picker'; import { UploadThemes } from '../../components/ThemesConfig'; import apiUpload from '../../api/apiUpload'; import utils from '../../utils/utils'; import app from '../../../app.json'; const options = { width: 300, height: 300, cropping: true, multiple: false, mediaType: 'photo', writeTempFile: false, compressImageQuality: 0.8, compressImageMaxWidth: 720, compressImageMaxHeight: 1280, ...UploadThemes } export default class VehicleDetail extends Component { constructor(props) { super(props); this.state = { isEdit: false, vehicleInfo: { brand: "", model: "", connectorType: "" }, brandOptions: [], modelOptions: [], typeOptions: [] }; } componentDidMount() { this.getBrandOptions(); const params = this.props.route.params; if (params.id) { this.setState({ isEdit: true }) Dialog.showProgressDialog(); this.getVehicleData(params.id); } } getBrandOptions() { apiUser.getVehicleBrandList().then(res => { if (res.data) { this.setState({ brandOptions: res.data }) } }).catch((err) => { Dialog.dismissLoading(); toastShort(err) }); const types = [] TypeImageList.forEach((item, index) => { types.push(item.name); }) this.setState({ typeOptions: types }) } getModelOptions() { if (this.state.vehicleInfo.brand) { apiUser.getVehicleModelByBrand({ brand: this.state.vehicleInfo.brand }).then(res => { if (res.data) { this.setState({ modelOptions: res.data }) } }).catch((err) => { toastShort(err) this.setState({ modelOptions: [] }) }); } else { this.setState({ modelOptions: [] }) } } getVehicleData(id) { apiUser.getVehicleById({ vehiclePk: id }).then(res => { Dialog.dismissLoading(); if (res.data) { const info = res.data; this.setState({ vehicleInfo: info }, () => { this.getModelOptions(); }); } }).catch((err) => { Dialog.dismissLoading(); toastShort(err) }); } changeForm(key, value) { const form = { ...this.state.vehicleInfo } form[key] = value; this.setState({ vehicleInfo: form }) } uploadImage() { ImageCropPicker.openPicker({ ...options, cropperToolbarTitle: $t('common.cropperTitle') }).then(image => { if (image.path) { apiUpload.uploadImage(image.path, image.mime, 'PDVL').then(res => { if (res.success && res.data.picturePath) { this.changeForm("vehiclePhoto", res.data.picturePath) utils.logEventTracking("file_upload", res.data.picturePath) toastShort($t('common.uploadSuccess')); } else { toastShort($t('common.uploadFailed')); } }).catch(err => { toastShort(err); }); } }).catch(err1 => { //console.log(err1); }); } validate() { if (!this.state.vehicleInfo.brand) { toastShort($t('profile.msgSelectBrand')); return; } if (!this.state.vehicleInfo.vehicleModelId) { toastShort($t('profile.msgSelectModel')); return; } if (!this.state.vehicleInfo.licensePlate) { if (app.vehicle.showLicencePlate) { toastShort($t('profile.msgInputPlate')); } else { toastShort($t('profile.msgInputCarNo')); } return; } /*if (!this.state.vehicleInfo.batteryCapacity) { toastShort($t('profile.msgInputBatCap')); return; } if (!this.state.vehicleInfo.connectorType) { toastShort($t('profile.msgSelectType')); return; } if (!this.state.vehicleInfo.vehiclePhoto) { toastShort($t('profile.msgVehiclePhoto')); return; }*/ //console.log("车辆信息", this.state.vehicleInfo); if (this.state.isEdit) { this.updateVehicle(this.state.vehicleInfo) } else { this.addVehicle(this.state.vehicleInfo) } } addVehicle(params) { Dialog.showProgressDialog(); apiUser.addVehicle(params).then(res => { Dialog.dismissLoading(); toastShort($t('common.addSuccess')); goBack(); }).catch((err) => { Dialog.dismissLoading(); toastShort(err); }); } updateVehicle(params) { Dialog.showProgressDialog(); apiUser.updateVehicle(params).then(res => { Dialog.dismissLoading(); toastShort($t('common.updateSuccess')); goBack(); }).catch((err) => { Dialog.dismissLoading(); toastShort(err); }); } getAsterisk() { if (app.vehicle.showRequiredAsterisk) { return " *"; } else { return ""; } } render() { return ( {$t('profile.vehicleBrand')} { this.changeForm("brand", value); this.getModelOptions(); }} /> this.changeForm("vehicleModelId", value)} /> this.changeForm("licensePlate", text)} /> this.changeForm("batteryCapacity", text)} /> kWh { app.vehicle.requireConnectorType && <> {$t('profile.connecterType')} this.changeForm("connectorType", value)} autoSelect={false} /> } { app.vehicle.requireImageUpload && <> {$t('profile.plsUploadImageCar')} { this.state.vehicleInfo.vehiclePhoto ? this.uploadImage()}> : this.uploadImage()}> {$t('profile.selectUpload')} {$t('profile.max4mbPhoto')} } }