<template>
    <el-card class="el-card">
        <el-form :inline="true" :model="newProductData" size="mini">
            <div style="margin-bottom: 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-select
                                style="width: 60%"
                                v-model="newProductData.thirdSys" placeholder="请选择第三方系统"
                            >
                                <el-option

                                    v-for="item in thirdSys"
                                    :key="item.value"
                                    :label="item.thirdName"
                                    :value="item.thirdId">
                                    <span style="float: left">{{ item.thirdName }}</span>
                                    <span style="float: right; color: #8492a6; font-size: 13px">{{
                                            item.thirdId
                                        }}</span>
                                </el-option>
                            </el-select>
                        </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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.code"
                            ></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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.name"
                            ></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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.spec"
                            ></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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.measname"
                            ></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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.manufactory"
                            ></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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.registerNo"
                            ></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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.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: 60%"
                                size="small"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.qxlb"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>

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

                    <el-col :span="20" class="el-col" style="margin-top: 12px">
                        <div class="text item">
                            <div class="itemTag">
                                <span>产品描述:&nbsp;</span>
                            </div>

                            <el-input
                                style="width: 85%"
                                type="textarea"
                                size="mini"
                                rows="4"
                                placeholder="请输入内容"
                                v-model.trim="newProductData.cpms"
                            ></el-input>
                        </div>
                    </el-col>
                </el-row>
            </div>

            <div style='text-align: center; margin-bottom: 10px;'>
                <el-button type="primary" @click="saveNewProduct">提交</el-button>
                <el-button type="primary" @click="closeAddDialog">取消</el-button>
            </div>
        </el-form>
    </el-card>
</template>

<script>
import {saveNewProduct} from "@/api/thrsys/thrProducts";


import {getBasicThirdSys} from "@/api/basic/basicThirdSys";
import {addProduct} from "@/api/thrsys/thrProductsAdd";

export default {
    name: "ThrProductsAdd",
    props: {
        newType: {
            type: Object,
            required: true,
        },
        closeAddDialog: {
            type: Function,
            required: true,
        },
    },
    data() {
        return {
            newProductData: {
                name: '',
                code: null,
                registerNo: null,
                manufactory: null,
                spec: null,
                measname: null,
                ylqxzcrbarmc: null,
                ylqxzcrbarywmc: null,
                cplb: null,
                flbm: null,
                sptm: null,
                ybbm: null,
                tyshxydm: null,
                qxlb: null,
                cpms: null,
                thirdSys: null,
            },
            thirdSys: [],
        }

    },
    methods: {
        saveNewProduct() {
            this.addProductVisible = false;
            if (this.$isBlank(this.newProductData.thirdSys)) {
                this.$message.warning("第三方系统不能为空！");
                return;
            }
            if (this.$isBlank(this.newProductData.code)) {
                this.$message.warning("产品编码为空！");
                return;
            }
            if (this.$isBlank(this.newProductData.name)) {
                this.$message.warning("产品名称不能为空！");
                return;
            }

            if (this.$isBlank(this.newProductData.spec)) {
                this.$message.warning("规格型号不能为空");
                return;
            }
            if (this.$isBlank(this.newProductData.measname)) {
                this.$message.warning("计量单位不能为空！");
                return;
            }
            if (this.$isBlank(this.newProductData.manufactory)) {
                this.$message.warning("生产厂家不能为空！");
                return;
            }
            if (this.$isBlank(this.newProductData.registerNo)) {
                this.$message.warning("注册证号不能为空！");
                return;
            }


            if (this.newType == 1) {
                saveNewProduct(this.newProductData).then((res) => {
                    if (res.code == 20000) {
                        this.$message.success("添加成功");
                        //清空弹窗数据
                        this.$emit("closeAddDialog", true);
                        this.closeAddDialog();
                    }
                }).catch((error) => {
                    this.$message.error(error.message);
                })
            } else if (this.newType == 2) {
                addProduct(this.newProductData).then((res) => {
                    if (res.code == 20000) {
                        this.$message.success("添加成功");
                        //清空弹窗数据
                        this.closeAddDialog(true);
                    }
                }).catch((error) => {
                    this.$message.error(error.message);
                })
            }

        },
        getBasicThirdSys() {
            let query = {
                enabled: true,
            };
            getBasicThirdSys(query)
                .then((response) => {
                    this.thirdSys = response.data.list || [];
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                });
        },
    },
    created() {
        this.getBasicThirdSys();
    },
}
</script>

<style scoped>
.el-row {
    margin-top: 10px;
}
</style>
