<template>
    <div>


        <el-card class="el-card">
            <div>
                <el-form :inline="true" :model="unionQuery" class="query-form" size="mini">
                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.nameCode"
                                size="mini"
                                placeholder="最小单元销售标识"
                                @keyup.enter.native="keyup_submit($event)"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.cpmctymc"
                                size="mini"
                                placeholder="产品通用名"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.ylqxzcrbarmc"
                                size="mini"
                                placeholder="医疗器械注册人"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.ggxh"
                                placeholder="规格型号"
                                size="mini"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.zczbhhzbapzbh"
                                placeholder="注册/备案凭证号"
                                size="mini"
                            ></el-input>
                        </el-form-item>

                        <el-form-item class="query-form-item">
                            <el-button-group>
                                <el-button type="primary" icon="search" @click="getList" size="mini"
                                >查询
                                </el-button
                                >

                                <el-button
                                    style="margin-right: 20px"
                                    type="primary"
                                    size="mini"
                                    icon="search"
                                    @click="combine"
                                >选入
                                </el-button
                                >
                            </el-button-group>
                        </el-form-item>
                    </el-row>
                </el-form>

                <el-table
                    :data="udidlList"
                    style="width: 100%"
                    @row-click="intentDetail"
                    v-loading="loading"
                    :row-class-name="tableRowClassName"
                    @selection-change="handleSelectionUdiChange"
                >
                    <el-table-column
                        type="selection"
                        width="55"
                        :selectable="checkSelectable"
                    ></el-table-column>
                    <el-table-column label="序号" type="index"></el-table-column>
                    <el-table-column
                        label="产品标识"
                        prop="nameCode"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        label="产品名称"
                        prop="cpmctymc"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        label="规格型号"
                        prop="ggxh"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        label="医疗器械注册人"
                        prop="ylqxzcrbarmc"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        label="注册证编号"
                        prop="zczbhhzbapzbh"
                        show-overflow-tooltip
                    ></el-table-column>
                </el-table>
                <el-pagination
                    :page-size="unionQuery.limit"
                    @current-change="handleCurrentChange"
                    layout="prev, pager, next"
                    :total="total"
                    :current-page="unionQuery.page"
                ></el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
import {getUdiInfos} from "../../api/basic/udiInfo";
import {combineUdi} from "../../api/basic/udiRelevance";
import {getBasicThirdSys} from "../../api/basic/basicThirdSys";
import {getInvbasdoc} from "../../api/basic/getErps";

export default {
    name: "closeDialog",
    props: {
        closeDialog: {
            type: Function,
            required: true,
        },
    },

    data() {
        return {
            erpQuery: {
                code: null,
                name: "",
                thirdSys: null,
                page: 1,
                limit: 10,
            },
            unionQuery: {
                page: 1,
                limit: 20,
                nameCode: "",
                cpmctymc: "",
                ylqxzcrbarmc: "",
                ggxh: "",
                zczbhhzbapzbh: null,
            },
            combineQuery: {
                thirdId: "",
                keys: [],
                thirdSys: null,
            },
            checked: true,
            udidlList: [],
            erpList: [],
            pageTotal: 1,
            total: 1,
            currentRow: null,
            loading: false,
            erpLloading: false,
            multipleUdiSelection: [],
            thirdSys: [],
            thirdSysFk: null,
        };
    },
    methods: {
        getList() {
            if (
                this.unionQuery.nameCode == "" &&
                this.unionQuery.cpmctymc == "" &&
                this.unionQuery.ylqxzcrbarmc == "" &&
                this.unionQuery.ggxh == ""
            ) {
                this.$message.warning("请输入查询条件");
                return;
            }
            this.loading = true;
            this.unionQuery.page = 1;
            getUdiInfos(this.unionQuery)
                .then((response) => {
                    this.loading = false;
                    if (response.code == 20000) {
                        this.udidlList = response.data.list || [];
                        this.total = response.data.total || 0;
                    } else {
                        this.$message.error(response.message);
                    }

                })
                .catch(() => {
                    this.loading = false;
                    this.udidlList = [];
                    this.total = 0;
                });
        },

        checkSelectable(row) {
            return !row.check;
        },
        handleCurrentChange(val) {
            this.unionQuery.page = val;
            this.getList();
        },
        tableRowClassName({row}) {
            if (row.checked) return "warning-row";
            return "";
        },
        handleErpChange(val) {
            if (val.checked) {
                this.$message.warning("该产品已被选入");
                return;
            }
            this.currentRow = val;
            this.unionQuery.cpmctymc = this.currentRow.name;
            this.unionQuery.zczbhhzbapzbh = this.currentRow.registerNo;
            this.unionQuery.ggxh = this.currentRow.spec;
        },
        handleErpPageChange(val) {
            this.erpQuery.page = val;
            this.getErpList();
        },
        getErpList() {
            this.erplLoading = true;
            this.erpQuery.thirdSys = this.thirdSysFk;
            getInvbasdoc(this.erpQuery)
                .then((response) => {
                    this.erplLoading = false;
                    if (response.code == 20000) {
                        this.erpList = response.data.list || [];
                        this.pageTotal = response.data.total || 0;
                    } else {
                        this.$message.error(response.message);
                        this.erpList = [];
                        this.pageTotal = 0;
                    }

                })
                .catch(() => {
                    this.erplLoading = false;
                    this.erpList = [];
                    this.pageTotal = 0;
                });
        },
        keyupErp_submit(event) {
            this.getErpList();
            event.target.select();
        },
        intentBack() {
            this.$router.go(-1);
        },
        combine() {
            let selectData = this.multipleUdiSelection;
            var ids = [];
            selectData.forEach((obj) => {
                ids.push(obj.uuid);
            });

            this.combineQuery.keys = ids;
            this.combineQuery.thirdSys = this.thirdSysFk;
            if (this.checked) {
                this.combineQuery.thirdId = this.currentRow.code;
                this.combineQuery.erpName = this.currentRow.name;
            } else {
                this.combineQuery.thirdId = "";
            }
            combineUdi(this.combineQuery)
                .then((response) => {
                    if (response.code == 20000) {
                        this.$emit("closeUdi", true);
                    } else {
                        this.$emit("closeUdi", false);
                    }
                })
                .catch(() => {
                    this.$emit("closeUdi", false);
                });
        },

        intentDetail(row) {
            this.$emit("productInfo", row);
            this.closeDialog();
        },

        handleSelectionUdiChange(val) {
            this.multipleUdiSelection = val;
        },

        keyup_submit(event) {
            this.getList();
            event.target.select();
        },

        getBasicThirdSys() {
            let query = {
                enabled: true,
            };
            getBasicThirdSys(query)
                .then((response) => {
                    this.thirdSys = response.data.list || [];
                    this.thirdSysFk = this.thirdSys[0].thirdId;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                });
        },
    },
    created() {
        this.getBasicThirdSys();
    },
};
</script>

<style scoped>
.checkitemTag {
    float: left;
    text-align: left;
    margin-top: 5px;
    width: 100%;
}

div /deep/ .el-table .warning-row {
    background: #bebebe;
}

div /deep/ .el-table .success-row {
    background: #ffffff;
}
</style>
