<template>
    <div>
        <el-form
            :model="inputQuery"
            :rules="rules"
            ref="inputQuery"
            label-width="100px"
            style="margin-top: -50px"
        >
            <el-card class="el-card">
                <div style="display: flex">
                    <el-button
                        type="primary"
                        size="small"
                        icon="search"
                        style="margin: 0 100px 20px auto; width: 100px"
                        @click="submitUpload()"
                        :loading="loading"
                    >保存
                    </el-button
                    >
                </div>
                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="注册/备案号:" label-width="150px" prop="recordCode">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.recordCode"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="注册/备案产品名称:" label-width="150px" prop="recordProductName">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.recordProductName"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="注册/备案人名称:" label-width="150px" prop="recordPeopleName">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.recordPeopleName"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="代理人名称:" label-width="150px" prop="agentName">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.agentName"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="8" class="el-col">
                        <el-form-item label="注册人住所地区:" label-width="150px" prop="recordPeopleArea">
                            <el-cascader
                                size="small"
                                :options="options"
                                v-model="selectedOptions"
                                placeholder="请选择所在地区"
                                style="width: 100%"
                                @change="handleChange"
                            >
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="2" class="el-col">
                        <el-button
                            type="primary"
                            size="small"
                            icon="search"
                            @click="clearRecordPeopleArea()"
                            :loading="loading"
                        >清空
                        </el-button
                        >
                    </el-col>
                    <el-col :span="8" class="el-col">
                        <el-form-item label="代理人住所地区:" label-width="150px" prop="agentArea">
                            <el-cascader
                                size="small"
                                :options="options"
                                v-model="selectedOptions2"
                                placeholder="请选择生产地区"
                                style="width: 100%"
                                @change="handleChange2"
                            >
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="2" class="el-col">
                        <el-button
                            type="primary"
                            size="small"
                            icon="search"
                            @click="clearAgentArea()"
                            :loading="loading"
                        >清空
                        </el-button
                        >
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="注册人住所地址:" label-width="150px" prop="recordPeopleAddress">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.recordPeopleAddress"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="代理人住所地址:" label-width="150px" prop="agentAddress">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.agentAddress"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="医疗器械管理类别:" label-width="150px" prop="productManageType">
                            <el-select
                                size="small"
                                v-model="inputQuery.productManageType"
                                placeholder="医疗器械管理类别"
                                style="width: 100%;"
                            >
                                <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-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="器械类别:" label-width="150px" prop="productType">
                            <el-select
                                size="small"
                                v-model="inputQuery.productType"
                                placeholder="医疗器械管理类别"
                                style="width: 100%;"
                            >
                                <el-option label="器械" value="1"></el-option>
                                <el-option label="诊断试剂" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="器械目录分类编码:" label-width="150px" prop="productDirectoryCode">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productDirectoryCode"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :span="10" class="el-col">-->
<!--                        <el-form-item label="说明书附件:" label-width="150px" prop="instructions">-->
<!--                            <el-input-->
<!--                                size="small"-->
<!--                                placeholder="请输入内容"-->
<!--                                v-model="inputQuery.instructions"-->
<!--                                @input="change()"-->
<!--                            ></el-input>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
                </el-row>

<!--                <el-row :gutter="20" class="el-row" type="flex">-->
<!--                    <el-col :span="20" class="el-col">-->
<!--                        <el-form-item label="注册/备案证电子档案:" label-width="150px" prop="recordText">-->
<!--                            <el-input-->
<!--                                type="textarea"-->
<!--                                size="small"-->
<!--                                placeholder="请输入内容"-->
<!--                                v-model="inputQuery.recordText"-->
<!--                                @input="change()"-->
<!--                            ></el-input>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                </el-row>-->

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="产地:" label-width="150px" prop="placeOrigin">
                            <el-select
                                size="small"
                                v-model="inputQuery.placeOrigin"
                                placeholder="产地"
                                style="width: 100%;"
                            >
                                <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-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="生产国或地区:" label-width="150px" prop="productionRegion">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productionRegion"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20" class="el-col">
                        <el-form-item label="生产地址:" label-width="150px" prop="productAddress">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productAddress"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20" class="el-col">
                        <el-form-item label="生产厂商名称:" label-width="150px" prop="productionCompanyName">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productionCompanyName"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20" class="el-col">
                        <el-form-item label="售后服务机构:" label-width="150px" prop="afterSale">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.afterSale"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="20" class="el-col">
                        <el-form-item label="规格型号:" label-width="150px" prop="specification">
                            <el-input
                                type="textarea"
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.specification"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20" class="el-col">
                        <el-form-item label="结构及组成:" label-width="150px" prop="structure">
                            <el-input
                                type="textarea"
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.structure"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20" class="el-col">
                        <el-form-item label="适用范围:" label-width="150px" prop="scope">
                            <el-input
                                type="textarea"
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.scope"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20" class="el-col">
                        <el-form-item label="其它内容:" label-width="150px" prop="other">
                            <el-input
                                type="textarea"
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.other"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="20" class="el-col">
                        <el-form-item label="备注:" label-width="150px" prop="remark">
                            <el-input
                                type="textarea"
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.remark"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
<!--                    <el-col :span="2" class="el-col">-->
<!--                        <div class="ao-text">-->
<!--                            <span>文件：</span>-->
<!--                        </div>-->
<!--                    </el-col>-->
                    <el-col :span="10" class="el-col">
                        <el-form-item label="电子档:" label-width="150px">
                            <el-upload
                                class="upload-demo"
                                ref="upload"
                                :action="this.uploadUrl"
                                :on-preview="uploadHandlePreview"
                                :on-remove="uploadHandleRemove"
                                :limit="1"
                                :on-exceed="uploadHandleExceed"
                                accept=".jpg,.png"
                                :on-change="uploadOnchange"
                                :on-success="uploadHandleSuccess"
                                :on-error="uploadHandleError"
                                :file-list="fileList"
                                :data="{type:'image4'}"
                                :auto-upload="false">
                                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                                <div>只能上传 .jpg,.png 文件，且不超过 2 MB</div>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="说明书附件:" label-width="150px">
                            <el-upload
                                class="upload-demo"
                                ref="upload2"
                                :action="this.uploadUrl"
                                :on-preview="uploadHandlePreview"
                                :on-remove="uploadHandleRemove"
                                :limit="1"
                                :on-exceed="uploadHandleExceed"
                                accept=".jpg,.png"
                                :on-change="uploadOnchange"
                                :on-success="uploadHandleSuccess2"
                                :on-error="uploadHandleError"
                                :file-list="fileList2"
                                :data="{type:'image4'}"
                                :auto-upload="false">
                                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                                <div>只能上传 .jpg,.png 文件，且不超过 2 MB</div>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-card>

        </el-form>
    </div>
</template>

<script>
import {getRegistrationInfo, addOrModifyRegistration} from "../../api/basic/suppliersRegistrationBasic";
import store from "../../store/index";
import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode,
} from "element-china-area-data";
import {BASE_URL} from "../../config/app";
import suppliersEnterpriseBasicAddCert from "./suppliersEnterpriseBasicAddCert";
import draggable from "vuedraggable";

export default {
    name: "suppliersEnterpriseBasicAdd",
    props: {
        closeRegistrationDialog: {
            type: Function,
            required: true,
        },
        enterpriseId: "",
        registrationId: "",
    },
    data() {
        return {
            inputQuery: {
                recordCode: "",
                recordProductName: "",
                productManageType: "",
                productType: "",
                recordPeopleArea: "",
                recordPeopleAddress: "",
                recordPeopleName: "",
                productDirectoryCode: "",
                specification: "",
            },
            loading: false,
            classesDisplay: false,
            selectedOptions: [],
            selectedOptions2: [],
            options: regionDataPlus,
            rules: {
                recordCode: [
                    {
                        required: true,
                        message: "请输入注册/备案号",
                        trigger: "blur"
                    }
                ],
                recordProductName: [
                    {
                        required: true,
                        message: "请输入注册/备案产品名称",
                        trigger: "blur"
                    }
                ],
                productManageType: [
                    {
                        required: true,
                        message: "请选择医疗器械管理类别",
                        trigger: "change"
                    }
                ],
                productType: [
                    {
                        required: true,
                        message: "请选择器械类别",
                        trigger: "change"
                    }
                ],
                recordPeopleArea: [
                    {
                        required: true,
                        message: "请选择注册人住所地区",
                        trigger: "change"
                    }
                ],
                recordPeopleAddress: [
                    {
                        required: true,
                        message: "请输入注册人住所地址",
                        trigger: "change"
                    }
                ],
                recordPeopleName: [
                    {
                        required: true,
                        message: "请输入注册/备案人名称",
                        trigger: "change"
                    }
                ],
                productDirectoryCode: [
                    {
                        required: true,
                        message: "请输入器械目录分类编码",
                        trigger: "change"
                    }
                ],
                specification: [
                    {
                        required: true,
                        message: "请输入规格型号",
                        trigger: "change"
                    }
                ],
            },
            uploadUrl: "",
            fileList: [],
            fileList2: [],
            fileUrl: "",
            idQuery: {
                id: "",
            },
        };
    },
    created() {
        if (this.$isNotBlank(this.registrationId)) {
            this.getData();
        }
        this.inputQuery.enterpriseId = this.enterpriseId;
        this.uploadUrl = BASE_URL + "/udiwms/upload/register/file";
        this.fileUrl = BASE_URL + "/udiwms/image/register/file/getImage?type=image4&name=";
    },
    components: {
        draggable,
        suppliersEnterpriseBasicAddCert,
    },
    methods: {
        submitUpload() {
            this.$refs["inputQuery"].validate(valid => {
                if (valid) {
                    if (this.$refs.upload.uploadFiles.length > 0) {
                        this.$refs.upload.submit();
                    } else if (this.$refs.upload2.uploadFiles.length > 0) {
                        this.$refs.upload2.submit();
                    } else {
                        this.onModifySubmit('inputQuery')
                    }
                }
            });
        },
        onModifySubmit(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.loading = true;
                    let urlName = this.$isBlank(this.registrationId) ? 'add' : 'update';
                    addOrModifyRegistration(this.inputQuery, urlName).then(response => {
                        this.loading = false;
                        if (response.code === 20000) {
                            this.closeRegistrationDialog(true);
                            this.$message.success("保存成功");
                        } else {
                            this.$message.error(response.message);
                        }
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        clearRecordPeopleArea() {
            this.inputQuery.recordPeopleArea = ' ';
            this.inputQuery.recordPeopleAreaCode = ' ';
            this.selectedOptions = [];
        },
        clearAgentArea() {
            this.inputQuery.agentArea = ' ';
            this.inputQuery.agentAreaCode = ' ';
            this.selectedOptions2 = [];
        },
        getData() {
            this.loading = true;
            let tQuery = {
                id: this.registrationId,
            };
            getRegistrationInfo(tQuery).then((response) => {
                console.log(response)
                this.loading = false;
                this.inputQuery = response.data;
                this.selectedOptions = this.inputQuery.recordPeopleAreaCode.split(",");
                this.selectedOptions2 = this.inputQuery.agentAreaCode.split(",");
            });
        },
        openFile() {
            window.open(this.fileUrl + this.inputQuery.filePath);
        },
        toViewCompanyCert(row) {
            window.open(this.certFileUrl + row.filePath);
        },
        handleChange(value) {
            this.inputQuery.recordPeopleArea =
                CodeToText[value[0]] + CodeToText[value[1]] + CodeToText[value[2]];
            this.inputQuery.recordPeopleAreaCode = value.toString();
        },
        handleChange2(value) {
            this.inputQuery.agentArea =
                CodeToText[value[0]] + CodeToText[value[1]] + CodeToText[value[2]];
            this.inputQuery.agentAreaCode = value.toString();
        },
        uploadHandleRemove(file, fileList) {
            console.log(file, fileList);
        },
        uploadHandlePreview(file) {
            console.log(file);
            console.log(this.fileList);
        },
        uploadHandleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        uploadOnchange(file, fileList) {
            let fileName = file.name;
            let uid = file.uid
            let pos = fileName.lastIndexOf(".");
            let lastName = fileName.substring(pos, fileName.length);
            if (lastName.toLowerCase() !== ".jpg" && lastName.toLowerCase() !== ".png") {
                this.$message.error("上传文件只能是 .jpg,.png 格式");
                for (let i = 0; i < fileList.length; i++) {//从list删除
                    if (fileList[i].uid === uid) {
                        fileList.splice(i, 1)
                    }
                }
                return;
            }
            // 限制上传文件的大小
            const isLt = file.size / 1024 / 1024 / 2 <= 1;
            if (!isLt) {
                this.$message.error("上传文件大小不能超过 2MB");
                for (let i = 0; i < fileList.length; i++) {
                    if (fileList[i].uid === uid) {
                        fileList.splice(i, 1)
                    }
                }
            }
            return isLt;
        },
        uploadHandleSuccess(response, file, fileList) {
            console.log(response)
            if (response.code === 20000) {
                if (this.$isBlank(this.inputQuery.instructions)) {
                    this.inputQuery.filePath = response.data.name;
                } else {
                    this.inputQuery.newFilePath = response.data.name;
                }
                if (this.$refs.upload2.uploadFiles.length > 0) {
                    this.$refs.upload2.submit();
                } else {
                    this.onModifySubmit('inputQuery');
                }
            } else {
                this.$message.error("文件上传失败：" + response.message);
            }
        },
        uploadHandleSuccess2(response, file, fileList) {
            console.log(response)
            if (response.code === 20000) {
                if (this.$isBlank(this.inputQuery.instructions)) {
                    this.inputQuery.instructions = response.data.name;
                } else {
                    this.inputQuery.newInstructions = response.data.name;
                }
                this.onModifySubmit('inputQuery');
            } else {
                this.$message.error("文件上传失败：" + response.message);
            }
        },
        uploadHandleError() {

        },
    }
};
</script>
<style>
.itemTag {
    float: left;
    text-align: left;
    margin-top: 10px;
    width: 120px;
}

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

.el-card {
    margin-right: 20px;
    margin-top: 15px;
    /*transition: all .5s;*/
}

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

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