<template>
    <div>
        <el-form :inline="true" :model="filterQuery" class="query-form" size="mini">
            <el-form-item class="query-form-item">
                <el-input
                    v-model="filterQuery.inventoryCode" clearable
                    placeholder="产品编码"
                ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
                <el-input
                    v-model="filterQuery.inventoryName" clearable
                    placeholder="产品名称"
                ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
                <el-input
                    v-model="filterQuery.spec" clearable
                    placeholder="规格型号"
                ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
                <el-input
                    v-model="filterQuery.registerCertNo" clearable
                    placeholder="注册/备案凭证号"
                ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
                <el-input
                    v-model="filterQuery.manufactory" clearable
                    placeholder="生厂厂家"
                ></el-input>
            </el-form-item>
            <el-form-item class="query-form-item">
                <el-select v-model="filterQuery.filterCount" placeholder="过滤条件">
                    <el-option label="全部" value="0"></el-option>
                    <el-option label="库存等于0" value="1"></el-option>
                    <el-option label="库存大于0" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="query-form-item">
                <el-select v-model="filterQuery.thirdSys" placeholder="请选择第三方系统">
                    <el-option
                        v-for="item in thirdSys"
                        :key="item.value"
                        :label="item.thirdName"
                        :value="item.thirdId">
                        <span style="float: left">{{ item.thirdName }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button-group style="display:flex;">
                    <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="selectExport" v-if="selectType==0"
                    >选中导出
                    </el-button>
                    <el-button type="primary" icon="search" @click="allExport" v-if="selectType==0"
                    >结果全部导出
                    </el-button>
                    <el-button type="primary" icon="search" @click="uploadSMP"
                               v-if="selectType==1"
                    >选中上传
                    </el-button>
                    <el-button type="primary" icon="search" @click="uploadSMP"
                               v-if="selectType==1"
                    >一键上传
                    </el-button>
                    <el-button type="primary" icon="search" @click="downloadErp"
                               v-if="selectType==3"
                    >选中下载
                    </el-button>
                    <el-button type="primary" icon="search" @click="downloadAllErp"
                               v-if="selectType==3"
                    >结果全部下载
                    </el-button>
                </el-button-group>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="list" style="width: 100%"    @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="序号" type="index" width="70"></el-table-column>
            <el-table-column label="产品编码" prop="code" show-overflow-tooltip></el-table-column>
            <el-table-column label="产品名称" prop="name" show-overflow-tooltip></el-table-column>
            <el-table-column label="规格型号" prop="spec" show-overflow-tooltip></el-table-column>
            <el-table-column label="批次号" prop="batchNo" show-overflow-tooltip></el-table-column>
            <el-table-column label="仓位" prop="warehouseName" show-overflow-tooltip></el-table-column>
            <el-table-column label="货位" prop="spaceName" show-overflow-tooltip></el-table-column>
            <el-table-column label="注册证号" prop="registerCertNo" show-overflow-tooltip></el-table-column>
            <el-table-column label="生产日期" prop="manufacturingDate" show-overflow-tooltip></el-table-column>
            <el-table-column label="失效日期" prop="expirationDate" show-overflow-tooltip></el-table-column>
            <el-table-column label="数量" prop="count" show-overflow-tooltip></el-table-column>
        </el-table>
        <el-pagination
            :page-size="filterQuery.limit"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
            :current-page="filterQuery.page"
        ></el-pagination>
    </div>
</template>

<script>
    import {
        delInvProducts, delAll,inpisDlAll
    } from "../../api/thrsys/thrInvProducts";
    import {getBasicThirdSys, filterDetailByKey} from "../../api/basic/basicThirdSys";
    import {selectIp} from "../../api/param/systemParamConfig";
    import {getOnhands} from "../../api/inventory/onHand.js";
    import {exportExcel} from "../../api/thrsys/thrInvProductsExport"

    export default {

        name: "thrInvProductsSelect",
        props: {
            selectType: {
                type: Object,
                required: true,
            },
            isDownThrSys: {
                type: Object,
                required: true,
            },
        },


        data() {
            return {
                filterQuery: {
                    thrInvProductsEntities:[],
                    inventoryCode: null,
                    inventoryName: null,
                    thirdSys: null,
                    spec: null,
                    registerCertNo: null,
                    manufactory: null,
                    page: 1,
                    limit: 100,
                    filterCount:null,
                },
                total: 0,
                list: [],
                thirdSys: [],
                thirdSysDetail: null,
                fileList: [],
                uploadData: {
                    thirdSys: "thirdId",
                },
                uploadFileUrl: null,
            };
        },

        methods: {
            onReset() {
                this.$router.push({
                    path: "",
                });
                this.filterQuery = {
                    thrInvProductsEntities:[],
                    inventoryCode: null,
                    inventoryName: null,
                    thirdSys: null,
                    spec: null,
                    registerCertNo: null,
                    manufactory: null,
                    page: 1,
                    limit: 100,
                    filterCount:null,
                };
                this.getList();
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            getList() {
                this.loading = true;
                this.filterQuery.isDownThrSys = this.isDownThrSys;
                getOnhands(this.filterQuery)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.list = response.data.list || [];
                            this.total = response.data.total || 0;
                        }else {
                            this.$message.error(response.message);
                        }
                        this.loading = false;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                        this.total = 0;
                    });
            },
            deleteDialog(row) {
                this.$confirm("此操作将永久删除该产品信息, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {

                        let dQuery = {
                            id: row.id,
                        };
                        delInvProducts(dQuery)
                            .then((response) => {
                                this.loading = false;
                                if (response.code == 20000) {
                                    this.$message.success("删除成功");
                                    this.getList();
                                } else {
                                    this.$message.error(response.message);
                                }
                            })
                            .catch(() => {
                                this.loading = false;
                            });

                    })
                    .catch(() => {
                    });
            },
            clearAll() {
                this.$confirm("此操作将清空所有库存产品信息, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        delAll()
                            .then((response) => {
                                this.loading = false;
                                if (response.code == 20000) {
                                    this.$message.success("删除成功");
                                    this.getList();
                                } else {
                                    this.$message.success(response.message);
                                }
                            })
                            .catch(() => {
                                this.loading = false;
                            });

                    })
                    .catch(() => {
                    });
            },
            handleCurrentChange(val) {
                this.filterQuery.page = val;
                this.getList();
            },
            handleChange(response, files, fileList) {
                console.log(response);
                if (response.code != 20000) {
                    this.$message.error(response.message);
                } else {
                    console.log(files[0] + "\n" + this.fileList[0] + "\n" + fileList[0]);
                    this.$message.success("文件上传成功,请稍后刷新查看！");
                }
            },
            getBasicThirdSys() {
                let query = {
                    enabled: true,
                };
                getBasicThirdSys(query)
                    .then((response) => {
                        this.thirdSys = response.data.list || [];
                        this.filterQuery.thirdSys = this.thirdSys[0].thirdId;
                        this.uploadData.thirdSys = this.filterQuery.thirdSys;
                        this.getThirdSysDetail();
                        this.selectSysParam();
                        this.getList();
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                    });
            },
            selectSysParam() {
                let query = {
                    key: "thirdIpUrl",
                    thirdSysFk: this.filterQuery.thirdSys
                };
                selectIp(query).then((response) => {
                    if (response.code == 20000) {
                        this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/invpi/upload";
                    }
                });
            },
            thirdSysChange() {
                this.uploadData.thirdSys = this.filterQuery.thirdSys;
                this.getThirdSysDetail();
            },
            getThirdSysDetail() {
                let query = {
                    thirdSysFk: this.filterQuery.thirdSys,
                    key: "invPiUrl",
                };
                filterDetailByKey(query)
                    .then((response) => {
                        this.thirdSysDetail = response.data;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                    });

            },
            selectExport() {
                var selectData = this.multipleSelection;
                selectData.forEach((obj) => {
                    this.filterQuery.thrInvProductsEntities.push(obj);
                });
                this.allExport();
            },
            allExport() {
                exportExcel(this.filterQuery)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.$message({
                                type: "success",
                                message: "导出成功，后台正在生成Excel文件，请稍后刷新查看！",
                            });
                            this.$emit("cancelDialog", true);
                        } else {
                            this.$message({
                                type: "error",
                                message: response.message,
                            });
                        }

                    })
                    .catch(() => {
                        this.$message({
                            type: "error",
                            message: "上传失败",
                        });
                    });
            },


            downloadErp(){
                var selectData = this.multipleSelection;
                selectData.forEach((obj) => {
                    this.filterQuery.thrInvProductsEntities.push(obj);
                });
                this.downloadAllErp();
            },

            downloadAllErp(){
                inpisDlAll(this.filterQuery)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.$message.success("提交成功，后台正在下载！")
                            this.$emit("cancelDialog", true);
                        } else {
                            this.$message.error(response.message)
                        }

                    })
                    .catch(() => {
                        this.$message.error("下载失败！");
                    });
            },
        },
        components: {},
        mounted() {
        },
        created() {
            this.getBasicThirdSys();

        },
    };


</script>

<style scoped>

</style>
