<template>
    <view>
        <u-form-item label="供应商" :prop="'wareHouse[' + rowIndex + '].supplierName'" :required="true">
            <u--input :value="currentValue" placeholder="请选择供应商" readonly :disabled="disabled"
                @focus="openPicker"></u--input>
        </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" @tap="selectItem(item)">
                        <view class="selector-name">{{ item.name }}</view>
                        <view class="selector-meta">
                            <text v-if="item.address">地址：{{ item.address }}</text>
                            <text v-if="item.telPhone">联系电话：{{ item.telPhone }}</text>
                        </view>
                    </view>
                </view>
            </scroll-view>
        </view>
    </view>
</template>

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

export default {
    name: 'WareHouseSupplierSelector',
    props: {
        rowIndex: Number,
        inputForm: Object,
        disabled: Boolean
    },
    data() {
        return {
            visible: false,
            loading: false,
            keyword: '',
            suppliers: [],
            supplierService: null
        }
    },
    computed: {
        currentRow() {
            return (this.inputForm.wareHouse || [])[this.rowIndex] || {}
        },
        currentValue() {
            return this.currentRow.supplierName || ''
        },
        filteredList() {
            const keyword = (this.keyword || '').trim().toLowerCase()
            if (!keyword) {
                return this.suppliers
            }
            return this.suppliers.filter(item => (item.name || '').toLowerCase().includes(keyword))
        }
    },
    created() {
        this.supplierService = new SupplierService()
    },
    methods: {
        async openPicker() {
            if (this.disabled) {
                return
            }
            this.visible = true
            this.keyword = ''
            if (!this.suppliers.length) {
                await this.loadSuppliers()
            }
        },
        closePicker() {
            this.visible = false
        },
        async loadSuppliers() {
            this.loading = true
            try {
                const data = await this.supplierService.list({
                    current: 1,
                    size: -1
                })
                this.suppliers = (data && data.records) || []
            } catch (e) {
                this.suppliers = []
                uni.showToast({
                    title: '供应商数据加载失败',
                    icon: 'none'
                })
            } finally {
                this.loading = false
            }
        },
        selectItem(item) {
            this.$emit('selected', {
                index: this.rowIndex,
                item
            })
            this.closePicker()
        }
    }
}
</script>

<style scoped>
.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: 68vh;
    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-meta {
    display: flex;
    flex-direction: column;
    gap: 8rpx;
    margin-top: 12rpx;
    font-size: 24rpx;
    color: #64748b;
}
</style>
