index.vue 29.4 KB
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
<template>
    <view class="sell-page">
        <!-- 表单内容 -->
        <view class="form-container">
            <!-- 车辆照片上传 -->
            <view class="form-section">
                <text class="section-title">添加车辆照片</text>
                <view class="upload-grid">
                    <!-- 正面照 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.front" class="upload-button" @click="triggerUpload('front')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.front)">
                            <image :src="uploadedImages.front" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('front')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">正面照</text>
                    </view>

                    <!-- 左侧照 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.left" class="upload-button" @click="triggerUpload('left')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.left)">
                            <image :src="uploadedImages.left" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('left')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">左侧照</text>
                    </view>

                    <!-- 右侧照 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.right" class="upload-button" @click="triggerUpload('right')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.right)">
                            <image :src="uploadedImages.right" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('right')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">右侧照</text>
                    </view>

                    <!-- 其他 -->
                    <view class="upload-item">
                        <!-- 上传按钮 -->
                        <view v-if="!uploadedImages.other" class="upload-button" @click="triggerUpload('other')">
                            <Plus class="upload-icon" />
                        </view>
                        <!-- 图片预览 -->
                        <view v-else class="image-preview" @click="previewImage(uploadedImages.other)">
                            <image :src="uploadedImages.other" class="preview-image" mode="aspectFill" />
                            <view class="delete-btn" @click.stop="deleteImage('other')">
                                <Close class="delete-icon" />
                            </view>
                        </view>
                        <text class="upload-label">其他</text>
                    </view>
                </view>

                <!-- 图片预览组件 -->
                <nut-image-preview v-model:show="previewVisible" :images="previewImages" :init-no="previewIndex" @close="closePreview" />
            </view>

            <!-- 所在学校 -->
            <view class="form-section">
                <view class="form-item" @click="showSchoolPicker">
                    <view class="form-item-left">
                        <Location class="form-icon" />
                        <text class="form-label">所在学校</text>
                    </view>
                    <view class="form-item-right">
                        <text class="form-value">{{ formData.school || '上海理工大学' }}</text>
                        <Right class="arrow-icon" />
                    </view>
                </view>
            </view>

            <!-- 车辆详情表单 -->
            <nut-form ref="formRef" :model-value="formData">
                <view class="form-section">
                    <!-- 车型品牌 -->
                    <nut-form-item label-position="top" label="车型品牌" prop="brand" required :rules="[{ required: true, message: '请选择车型品牌' }]">
                        <view class="form-item-content" @click="showBrandPicker">
                            <text class="form-value">{{ formData.brand || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 车辆型号 -->
                    <nut-form-item label-position="top" label="车辆型号" prop="model">
                        <view class="form-item-content" @click="showModelPicker">
                            <text class="form-value">{{ formData.model || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 车辆出厂年份 -->
                    <nut-form-item label-position="top" label="车辆出厂年份" prop="year">
                        <view class="form-item-content" @click="showYearPicker">
                            <text class="form-value">{{ formData.year || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 新旧程度 -->
                    <nut-form-item label-position="top" label="新旧程度" prop="condition" required
                        :rules="[{ required: true, message: '请选择新旧程度' }]">
                        <view class="form-item-content" @click="showConditionPicker">
                            <text class="form-value">{{ formData.condition || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 行驶里程 -->
                    <nut-form-item label="行驶里程" prop="mileage" required
                        :rules="[{ required: true, message: '请输入行驶里程' }]">
                        <nut-input v-model="formData.mileage" placeholder="1200" type="number" input-align="right">
                            <template #right>
                                <text class="unit">公里</text>
                            </template>
                        </nut-input>
                    </nut-form-item>

                    <!-- 续航里程 -->
                    <nut-form-item label="续航里程" prop="range">
                        <nut-input v-model="formData.range" placeholder="60" type="number" input-align="right">
                            <template #right>
                                <text class="unit">公里</text>
                            </template>
                        </nut-input>
                    </nut-form-item>

                    <!-- 最高时速 -->
                    <nut-form-item label="最高时速" prop="maxSpeed">
                        <nut-input v-model="formData.maxSpeed" placeholder="25" type="number" input-align="right">
                            <template #right>
                                <text class="unit">km/h</text>
                            </template>
                        </nut-input>
                    </nut-form-item>

                    <!-- 电池容量 -->
                    <nut-form-item label="电池容量" prop="batteryCapacity">
                        <nut-input v-model="formData.batteryCapacity" placeholder="20" type="number"
                            input-align="right">
                            <template #right>
                                <text class="unit">Ah</text>
                            </template>
                        </nut-input>
                    </nut-form-item>

                    <!-- 电池损耗度 -->
                    <nut-form-item label-position="top" label="电池损耗度" prop="batteryWear">
                        <view class="form-item-content" @click="showBatteryWearPicker">
                            <text class="form-value">{{ formData.batteryWear || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 刹车磨损度 -->
                    <nut-form-item label-position="top" label="刹车磨损度" prop="brakeWear">
                        <view class="form-item-content" @click="showBrakeWearPicker">
                            <text class="form-value">{{ formData.brakeWear || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>

                    <!-- 轮胎磨损度 -->
                    <nut-form-item label-position="top" label="轮胎磨损度" prop="tireWear">
                        <view class="form-item-content" @click="showTireWearPicker">
                            <text class="form-value">{{ formData.tireWear || '请选择' }}</text>
                            <Right class="arrow-icon" />
                        </view>
                    </nut-form-item>
                </view>
            </nut-form>

            <!-- 价格信息 -->
            <view class="form-section">
                <view class="form-item">
                    <view class="form-item-left">
                        <Location class="form-icon" />
                        <text class="form-label">出让价格</text>
                    </view>
                    <view class="form-item-right">
                        <text class="price-symbol">¥</text>
                        <input v-model="formData.sellingPrice" placeholder="3,200" type="text" class="price-input" />
                    </view>
                </view>

                <view class="form-item">
                    <view class="form-item-left">
                        <Location class="form-icon" />
                        <text class="form-label">市场价</text>
                    </view>
                    <view class="form-item-right">
                        <text class="market-price-symbol">¥</text>
                        <input v-model="formData.marketPrice" placeholder="6,500" type="text"
                            class="market-price-input" />
                    </view>
                </view>
            </view>

            <!-- 车辆描述 -->
            <view class="form-section">
                <nut-textarea v-model="formData.description" placeholder="请描述车辆详情,如使用感受、车况特点等" :max-length="200"
                    :rows="4" show-word-limit />
            </view>
        </view>

        <!-- 底部按钮 -->
        <view class="bottom-actions">
            <nut-button v-if="!isAuthMode" color="#f97316" size="large" block @click="onPublish">
                {{ isEditMode ? '保存修改' : '确认发布' }}
            </nut-button>
            <nut-button v-else color="#f97316" size="large" block @click="onPublish">
                确认发布
            </nut-button>
        </view>

        <!-- 选择器弹窗 -->
        <!-- 学校选择 -->
        <nut-popup v-model:visible="schoolPickerVisible" position="bottom">
            <nut-picker v-model="schoolValue" :columns="schoolOptions" title="选择学校" @confirm="onSchoolConfirm"
                @cancel="schoolPickerVisible = false" />
        </nut-popup>

        <!-- 品牌选择 -->
        <nut-popup v-model:visible="brandPickerVisible" position="bottom">
            <nut-picker v-model="brandValue" :columns="brandOptions" title="选择车型品牌" @confirm="onBrandConfirm"
                @cancel="brandPickerVisible = false" />
        </nut-popup>

        <!-- 型号选择 -->
        <nut-popup v-model:visible="modelPickerVisible" position="bottom">
            <nut-picker v-model="modelValue" :columns="modelOptions" title="选择车辆型号" @confirm="onModelConfirm"
                @cancel="modelPickerVisible = false" />
        </nut-popup>

        <!-- 年份选择 -->
        <nut-popup v-model:visible="yearPickerVisible" position="bottom">
            <nut-picker v-model="yearValue" :columns="yearOptions" title="选择出厂年份" @confirm="onYearConfirm"
                @cancel="yearPickerVisible = false" />
        </nut-popup>

        <!-- 新旧程度选择 -->
        <nut-popup v-model:visible="conditionPickerVisible" position="bottom">
            <nut-picker v-model="conditionValue" :columns="conditionOptions" title="选择新旧程度"
                @confirm="onConditionConfirm" @cancel="conditionPickerVisible = false" />
        </nut-popup>

        <!-- 电池损耗度选择 -->
        <nut-popup v-model:visible="batteryWearPickerVisible" position="bottom">
            <nut-picker v-model="batteryWearValue" :columns="wearLevelOptions" title="选择电池损耗度"
                @confirm="onBatteryWearConfirm" @cancel="batteryWearPickerVisible = false" />
        </nut-popup>

        <!-- 刹车磨损度选择 -->
        <nut-popup v-model:visible="brakeWearPickerVisible" position="bottom">
            <nut-picker v-model="brakeWearValue" :columns="wearLevelOptions" title="选择刹车磨损度"
                @confirm="onBrakeWearConfirm" @cancel="brakeWearPickerVisible = false" />
        </nut-popup>

        <!-- 轮胎磨损度选择 -->
        <nut-popup v-model:visible="tireWearPickerVisible" position="bottom">
            <nut-picker v-model="tireWearValue" :columns="wearLevelOptions" title="选择轮胎磨损度" @confirm="onTireWearConfirm"
                @cancel="tireWearPickerVisible = false" />
        </nut-popup>
    </view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { Plus, Right, Location, RectLeft, Close } from '@nutui/icons-vue-taro'
import Taro from '@tarojs/taro'
// import BASE_URL from '@/utils/config';
import './index.less'

// 获取页面参数
const instance = Taro.getCurrentInstance()
const { id, mode, type } = instance.router?.params || {}
const isEditMode = ref(mode === 'edit' && id)
const isAuthMode = ref(type === 'auth' && id)
const carId = ref(id || '')

// 文件上传相关
// const frontFileList = ref([])  // 正面照(保留用于兼容旧代码)
// const leftFileList = ref([])   // 左侧照(保留用于兼容旧代码)
// const rightFileList = ref([])  // 右侧照(保留用于兼容旧代码)
// const otherFileList = ref([])  // 其他照片(保留用于兼容旧代码)

// 已上传图片的URL
const uploadedImages = reactive({
    front: '',
    left: '',
    right: '',
    other: ''
})

// 图片预览相关
const previewVisible = ref(false)
const previewImages = ref([])
const previewIndex = ref(0)

const closePreview = () => {
    previewVisible.value = false
}

// 表单数据
const formData = reactive({
    school: '',
    brand: '',
    model: '',
    year: '',
    condition: '',
    mileage: '1200',
    range: '60',
    maxSpeed: '25',
    batteryCapacity: '20',
    batteryWear: '',
    brakeWear: '',
    tireWear: '',
    sellingPrice: '3,200',
    marketPrice: '6,500',
    description: ''
})

// 选择器显示状态
const schoolPickerVisible = ref(false)
const brandPickerVisible = ref(false)
const modelPickerVisible = ref(false)
const yearPickerVisible = ref(false)
const conditionPickerVisible = ref(false)
const batteryWearPickerVisible = ref(false)
const brakeWearPickerVisible = ref(false)
const tireWearPickerVisible = ref(false)

// 选择器值
const schoolValue = ref([])
const brandValue = ref([])
const modelValue = ref([])
const yearValue = ref([])
const conditionValue = ref([])
const batteryWearValue = ref([])
const brakeWearValue = ref([])
const tireWearValue = ref([])

// 选择器选项数据
const schoolOptions = ref([
    { text: '上海理工大学', value: '上海理工大学' },
    { text: '复旦大学', value: '复旦大学' },
    { text: '上海交通大学', value: '上海交通大学' },
    { text: '同济大学', value: '同济大学' },
    { text: '华东师范大学', value: '华东师范大学' },
    { text: '上海大学', value: '上海大学' },
    { text: '东华大学', value: '东华大学' },
    { text: '上海财经大学', value: '上海财经大学' }
])

const brandOptions = ref([
    { text: '小牛电动', value: '小牛电动' },
    { text: '雅迪', value: '雅迪' },
    { text: '爱玛', value: '爱玛' },
    { text: '台铃', value: '台铃' },
    { text: '绿源', value: '绿源' },
    { text: '新日', value: '新日' },
    { text: '立马', value: '立马' },
    { text: '其他', value: '其他' }
])

const modelOptions = ref([
    { text: 'NGT', value: 'NGT' },
    { text: 'UQi+', value: 'UQi+' },
    { text: 'Gova G2', value: 'Gova G2' },
    { text: 'MQi+', value: 'MQi+' },
    { text: 'NQi GTS', value: 'NQi GTS' },
    { text: 'RQi', value: 'RQi' },
    { text: '其他型号', value: '其他型号' }
])

const yearOptions = ref([
    { text: '2024年', value: '2024年' },
    { text: '2023年', value: '2023年' },
    { text: '2022年', value: '2022年' },
    { text: '2021年', value: '2021年' },
    { text: '2020年', value: '2020年' },
    { text: '2019年', value: '2019年' },
    { text: '2018年及以前', value: '2018年及以前' }
])

const conditionOptions = ref([
    { text: '全新', value: '全新' },
    { text: '9成新', value: '9成新' },
    { text: '8成新', value: '8成新' },
    { text: '7成新', value: '7成新' },
    { text: '6成新', value: '6成新' },
    { text: '5成新及以下', value: '5成新及以下' }
])

const wearLevelOptions = ref([
    { text: '全新', value: '全新' },
    { text: '轻微磨损', value: '轻微磨损' },
    { text: '中度磨损', value: '中度磨损' },
    { text: '重度磨损', value: '重度磨损' },
    { text: '需要更换', value: '需要更换' }
])

/**
 * 返回上一页
 */
const goBack = () => {
    Taro.redirectTo({
        url: '/pages/index/index'
    })
}

/**
 * 触发图片上传
 * @param {String} type - 图片类型 (front/left/right/other)
 */
const triggerUpload = (type) => {
    Taro.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
            const tempFilePath = res.tempFilePaths[0]
            uploadImage(tempFilePath, type)
        },
        fail: function () {
            Taro.showToast({
                title: '选择图片失败',
                icon: 'none'
            })
        }
    })
}

/**
 * 上传图片到服务器
 * @param {String} filePath - 图片文件路径
 * @param {String} type - 图片类型 (front/left/right/other)
 */
const uploadImage = (filePath, type) => {
    // 显示上传中提示
    Taro.showLoading({
        title: '上传中',
        mask: true
    })

    // 模拟上传成功(实际项目中替换为真实的上传逻辑)
    setTimeout(() => {
        Taro.hideLoading()

        // 模拟服务器返回的图片URL
        const mockImageUrl = filePath // 在实际项目中,这里应该是服务器返回的URL

        // 更新对应类型的图片
        uploadedImages[type] = mockImageUrl

        Taro.showToast({
            title: '上传成功',
            icon: 'success'
        })
    }, 1500)

    // 真实上传逻辑(注释掉的代码供参考)
    /*
    Taro.uploadFile({
        url: BASE_URL + '/admin/?m=srv&a=upload',
        filePath: filePath,
        name: 'file',
        header: {
            'content-type': 'multipart/form-data'
        },
        success: function (res) {
            try {
                const upload_data = JSON.parse(res.data)
                Taro.hideLoading()

                if (res.statusCode === 200 && upload_data.data) {
                    // 上传成功,更新对应类型的图片
                    uploadedImages[type] = upload_data.data.src

                    Taro.showToast({
                        title: '上传成功',
                        icon: 'success'
                    })
                } else {
                    throw new Error('上传失败')
                }
            } catch (error) {
                Taro.hideLoading()
                Taro.showToast({
                    icon: 'error',
                    title: '服务器错误,稍后重试!',
                    mask: true
                })
            }
        },
        fail: function () {
            Taro.hideLoading()
            Taro.showToast({
                icon: 'error',
                title: '上传失败,请重试',
                mask: true
            })
        }
    })
    */
}

/**
 * 预览图片
 * @param {String} imageUrl - 图片URL
 */
const previewImage = (imageUrl) => {
    previewImages.value = [{ src: imageUrl }]
    previewIndex.value = 0
    previewVisible.value = true
}

/**
 * 删除图片
 * @param {String} type - 图片类型 (front/left/right/other)
 */
const deleteImage = (type) => {
    uploadedImages[type] = ''
    Taro.showToast({
        title: '删除成功',
        icon: 'success'
    })
}

// 保留旧的上传逻辑作为备用(已注释)
// const afterRead = (event, type) => { ... }

// 保留旧的删除逻辑作为备用(已注释)
// const onDeleteFront = (file, index) => { frontFileList.value.splice(index, 1) }
// const onDeleteLeft = (file, index) => { leftFileList.value.splice(index, 1) }
// const onDeleteRight = (file, index) => { rightFileList.value.splice(index, 1) }
// const onDeleteOther = (file, index) => { otherFileList.value.splice(index, 1) }

/**
 * 显示学校选择器
 */
const showSchoolPicker = () => {
    schoolPickerVisible.value = true
}

/**
 * 显示品牌选择器
 */
const showBrandPicker = () => {
    brandPickerVisible.value = true
}

/**
 * 显示型号选择器
 */
const showModelPicker = () => {
    if (!formData.brand) {
        Taro.showToast({
            title: '请先选择品牌',
            icon: 'none'
        })
        return
    }
    modelPickerVisible.value = true
}

/**
 * 显示年份选择器
 */
const showYearPicker = () => {
    yearPickerVisible.value = true
}

/**
 * 显示新旧程度选择器
 */
const showConditionPicker = () => {
    conditionPickerVisible.value = true
}

/**
 * 显示电池损耗度选择器
 */
const showBatteryWearPicker = () => {
    batteryWearPickerVisible.value = true
}

/**
 * 显示刹车磨损度选择器
 */
const showBrakeWearPicker = () => {
    brakeWearPickerVisible.value = true
}

/**
 * 显示轮胎磨损度选择器
 */
const showTireWearPicker = () => {
    tireWearPickerVisible.value = true
}

/**
 * 学校选择确认
 */
const onSchoolConfirm = ({ selectedValue }) => {
    formData.school = selectedValue[0]
    schoolPickerVisible.value = false
}

/**
 * 品牌选择确认
 */
const onBrandConfirm = ({ selectedValue }) => {
    formData.brand = selectedValue[0]
    formData.model = '' // 重置型号
    brandPickerVisible.value = false
}

/**
 * 型号选择确认
 */
const onModelConfirm = ({ selectedValue }) => {
    formData.model = selectedValue[0]
    modelPickerVisible.value = false
}

/**
 * 年份选择确认
 */
const onYearConfirm = ({ selectedValue }) => {
    formData.year = selectedValue[0]
    yearPickerVisible.value = false
}

/**
 * 新旧程度选择确认
 */
const onConditionConfirm = ({ selectedValue }) => {
    formData.condition = selectedValue[0]
    conditionPickerVisible.value = false
}

/**
 * 电池损耗度选择确认
 */
const onBatteryWearConfirm = ({ selectedValue }) => {
    formData.batteryWear = selectedValue[0]
    batteryWearPickerVisible.value = false
}

/**
 * 刹车磨损度选择确认
 */
const onBrakeWearConfirm = ({ selectedValue }) => {
    formData.brakeWear = selectedValue[0]
    brakeWearPickerVisible.value = false
}

/**
 * 轮胎磨损度选择确认
 */
const onTireWearConfirm = ({ selectedValue }) => {
    formData.tireWear = selectedValue[0]
    tireWearPickerVisible.value = false
}

/**
 * 发布/保存车辆
 */
const onPublish = () => {
    if (!validateForm()) return

    const loadingTitle = isEditMode.value ? '保存中...' : '发布中...'
    Taro.showLoading({ title: loadingTitle })

    // 收集所有上传的图片URL
    const images = {
        front: uploadedImages.front || '',
        left: uploadedImages.left || '',
        right: uploadedImages.right || '',
        other: uploadedImages.other || ''
    }

    // 构建提交数据
    const submitData = {
        ...formData,
        images: images,
        imageUrls: Object.values(images).filter(url => url) // 过滤空URL
    }

    if (isEditMode.value) {
        submitData.id = carId.value
    }

    // TODO: 在此处调用实际的API接口提交数据
    // if (isEditMode.value) {
    //   updateCar(carId.value, submitData)
    // } else {
    //   createCar(submitData)
    // }

    // 模拟请求
    setTimeout(() => {
        Taro.hideLoading()
        const successTitle = isEditMode.value ? '保存成功' : '发布成功'
        Taro.showToast({
            title: successTitle,
            icon: 'success'
        })

        // 成功后跳转
        setTimeout(() => {
            if (isEditMode.value) {
                Taro.navigateBack()
            } else {
                Taro.switchTab({ url: '/pages/index/index' })
            }
        }, 1500)
    }, 2000)
}

/**
 * 表单验证
 * @returns {boolean} 验证结果
 */
const validateForm = () => {
    // 检查是否至少上传了一张图片
    const hasImages = uploadedImages.front || uploadedImages.left || uploadedImages.right || uploadedImages.other
    if (!hasImages) {
        Taro.showToast({ title: '请上传车辆图片', icon: 'none' })
        return false
    }

    if (!formData.brand) {
        Taro.showToast({ title: '请选择车型品牌', icon: 'none' })
        return false
    }

    if (!formData.condition) {
        Taro.showToast({ title: '请选择新旧程度', icon: 'none' })
        return false
    }

    if (!formData.mileage || formData.mileage <= 0) {
        Taro.showToast({ title: '请输入正确的行驶里程', icon: 'none' })
        return false
    }

    if (!formData.sellingPrice) {
        Taro.showToast({ title: '请输入出让价格', icon: 'none' })
        return false
    }

    return true
}

/**
 * 加载车辆数据(编辑模式)
 */
const loadCarData = async () => {
    if (!isEditMode.value || !carId.value) return

    try {
        Taro.showLoading({ title: '加载中...' })

        // TODO: 调用真实API获取车辆数据
        // const carData = await getCarById(carId.value)

        // 模拟API响应数据
        const mockCarData = {
            school: '上海理工大学',
            brand: '小牛电动',
            model: 'NGT',
            year: '2023年',
            condition: '9成新',
            mileage: '1200',
            range: '60',
            maxSpeed: '25',
            batteryCapacity: '20',
            batteryWear: '轻微磨损',
            brakeWear: '轻微磨损',
            tireWear: '轻微磨损',
            sellingPrice: '3,200',
            marketPrice: '6,500',
            description: '车况良好,电池续航正常,无重大事故,平时保养得当。',
            images: {
                front: 'https://picsum.photos/300/200?random=1',
                left: 'https://picsum.photos/300/200?random=2',
                right: 'https://picsum.photos/300/200?random=3',
                other: 'https://picsum.photos/300/200?random=4'
            }
        }

        // 填充表单数据
        Object.assign(formData, {
            school: mockCarData.school,
            brand: mockCarData.brand,
            model: mockCarData.model,
            year: mockCarData.year,
            condition: mockCarData.condition,
            mileage: mockCarData.mileage,
            range: mockCarData.range,
            maxSpeed: mockCarData.maxSpeed,
            batteryCapacity: mockCarData.batteryCapacity,
            batteryWear: mockCarData.batteryWear,
            brakeWear: mockCarData.brakeWear,
            tireWear: mockCarData.tireWear,
            sellingPrice: mockCarData.sellingPrice,
            marketPrice: mockCarData.marketPrice,
            description: mockCarData.description
        })

        // 填充图片数据
        Object.assign(uploadedImages, mockCarData.images)

        Taro.hideLoading()
    } catch (error) {
        console.error('加载车辆数据失败:', error)
        Taro.hideLoading()
        Taro.showToast({
            title: '加载数据失败',
            icon: 'none'
        })
    }
}

// 页面加载时执行
onMounted(() => {
    if (isEditMode.value) {
        loadCarData()
    }
    // 动态修改标题
    wx.setNavigationBarTitle({
        title: isEditMode.value ? '编辑车源' : '发布车源'
    });
    if (isAuthMode.value) {
        wx.setNavigationBarTitle({
            title: '发布车源'
        });
    }
})
</script>