<template>
    <div>
        <div style="display: flex;  width: 100%;">
            <!--<el-checkbox v-model="checked" class="checkitemTag"-->
            <!--&gt;是否关联第三方产品信息-->
            <!--</el-checkbox-->
            <!--&gt;-->
            <span class="checkitemTag"></span>
            <el-button
                style="margin-right: 20px;float: right"
                type="primary"
                size="mini"
                icon="search"
                @click="combine"
            >选入
            </el-button
            >
        </div>

        <el-card class="el-card" v-if="checked">
            <div>
                <el-form :inline="true" :model="erpQuery" size="mini">
                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="erpQuery.code"
                                placeholder="产品编码/商品条码/医保编码"
                                clearable="true"
                                @keyup.enter.native="keyupErp_submit($event)"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input v-model="erpQuery.name"
                                      clearable="true"
                                      placeholder="产品名称"></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input v-model="erpQuery.registerNo"
                                      clearable="true"
                                      placeholder="注册/备案凭证号"></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-input v-model="erpQuery.manufactory"
                                      clearable="true"
                                      placeholder="生产厂家"></el-input>
                        </el-form-item>
                    </el-row>

                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="erpQuery.spec"
                                clearable="true"
                                placeholder="规格型号"
                            ></el-input>
                        </el-form-item>
                        <el-form-item class="query-form-item">
                            <el-select v-model="thirdSysFk" placeholder="请选择第三方系统" disabled="true">
                                <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 style="display: flex">
                            <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                            <el-button type="primary" icon="search" @click="searchErpList">查询</el-button>
                        </el-form-item>
                    </el-row>
                </el-form>

                <el-table
                    :data="erpList"
                    style="width: 100%"
                    highlight-current-row="true"
                    v-loading="erpLloading"

                    @current-change="handleErpChange"
                >
                    <!--                     :row-class-name="tableRowClassName"-->
                    <el-table-column label="序号" type="index"></el-table-column>
                    <el-table-column label="产品编码" prop="code"></el-table-column>
                    <el-table-column label="产品名称" prop="name"></el-table-column>
                    <el-table-column label="计量单位" prop="measname"></el-table-column>
                    <el-table-column label="规格型号" prop="spec"></el-table-column>
                    <el-table-column label="注册/备案凭证号" prop="registerNo"></el-table-column>
                    <el-table-column label="生产厂家" prop="manufactory"></el-table-column>
                </el-table>
                <el-pagination
                    :page-size="erpQuery.limit"
                    @current-change="handleErpPageChange"
                    layout="prev, pager, next"
                    :total="pageTotal"
                    :curret-page="erpQuery.page"
                ></el-pagination>
            </div>
        </el-card>

        <el-card class="el-card">
            <div>
                <el-form :inline="true" :model="unionQuery" size="mini">
                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.nameCode"
                                size="mini"
                                placeholder="请输入完整DI"
                                clearable="true"
                                @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"
                                size="mini"
                                clearable="true"
                                placeholder="器械通用名称"
                            ></el-input>
                        </el-form-item>


                    </el-row>
                    <el-row>
                        <el-form-item class="query-form-item">
                            <el-input
                                v-model="unionQuery.ggxh"
                                placeholder="规格型号"
                                clearable="true"
                                size="mini"
                                :disabled="(unionQuery.cpmctymc=='' && unionQuery.nameCode=='' && (unionQuery.ylqxzcrbarmc==null || unionQuery.ylqxzcrbarmc==''))"
                            ></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="search" @click="serchList" size="mini"
                                >查询
                                </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-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-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} from "@/api/basic/udiInfo";
import {combineUdi} from "@/api/basic/udiRelevance";
import {getBasicThirdSys} from "@/api/basic/basicThirdSys";
import {getInvbasdoc} from "@/api/basic/getErps";
import selectDiDetail from "./SelectDIDetailDialog";

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

    data() {
        return {
            erpQuery: {
                code: null,
                name: "",
                thirdSys: null,
                spec: null,
                manufactory: null,
                registerNo: null,
                page: 1,
                limit: 10,
            },
            unionQuery: {
                page: 1,
                limit: 20,
                nameCode: "",
                cpmctymc: "",
                ylqxzcrbarmc: null,
                ggxh: "",
                zczbhhzbapzbh: "",
                isCheck: 1
            },
            combineQuery: {
                thirdId: "",
                keys: [],
                thirdSys: null,
                manufactory: null,
                price: null,
                erpName: null,
                measname: null,
            },
            fromOptions: [],
            checked: true,
            udidlList: [],
            erpList: [],
            pageTotal: 1,
            total: 1,
            currentRow: null,
            loading: false,
            erpLloading: false,
            multipleUdiSelection: [],
            thirdSys: [],
            thirdSysFk: null,
            diDetails: null,
            selectDialog: false,
        };
    },
    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.erpQuery = {
                code: null,
                name: "",
                thirdSys: null,
                spec: null,
                manufactory: null,
                registerNo: null,
                page: 1,
                limit: 10,
            };
            this.getErpList();
        },
        serchList() {
            this.unionQuery.page = 1;
            this.getList();
        },
        getList() {
            if (
                this.unionQuery.nameCode == "" &&
                this.unionQuery.cpmctymc == "" &&
                this.unionQuery.ylqxzcrbarmc == "" &&
                this.unionQuery.ggxh == ""
            ) {
                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;
                });
        },

        checkSelectable(row) {
            return !row.check;
        },
        handleCurrentChange(val) {
            this.unionQuery.page = val;
            this.getList();
        },
        tableRowClassName({row}) {
            if (row.checked) return "warning-row";
            return "";
        },
        handleErpChange(val) {
            console.log(val);
            this.currentRow = val;
            // this.unionQuery.cpmctymc = this.currentRow.name;
            // this.unionQuery.zczbhhzbapzbh = this.currentRow.registerNo;
            // this.unionQuery.ggxh = this.currentRow.spec;
        },
        diDetail(row) {
            this.diDetails = row;
            this.selectDialog = true;
        },
        searchErpList() {
            this.erpQuery.page = 1;
            this.getErpList();
        },
        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.erpQuery.page = 1;
            this.getErpList();
            event.target.select();
        },
        intentBack() {
            this.$router.go(-1);
        },

        confirmCombine(ids) {
            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 = "";
            }
            this.combineQuery.manufactory = this.currentRow.manufactory;
            this.combineQuery.price = this.currentRow.price;
            this.combineQuery.measname = this.currentRow.measname;
            combineUdi(this.combineQuery)
                .then((response) => {
                    if (response.code == 20000) {
                        this.$emit("closeUdi", true);
                    } else {
                        // this.$emit("closeUdi", false);
                        this.$message.warning(response.message);
                    }
                })
                .catch(() => {
                    this.$emit("closeUdi", false);

                });
        },
        combine() {
            let selectData = this.multipleUdiSelection;
            var ids = [];
            selectData.forEach((obj) => {
                ids.push(obj.uuid);
            });

            if (this.currentRow == null) {
                this.$message.warning("未选择第三方产品信息");
                return;
            }
            if (ids.length == 0) {
                this.$message.warning("未选择医疗器械产品信息");
                return;
            }
            console.log("this.currentRow.checked =" + this.currentRow.checked);
            if (this.currentRow != null && this.currentRow.checked) {

                this.$confirm("产品编码:" + this.currentRow.code + "已被添加，是否继续添加", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                    .then(() => {
                        this.confirmCombine(ids);
                    })
                    .catch(() => {
                    });
                return;
            } else {
                this.confirmCombine(ids);
            }
        },

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

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

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

        getBasicThirdSys() {
            let query = {
                enabled: true,
                mainSys: true,
            };
            getBasicThirdSys(query)
                .then((response) => {
                    this.thirdSys = response.data.list || [];

                    this.thirdSysFk = this.thirdSys[0].thirdId;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                });
        },
        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 = [];
                });
        },
    },
    created() {
        this.getBasicThirdSys();
    },
    components: {
        selectDiDetail,
    },
};
</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>
