<template>
    <div>

        <el-form :inline="true" :model="query" class="query-form" size="mini">
            <el-form-item class="query-form-item">
                <el-input v-model="query.code" placeholder="条码查询" clearable></el-input>
            </el-form-item>
            <el-form-item>
                <el-button-group>
                    <el-button type="primary" icon="search" @click="searchCode"
                    >查询
                    </el-button
                    >
                </el-button-group>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="codeArray" style="width: 100%">
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column
                label="条码"
                prop="code"
                width="500"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="操作"
                width="120"
                fixed="right"
                v-if="codeQuery.edit"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        size="small"
                        @click.stop="true"
                        @click.native="deleteCountCode(scope.row)"
                    >减一
                    </el-button
                    >
                </template>
            </el-table-column>
        </el-table>

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

<script>

import {getCountCodesList, deleteCode} from "@/api/inventory/invCountCodes";

export default {
    name: "codeQuery",
    props: {
        codeQuery: {
            type: Object,
            required: true,
        },
        getCountOrderDetail: {
            type: Function,
            required: true
        }
    },
    data() {
        return {
            query: {
                productId: null,
                orderIdFk: null,
                page: 1,
                limit: 20
            },
            loading: true,
            codeArray: [],
            total: 0,
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.query = {
                productId: this.codeArray.productId,
                orderIdFk: this.codeQuery.orderIdFk,
                page: 1,
                limit: 20
            };
            this.getCodeList();
        },
        onSubmit() {
            this.$router.push({
                path: "",
                query: this.query,
            });
            this.getCodeList();
        },
        handleSizeChange(val) {
            this.query.limit = val;
            this.getCodeList();
        },
        handleCurrentChange(val) {
            this.query.page = val;
            this.getCodeList();
        },
        searchCode() {
            this.query.page = 1;
            this.getCodeList();
        },
        getCodeList() {
            this.loading = true;
            this.query.productId = this.codeQuery.productId;
            this.query.orderIdfK = this.codeQuery.orderIdfK;
            getCountCodesList(this.query).then((res) => {
                this.loading = false;
                if (res.code === 20000) {
                    this.codeArray = res.data.list || [];
                    this.total = res.data.total || 0;
                } else {
                    this.codeArray = [];
                    this.total = 0;
                }
            }).catch((error) => {
                this.loading = false;
                this.codeArray = [];
                this.total = 0;
            })
        },
        deleteCountCode(row) {
            this.$confirm('是否删除此条码?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    orderIdFk: row.orderIdFk,
                    productId: row.productId,
                    code: row.code
                };
                deleteCode(params).then((res) => {
                    if (res.code === 20000) {
                        this.$message.success("删除成功！");
                        this.getCodeList();
                        this.getCountOrderDetail();
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            }).catch(() => {
            });
        },
    },
    created() {
        this.getCodeList();
    },
};
</script>



