<template>
    <ifrm ref="ifrm">
        <!-- 子件入库 -->
        <uni-forms ref="wrForm" class="formBox">
            <uni-forms-item>
                <input type="text" v-model="formData.cardNo" @confirm="confirm" class="uni-input-border"
                    placeholder="流程卡" />
            </uni-forms-item>
            <uni-forms-item>
                <uni-combox class="example-body" :candidates="shCandidates" placeholder="请选择仓库"
                    v-model="formData.codeName" @change="codeNameChange"></uni-combox>
            </uni-forms-item>
            <uni-forms-item>
                <uni-combox class="example-body" :disabled="formData.shId == null" :candidates="cslCandidates"
                    placeholder="请选择库位" v-model="formData.location" @change="locationChange"></uni-combox>
            </uni-forms-item>
        </uni-forms>
        <t-table>
            <t-tr>
                <t-td>车间订单号</t-td>
                <t-td>{{ tableObj.woCode }}</t-td>
            </t-tr>
            <t-tr>
                <t-td>流程卡号</t-td>
                <t-td>{{ tableObj.cardNo }}</t-td>
            </t-tr>
            <t-tr>
                <t-td>批次号</t-td>
                <t-td>{{ tableObj.batchNo }}</t-td>
            </t-tr>
            <t-tr>
                <t-td>零件号</t-td>
                <t-td>{{ tableObj.partCode }}</t-td>
            </t-tr>
            <t-tr>
                <t-td>订单数量</t-td>
                <t-td>{{ tableObj.makeQty }}</t-td>
            </t-tr>
            <t-tr>
                <t-td>加工班组</t-td>
                <t-td>{{ tableObj.tsName }}</t-td>
            </t-tr>
        </t-table>
        <view class="buttonBox"><button class="button" type="primary" @click="submitClick">入库</button></view>
    </ifrm>
</template>

<script>
    import ifrm from '@/pages/index/ifrm';
    import tTable from '@/components/t-table/t-table.vue';
    import tTh from '@/components/t-table/t-th.vue';
    import tTr from '@/components/t-table/t-tr.vue';
    import tTd from '@/components/t-table/t-td.vue';
    import scan from '../../mixin/scan.js';
    export default {
        mixins: [scan],
        components: {
            ifrm,
            tTable,
            tTh,
            tTr,
            tTd
        },
        data() {
            return {
                tableObj: {},
                formData: {
                    cardNo: '',
                    codeName: "",
                    location: "",
                    shId: null,
                    cslId: null,
                    wpId: null
                },
                shCandidates: [],
                cslCandidates: [],
                storehouseList: [],
                storeLocationList: [],
            };
        },
        methods: {},
        onNavigationBarButtonTap(btn) {
            this.$refs.ifrm.topMenuClick(btn);
        },
        onShow() {
            this.findStorehouse()
        },
        methods: {
            getBarCode(code, isConfirm = false) {
                this.getData(code);
            },
            confirm(e) {
                this.getBarCode(e.target.value, true);
            },
            getData(code) {
                if (!code) return;
                this.formData.cardNo = code;
                this.$ajax.request({
                    url: 'pdaLoad/loadSubOrder/' + code,
                    method: 'GET',
                    success: data => {
                        this.formData.cardNo = '';
                        this.tableObj = data;
                        this.formData.wpId = data.wpId
                        uni.showToast({
                            title: '数据获取成功'
                        });
                    }
                });
            },
            submitClick() {

                let title = '';
                if (this.formData.cslId == null) {
                    title = '请选择库位';
                }
                if (this.formData.shId == null) {
                    title = '请选择仓库';
                }
                if (!(Object.keys(this.tableObj).length > 0)) {
                    title = '请扫描流程卡';
                }
                if (title != '') {
                    return uni.showToast({
                        title,
                        icon: 'none'
                    });
                }
                this.$ajax.request({
                    url: 'pdaSave/saveSubOrder',
                    data: this.formData,
                    method: 'POST',
                    success: data => {
                        this.tableObj = {};
                        uni.showToast({
                            title: '入库成功'
                        });
                        this.formData = {
                            cardNo: '',
                            codeName: "",
                            location: "",
                            shId: null,
                            cslId: null,
                            wpId: null
                        };
                        this.cslCandidates = []
                        this.storeLocationList = []
                    }
                });
            },
            // 获取仓库
            findStorehouse() {
                this.$ajax.request({
                    url: 'comBox/basic/findStorehouse',
                    method: 'POST',
                    data: {},
                    success: res => {
                        this.shCandidates = []
                        this.storehouseList = []
                        if (res.length > 0) {
                            res.forEach(item => {
                                this.shCandidates.push(item.shCode + '-' + item.shName)
                                this.storehouseList.push(item)
                            })
                        } else {
                            uni.showToast({
                                title: '暂无可用仓库',
                                icon: "none"
                            });
                        }
                    }
                });
            },
            // 获取库位 保存库房id
            codeNameChange(e) {
                this.storehouseList.forEach(item => {
                    if (e == (item.shCode + '-' + item.shName)) {
                        this.formData.shId = item.shId
                    }
                })
                // 获取库位数据
                this.$ajax.request({
                    url: 'comBox/basic/findStorageLocationByShId/' + this.formData.shId,
                    method: 'POST',
                    data: {},
                    success: res => {
                        this.cslCandidates = []
                        this.storeLocationList = []
                        this.formData.location = ""
                        res.forEach(item => {
                            this.cslCandidates.push(item.location)
                            this.storeLocationList.push(item)
                        })
                    }
                });
            },
            // 保存库位id
            locationChange(e) {
                this.storeLocationList.forEach(item => {
                    if (e == item.location) {
                        this.formData.cslId = item.cslId
                    }
                })
            }
        }
    };
</script>
<style scoped lang="less">
    .uni-forms-item {
        margin-bottom: 6px !important;
    }

    .example-body {
        border: 1px solid #bcbcbc;
        border-radius: 4px;
        padding: 0 8px;
    }
</style>