<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
                                :disabled="unionQuery.ylqxzcrbarmc==null || unionQuery.ylqxzcrbarmc==''"
                                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 || unionQuery.ylqxzcrbarmc==''))"
                                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 || unionQuery.ylqxzcrbarmc==''))"
                                placeholder="注册证/备案号"
                                size="mini"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-button-group>
                                <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                                <el-button type="primary" icon="search" @click="searchList" size="mini"
                                >查询
                                </el-button
                                >
                                <el-button type="primary" icon="search" @click="getSuperSeaech" size="mini"
                                >国家同步库查询
                                </el-button
                                >
                                <el-button type="primary" size="mini" icon="search" @click="checkCombine"
                                           :loading="combineLoading"
                                >选入
                                </el-button
                                >
                            </el-button-group>
                        </el-form-item>
                    </el-row>
                </el-form>

                <el-table
                    :data="udidlList"
                    style="width: 100%"
                    v-loading="loading"
                    highlight-current-row="true"
                    :row-class-name="tableRowClassName"
                    @current-change="handleErpChange"
                    ref="diList"
                    @selection-change="handleSelectionUdiChange"
                >

                    <el-table-column
                        v-if="relId==null"
                        type="selection"
                        :selectable="checkSelectable"
                        width="55"
                    ></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-column label="操作" fixed="right">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="small"
                                @click="diDetail(scope.row)"
                            >详情
                            </el-button
                            >
                        </template>
                    </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>


        <el-card class="el-card">
            <el-form
                :model="editQuery"
                ref="editQuery"
                border
                label-width="100px"
            >

                <el-row :gutter="20" class="el-row" type="flex">

                    <H3 style="margin-left: 10px;margin-bottom: 15px">产品信息补齐：</H3>
                </el-row>
                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="7" class="el-col">
                        <div class="text item">
                            <span>产品编码预上传至第三方系统:&nbsp;</span>
                        </div>
                    </el-col>
                    <el-col :span="8" class="el-col">
                        <el-checkbox-group v-model="editQuery.selectThirdSys">
                            <el-checkbox
                                v-for="item in thirdSys"
                                :key="item.thirdId"
                                :label="item.thirdId"
                            >{{ item.thirdName }}
                            </el-checkbox
                            >
                        </el-checkbox-group>
                    </el-col>

                </el-row>

                <el-divider>

                </el-divider>
                <el-row :gutter="20" class="el-row" type="flex" style="margin-top: 15px">
                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>商品条码:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.sptm"
                            ></el-input>
                        </div>
                    </el-col>
                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>医保编码:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.ybbm"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>计量单位:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.measname"
                            ></el-input>
                        </div>
                    </el-col>
                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>生产厂家:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                v-model="editQuery.manufactory"
                                splaceholder="请输入内容"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">

                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>商品名称:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.spmc"
                            ></el-input>
                        </div>
                    </el-col>

                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>产品描述:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.cpms"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>产品价格:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                type='number'
                                step="0.01"
                                v-model="editQuery.price"
                            ></el-input>
                        </div>
                    </el-col>

                </el-row>


                <el-row :gutter="20" class="el-row" type="flex">

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable1">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle1 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak1"
                            ></el-input>
                        </div>
                    </el-col>

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable2">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle2 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak2"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable3">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle3 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak3"
                            ></el-input>
                        </div>
                    </el-col>

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable4">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle4 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak4"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable5">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle5 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak5"
                            ></el-input>
                        </div>
                    </el-col>

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable6">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle6 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak6"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable7">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle7 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak7"
                            ></el-input>
                        </div>
                    </el-col>

                    <el-col :span="12" class="el-col" v-if="productRemarkSet.remarkEnable8">
                        <div class="text item">
                            <div class="itemTag">
                                <span>{{ productRemarkSet.remarkTitle8 }}:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.basicPrductRemak8"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

            </el-form>
        </el-card>


        <el-dialog
            title="器械信息详情"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="selectDialog"
            width="65%"
            append-to-body
            v-if="selectDialog"
        >
            <selectDiDetail :editQuery="diDetails"
            ></selectDiDetail>
        </el-dialog>

    </div>
</template>

<script>
import {getUdiInfos, filterCompany, superSearch} from "../../api/basic/udiInfo";
import {combineUdi} from "../../api/basic/udiRelevance";
import {addDiProduct} from "@/api/thrsys/thrProductsAddDi";
import selectDiDetail from "../basic/SelectDIDetailDialog"
import store from "@/store";
import {getBasicThirdSys} from "@/api/basic/basicThirdSys";
import {finProductSet} from "@/api/param/systemParamConfig";

export default {
    name: "closeDialog",
    props: {
        closeDialog: {
            type: Function,
            required: true,
        },
        relId: {
            type: Number,
            required: true,
        },
        isImport: {
            type: Object,
            required: true,
        },
        data: {
            type: Object,
            required: true,
        },
        thirdId: {
            type: Object,
            required: true,
        },
        idSpliUdi: {
            type: Object,
            required: true,
        }
    },

    data() {
        return {
            unionQuery: {
                page: 1,
                limit: 10,
                udiCode: null,
                nameCode: "",
                cpmctymc: "",
                ylqxzcrbarmc: null,
                ggxh: "",
                zczbhhzbapzbh: "",
                isCheck: 1
            },
            combineLoading: false,
            combineQuery: {
                idSpliUdi: false,
                thirdId: "",
                relId: "",
                erpName: "",
                thirdName: null,
                keys: [],
            },
            checked: true,
            udidlList: [],
            erpList: [],
            pageTotal: 1,
            total: 1,
            currentRow: null,
            fromOptions: [],
            loading: false,
            erpLloading: false,
            multipleUdiSelection: [],
            selectDialog: false,
            diDetails: null,

            editQuery: {
                selectThirdSys: [],
                spmc: null,
                cpms: null,
                measname: null,
                sptm: null,
                uuids: null,
                customerId: store.getters.customerId,

            },
            thirdSys: [],
            curSelectDi: null,
            selectThirdSys: [],
            productRemarkSet: {},

        };
    },
    methods: {
        onReset() {
            this.unionQuery = {
                page: 1,
                limit: 10,
                udiCode: null,
                nameCode: "",
                cpmctymc: "",
                ylqxzcrbarmc: null,
                ggxh: "",
                zczbhhzbapzbh: "",
                isCheck: 1
            };
            // this.getList();
        },
        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;
                    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;
                });
        },

        getSuperSeaech() {
            this.$confirm("此操作将访问国家库查询DI数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    if (this.unionQuery.nameCode == "") {
                        this.$message.warning("请输入完成DI信息进行查询！");
                        return;
                    }
                    this.loading = true;
                    superSearch(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;
                        });
                })
                .catch(() => {
                });
        },

        keyup_submit(event) {
            this.unionQuery.page = 1;
            this.getList();
            event.target.select();
        },

        checkSelectable(row) {
            return !row.check;
        },

        diDetail(row) {
            this.diDetails = row;
            this.selectDialog = true;
        },

        handleCurrentChange(val) {
            this.unionQuery.page = val;
            this.getList();
        },
        tableRowClassName({row, rowIndex}) {
            if (row.check) return "warning-row";
            return "";
        },
        handleErpChange(val) {
            this.currentRow = val;
        },
        intentBack() {
            this.closeDialog();
        },

        checkCombine() {
            if (this.multipleUdiSelection.length === 0) {
                this.$message.warning("请选择需要添加的产品数据");
                return;
            }
            let uuids = [];
            uuids.push(this.curSelectDi.uuid);
            this.editQuery.uuids = uuids;
            addDiProduct(this.editQuery).then((res) => {
                if (res.code === 20000) {
                    this.$message.success("添加成功");
                    this.$emit("closeUdi", true);
                } else {
                    this.$message.error(res.message);
                }
            }).catch((error) => {
            });
        },

        combine() {
            if (this.idSpliUdi) {
                this.combineQuery.idSpliUdi = true;
                this.combineQuery.thirdId = this.thirdId;
                this.combineQuery.erpName = this.data.thirdName;
            }
            combineUdi(this.combineQuery)
                .then((response) => {
                    this.combineLoading = false;
                    if (response.code == 20000) {
                        this.$emit("closeUdi", true);
                    } else {
                        this.$message.error(response.message);
                        this.$emit("closeUdi", false);
                    }
                })
                .catch(() => {
                    this.combineLoading = false;

                    this.$emit("closeUdi", false);
                });
        },

        handleSelectionUdiChange(selection) {
            this.curSelectDi = selection[0];
            if (selection.length > 1) {
                this.$refs.diList.clearSelection();
                this.$refs.diList.toggleRowSelection(selection.pop());
            }
            this.multipleUdiSelection = selection;

        },
        findMethod(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.fromOptions = [];
                });
        },
        getBasicThirdSys() {
            let query = {
                enabled: true,
            };
            getBasicThirdSys(query)
                .then((response) => {
                    if (response.code == 20000) {
                        this.thirdSys = response.data.list || [];
                        if (this.thirdSys.length > 0) {
                            this.editQuery.thirdSysFk = this.thirdSys[0].thirdId;
                        }
                    }

                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                });
        },
        findBasicProductSet() {
            finProductSet().then((response) => {
                if (response.code == 20000) {
                    this.productRemarkSet = response.data;
                } else {
                    //出错了
                }
            });
        },

    },
    created() {
        this.getBasicThirdSys();
        this.findBasicProductSet();
    },
    components: {
        selectDiDetail,
    },
};
</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;
}

.el-card {
    margin-right: 20px;
    /*transition: all .5s;*/
}

.el-row {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 6px;
}

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

</style>
