<template>
    <el-dialog :title="rowItem.isBatch ? '批量出库' : '出库'" append-to-body :modelValue="openShow" width="90%" @close="closeDialog" fullscreen>
        <div style="height: 50px;">
            <el-input v-model="formInline.orderId" placeholder="扫描出库单号条形码" @keyup.enter.native="query" style="width: 200px;float: left;"></el-input>
            <el-button type="danger" @click="delFn" style="float: right;">删除</el-button>
        </div>
       
        <avue-crud :option="option" :table-loading="loading" :data="data" v-model="form" v-model:page="page" ref="crud"
            @search-change="searchChange" @search-reset="searchReset" :before-open="beforeOpen"
            @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
            @refresh-change="refreshChange" @on-load="onLoad" @row-save="rowSave"
            @cell-click="cellClick">

        </avue-crud>
        
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script>
export default {
    props: {
        showDialog: {
            type: Boolean,
            default: false
        },
        rowItem: {
            type: Object,
            default: () => ({})
        }
    },
    data() {
        return {
            openShow: false,
            formInline: {
                orderId: '',
                number2: ''
            },
            option: {
                columnSort: true,
                tip: false,
                align: 'center',
                calcHeight: 32,
                simplePage: false,
                page: false,
                searchShow: true,
                searchMenuSpan: 6,
                searchIcon: true,
                searchIndex: 3,
                tree: false,
                border: true,
                index: false,
                selection: true,
                viewBtn: false,
                delBtn: false,
                editBtn: true,
                cellEdit: true,
                menu: true,

                editBtnText: '修改',
                editBtnIcon: ' ',
                delBtnIcon: ' ',
                addBtn: false,
                labelWidth: 140,
                searchLabelWidth: 120,
                menu: false,
                menuWidth: 200,
                dialogWidth: 1200,
                dialogClickModal: false,
                searchEnter: true,
                excelBtn: true,
                gridBtn: false,
                searchShowBtn: false,
                showOverflowTooltip: true,
                header: false,
                searchLabelPosition:'left',
                searchLabelPosition:'left',
                searchGutter:24,
                searchSpan:6,
                menuAlign: 'left',
                gridBtn:false,
                searchMenuPosition:'right',
                addBtnIcon: ' ',
            viewBtnIcon: ' ',
            delBtnIcon: ' ',
            editBtnIcon: ' ',

                column: [
                    {
                        label: '车间订单号',
                        prop: 'woCode',
                        search: false,
                    },
                    {
                        label: '物料号',
                        prop: 'c_materiel_id',
                        search: false,
                    },
                    {
                        label: '物料名称',
                        prop: 'partNames',
                        search: false,
                    },
                    {
                        label: '物料批次号',
                        prop: 'goodbatchNo',
                        search: false,
                    },
                    {
                        label: '粉重',
                        prop: 'fenzhong',
                        search: false,
                    },
                    {
                        label: '是否印字',
                        prop: 'shifouyinzi',
                        search: false,
                    },
                    {
                        label: '厚度',
                        prop: 'houdu',
                        search: false,
                    },
                    {
                        label: '仓库',
                        prop: 'bsStoreRoomSet',
                        search: false,
                    },
                    {
                        label: '库位',
                        prop: 'goodsKuwei',
                        search: false,
                        type: 'select',
                        dicData: [
                            { label: '库位', value: '库位一' },
                            { label: '库位二', value: '库位二' },
                            { label: '库位三', value: '库位三' },
                            { label: '库位四', value: '库位四' },
                        ],
                        // rules: [
                        //     { required: true, message: '请选择库位', trigger: 'change' }
                        // ],
                        editDisabled: false,
                        addDisabled: false,
                        cell: true
                    },
                    {
                        label: '出库数量',
                        prop: 'chukushuliang',
                        search: false,
                    },
                    {
                        label: '实际出库玻璃饼型号',
                        prop: 'actualPartCode',
                        search: false,
                        type: 'select',
                        dicData: [
                            { label: '型号A', value: 'A' },
                            { label: '型号B', value: 'B' },
                            { label: '型号C', value: 'C' },
                            { label: '型号D', value: 'D' },
                        ],
                        // rules: [
                        //     { required: true, message: '请选择实际出库玻璃饼型号', trigger: 'change' }
                        // ],
                        editDisabled: false,
                        addDisabled: false,
                        cell: true
                    },
                    {
                        label: '实际库位',
                        prop: 'actualKuwei',
                        search: false,
                        type: 'select',
                        dicData: [
                            { label: '库位', value: '库位一' },
                            { label: '库位二', value: '库位二' },
                            { label: '库位三', value: '库位三' },
                            { label: '库位四', value: '库位四' },
                        ],
                        
                    },

                ]
            },
            data: [],

        }
    },
    mounted() {
        this.openShow = this.showDialog;
        this.onLoad();
    },
    watch: {
        showDialog(newVal) {
            this.openShow = newVal;
            if (newVal) {
                this.onLoad();
            }
        },
        rowItem: {
            handler() {
                if (this.openShow) {
                    this.onLoad();
                }
            },
            deep: true
        }
    },
    methods: {
        query() {
            this.formInline.orderId = ''
            this.data.push({
                brCode: 'aaaa',
                woCode: 'WO-090899899',
                partCode: 'E6-78787-E1',
                partName: '零件一',
                batchNo: '9098900',
                okNum: '88',
                c_materiel_id: '89',
                partNames: '8989',
                xuqiushuliang: 99,
                chukushuliang: 67,
                goodbatchNo: '898989',
                bsStoreRoomSet: '仓库一',
                goodsKuwei: '库位一',
                status: 1,
                times: '2025-09-08'
            })
        },
        closeDialog() {
            this.openShow = false
            this.$emit('closeDialog');
        },
        submit() {
            this.$message.success('提交成功')
            this.openShow = false
        },
        delFn() {
            this.$confirm('确定将选择数据删除?', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    // return removeApiScope(this.scopeIds);
                })
                .then(() => {
                    // this.onLoadScope(this.pageScope);
                    // this.$message({
                    //     type: 'success',
                    //     message: '操作成功!',
                    // });
                    // this.$refs.crudScope.toggleSelection();
                });
        },
        cellClick(row, column, cell, event) {
            if (column.property === 'actualPartCode' || column.property === 'goodsKuwei') {
                this.$refs.crud.rowCellEdit(row, column);
            }
        },
        onLoad() {
            if (this.rowItem) {
                if (this.rowItem.isBatch && this.rowItem.items) {
                    this.data = this.rowItem.items.map(item => ({
                        ...item,
                    
                    }));
                } 
                else if (this.rowItem.brCode) {
                    this.data = [{
                        ...this.rowItem,
                        actualPartCode: this.rowItem.actualPartCode || 'A',
                        actualKuwei: this.rowItem.actualKuwei || '库位一'
                    }];
                } else {
                    this.data = [
                        {
                            brCode: 'BR2023001',
                            woCode: 'WO-2023001',
                            partCode: 'E6-78787-E1',
                            partName: '光纤连接器组件',
                            batchNo: 'B2023001',
                            okNum: '100',
                            c_materiel_id: 'MAT001',
                            partNames: '光纤连接器',
                            xuqiushuliang: 100,
                            chukushuliang: 80,
                            goodbatchNo: 'GB2023001',
                            bsStoreRoomSet: 'A区仓库',
                            goodsKuwei: 'A-01-01',
                            status: 1,
                            times: '2023-06-15',
                            // 添加默认的实际出库玻璃饼型号和库位
                            actualPartCode: 'A',
                            actualKuwei: '库位一'
                        },
                        {
                            brCode: 'BR2023002',
                            woCode: 'WO-2023002',
                            partCode: 'E6-78787-E2',
                            partName: '光纤适配器组件',
                            batchNo: 'B2023002',
                            okNum: '150',
                            c_materiel_id: 'MAT002',
                            partNames: '光纤适配器',
                            xuqiushuliang: 150,
                            chukushuliang: 120,
                            goodbatchNo: 'GB2023002',
                            bsStoreRoomSet: 'B区仓库',
                            goodsKuwei: 'B-02-03',
                            status: 1,
                            times: '2023-06-16',
                            actualPartCode: 'B',
                            actualKuwei: '库位二'
                        },
                        {
                            brCode: 'BR2023003',
                            woCode: 'WO-2023003',
                            partCode: 'E6-78787-E3',
                            partName: '光纤耦合器组件',
                            batchNo: 'B2023003',
                            okNum: '200',
                            c_materiel_id: 'MAT003',
                            partNames: '光纤耦合器',
                            xuqiushuliang: 200,
                            chukushuliang: 180,
                            goodbatchNo: 'GB2023003',
                            bsStoreRoomSet: 'C区仓库',
                            goodsKuwei: 'C-03-02',
                            status: 1,
                            times: '2023-06-17',
                            actualPartCode: 'C',
                            actualKuwei: '库位三'
                        }
                    ];
            }
            this.page.total = this.data.length
        }
    }
}
}
</script>
<style lang="scss" scoped></style>