<template>
    <div>
        <el-form :inline="true" :model="filterQuery" size="mini" style="margin-top: 20px;margin-left: 15px;">
            <el-row style="width: 1000px;">
                <el-form-item class="query-form-item">
                    <el-input v-model="filterQuery.nameCode"
                              style="width: 600px"
                              placeholder="请输入完整DI"
                              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
                        clearable
                        v-model="filterQuery.cpmctymc"
                        placeholder="产品通用名"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input
                        clearable
                        v-model="filterQuery.ggxh"
                        placeholder="规格型号"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input
                        clearable
                        v-model="filterQuery.cpms"
                        placeholder="产品描述"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input
                        v-model="filterQuery.zczbhhzbapzbh"
                        placeholder="注册/备案凭证号"
                    ></el-input>
                </el-form-item>
            </el-row>

            <el-row>
                <el-form-item class="query-form-item">
                    <el-input
                        clearable
                        v-model="filterQuery.ylqxzcrbarmc"
                        placeholder="医疗器械注册人"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input
                        clearable
                        v-model="filterQuery.manufactory"
                        placeholder="生产企业"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input
                        clearable
                        v-model="filterQuery.thirdId"
                        placeholder="第三方产品编码"
                    ></el-input>
                </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="search"
                        >查询
                        </el-button
                        >
                        <el-button type="primary" icon="search" @click="intentDetail"
                        >选入器械信息
                        </el-button
                        >
                    </el-button-group>
                </el-form-item>
            </el-row>
        </el-form>

        <el-table
            v-loading="loading"
            :data="list"
            style="width: 100%; margin-left: 10px;"
            @selection-change="handleSelectionChange"
        >
            <el-table-column label="序号" type="index" fixed></el-table-column>
            <el-table-column
                label="最小销售产品标识"
                prop="nameCode"
                width="140"
                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="cpms"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="注册/备案凭证号"
                prop="zczbhhzbapzbh"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="生产厂家"
                prop="manufactory"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="第三方产品编码"
                prop="thirdId"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column 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="deleteDialog(scope.row)"
                    >删除
                    </el-button
                    >
                </template>
            </el-table-column>
        </el-table>

        <el-dialog
            title="产品信息编辑"
            :visible.sync="editDialogVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            width="70%"
            v-if="editDialogVisible"
        >
            <el-card>
                <div style=" float: left;
                        text-align: left;
                        font-weight: bold;
                        margin-bottom: 8px"
                >
                    <span>产品标识:</span>
                </div>
                <div style=" float: right;
                        text-align: right;
                        margin-bottom: 8px;"
                >
                </div>
                <el-table
                    v-loading="loading"
                    :data="detailList"
                    style="width: 100%; "
                    border
                    key="3"
                    @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-card>
            <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"
                                :disabled="!checked"
                                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"
                                :disabled="!checked"
                                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="true"
                                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
                                v-model="editQuery.manufactory"
                                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">
                        <div class="text item">
                            <div class="itemTag">
                                <span>是否包含批号:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.scbssfbhph"
                            ></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"
                                v-model="editQuery.scbssfbhxlh"
                                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%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.scbssfbhscrq"
                            ></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"
                                v-model="editQuery.scbssfbhsxrq"
                                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">
                        <div class="text item">
                            <div class="itemTag">
                                <span>产品描述:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                :disabled="true"
                                size="small"
                                splaceholder="请输入内容"
                                v-model="editQuery.cpms"
                            ></el-input>
                        </div>
                    </el-col>
                    <el-col :span="12" class="el-col">
                        <div class="text item">
                            <div class="itemTag">
                                <span>第三方系统ID:&nbsp;</span>
                            </div>
                            <el-input
                                style="width: 65%"
                                size="small"
                                :disabled="!checked"
                                splaceholder="请输入内容"
                                v-model="editQuery.thirdId"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>
                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                    </el-col>
                    <el-col :span="4" class="el-col">
                        <el-form-item prop="name">
                            <el-checkbox v-model="isUseDyValue" 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 :disabled="!checked" v-model="editQuery.disable" class="text item itemTag"
                                         @change="diableChange"
                            >是否禁用
                            </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="checked=true"
                >编辑
                </el-button>
                <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-dialog
            title="选入器械信息"
            :visible.sync="selectUdiDialogVisible"
            width="85%"
            v-if="selectUdiDialogVisible"
        >
            <selectUdi @closeUdi="closeUdi"></selectUdi>
        </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 {
    getProductInfos,
    deleteProductInfos,
    updateProductInfos
} from "../../api/receipts/productInfo";
import {
    filterByUuid,
    updatetUdiInfos,
    deletetUdiInfos,
    disableUdi
} from "../../api/erp/udiRelevance";
import selectUdi from "../product/udiRelevance";

export default {
    data() {
        return {
            filterQuery: {
                udiCode: null,
                ylqxzcrbarmc: "",
                cpmctymc: "",
                nameCode: "",
                ggxh: null,
                zczbhhzbapzbh: null,
                thirdId: null,
                cpms: null,
                manufactory: null,
                page: 1,
                limit: 20,
                addType: 1
            },
            editQuery: null,
            isUseDyCheck: false,
            relevanceEdit: {
                zxxsbzbhsydysl: null,
                uuid: null,
                isUseDy: false,
                thirdId: null
            },
            checked: false,
            isUseDyValue: false,
            list: [],
            total: 0,
            detailList: [],
            multipleSelection: [],
            editDialogVisible: false,
            selectUdiDialogVisible: false,
            loading: false
        };
    },

    methods: {
        onReset() {
            this.$router.push({
                path: ""
            });
            this.filterQuery = {
                unit: "",
                coName: "",
                nameCode: "",
                page: 1,
                limit: 20,
                addType: 1
            };
            this.getList();
        },
        search() {
            this.filterQuery.page = 1;
            this.getList();
        },
        getList() {
            this.loading = true;
            getProductInfos(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;
                });
        },
        handleDetail(row) {
            this.editQuery = row;
        },
        deleteOrders(data) {
            this.loading = true;
            let tquery = {
                id: data.uuid + ""
            };

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

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

        exportTxt() {
        },

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

        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        cancelDialog() {
            this.editDialogVisible = false;
            this.checked = false;
        },
        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        handleModifyClick(row) {
            this.editDialogVisible = true;
            this.editQuery = row;
            this.checked = false;
            this.isUseDyValue = row.isUseDy == 1;

            let ttquery = {
                uuid: row.uuid,
                page: 1,
                limit: 10
            };
            this.getDetailList(ttquery);
        },
        closeUdi(val) {
            this.selectUdiDialogVisible = false;
            if (val) {
                this.getList();
            } else {
            }
        },
        getDetailList(detailQuery) {
            filterByUuid(detailQuery)
                .then((response) => {
                    this.isUseDyCheck = false;
                    this.detailList = response.data || [];
                    this.detailList.forEach((item, index, array) => {
                        //执行代码
                        if (item.diType == 1) {
                            this.editQuery = item;
                        }
                        if (item.zxxsbzbhsydysl > 1) {
                            this.isUseDyCheck = true;
                        }
                    });
                })
                .catch(() => {
                    this.detailList = [];
                });
        },
        diableChange() {
            let query = {
                relId: this.editQuery.id,
                isDisable: this.editQuery.disable
            }
            disableUdi(query)
                .then((response) => {
                    if (response.code != 20000) {
                        this.editQuery.disable = false;
                        this.$message.error(response.message);
                    } else {
                        if (this.editQuery.disable) {
                            this.$message.success("已禁用");
                        } else {
                            this.$message.success("已取消禁用");
                        }
                    }
                })
                .catch(() => {
                    this.loading = false;
                    this.detailList = [];
                });
        },
        onAddSubmit() {
            this.checked = false;
            this.relevanceEdit = {
                id: this.editQuery.id,
                diType: this.editQuery.diType,
                versionNumber: this.editQuery.versionNumber,
                zxxsbzbhsydysl: this.editQuery.zxxsbzbhsydysl,
                uuid: this.editQuery.uuid,
                isUseDy: this.editQuery.isUseDy,
                thirdId: this.editQuery.thirdId,
                manufactory: this.editQuery.manufactory,
                isDisable: this.editQuery.isDisable,
                sptm: this.editQuery.sptm,
                ybbm: this.editQuery.ybbm
            };
            if (this.isUseDyValue === true) {
                this.relevanceEdit.isUseDy = 1;
            } else {
                this.relevanceEdit.isUseDy = 0;
            }
            updatetUdiInfos(this.relevanceEdit)
                .then((response) => {
                    this.loading = false;
                    this.getList();
                    this.cancelDialog();
                })
                .catch(() => {
                    this.loading = false;
                    this.cancelDialog();
                });
        },
        keyup_submit(event) {
            this.unionQuery.page = 1;
            this.getList();
            event.target.select();
        }
    },
    mounted() {
    },
    components: {
        selectUdi
    },
    created() {
        this.getList();
    }
};
</script>
<style scoped>
.checkitemTag {
    float: left;
    text-align: left;
    margin-top: 5px;
    width: 100%;
}

.itemTag {
    float: left;
    text-align: left;
    margin-top: 10px;
    width: 25%;
}

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

.query-form-item {
    margin-bottom: 5px;
}


</style>
