<template>
    <div>
        <el-card>
            <el-form :inline="true" :model="filterQuery" size="mini">
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.invStorageCode" placeholder="请选择当前仓库" clearable="true"
                                   @change="invChange" size="mini">
                            <el-option
                                v-for="item in storageList"
                                :key="item.name"
                                :label="item.name"
                                :value="item.code">
                                <span style="float: left">{{ item.name }}</span>
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.invWarehouseCode" placeholder="请选择当前分库" clearable="true"
                                   @change="invWarehouseChange" size="mini">
                            <el-option
                                v-for="item in subInvList"
                                :key="item.name"
                                :label="item.name"
                                :value="item.code">
                                <span style="float: left">{{ item.name }}</span>
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.invSpaceCode" placeholder="请选择当前货位" clearable="true"
                                   size="mini">
                            <el-option
                                v-for="item in spaceCodeList"
                                :key="item.name"
                                :label="item.name"
                                :value="item.code">
                                <span style="float: left">{{ item.name }}</span>
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button-group>
                            <el-button
                                type="primary"
                                icon="el-icon-refresh"
                                @click="onReset"
                            ></el-button>
                            <el-button type="primary" icon="search" @click="onSubmit"
                            >查询
                            </el-button
                            >
                            <el-button type="primary" icon="search" @click="goodsPlacement"
                            >摆放
                            </el-button
                            >
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>

            <el-table v-loading="loading" :data="list" style="width: 100%">
                <el-table-column label="序号" type="index" fixed></el-table-column>
                <el-table-column label="仓库" prop="invStorageName" width="180"></el-table-column>
                <el-table-column label="分库" prop="invWarehouseName" width="180"></el-table-column>
                <el-table-column label="货位" prop="invSpaceName" width="180"></el-table-column>
                <el-table-column label="产品标识" prop="nameCode" width="120"></el-table-column>
                <el-table-column label="产品名称" prop="productName" width="200"></el-table-column>
                <el-table-column label="规格型号" prop="ggxh" width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="批次号" prop="batchNo" width="120"></el-table-column>
                <el-table-column label="生产日期" prop="productionDate" width="120"></el-table-column>
                <el-table-column label="失效日期" prop="expireDate" width="120"></el-table-column>
                <el-table-column label="批准文号" prop="zczbhhzbapzbh" v-if="showSup" show-overflow-tooltip
                                 width="120"></el-table-column>
                <el-table-column label="生产厂家" prop="ylqxzcrbarmc" v-if="showSup" show-overflow-tooltip
                                 width="120"></el-table-column>
            </el-table>
        </el-card>

        <el-pagination
            :page-size="filterQuery.limit"
            :current-page="filterQuery.page"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>

        <el-dialog
            title="货物摆放"
            :visible.sync="placeDialogVisible"
            width="70%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="placeDialogVisible"
        >
            <el-form :model="formData" :rules="formRules" ref="dataForm">
                <el-button-group style="display: flex;margin: -50px 0 10px 80%; height: 35px">
                    <el-button
                        type="primary"
                        @click.native="saveData()"
                    >提交
                    </el-button
                    >
                </el-button-group>
                <el-row :gutter="20" style="margin-top: 0px">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>当前仓库：</span>
                        </div>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item prop="locStorageCode">
                            <el-select v-model="formData.invStorageCode" placeholder="当前仓库信息"
                                       @change="locInChange"
                                       :disabled="codeArray.length>0"
                                       clearable>
                                <el-option
                                    v-for="item in storageList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>


                    <el-col :span="3">
                        <div class="ao-text">
                            <span>当前分库：</span>
                        </div>
                    </el-col>

                    <el-col :span="7">
                        <el-form-item prop="invWarehouseCode">
                            <el-select v-model="formData.invWarehouseCode" placeholder="当前分库信息"
                                       :disabled="codeArray.length>0"
                                       @change="subStorageChange"
                                       clearable>
                                <el-option
                                    v-for="item in formSubInvList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>


                <el-row :gutter="20">
                    <el-col :span="3">
                        <div class="ao-text">
                            <span>当前货位：</span>
                        </div>
                    </el-col>
                    <el-col :span="7">
                        <el-form-item class="query-form-item" prop="invSpaceCode"
                        >
                            <el-select v-model="formData.invSpaceCode" placeholder="当前货位信息"
                                       :disabled="codeArray.length>0"
                                       clearable>
                                <el-option
                                    v-for="item in formSpaceList"
                                    :key="item.name"
                                    :label="item.name"
                                    :value="item.code">
                                    <span style="float: left">{{ item.name }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" style="margin-top: 12px">
                    <el-col :span="2">
                        <div class="ao-text">
                            <el-link
                                target="_blank">{{ scanText }}
                            </el-link>
                        </div>
                    </el-col>
                    <el-col :span="16">
                        <el-form-item prop="code">
                            <el-input
                                id="inputer"
                                @focus="getInputFocus($event)"
                                @keypress.enter.native="enterKey($event)"
                                ref='inputRef'
                                style="ime-mode:disabled"
                                type="tel"
                                v-model="formData.code"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="2">
                        <el-button
                            type="primary"
                            size="small"
                            @click.native.stop="addCode()"
                            style="height: 31px; margin-left: 20px"
                        >添加
                        </el-button
                        >
                    </el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="14" style="margin-left: 40px">

                    </el-col>
                    <el-col :span="3">
                        <div class="text item" style="margin-left: 30px"> 条码数量：{{ codeArray.length }}
                        </div>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" :data="codeArray" style="width: 100%;margin-top: 20px;"
                          max-height="300" height="300" ref="multipleTable">
                    <el-table-column label="序号" type="index" width="50"></el-table-column>
                    <el-table-column width="300" label="条码" prop="code" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="150">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="small"
                                @click.stop="true"
                                @click.native="deleteCodeArray(scope.$index, scope.row)"
                            >删除
                            </el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>
            </el-form>
        </el-dialog>

    </div>
</template>

<script>
import draggable from "vuedraggable";
import {filterUplLocInv} from "@/api/basic/invWarehouse";
import {filterSubByInv} from "@/api/basic/invSubWarehouse";
import {getSpaceCodeList} from "@/api/inventory/invSpace";
import {getPlacementDetailList, bindInvSpace, checkCodeSpace} from "@/api/inventory/InvProducts";
import {isBlank} from "@/utils/strUtil";
import {userInfo} from "@/api/auth/login";


export default {
    data() {
        return {
            filterQuery: {
                invWarehouseCode: null,
                page: 1,
                limit: 20,
                invStorageCode: null,
                invSpaceCode: null,
            },
            detailQuery: {
                code: null,
                productIdFk: null,
                page: 1,
                limit: 20,
            },
            list: [],
            total: 0,
            loading: false,
            index: null,
            dialogTableVisible: false,
            formLoading: false,
            dialogVisible: false,
            deleteLoading: false,
            idQuery: null,
            showSup: false,
            fromOptions: [],
            storageOptions: [],
            storageList: [],
            subInvList: [],
            spaceCodeList: [],
            formSubInvList: [],
            formSpaceList: [],
            placeDialogVisible: false,
            formData: {
                invStorageCode: null,
                invWarehouseCode: null,
                invSpaceCode: null,
                code: null,
                codeArray: []
            },
            formRules: [],
            codeArray: [],
            scanText: "扫码录入：",
            originCode: null,
        };
    },
    components: {
        draggable,
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                invStorageCode: null,
                invWarehouseCode: null,
                invSpaceCode: null,
                page: 1,
                limit: 20,
            };
            this.getList();
        },
        onSubmit() {
            this.getList();
        },
        handleSizeChange(val) {
            this.filterQuery.limit = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        closeDialog() {
            this.getList();
        },
        getList() {
            this.loading = true
            getPlacementDetailList(this.filterQuery).then((res) => {
                this.loading = false;
                if (res.code === 20000) {
                    this.list = res.data.list || [];
                    this.total = res.data.total || 0;
                } else {
                    this.list = [];
                    this.total = 0;
                }
            }).catch((error) => {
                this.loading = false;
                this.list = [];
                this.total = 0;
            });
        },
        getStorage() {
            this.storageList = [];
            filterUplLocInv()
                .then((response) => {
                    this.storageList = response.data || [];
                })
                .catch(() => {
                });
        },
        invChange() {
            this.filterQuery.invWarehouseCode = null;
            this.filterQuery.invSpaceCode = null;
            this.subInvList = [];
            this.spaceCodeList = [];
            let query = {
                pcode: this.filterQuery.invStorageCode
            };
            filterSubByInv(query)
                .then((response) => {
                    this.subInvList = response.data || [];
                })
                .catch(() => {
                });
        },
        invWarehouseChange() {
            this.filterQuery.invSpaceCode = null;
            this.spaceCodeList = [];
            let params = {
                invStorageCode: this.filterQuery.invStorageCode,
                invWarehouseCode: this.filterQuery.invWarehouseCode,
                status: true
            };
            getSpaceCodeList(params).then((res) => {
                this.spaceCodeList = res.data || [];
            })
        },
        goodsPlacement() {
            this.placeDialogVisible = true;
            this.formData = {
                code: null,
                invStorageCode: null,
                invWarehouseCode: null,
                invSpaceCode: null
            };
            this.codeArray = [];
        },
        saveData() {
            if (isBlank(this.formData.invStorageCode) || isBlank(this.formData.invWarehouseCode)
                || isBlank(this.formData.invSpaceCode)) {
                this.$message.warning("请选择仓库和货位信息");
                return;
            }
            if (this.codeArray.length === 0) {
                this.$message.warning("请扫描需要摆放的货物");
                return;
            }
            this.formData.codeArray = [];
            this.codeArray.forEach(item => {
                this.formData.codeArray.push(item.code);
            })
            bindInvSpace(this.formData).then((res) => {
                if (res.code === 20000) {
                    this.$message.success("绑定成功");
                    this.placeDialogVisible = false;
                    this.getList();
                } else {
                    this.$message.error("绑定失败");
                }
            })
        },
        deleteCodeArray(index, row) {
            for (let i = 0; i < this.codeArray.length; i++) {
                if (this.codeArray[i] === row) {
                    this.codeArray.splice(i, 1);
                    return;
                }
            }
        },
        getInputFocus(event) {
            event.currentTarget.select();
        },
        enterKey(event) {
            this.checkSuccess = true;
            let tQuery = {
                code: this.formData.code.trim(),
                invStorageCode: this.formData.invStorageCode,
                invWarehouseCode: this.formData.invWarehouseCode,
                invSpaceCode: this.formData.invSpaceCode
            }
            this.addCode(event);
            this.$refs.inputRef.focus();
            this.$refs.inputRef.select();
        },
        addCode(event) {
            this.originCode = "";
            if (event != null) {
                event.target.select();
            }
            this.actionEnable = true;
            this.$refs.inputRef.select();
            if (isBlank(this.formData.invStorageCode)) {
                this.$message.error('当前仓库不能为空');
                return;
            }

            if (isBlank(this.formData.invWarehouseCode)) {
                this.$message.error('当前分库不能为空');
                return;
            }

            if (isBlank(this.formData.invSpaceCode)) {
                this.$message.error('当前货位不能为空');
                return;
            }

            this.formData.code = this.formData.code.trim();
            if (this.$isBlank(this.formData.code)) return;
            let tQuery = Object.assign(JSON.parse(JSON.stringify(this.formData)));
            this.checkCode(tQuery);
            this.$refs.inputRef.select();
        },
        locInChange() {
            this.formData.invWarehouseCode = null;
            this.formData.invSpaceCode = null;
            this.formSubInvList = [];
            this.formSpaceList = [];
            let params = {pcode: this.formData.invStorageCode};
            filterSubByInv(params).then((res) => {
                this.formSubInvList = res.data || [];
            })
        },
        subStorageChange() {
            this.formData.invSpaceCode = null;
            this.formSpaceList = [];
            let params = {
                invStorageCode: this.formData.invStorageCode,
                invWarehouseCode: this.formData.invWarehouseCode,
                status: true
            };
            getSpaceCodeList(params).then((res) => {
                this.formSpaceList = res.data || [];
            })
        },
        checkCode(tQuery) {
            //检验是否重复添加
            let flag = false;
            this.codeArray.forEach(item => {
                if (item.code === this.formData.code) {
                    this.$message.error("重复条码！");
                    flag = true;
                    return;
                }
            })
            if (flag) {
                return;
            }
            //校验此条码是否已经被绑定到其他货位，如已被绑定，则提示，点击确定之后，重置此库存数据的仓库和分库数据
            checkCodeSpace(tQuery).then((res) => {
                if (res.code === 20000) {
                    this.codeArray.push({code: this.formData.code});
                } else if (res.code === 8) {
                    this.loading = false;
                    this.$message.error(res.message);
                } else if (res.code === 7) {
                    this.$confirm(res.data, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.codeArray.push(this.formData.code);
                    }).catch(() => {
                        this.loading = false;
                        this.$message({
                            type: 'info',
                            message: '已取消'
                        });
                    });
                }
            });
        },
    },
    created() {
        this.getStorage();
        //获取用户默认仓库信息
        userInfo().then((res) => {
            if (res.code === 20000) {
                if (!isBlank(res.data.locInvCode)) {
                    this.filterQuery.invStorageCode = res.data.locInvCode;
                    this.invChange();
                    this.filterQuery.invWarehouseCode = res.data.locSubInvCode;
                    this.invWarehouseChange();
                }
                this.getList();
            }
        }).catch((error) => {
        });
    },
};
</script>

<style type="text/scss" lang="scss">
</style>
