<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-input v-model="filterQuery.nameCode" placeholder="最小销售产品标识" :clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input v-model="filterQuery.cpmctymc" placeholder="产品名称" :clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input v-model="filterQuery.ggxh" placeholder="规格型号" :clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input v-model="filterQuery.batchNo" placeholder="批次号" :clearable="true"></el-input>
                    </el-form-item>
                </el-row>
                <el-row style="width: 100%;">
                    <el-form-item class="query-form-item">
                        <el-input v-model="filterQuery.manufactory" placeholder="生产厂家" :clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input v-model="filterQuery.zczbhhzbapzbh" placeholder="注册/备案证号"
                                  :clearable="true"></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="filterQuery.invStorageCode" placeholder="请选择仓库" clearable="true" 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>
                        <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-group>
                    </el-form-item>
                </el-row>
            </el-form>

            <el-table v-loading="loading" :data="list" style="width: 100%">
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="产品名称" width="200" prop="productsName"></el-table-column>
                <el-table-column label="产品标识" width="150" prop="nameCode"></el-table-column>
                <el-table-column label="规格型号" width="240" prop="ggxh"></el-table-column>
                <el-table-column label="批次号" prop="batchNo"></el-table-column>
                <el-table-column label="生产日期" prop="productionDate"></el-table-column>
                <el-table-column label="失效日期" prop="expireDate"></el-table-column>
                <el-table-column label="入库数量" prop="inCount"></el-table-column>
                <el-table-column label="出库数量" prop="outCount"></el-table-column>
                <el-table-column label="结余数量" prop="reCount"></el-table-column>
                <el-table-column label="仓库" prop="invStorageName"></el-table-column>
                <el-table-column label="注册/备案证号" width="180" prop="zczbhhzbapzbh"></el-table-column>
                <el-table-column label="生产厂家" width="200" prop="manufactory"></el-table-column>
                <el-table-column label="操作" width="80" fixed="right">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="intentDetail(scope.row)"
                        >详情
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-dialog
            title="库存码详情"
            :visible.sync="codeDetailVisible"
            width="65%"
            v-if="codeDetailVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <codeDetail
                :idQuery="idQuery"
                v-on:closeDetailDialog="closeDetailDialog"
            ></codeDetail>
        </el-dialog>
        <el-pagination
            :current-page="filterQuery.page"
            :page-size="filterQuery.limit"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>
    </div>
</template>

<script>
import {
    filterProducts,
    deleteProducts
} from "../../api/warehouse/InvProducts";
import draggable from "vuedraggable";
import codeDetail from "../inventory/InvPreProductsDetail";
import store from "../../store";
import {getBusType} from "../../api/warehouse/BusRole";
import {filterAllByUser} from "@/api/basic/invWarehouse";


export default {
    data() {
        return {
            filterQuery: {
                cpmctymc: null,
                nameCode: null,
                relIdFk: null,
                batchNo: null,
                ggxh: null,
                manufactory: null,
                zczbhhzbapzbh: null,
                customerId: store.getters.customerId,
                invStorageCode: null,
                page: 1,
                limit: 20
            },
            detailQuery: {
                code: null,
                productIdFk: null,
                page: 1,
                limit: 20
            },
            list: [],
            codeDetailVisible: false,
            total: 0,
            loading: true,
            index: null,
            dialogTableVisible: false,
            formLoading: false,
            dialogVisible: false,
            deleteLoading: false,
            busTypes: [],
            idQuery: null,
            storageList: [],
        };
    },
    components: {
        draggable,
        codeDetail
    },
    methods: {
        onReset() {
            this.$router.push({
                path: ""
            });
            this.filterQuery = {
                cpmctymc: null,
                nameCode: null,
                relIdFk: null,
                batchNo: null,
                ggxh: null,
                manufactory: null,
                zczbhhzbapzbh: null,
                customerId: store.getters.customerId,
                invStorageCode: null,
                page: 1,
                limit: 20
            };
            this.getList();
        },
        onSubmit() {
            this.loading = true;
            this.filterQuery.page = 1;
            this.getList();
        },
        handleSizeChange(val) {
            this.filterQuery.limit = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        handleUnitClick(row) {
            this.curIndex = row.id;
            this.dialogTableVisible = true;
        },
        closeDetailDialog(val) {
            this.codeDetailVisible = false;
        },

        closeDialog() {
            this.getList();
        },
        getList() {
            this.loading = true;
            this.filterQuery.customerId = store.getters.customerId;
            filterProducts(this.filterQuery)
                .then((response) => {
                    this.loading = false;
                    this.list = response.data.list || [];
                    let tempList = this.list;
                    for (let i = 0; i < tempList.length; i++) {
                        let reCount = tempList[i].inCount - tempList[i].outCount;
                        tempList[i].reCount = reCount;
                    }
                    this.list = tempList;
                    this.total = response.data.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        getStorage() {
            this.storageList = [];
            filterAllByUser()
                .then((response) => {
                    this.storageList = response.data || [];
                })
                .catch(() => {
                });
        },
        deleteOrders(data) {
            this.loading = true;
            let query = {
                id: data
            };
            deleteProducts(query)
                .then((response) => {
                    if (response.code == 20000) {
                        this.getList();
                        this.$message({
                            type: "success",
                            message: "删除成功！"
                        });
                    } else {
                        this.$message.error(response.message);
                    }

                })
                .catch(() => {
                });
        },
        intentDetail(row) {
            this.codeDetailVisible = true;
            this.idQuery = row;
        },
        getBusType() {
            let query = {
                enabled: true
            };
            getBusType(query)
                .then((response) => {
                    this.busTypes = response.data.list || [];
                    this.filterQuery.billAction = this.busTypes[0].action;
                    // this.getList();
                })
                .catch(() => {
                });
        },
        getActionName(action) {
            for (let i = 0; i < this.busTypes.length; i++) {
                if (this.busTypes[i].action === action) {
                    return this.busTypes[i].name;
                }
            }
        }
    },
    filters: {
        statusFilterType(status) {
            const statusMap = {
                false: "success",
                true: "danger"
            };
            return statusMap[status];
        }
    },
    mounted() {
        document.body.ondrop = function (event) {
            event.preventDefault();
            event.stopPropagation();
        };
    },
    created() {
        this.getBusType();
        let query = this.$route.query;
        this.filterQuery = Object.assign(this.filterQuery, query);
        this.filterQuery.limit = parseInt(this.filterQuery.limit);
        // 加载表格数据
        this.getList();
        this.getStorage();
    }
};
</script>

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