<!-- 唯一码发放页面 -->
<template>
    <view class="content">
        <view class="remarkLine">
            <span>备注：</span>
            <uni-easyinput type="text" v-model="formData.remark" :inputBorder="false" placeholder="请输入备注" />
        </view>

        <!-- 物料列表 - 添加物料 -->
        <material-list ref="materialRef" :formData="formData" :remark="formData.remark" isEdit="1" :backStr="backStr"
            :pathParams="pathParams" />
        <button class="bottom-btn" type="primary" @click="handleSubmit">提交</button>
    </view>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import { addUniqueCode, editUniqueCode } from '@/api/uniqueCode';
import MaterialList from '../../../components/MaterialList.vue';
import { computed } from 'vue';
import _ from 'lodash';

// 数据：路径参数
const pathParams = ref('')
// 数据：物料、备注
const formData = ref({ remark: '', material: [] })
// ref:绑定物料列表
const materialRef = ref([])
const backStr = computed(() => pathParams.value.back == 'inbound' ? `issueUniqueCode_inbound` : 'issueUniqueCode')
// 接收路径参数 - 修改标题- 携带code、id、materialId
onLoad((options) => {
    pathParams.value = options

})
onShow(() => {
    if (pathParams.value.title) {
        uni.setNavigationBarTitle({ title: pathParams.value.title })
    }
    if (pathParams.value.back == 'inbound') {
        uni.setNavigationBarTitle({ title: '快速生成唯一码' })
    }
})

onMounted(() => {
    // 快速生成唯一码时 已选择的物料列表初始为空 点击添加物料 去选择
    if (pathParams.value.back != 'inbound') {
        getMaterialList();
    }

})
// 创建/编辑 唯一码
const handleSubmit = () => {
    //  物料最新编辑数据
    const material = materialRef.value.getMaterialList()?.[0]

    const params = {
        remark: formData.value.remark,
        material: { ...material.material, unitId: '23' }
    }
    if (pathParams.value.back == `issueUniqueCode_inbound`) {
        params.billNo = pathParams.value.billNo
    }
    if (pathParams.value.id) {
        params.id = pathParams.value.id; //唯一码id
        params.material.id = formData.value.material?.[0]?.uniqueCodeMaterialId; //唯一码物料id
        params.material.materialId = formData.value.material?.[0]?.materialId; //物料id
        editUniqueCode(params).then((response) => {
            if (response.code == 200) {
                uni.showToast({
                    title: '唯一码编辑成功',
                    mask: true,
                    icon: 'success',
                })
                uni.navigateTo({
                    url: `/pages/warehousing/uniqueCode/myUniqueCode/detail?id=${pathParams.value.id}&title=${pathParams.value.title}`,
                });
            }

        })
    } else {
        addUniqueCode(params).then((response) => {
            if (response.code == 200) {
                uni.showToast({
                    title: '唯一码创建成功',
                    mask: true,
                    icon: 'success',
                })
                if (_.includes(pathParams.value?.back, 'inbound')) {
                    // /pages/warehousing/uniqueCode/myUniqueCode/detail?id=38&code=600038
                    uni.navigateTo({
                        url: `/pages/warehousing/stockIn/components/inbound?billNo=${pathParams.value.billNo}`,
                    });
                } else {
                    uni.switchTab({
                        url: "/pages/warehousing/index"
                    })
                }
                // /pages/warehousing/stockIn/components/inbound?billNo=WR1775032495153

            }

        })
    }
}

// 获取缓存内的物料列表
const getMaterialList = () => {
    const value = uni.getStorageSync('app_material');
    if (value) {
        formData.value.material = value
        if (value?.[0]?.uniqueCodeRemark) {
            formData.value.remark = value?.[0]?.uniqueCodeRemark
        }
    }
}

</script>
<style scoped lang="scss">
.remarkLine {
    display: flex;
    align-items: center;
    background-color: #fff;
    justify-content: space-between;
    padding: 8rpx 24rpx;
    margin-bottom: 8rpx;
    min-height: 40rpx;

    span {
        font-size: 14px;
    }

    p {
        font-weight: 700;
        font-size: 14px;

    }

}

::v-deep .uv-button {
    height: 20px;
}

::v-deep .form {
    .title {
        color: #424242;
    }

    .tag {
        .uni-tag {
            display: flex;
            align-items: center;
            height: calc(25px - 6px);

        }

        .tag-form {
            display: flex;
            align-items: center;
        }

        .uni-forms-item {
            margin-bottom: 0;
            align-items: center;

            .uni-easyinput__content,
            .uni-easyinput__content-input {
                height: 25px;
                width: 80px;
            }
        }

    }

    .remark {
        .uni-forms-item {
            margin-bottom: 0;
            margin-top: 16rpx;
            display: flex;
            align-items: center;

            .uni-forms-item__label {
                color: #212121;
                height: 40rpx;
            }

            .uni-easyinput__content-input {
                height: 40rpx;

                .uni-input-wrapper {
                    text-align: right;
                }
            }

            .uni-input-placeholder {
                text-align: right;
                font-size: 13px;

            }
        }
    }


}
</style>