<template>
    <div>
        <el-card class="el-card">
            <div>
                <el-form :inline="true" :model="unionQuery" size="mini">
                    <el-row style="width: 1000px">
                        <el-form-item class="query-form-item">
                            <el-input v-model="unionQuery.udiCode"
                                      style="width: 600px"
                                      placeholder="请扫描或输入UDI码"
                                      :clearable="true"
                                      @keyup.enter.native="keyup_submit($event)"></el-input>
                        </el-form-item>
                    </el-row>

                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.nameCode" :clearable="true"
                                size="mini"
                                placeholder="请输入完整DI"
                                @keyup.enter.native="keyup_submit($event)"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-select
                                v-model="unionQuery.ylqxzcrbarmc"
                                filterable
                                remote
                                :clearable="true"
                                reserve-keyword
                                placeholder="医疗器械注册人"
                                :remote-method="findMethod"
                                :loading="loading"
                            >
                                <el-option
                                    v-for="item in fromOptions"
                                    :key="item.ylqxzcrbarmc"
                                    :label="item.ylqxzcrbarmc"
                                    :value="item.ylqxzcrbarmc"
                                >
                                    <span style="float: left">{{ item.ylqxzcrbarmc }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.cpmctymc" :clearable="true"
                                size="mini"
                                placeholder="器械通用名称"
                            ></el-input>
                        </el-form-item>


                    </el-row>

                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.ggxh" :clearable="true"
                                :disabled="(unionQuery.cpmctymc=='' && unionQuery.nameCode=='' && unionQuery.ylqxzcrbarmc==null)"
                                placeholder="规格型号"
                                size="mini"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.zczbhhzbapzbh" :clearable="true"
                                :disabled="(unionQuery.cpmctymc=='' && unionQuery.nameCode=='' && unionQuery.ylqxzcrbarmc==null)"
                                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="searchList" size="mini"
                                >查询
                                </el-button
                                >
                                <el-button type="primary" size="mini" icon="search" @click="combine"
                                           :loading="combineLoading"
                                >选入
                                </el-button
                                >
                                <el-button type="primary" size="mini" icon="search" @click="combineUdiAll"
                                >结果全部选入
                                </el-button
                                >
                            </el-button-group>
                        </el-form-item>
                    </el-row>
                </el-form>
                <!--//    :row-class-name="tableRowClassName"-->
                <el-table
                    :data="udidlList"
                    style="width: 100%"
                    @row-click="intentDetail"
                    v-loading="loading"

                    @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"
                ></el-pagination>
            </div>
            <div style="text-align: center">
                <el-button type="primary" size="small" icon="search" @click="combine"
                >确定
                </el-button
                >
                <el-button type="primary" size="small" icon="search" @click="intentBack"
                >取消
                </el-button
                >
            </div>
        </el-card>
    </div>
</template>

<script>
    import {filterCompany} from "../../api/basic/udiInfo";
    import {
        getUdiInfos,
        combineProduct,
        combineUdi, combineAllUdi
    } from "../../api/erp/udiRelevance";

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

        data() {
            return {
                erpQuery: {
                    key: "",
                    page: 1,
                    limit: 10,
                },
                unionQuery: {
                    page: 1,
                    limit: 10,
                    udiCode: "",
                    nameCode: "",
                    cpmctymc: "",
                    ylqxzcrbarmc: "",
                    ggxh: "",
                    zczbhhzbapzbh: "",
                    isCheck: 1
                },
                combineQuery: {
                    thirdId: "",
                    keys: [],
                },
                combineLoading: false,
                checked: false,
                udidlList: [],
                erpList: [],
                currentRow: null,
                multipleUdiSelection: [],
                fromOptions: [],
            };
        },
        methods: {
            searchList() {
                this.unionQuery.page = 1;
                this.getList();
            },
            getList() {
                if (
                    this.unionQuery.udiCode == "" &&
                    this.unionQuery.nameCode == "" &&
                    this.unionQuery.cpmctymc == "" &&
                    this.unionQuery.ylqxzcrbarmc == "" &&
                    this.unionQuery.ggxh == "" &&
                    this.unionQuery.zczbhhzbapzbh == ""
                ) {
                    this.$message.warning("请输入查询条件");
                    return;
                }
                this.loading = true;
                getUdiInfos(this.unionQuery)
                    .then((response) => {
                        this.loading = false;
                        this.udidlList = response.data.list || [];
                        this.total = response.data.total || 0;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.udidlList = [];
                        this.total = 0;
                    });
            },
            checkSelectable(row) {
                return !row.check;
            },
            tableRowClassName({row, rowIndex}) {
                if (row.check) return "warning-row";
                return "";
            },
            handleCurrentChange(val) {
                this.unionQuery.page = val;
                this.getList();
            },
            getErpList() {
                this.loading = true;
                getErpProduct(this.erpQuery)
                    .then((response) => {
                        this.loading = false;
                        this.erpList = response.data || [];
                    })
                    .catch(() => {
                        this.loading = false;
                        this.erpList = [];
                    });
            },
            intentBack() {
                this.$emit("closeUdi", false);
            },
            combine() {
                let selectData = this.multipleUdiSelection;
                var ids = [];
                selectData.forEach((obj, index) => {
                    ids.push(obj.uuid);
                });

                this.combineQuery.keys = ids;
                if (this.checked) {
                    this.combineQuery.thirdId = this.currentRow.id;
                } else {
                    this.combineQuery.thirdId = "";
                }
                combineUdi(this.combineQuery)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.$emit("closeUdi", true);
                            this.$message.success("添加成功！");
                        } else {
                            this.$emit("closeUdi", false);
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.$emit("closeUdi", false);
                        this.$message.error(response.message);
                    });
            },

            combineUdiAll() {
                this.$confirm("此操作将在后台自动下载所有查询结果, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        combineAllUdi(this.unionQuery)
                            .then((response) => {
                                this.loading = false;
                                if (response.code == 20000) {
                                    this.$message.success(response.data);
                                    this.$emit("closeUdi", true);
                                } else {
                                    this.$message.error(response.message);
                                }
                            })
                            .catch(() => {
                            });
                    })
                    .catch(() => {
                    });


            },

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

            handleSelectionUdiChange(val) {
                console.log("----" + val);
                this.multipleUdiSelection = val;
            },
            keyup_submit(event) {
                this.unionQuery.page = 1;
                this.getList();
                event.target.select();
            },
            findMethod(query) {
                console.log(query);
                let cQuery = {
                    ylqxzcrbarmc: query,
                    page: 1,
                    limit: 10,
                };
                filterCompany(cQuery)
                    .then((response) => {
                        this.loading = false;
                        this.fromOptions = response.data || [];
                    })
                    .catch(() => {
                        this.loading = false;
                        this.options = [];
                    });
            },
        },
    };
</script>

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

    .query-form-item {
        display: block !important;
        margin-right: 10px;
        margin-bottom: 5px;
    }

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

    .el-col {
        border-radius: 4px;
        flex-wrap: wrap;
    }

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

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