<!--点击入库单入库后的入库页面 -->
<template>
    <view class="page">
        <navigation title="入库" :back-url="backUrl"></navigation>
        <!-- 仓库信息 - 仓库、存储区 -->
        <warehousing-info ref="warehousingInfoRef" :warehouseInfo="warehouseInfo"
            :pathParams="{ ...pathParams, back: 'stockIn' }" />
        <!-- 物料列表 - 添加物料 -->
        <material-list ref="materialRef" :formData="formData" isEdit="5" :pathParams="pathParams":extendData="{ selectMaterialList }" />
        <!-- 底部操作栏 -->
        <view class="bottom">
            <uv-button type="primary" @click="submitForm">入库</uv-button>
        </view>
    </view>
</template>

<script setup>
import _ from 'lodash';
import { ref, onMounted } from 'vue';
import { onLoad } from "@dcloudio/uni-app";
import { objectToQuery } from '../../../until';
import { inboundFinish, stockInDetail } from '@/api/stockIn';
import Navigation from '../../../components/Navigation.vue';
import MaterialList from '../../../components/MaterialList.vue';
import WarehousingInfo from '../../../components/WarehousingInfo.vue';

//  stockIn-putAway：入库单入库编辑 
const backUrl = ref('')
// 数据：路径参数
const pathParams = ref('')
// ref：物料绑定
const materialRef = ref([])
// ref：仓库信息绑定
const warehousingInfoRef = ref([])
// ref:选中的物料
const selectMaterialList = ref([])
// 数据：物料列表
const formData = ref([{ remark: '', material: [] }])
// 数据：仓库信息
const warehouseInfo = ref({ warehousing: {}, storageArea: {}, remark: '' })

// 数据：获取缓存信息
const getMaterialList = () => {
    // 获取仓库信息
    const warehouse = uni.getStorageSync('app_warehousing');
    warehouseInfo.value.warehousing = warehouse

    // 获取存储区信息
    const storageArea = uni.getStorageSync('app_storageArea');
    warehouseInfo.value.storageArea = storageArea
    // 获取物料信息 - 有uniqueCode
    const material = uni.getStorageSync('app_material');
    formData.value.material = material
    // 获取物料信息 - 无uniqueCode
    const selectMaterial = uni.getStorageSync('app_material_select_list');
    selectMaterialList.value = selectMaterial
    // 获取入库单备注
    const billRemark = uni.getStorageSync('app_billRemark');
    warehouseInfo.value.remark = billRemark
    console.log('获取缓存', material, selectMaterial);

}

// 方法：获取详情 - 更新物料得值 获取唯一码后回来
const getDetailInfo = () => {
    stockInDetail({ billNo: pathParams.value.billNo }).then((res) => {
        setDetailInfoToStorage(res.data?.itemList)
    })
}
// 方法：存入缓存 - 更新物料得值 获取唯一码后回来
const setDetailInfoToStorage = (val) => {
    // 过滤出有uniqueCode得值
    const material = _.filter(val, (i) => i?.uniqueCode && i.status == '0')
    const materialSelectList = _.filter(val, (i) => !i?.uniqueCode && i.status == '0')
    uni.setStorageSync('app_material', material)
    uni.setStorageSync('app_material_select_list', materialSelectList)
    getMaterialList()
}
// 入库:提交表单
const submitForm = () => {
    const info = warehousingInfoRef.value.getWarehousingInfo()
    const materialInfo = materialRef.value.getMaterialList()
    // 过滤出已删除的物料
    const _materialInfo = _.filter(materialInfo, { isDelete: '0' })
    let params = {
        billType: '1',//0入库申请 1入库成功 2出库申请 3出库成功 4入库单已作废 5出库单已作废
        billNo: pathParams.value.billNo,
        warehouseCode: info.warehousing?.[0].deptCode || info.warehousing?.[0].deptId,
        warehouseName: info.warehousing?.[0].deptName,
        areaCode: info.storageArea?.[0].deptCode || info.storageArea?.[0].deptId,
        areaName: info.storageArea?.[0].deptName,
        remark: info.remark,
        billRemark: info.remark,
        itemList: _.map(_materialInfo, (i) => ({ ...i.material }))
    }
    try {
        inboundFinish(params).then((res) => {
            uni.navigateTo({ url: backUrl.value });
        })
    } catch (err) {
        console.log('入库单入库失败==》', err);
    }


}
// 数据：路径参数
onLoad((options) => {
    if (!options.billNo) {
        uni.showToast({ title: '参数错误,无唯一码ID', icon: 'none' })
        return
    }
    pathParams.value = options
    const query = objectToQuery(pathParams.value)
    backUrl.value = `/pages/warehousing/stockIn/components/detail${query}`
    getDetailInfo()

})

onMounted(() => {
    getMaterialList()
})
</script>

<style lang="scss" scoped>
.page {
    background: #f5f5f5;
    min-height: 100vh;
}

/* 底部按钮 */
::v-deep .bottom {
    position: fixed;
    bottom: 0;
    height: 60rpx;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;

    .uv-button-wrapper {
        width: 100%;
        border-radius: 0;
    }

}
</style>