<template>
    <view>
        <u-form-item label="采购编号" prop="purchaseNo">
            <view class="selector-trigger" @tap.stop="openPicker">
                <u--input :value="currentValue" placeholder="请选择采购编号" :disabled="disabled" @focus="openPicker"
                    @click="openPicker" @tap="openPicker"></u--input>
            </view>
        </u-form-item>

        <view class="selector-mask" :class="{ show: visible }" @tap="closePicker"></view>
        <view class="selector-panel" :class="{ show: visible }">
            <view class="selector-header">
                <view class="selector-action" @tap="closePicker">取消</view>
                <view class="selector-title">选择采购单</view>
                <view class="selector-action selector-action-placeholder">取消</view>
            </view>
            <view class="selector-search">
                <u-search :show-action="false" v-model="keyword" placeholder="搜索采购编号或采购名称"></u-search>
            </view>
            <scroll-view scroll-y class="selector-list">
                <view v-if="loading" class="selector-state">加载中...</view>
                <view v-else-if="filteredList.length === 0" class="selector-state">暂无可选采购单</view>
                <view v-else class="selector-list-inner">
                    <view class="selector-card" v-for="item in filteredList" :key="item.id || item.purchaseNo"
                        @tap="selectItem(item)">
                        <view class="selector-name">{{ item.purchaseNo }}</view>
                        <view class="selector-desc">{{ item.purchaseSketch || '未填写采购名称' }}</view>
                        <view class="selector-meta">
                            <text v-if="item.handledByName">经办人：{{ item.handledByName }}</text>
                            <text v-if="item.createDate">申请时间：{{ item.createDate }}</text>
                        </view>
                    </view>
                </view>
            </scroll-view>
        </view>
    </view>
</template>

<script>
import WareHouseService from '@/api/psi/WareHouseService'

export default {
    name: 'PurchaseSelector',
    props: {
        inputForm: Object,
        disabled: Boolean,
        required: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            visible: false,
            loading: false,
            keyword: '',
            purchaseList: [],
            wareHouseService: null
        }
    },
    computed: {
        currentValue() {
            return (this.inputForm && this.inputForm.purchaseNo) || ''
        },
        filteredList() {
            const keyword = (this.keyword || '').trim().toLowerCase()
            if (!keyword) {
                return this.purchaseList
            }
            return this.purchaseList.filter(item => {
                const purchaseNo = (item.purchaseNo || '').toLowerCase()
                const purchaseSketch = (item.purchaseSketch || '').toLowerCase()
                return purchaseNo.includes(keyword) || purchaseSketch.includes(keyword)
            })
        }
    },
    created() {
        this.wareHouseService = new WareHouseService()
    },
    methods: {
        async openPicker() {
            if (this.disabled) {
                return
            }
            this.visible = true
            this.keyword = ''
            if (!this.purchaseList.length) {
                await this.loadPurchaseList()
            }
        },
        closePicker() {
            this.visible = false
        },
        async loadPurchaseList() {
            this.loading = true
            try {
                const data = await this.wareHouseService.reimbursementList({
                    current: 1,
                    size: 50,
                    status: '5',
                    orders: []
                })
                this.purchaseList = (data && data.records) || []
            } catch (e) {
                this.purchaseList = []
                uni.showToast({
                    title: '采购单加载失败',
                    icon: 'none'
                })
            } finally {
                this.loading = false
            }
        },
        selectItem(item) {
            this.$emit('selected', item)
            this.closePicker()
        }
    }
}
</script>

<style scoped>
.selector-trigger {
    width: 100%;
}

.selector-mask {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    opacity: 0;
    visibility: hidden;
    transition: all 0.25s ease;
    z-index: 1000;
}

.selector-mask.show {
    opacity: 1;
    visibility: visible;
}

.selector-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 72vh;
    background: #f7f9fc;
    border-radius: 28rpx 28rpx 0 0;
    transform: translateY(100%);
    transition: transform 0.25s ease;
    z-index: 1001;
    display: flex;
    flex-direction: column;
}

.selector-panel.show {
    transform: translateY(0);
}

.selector-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 28rpx 32rpx 16rpx;
    background: #fff;
    border-bottom: 1rpx solid #eef2f7;
}

.selector-title {
    font-size: 32rpx;
    font-weight: 600;
    color: #1f2937;
}

.selector-action {
    min-width: 80rpx;
    font-size: 28rpx;
    color: #2979ff;
}

.selector-action-placeholder {
    opacity: 0;
}

.selector-search {
    padding: 20rpx 24rpx 8rpx;
    background: #fff;
}

.selector-list {
    flex: 1;
    padding: 16rpx 24rpx 32rpx;
}

.selector-list-inner {
    display: flex;
    flex-direction: column;
    gap: 16rpx;
}

.selector-state {
    padding-top: 120rpx;
    text-align: center;
    font-size: 28rpx;
    color: #94a3b8;
}

.selector-card {
    background: #fff;
    border-radius: 20rpx;
    padding: 24rpx;
    box-shadow: 0 8rpx 24rpx rgba(15, 23, 42, 0.06);
}

.selector-name {
    font-size: 30rpx;
    font-weight: 600;
    color: #0f172a;
}

.selector-desc {
    margin-top: 8rpx;
    font-size: 26rpx;
    color: #334155;
}

.selector-meta {
    display: flex;
    flex-direction: column;
    gap: 8rpx;
    margin-top: 12rpx;
    font-size: 24rpx;
    color: #64748b;
}
</style>
