<template>
    <div>
        <el-card class="el-card">
            <el-form :inline="true" :model="filterQuery" class="query-form" size="mini">
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="filterQuery.companyName"
                            placeholder="企业名称"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="filterQuery.creditCode"
                            placeholder="社会信用号"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="filterQuery.placeArea"
                            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="onSubmit"
                            >查询
                            </el-button
                            >
                        </el-button-group>
                    </el-form-item>

                    <el-form-item style="margin: 0 100px 0 auto">
                        <el-button-group>
                            <el-button type="primary" icon="search" @click="addInfoDialog()">添加</el-button>
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>

            <el-table v-loading="loading" :data="list" style="width: 100%" @row-click="getRegistrationList">
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="企业名称" prop="companyName" width="250"></el-table-column>
                <el-table-column label="社会信用号" prop="creditCode"></el-table-column>
                <el-table-column label="企业类型" prop="bussinessStatus" width="120">
                    <template slot-scope="scope">
                        <span>{{ companyTypeMap[scope.row.companyType] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="企业法人" prop="legalPersonName"></el-table-column>
                <el-table-column label="所在地区" prop="placeArea"></el-table-column>
                <el-table-column label="所在地址" prop="placeAddress" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="操作" fixed="right" width="120">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="addInfoDialog(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-pagination
                :page-size="filterQuery.limit"
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="total"
            ></el-pagination>
        </el-card>

        <el-card class="el-card">
            <el-form :inline="true" :model="registrationQuery" class="query-form" size="mini">
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordProductName"
                            placeholder="产品名称"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordCode"
                            placeholder="注册备案号"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordPeopleName"
                            placeholder="注册备案人"
                        ></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button-group>
                            <el-button
                                type="primary"
                                icon="el-icon-refresh"
                                @click="onReset2"
                            ></el-button>
                            <el-button type="primary" icon="search" @click="onSubmit2"
                            >查询
                            </el-button
                            >
                        </el-button-group>
                    </el-form-item>

                    <el-form-item style="margin: 0 100px 0 auto">
                        <el-button-group>
                            <el-button type="primary" icon="search" @click="registrationDialog()">添加</el-button>
                        </el-button-group>
                    </el-form-item>
                </el-row>
            </el-form>

            <el-table v-loading="registrationLoading"
                      :data="registrationList"
                      style="width: 100%">
                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column label="注册/备案产品名称" prop="recordProductName"></el-table-column>
                <el-table-column label="注册/备案号" prop="recordCode"></el-table-column>
                <el-table-column label="注册/备案人名称" prop="recordPeopleName"></el-table-column>
                <el-table-column label="管理类别" prop="productManageType">
                    <template slot-scope="scope">
                        <span>{{ productManageTypeMap[scope.row.productManageType] }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="批准日期" prop="create_date" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <i class="el-icon-time"></i>
                        <span>{{ scope.row.create_date }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="操作" fixed="right" width="250">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="registrationDialog(scope.row)"
                        >编辑
                        </el-button
                        >
                        <el-button
                            :disabled="scope.row.filePath === null || scope.row.filePath === ''"
                            type="text"
                            size="small"
                            @click.native="toViewRegistrationCert(scope.row, 1)"
                        >查看文件
                        </el-button
                        >
                        <el-button
                            :disabled="scope.row.instructions === null || scope.row.instructions === ''"
                            type="text"
                            size="small"
                            @click.native="toViewRegistrationCert(scope.row, 2)"
                        >查看说明书附件
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @click.native="deleteRegistration(scope.row)"
                        >删除
                        </el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                :page-size="registrationQuery.limit"
                @current-change="certHandleCurrentChange"
                layout="prev, pager, next"
                :total="certTotal"
            >
            </el-pagination>
        </el-card>

        <el-dialog
            title="生产企业信息编辑"
            :visible.sync="addInfoVisible"
            width="80%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="addInfoVisible"
            @close='closeDialog'
        >
            <suppliersEnterpriseBasicAdd
                :addCloseDialog="closeDialog"
                :enterpriseId="enterpriseId"
            ></suppliersEnterpriseBasicAdd>
        </el-dialog>

        <el-dialog
            title="注册/备案凭证编辑"
            :visible.sync="registrationVisible"
            width="80%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="registrationVisible"
            @close='closeDialog'
        >
            <suppliersRegistrationBasic
                :closeRegistrationDialog="closeDialog"
                :enterpriseId="registrationQuery.enterpriseId"
                :registrationId="registrationId"
            ></suppliersRegistrationBasic>
        </el-dialog>
    </div>
</template>

<script>
import {deleteCompany, getCompanyList} from "../../api/basic/suppliersEnterpriseBasic"
import {getRegistrationList, deleteRegistration} from "../../api/basic/suppliersRegistrationBasic";
import suppliersEnterpriseBasicAdd from "./suppliersEnterpriseBasicAdd";
import suppliersRegistrationBasic from "./suppliersRegistrationBasic";
import {deleteLog} from "../../api/thrsys/thrOrderExport";
import {deleteCompanyCert, filterCompanyCert} from "../../api/warehouse/suppliersEnterpriseBasicCert";
import {BASE_URL} from "../../config/app";

    export default {
        data() {
            return {
                filterQuery: {
                    companyName: "",
                    creditCode: "",
                    placeArea: "",
                    page: 1,
                    limit: 20,
                },
                value: "",
                total: 0,
                companyTypeMap: {
                    1: "境内企业",
                    2: "境外企业"
                },
                productManageTypeMap: {
                    1: "Ⅰ类",
                    2: "Ⅱ类",
                    3: "Ⅲ类",
                },
                list: [],
                addInfoVisible: false,
                registrationVisible: false,
                enterpriseId: "",
                registrationQuery: {
                    recordProductName: "",
                    recordCode: "",
                    recordPeopleName: "",
                    page: 1,
                    limit: 20
                },
                registrationList: [],
                certTotal: 0,
                registrationLoading: false,
                registrationId: "",
                fileUrl: "",
            };
        },
        filters: {
            statusFilterType(status) {
                const statusMap = {
                    0: "gray",
                    1: "success",
                    2: "danger",
                };
                return statusMap[status];
            },
            statusFilterName(status) {
                const statusMap = {
                    0: "禁用",
                    1: "正常",
                    2: "未验证",
                };
                return statusMap[status];
            },
        },
        methods: {
            onReset() {
                this.$router.push({
                    path: "",
                });
                this.filterQuery = {
                    companyName: "",
                    creditCode: "",
                    placeArea: "",
                    page: 1,
                    limit: 20,
                };
                this.getList();
                this.registrationList = [];
            },
            onSubmit() {
                this.getList();
                this.registrationList = [];
            },
            onReset2() {
                this.registrationQuery = {
                    recordProductName: "",
                    recordCode: "",
                    recordPeopleName: "",
                    page: 1,
                    limit: 20
                };
                this.getRegistrationList();
            },
            onSubmit2() {
                this.getRegistrationList();
            },
            toViewRegistrationCert(row, num) {
                let path = num === 1 ? row.filePath : row.instructions;
                window.open(this.fileUrl + path);
            },
            getList() {
                this.loading = true;
                getCompanyList(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;
                    });
            },
            addInfoDialog(row) {
                if (this.$isNotBlank(row)) {
                    this.enterpriseId = row.id;
                }
                this.addInfoVisible = true;
            },
            registrationDialog(row) {
                if (this.$isBlank(this.registrationQuery.enterpriseId)) {
                    return this.$message.error('未选择企业');
                }
                if (this.$isNotBlank(row)) {
                    this.registrationId = row.id;
                }
                this.registrationVisible = true;
            },
            deleteRegistration(row) {
                this.$confirm("是否删除?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }).then(() => {
                    this.certLoading = true;
                    let tQuery = {
                        id: row.id,
                        filePath: row.filePath,
                        filePath2: row.instructions,
                    };
                    deleteRegistration(tQuery)
                        .then((response) => {
                            this.certLoading = false;
                            if (response.code === 20000) {
                                this.getRegistrationList();
                                this.$message({
                                    type: "success",
                                    message: "删除成功！",
                                });
                            } else {
                                this.$message.error(response.message);
                            }
                        })
                        .catch(() => {
                            this.certLoading = false;
                        });
                }).catch(() => {
                });
            },
            closeDialog(type) {
                this.addInfoVisible = false;
                this.registrationVisible = false;
                this.enterpriseId = null;
                this.registrationId = null;
                this.getList();
                this.registrationList = [];
            },
            handleSizeChange(val) {
                this.filterQuery.limit = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.filterQuery.page = val;
                this.getList();
            },
            handleCheckedChange(val) {
                console.log(val);
                console.log(this.check);
            },
            getRegistrationList(row) {
                this.registrationLoading = true;
                if (this.$isNotBlank(row)) {
                    this.registrationQuery.enterpriseId = row.id;
                }
                getRegistrationList(this.registrationQuery)
                    .then((response) => {
                        this.registrationLoading = false;
                        this.registrationList = response.data.list || [];
                    })
                    .catch(() => {
                        this.registrationLoading = false;
                        this.registrationList = [];
                    });
            },
        },
        components: {
            suppliersEnterpriseBasicAdd,
            suppliersRegistrationBasic,
        },
        mounted() {
        },
        created() {
            this.fileUrl = BASE_URL + "/udiwms/image/register/file/getImage?type=image4&name=";
            this.getList();
        },
    };
</script>
