<template>
    <div>
        <el-form :inline="true" :model="query" class="query-form" size="mini">
            <el-row>
                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.genKey" placeholder="记录ID"></el-input>
                </el-form-item>

                <el-form-item class="query-form-item">
                    <el-select v-model="filterQuery.status" placeholder="处理状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option label="处理中" value="0"></el-option>
                        <el-option label="已处理" value="1"></el-option>
                        <el-option label="异常" value="2"></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="getList">查询</el-button>
                        <el-button type="primary" icon="search" @click="genInCode" v-if="systemParms.fromType!=0"
                        >生成内部码
                        </el-button
                        >
                    </el-button-group>
                </el-form-item>
            </el-row>
        </el-form>

        <el-table
            v-loading="loading"
            :data="list"
            style="width: 100%"
            @selection-change="handleSelectionChange"
        >
            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column
                label="记录ID"
                prop="genKey"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column
                label="来源"
                prop="fromType"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span>{{ fromStatus[scope.row.fromType] }}</span>
                </template>
            </el-table-column>
            <el-table-column
                label="更新日期"
                prop="updateTime"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="来源系统"
                prop="thirdSysFk"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="状态"
                prop="status"
                show-overflow-tooltip
            >
                <template slot-scope="scope">
                    <span>{{ status[scope.row.status] }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="160">
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="handleDetailClick(scope.row)"
                    >详情
                    </el-button
                    >
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="deleteDialog(scope.row.id)"
                    >删除
                    </el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
            title="产品信息生成内部码"
            :visible.sync="selectBasicUdiVisible"
            width="85%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="selectBasicUdiVisible"
        >
            <selectBasicUdiInfo :thirdSys="filterQuery.thirdSysFk" :closeDialog="cancelDialog"></selectBasicUdiInfo>
        </el-dialog>
        <el-dialog
            title="库存信息生成内部码"
            :visible.sync="selectInvProductsVisible"
            width="85%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="selectInvProductsVisible"
        >
            <selectInvProducts :thirdSys="filterQuery.thirdSysFk" :closeDialog="cancelDialog"></selectInvProducts>
        </el-dialog>
        <el-dialog
            title="内部码详情"
            :visible.sync="stockPrintDetailVisible"
            width="85%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="stockPrintDetailVisible"
        >
            <incodeLogDetail :currentRow="currentRow" :closeDialog="cancelDialog"></incodeLogDetail>
        </el-dialog>

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

<script>
    import {getIncodeLogs, delIncodeLogs, getParams} from "../../api/inventory/inCodelog.js";
    import {getBasicThirdSys} from "../../api/basic/basicThirdSys";
    import selectBasicUdiInfo from "./selectBasicUdiInfo";
    import selectInvProducts from "./SelectInvProducts";
    import incodeLogDetail from "./IncodeLogDetail";

    export default {
        data() {
            return {
                filterQuery: {
                    genKey: null,
                    status: null,
                    fromType: null,
                    page: 1,
                    limit: 20,
                    thirdSysFk: null,
                },
                selectBasicUdiVisible: false,
                selectInvProductsVisible: false,
                stockPrintDetailVisible: false,
                checked: false,
                list: [],
                detailList: [],
                thirdSys: [],
                thirdSysDetail: null,
                total: 0,
                currentRow: null,
                editQuery: null,
                systemParms: null,
                fromStatus: {
                    1: "产品信息",
                    2: "库存信息",
                    3: "第三方上传"
                },
                status: {
                    0: "未处理",
                    1: "已完成",
                    2: "处理异常"
                },
            };
        },

        methods: {
            onReset() {
                this.$router.push({
                    path: "",
                });
                this.filterQuery = {
                    fromType: null,
                    genKey: null,
                    status: null,
                    page: 1,
                    limit: 20,
                    thirdSysFk: null,
                };
                this.getList();
            },
            getList() {
                this.loading = true;
                getIncodeLogs(this.filterQuery)
                    .then((response) => {
                        this.loading = false;
                        this.list = response.data.list || [];
                        this.total = response.data.total || 0;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                        this.total = 0;
                    });
            },

            genInCode() {
                if (this.filterQuery.fromType == null) {
                    this.$message.warning("请选择数据来源！");
                } else {
                    if (this.filterQuery.fromType == 0) {
                        this.selectBasicUdiVisible = true;
                    } else if (this.filterQuery.fromType == 1) {
                        this.selectInvProductsVisible = true;
                    }
                }
            },
            handleDetailClick(row) {
                this.currentRow = row;
                console.log(this.currentRow.genKey)
                this.stockPrintDetailVisible = true;

            },
            cancelDialog() {
                this.selectBasicUdiVisible = false;
                this.selectInvProductsVisible = false;
            },
            handleCurrentChange(val) {
                this.filterQuery.page = val;
                this.getList();
            },
            deleteDialog(rowId) {
                this.$confirm("此操作将删除该内部码信息, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        let dQuery = {
                            id: rowId,
                        };
                        delIncodeLogs(dQuery)
                            .then((response) => {
                                this.loading = false;
                                if (response.code == 20000) {
                                    this.$message.success("删除成功");
                                } else {
                                    this.$message.error(response.message);
                                }
                                this.getList();
                            })
                            .catch(() => {
                                this.loading = false;
                            });
                    })
                    .catch(() => {
                    });
            },
            getBasicThirdSys() {
                let query = {
                    enabled: true,
                };
                getBasicThirdSys(query)
                    .then((response) => {
                        this.thirdSys = response.data.list || [];
                        for (let i = 0; i < this.thirdSys.length; i++) {
                            if (this.thirdSys[i].mainSys) {
                                this.filterQuery.thirdSysFk = this.thirdSys[i].thirdId;
                            }
                        }

                        this.getList();
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                    });
            },

            getSysParms() {
                getParams()
                    .then((response) => {
                        this.systemParms = response.data;
                        this.filterQuery.thirdSysFk = this.systemParms.thirdSys;
                        this.filterQuery.fromType = this.systemParms.fromType;
                    })
                    .catch(() => {
                    });
            },


        },
        mounted() {
        },
        components: {selectBasicUdiInfo, selectInvProducts, incodeLogDetail},
        created() {
            // this.getBasicThirdSys();
            this.getSysParms();
            // this.getList();
        },
    };
</script>
<style>
    .itemTag {
        float: left;
        text-align: left;
        margin-top: 10px;
        width: 100px;
    }

    .text {
        font-size: 13px;
        font-family: "Microsoft YaHei";
    }

    .el-row {
        display: flex;
        flex-wrap: wrap;

    }

    .el-col {
        border-radius: 4px;
        flex-wrap: wrap;
    }
</style>
