<!-- 仓库/存储区选择 -->
<template>
    <navigation :title="isWarehousing.value ? '仓库选择' : '存储区选择'" :back-url="backUrl"></navigation>

    <view class="contentBox">
        <view class="topSearch">
            <uni-easyinput type="text" v-model="queryParams.keyword" prefixIcon="search" :inputBorder="false"
                @iconClick="getList" placeholder="请输入搜索内容" />
        </view>

        <!-- 仓库/存储列表  -->
        <z-paging ref="pagingRef" class="containerBox" v-model="infoList" @query="getList">
            <uni-list class="listBox">
                <uni-list-item v-for="item in infoList" :key="item.id" clickable @click="onChecked(item)">
                    <template v-slot:body>
                        <view style="display: flex; flex-direction: column;  width: 100%;" v-if="isWarehousing">
                            <view class="line title">
                                <p>仓库名称</p>
                                <p>{{ item.deptName }}</p>
                            </view>
                            <view class="line content">
                                <p>仓库编码</p>
                                <p>{{ item.deptId }}</p>
                            </view>
                            <!-- todo 以下两个字段pc未维护暂时注释 -->
                            <!-- <view class=" line content">
                                <p>仓库类型</p>
                                <p></p>
                            </view>
                            <view class="line content">
                                <p>仓库位置</p>
                                <p></p>
                            </view> -->
                            <view class="line content">
                                <p>备注</p>
                                <p>
                                    <span v-if="item?.remark"></span>
                                    <span v-else class="empty">未填写</span>
                                </p>
                            </view>

                        </view>
                        <view v-else style="display: flex; flex-direction: column;width: 100%;">
                            <view class="line title">
                                <p>{{ item.deptName }} （{{ item.deptId }}）</p>
                            </view>
                            <view class="line content">
                                <p>
                                    <span v-if="item?.remark">{{ item?.remark }}</span>
                                    <span v-else class="empty">未填写</span>
                                </p>
                            </view>
                        </view>
                    </template>
                </uni-list-item>
            </uni-list>
        </z-paging>
    </view>
</template>
<script setup>
import { computed, ref, toRefs } from 'vue';
import { onShow } from "@dcloudio/uni-app";
import { getWarehousingInfo } from '@/api/system';
import getRepository from "@/api/getRepository";
import { objectToQuery } from '../until';
import Navigation from '../components/Navigation.vue';
import { watch } from 'vue';

const props = defineProps({
    // 是否为仓库查询
    isWarehousing: {
        type: Boolean,
        default: true,
        required: true
    },
    pathParams: {
        type: Object,
        default: null,
        required: false
    }

})
const { isWarehousing, pathParams } = toRefs(props)
// 数据：查询关键字
const queryParams = ref({
    keyword: ''
})
const backUrl = ref('')
// stockIn 入库单开单 stockIn_Detail入库单编辑  issueUniqueCode唯一码  issueUniqueCode_inbound入库生成唯一码  
const keyType = computed(() => pathParams.value.type || pathParams.value.back)
// 绑定：加载屏
const pagingRef = ref(null)
// 数据：仓库/存储区
const infoList = ref([])

// 列表判断：获取仓库列表/存储库列表
const getList = () => {
    if (isWarehousing.value) {
        // 仓库列表
        getWarehousing()
    } else {
        // 存储库列表
        getRepositoryInfo()
    }
}

// 列表：仓库列表
const getWarehousing = () => {
    const userInfo = uni.getStorageSync('app_user')
    const params = {
        userId: `${userInfo.userId}`,
        keyword: queryParams.value.keyword
    }
    getWarehousingInfo(params).then(res => {
        res.data.forEach(e => {
            e.showMore = false;
        });
        pagingRef.value.complete(res.data)
    }).catch(res => {
        pagingRef.value.complete(false)
    })
}
// 列表：存储区列表
const getRepositoryInfo = async () => {
    getRepository({ pageNum: 1, pageSize: 10 }).then(res => {
        res.data.forEach(e => {
            e.showMore = false;
        });
        pagingRef.value.complete(res.data)
    }).catch(res => {
        pagingRef.value.complete(false)
    })
}

// 返回路径
const toBack = (keyType) => {
    console.log('监听', keyType);
    let url = ''
    switch (keyType) {
        case 'stockIn':
            // 入库单开单
            url = '/pages/warehousing/stockIn/create'
            break;
        case 'stockOut':
            // 出库单开单
            url = '/pages/warehousing/stockOut/create'
            break;
        case 'stockOut-outbound':
            // 出库单出库
            url = '/pages/warehousing/stockOut/components/outAway'
            break;
    }

    const query = objectToQuery(pathParams.value)
    backUrl.value = `${url}${query}`

}
// 选择：仓库/存储区
const onChecked = (val) => {
    const flag = isWarehousing.value ? 'app_warehousing' : 'app_storageArea'
    let infoChecked = uni.getStorageSync(flag);
    if (!Array.isArray(infoChecked)) {
        infoChecked = [];
    }

    if (val) {
        infoChecked = [{ ...val }]
        uni.setStorageSync(flag, infoChecked);
    }

    // 返回list

    uni.navigateTo({
        url: backUrl.value
    })

}
//  监听：确保 pathParams 变化时一定执行 toBack()
watch(
    () => pathParams.value,
    (d) => {
        console.log('pathParams 变化：', d);
        if (!d) return
        const type = d.type || d.back;
        console.log(type, 'eeeeeee');

        toBack(type)
    },
    { deep: true, immediate: true }
)
onShow(() => {
    pagingRef.value?.reload?.()
})

</script>
<style scoped lang="scss">
.topSearch {
    position: fixed;
    top: 65rpx;
    left: 0;
    right: 0;
    z-index: 999;
    padding: 4rpx;
    background-color: #fff;
    border-bottom: 1px solid #eee;
    box-sizing: border-box;
}

.containerBox {
    padding-top: 120rpx !important;
    width: 100%;
    height: 100%;
    box-sizing: border-box;

    .zp-l-text-rpx {
        font-size: 12px;
    }
}

::v-deep.listBox {
    background-color: #F8F8FF;

    .uni-list-item {
        margin-bottom: 8rpx;
    }

    .uni-list-item__container {
        padding: 0;
    }

    .line {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #eee;
        padding: 12rpx;
    }

    .title {
        font-weight: 600;
        font-size: 14px;
    }

    .content {
        font-weight: 500;
        font-size: 13px;
    }

    .empty {
        color: #D3D3D3;
    }
}
</style>