<template>
    <div>
        <el-table
            v-loading="loading"
            :data="list"
            style="width: 100%"
            @selection-change="handleSelectionChange"
        >
            <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="注册/备案凭证"
                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>

                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="bindId(scope.row)"
                    >选入绑定
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
            title="产品信息编辑"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            append-to-body
            :close-on-press-escape="false"
            width="70%"
            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="thirdId"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    label="产品名称"
                    prop="thirdName"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column label="规格型号" prop="spec" show-overflow-tooltip></el-table-column>
                <el-table-column label="注册/备案凭证号" prop="registerNo" show-overflow-tooltip></el-table-column>
                <el-table-column label="生产厂家" prop="manufactory" 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%"
                                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">
                        <div class="text item">
                            <div class="itemTag">
                                <span>生产厂家:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                :disabled="!checked"
                                size="small"
                                splaceholder="请输入内容"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>


                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="12" class="el-col">
                    </el-col>
                    <el-col :span="4" class="el-col">
                        <el-form-item prop="name">
                            <el-checkbox v-model="checked" class="text item itemTag"
                                         :disabled="!isUseDyCheck"
                            >以使用单元数量入库
                            </el-checkbox>
                        </el-form-item>
                    </el-col>
                    <el-col :span="3" class="el-col">
                        <el-form-item prop="name">
                            <el-checkbox v-model="editQuery.isLock" class="text item itemTag" :disabled="true"
                            >是否锁定
                            </el-checkbox>
                        </el-form-item>
                    </el-col>
                    <el-col :span="3" class="el-col">
                        <el-form-item prop="name">
                            <el-checkbox v-model="editQuery.isDisable" class="text item itemTag" :disabled="true"
                            >是否禁用
                            </el-checkbox>
                        </el-form-item>
                    </el-col>

                </el-row>
            </el-form>

            <div style="text-align: center">
                <el-button type="primary" size="small" icon="search" @click="onAddSubmit"
                >提交
                </el-button>
                <el-button type="primary" size="small" icon="search" @click="cancelDialog"
                >取消
                </el-button>
            </div>
        </el-dialog>

        <el-pagination
            :page-size="filterQuery.limit"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>


    </div>
</template>

<script>
import {
    getUdiInfos, getAllUdiInfos,
    deletetUdiInfos,
    thirdSysDetail,
    removeRl, sysFilter
} from "../../api/basic/udiRelevance";
import {selectSysParamByKey} from "../../api/param/systemParamConfig";
import {filterByUuid} from "../../api/basic/udiInfo";
import {updateBind} from "../../api/inout/order"

import selectUdi from "../basic/UdiInfoRelevance.vue";
import selectErp from "../basic/UdiInfoselectErpUdi";
import selectLocalUdi from "../basic/UdiInfoSelectLocalUdi";

export default {
    name: "DialogSelectRl",
    props: {
        curRow: {
            type: Object,
            required: true,
        },
        closeBindDialog: {
            type: Function,
            required: true,
        },
    },
    data() {
        return {
            filterQuery: {
                nameCode: null,
                page: 1,
                limit: 20,
            },
            loading: false,
            editQuery: null,
            relevanceEdit: {
                id: null,
                thirdId: "",
                isDisable: null,
                isUseDy: false,
                isLock: null,
            },
            defaultSys: null,
            isImport: false,
            isImportUdi: false,
            isUploadSmp: false,
            isImportFile: false,
            uploadFileUrl: null,
            checked: false,
            list: [],
            detailList: [],
            sysList: [],
            filterList: [],
            total: 0,
            thirdNo: "",
            uuid: "111",
            thirdId: null,
            relId: "",
            thisData: {
                nameCode: null,
                cpmctymc: null,
                ggxh: null
            },
            multipleSelection: [],
            editDialogVisible: false,       //详情
            selectUdiDialogVisible: false,  //关联选入
            selectErpDialogVisible: false,  //选入ERP产品
            selectLocalVisible: false,      //关联UDI
            isUseDyCheck: false,
        };
    },

    methods: {
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                nameCode: null,
                page: 1,
                limit: 20,
            };
            this.getList();
        },
        getList() {
            this.loading = true;
            this.filterQuery.nameCode = this.curRow.nameCode;
            getAllUdiInfos(this.filterQuery)
                .then((response) => {
                    this.loading = false;
                    if (response.code == 20000) {
                        this.list = response.data.list || [];
                        this.total = response.data.total || 0;
                    } else {
                        this.$message.error(response.message);
                    }

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


        keyup_submit(event) {
            this.filterQuery.page = 1;
            this.getList();
            event.target.select();
        },
        getDetailList(detailQuery) {
            this.loading = true;
            filterByUuid(detailQuery)
                .then((response) => {
                    this.isUseDyCheck = false;
                    this.loading = false;
                    this.detailList = response.data || [];
                    this.detailList.forEach((item, index, array) => {
                        if (item.zxxsbzbhsydysl > 1) {
                            this.isUseDyCheck = true;
                        }
                    });
                })
                .catch(() => {
                    this.loading = false;
                    this.detailList = [];
                });
        },

        handleDetail(row) {
            this.editQuery = row;
            console.log(row);
        },
        deleteOrders(data) {
            this.loading = true;
            let tquery = {
                id: data.id + "",
            };

            deletetUdiInfos(tquery)
                .then((response) => {
                    if (response.code == 20000) {
                        this.getList();
                        this.$message({
                            type: "success",
                            message: "删除成功！",
                        });
                    } else {
                        this.$message.error(response.message);
                    }
                })
                .catch(() => {
                });
        },

        deleteDialog(rowId) {
            this.$confirm("此操作将永久删除该产品信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.deleteOrders(rowId);
                })
                .catch(() => {
                });
        },

        intentDetail() {
            this.isImportUdi = false;
            this.selectUdiDialogVisible = true;
        },
        addSelectErp(row) {
            this.defaultSys = row.sysId;
            this.isImport = false;
            this.selectErpDialogVisible = true;
        },

        removeErp(row) {
            this.$confirm("此操作将解除该绑定产品信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    let query = {
                        relId: this.editQuery.relId,
                        thirdSys: row.sysId
                    };
                    removeRl(query)
                        .then((response) => {
                            if (response.code == 20000) {
                                this.getThirdSysDetail()();
                                this.$message({
                                    type: "success",
                                    message: "删除成功！",
                                });
                            } else {
                                this.$message.error(response.message);
                            }
                        })
                        .catch(() => {
                        });
                })
                .catch(() => {
                });
        },
        bindId(val) {
            let query = {
                id: this.curRow.id,
                bindRlFk: val.id
            };
            updateBind(query).then((response) => {
                if (response.code == 20000) {
                    this.$message.success("绑定成功");
                    this.closeBindDialog(this.curRow.id);
                } else {
                    this.$message.error("绑定失败");
                }
            }).catch(() => {

            });

        },

        handleSelectionChange(val) {
            console.log("----" + val);
        },

        cancelDialog() {
            this.editDialogVisible = false;
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        handleModifyClick(row) {
            this.uuid = row.uuid;
            this.relId = row.relId;
            this.isImport = false;
            this.thisData = row;
            this.editDialogVisible = true;
            this.editQuery = row;
            this.relevanceEdit = {
                id: row.id,
                // thirdId: row.thirdId,
                isUseDy: row.isUseDy,
                isDisable: row.isDisable,
                isLock: row.isLock,
            };
            this.checked = row.isUseDy == 1;

            let ttquery = {
                uuid: row.uuid,
                page: 1,
                limit: 10,
            };
            console.log("this.editQuery.isUseDy = " + this.editQuery.isUseDy);

            this.getDetailList(ttquery);
            this.getThirdSysDetail();
        },
        closeUdi(val) {
            this.selectUdiDialogVisible = false;
            this.selectErpDialogVisible = false;
            this.selectLocalVisible = false;
            this.getThirdSysDetail();
            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 = [];
                });
        },
        getSysFilter() {
            sysFilter()
                .then((response) => {
                    this.loading = false;
                    this.filterList = response.data.list || [];
                })
                .catch(() => {
                    this.loading = false;
                    this.filterList = [];
                });

        },


        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: {
        selectUdi,
        selectErp,
        selectLocalUdi,
    }
    ,
    created() {
        this.getList();
    }
    ,
}
;
</script>
<style scoped>
.itemTag {
    float: left;
    text-align: left;
    margin-top: 10px;
    width: 25%;
}

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

.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;
}
</style>
