<template>
    <div>

        <el-form :inline="true" :model="filterQuery" size="mini">


            <el-row style="margin-top: 15px;width: 1000px">
                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.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="filterQuery.unionCode" placeholder="DI/医保编码/商品条码" clearable="true"></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.cpmctymc" placeholder="产品通用名" clearable="true"></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.ggxh" placeholder="规格型号" clearable="true"></el-input>
                </el-form-item>


                <el-form-item class="query-form-item">
                    <el-select v-model="filterQuery.filterType" placeholder="请选择过滤对照条件">
                        <el-option
                            v-for="item in filterList"
                            :key="item.filterType"
                            :label="item.sysName"
                            :value="item.filterType">
                            <span style="float: left">{{ item.sysName }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-row>

            <el-row>


                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.ylqxzcrbarmc" placeholder="生产企业/注册备案人" clearable="true"></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.thrPiId" placeholder="第三方产品编码" clearable="true"></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-select v-model="filterQuery.lockStatus" placeholder="状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option label="未提交" value="1"></el-option>
                        <el-option label="已提交" value="2"></el-option>
                        <el-option label="已锁定" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button-group>
                        <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="allUploadSMP"
                                   v-if="selectType==1"
                        >结果全部上传
                        </el-button>
                    </el-button-group>
                </el-form-item>
            </el-row>
        </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"></el-table-column>
            <el-table-column
                label="最小销售产品标识"
                prop="nameCode"
                width="140"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="产品编码"
                prop="thirdId"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="生产企业"
                prop="ylqxzcrbarmc"
                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="ERP产品ID"-->
            <!--prop="thirdId"-->
            <!--show-overflow-tooltip-->
            <!--&gt;</el-table-column>-->
            <!--<el-table-column-->
            <!--label="ERP产品名称"-->
            <!--prop="thirdName"-->
            <!--show-overflow-tooltip-->
            <!--&gt;</el-table-column>-->

            <el-table-column
                label="注册/备案凭证"
                prop="zczbhhzbapzbh"
                show-overflow-tooltip
            ></el-table-column>


            <el-table-column
                label="器械类别"
                prop="qxlb"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column width="200" label="操作" fixed="right">
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="handleModifyClick(scope.row)"
                    >详情
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
            title="产品信息编辑"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :visible.sync="editDialogVisible"
            width="70%"
            append-to-body
            v-if="editDialogVisible"
        >
            <el-table :data="sysList" style="width: 100%" border>
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column
                    label="第三方系统名称"
                    prop="sysName"
                    show-overflow-tooltip
                ></el-table-column>

                <el-table-column
                    label="第三方产品名称"
                    prop="thirdName"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    label="第三方产品ID"
                    prop="thirdId"
                    show-overflow-tooltip
                ></el-table-column>
            </el-table>
            <el-table
                v-loading="loading"
                :data="detailList"
                style="width: 100%; margin-top: 20px"
                border
                @current-change="handleDetail"
            >
                <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="packLevel"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    label="包装层级"
                    prop="bzcj"
                    show-overflow-tooltip
                ></el-table-column>
            </el-table>

            <el-form
                :model="editQuery"
                :rules="rules"
                ref="editQuery"
                label-width="100px"
                style="margin-top: 20px"
            >
                <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="请输入内容"
                                :disabled="true"
                                v-model="editQuery.cpmctymc"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.nameCode"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.ggxh"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.qxlb"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.cplb"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.flbm"
                            ></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%"
                                :disabled="true"
                                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%"
                                :disabled="true"
                                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%"
                                :disabled="true"
                                size="small"
                                splaceholder="ylqxzcrbarmc"
                                v-model="editQuery.ylqxzcrbarmc"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.ylqxzcrbarywmc"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.tyshxydm"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.zczbhhzbapzbh"
                            ></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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.sjcpbm"
                            ></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="请输入内容"
                                :disabled="true"
                                v-model="editQuery.packLevel"
                            ></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.bzcj"
                                :disabled="true"

                            ></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="请输入内容"
                                :disabled="true"
                                v-model="editQuery.bhxjsl"
                            ></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"
                                :disabled="true"
                                splaceholder="请输入内容"
                                v-model="editQuery.bhxjcpbm"

                            ></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"
                                :disabled="true"
                                splaceholder="请输入内容"
                                v-model="editQuery.bhzxxsbzsl"
                            ></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%"
                                :disabled="!checked"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.zxxsbzbhsydysl"
                            ></el-input>
                        </div>
                    </el-col>
                    <el-col :span="12" class="el-col">
                        <el-form-item prop="name">
                            <el-checkbox v-model="checked" class="text item itemTag"
                            >以使用单元数量入库
                            </el-checkbox>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

        </el-dialog>

        <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 {
    getUdiInfos,
    updateErpProduct,
    thirdSysDetail,
} from "../../api/basic/udiRelevance";
import {udiUpload, udiUploadStatus} from "../../api/thrsys/smp"
import {exportExcel} from "../../api/basic/udiInfoExport"
import {selectSysParamByKey} from "../../api/param/systemParamConfig";
import {filterByUuid} from "../../api/basic/udiInfo";


export default {
    name: "UdIInfoSelect",
    props: {
        selectType: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            filterQuery: {
                lockStatus: "3",
                uuids: [],
                ylqxzcrbarmc: "",
                cpmctymc: "",
                nameCode: "",
                page: 1,
                limit: 20,
                addType: 1,
            },

            editQuery: null,
            relevanceEdit: {
                id: null,
                thirdId: "",
                isUseDy: false,
            },
            isImport: false,
            isImportUdi: false,
            isUploadSmp: false,
            isImportFile: false,
            uploadFileUrl: null,
            checked: false,
            list: [],
            detailList: [],
            sysList: [],
            total: 0,
            thirdNo: "",
            uuid: "111",
            thirdId: null,
            relId: "",
            thisData: {
                nameCode: null,
                cpmctymc: null,
                ggxh: null
            },
            multipleSelection: [],
            editDialogVisible: false,
            selectUdiDialogVisible: false,
            selectErpDialogVisible: false,
            selectLocalVisible: false,
        };
    },

    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                uuids: [],
                ylqxzcrbarmc: "",
                cpmctymc: "",
                nameCode: "",
                page: 1,
                limit: 20,
                addType: 1,
            };
            this.getList();
        },
        getList() {
            this.loading = true;
            getUdiInfos(this.filterQuery)
                .then((response) => {
                    this.loading = false;
                    this.list = response.data.list || [];
                    this.total = response.data.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },

        getDetailList(detailQuery) {
            this.loading = true;
            filterByUuid(detailQuery)
                .then((response) => {
                    this.loading = false;
                    this.detailList = response.data || [];
                })
                .catch(() => {
                    this.loading = false;
                    this.detailList = [];
                });
        },

        handleDetail(row) {
            this.editQuery = row;
        },
        selectExport() {
            var selectData = this.multipleSelection;
            selectData.forEach((obj) => {
                this.filterQuery.uuids.push(obj.uuid);
            });
            this.allExport();

        },
        allExport() {
            exportExcel(this.filterQuery)
                .then((response) => {
                    if (response.code == 20000) {
                        this.$message({
                            type: "success",
                            message: "导出成功，后台正在生成udi文件，请稍后刷新查看！",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: response.message,
                        });
                    }

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

        intentDetail() {
            this.isImportUdi = false;
            this.selectUdiDialogVisible = true;
        },

        intentImportErp(val) {
            this.uuid = val.uuid;
            this.relId = val.id;
            this.isImport = true;
            this.thisData = val;
            this.selectErpDialogVisible = true;
        },
        uploadSMP() {
            var selectData = this.multipleSelection;
            selectData.forEach((obj) => {
                this.filterQuery.uuids.push(obj.uuid);
            });
            this.allUploadSMP();
        },

        allUploadSMP() {
            udiUploadStatus(this.filterQuery)
                .then((response) => {
                    if (response.code == 20000) {
                        this.$message({
                            type: "success",
                            message: "上传成功！",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: response.message,
                        });
                    }
                    this.$emit("cancelDialog", true);
                })
                .catch(() => {
                    this.$message({
                        type: "error",
                        message: "上传失败",
                    });
                    this.$emit("cancelDialog", true);
                });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        handleModifyClick(row) {
            this.editDialogVisible = true;
            this.editQuery = row;
            this.relevanceEdit = {
                id: row.id,
                thirdId: row.thirdId,
                isUseDy: row.isUseDy,
            };
            this.checked = row.isUseDy == 1;
            c
            let ttquery = {
                uuid: row.uuid,
                page: 1,
                limit: 10,
            };

            this.getDetailList(ttquery);
            this.getThirdSysDetail();
        },
        closeUdi(val) {
            this.selectUdiDialogVisible = false;
            this.selectErpDialogVisible = false;
            this.selectLocalVisible = false;
            if (val) {
                this.getList();
            }
        },
        getThirdSysDetail() {
            let query = {
                id: this.relevanceEdit.id,
            };

            thirdSysDetail(query)
                .then((response) => {
                    this.loading = false;
                    this.sysList = response.data.list || [];
                })
                .catch(() => {
                    this.loading = false;
                    this.sysList = [];
                });
        },
        uploadErpProduct() {
            updateErpProduct()
                .then((response) => {
                    if (response.code == 20000) {
                        this.$message({
                            type: "success",
                            message: response.data,
                        });
                    } else {
                        this.$message.error(response.message);
                    }
                })
                .catch(() => {
                });
        },

        closeDialog() {
            this.selectErpDialogVisible = false;
            this.selectLocalVisible = false;
        },

        selectSysParam() {
            let query = {
                paramKey: "smp_isUploadProuct",
            };
            selectSysParamByKey(query).then((response) => {
                if (response.code == 20000) {
                    if (response.data.paramValue == "1") {
                        this.isUploadSmp = true;
                    }
                }
            });
            let query1 = {
                paramKey: "upload_pi_file_enabled",
            };
            selectSysParamByKey(query1).then((response) => {
                if (response.code == 20000) {
                    if (response.data.paramValue == "1") {
                        this.isImportFile = true;
                    }
                }
            });
            let query2 = {
                paramKey: "upload_pi_file_url",
            };
            selectSysParamByKey(query2).then((response) => {
                if (response.code == 20000) {
                    this.uploadFileUrl = response.data.paramValue;
                }
            });
        },
    },
    mounted() {
    },
    components: {},
    created() {
        this.getList();
        this.selectSysParam();
    },
};
</script>
<style scoped>
.itemTag {
    float: left;
    text-align: left;
    margin-top: 10px;
    width: 25%;
}

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

.text {
    font-size: 13px;
    font-family: "Microsoft YaHei";
}

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

.el-col {
    border-radius: 4px;
    flex-wrap: wrap;
}
</style>
