<template>
    <div>

        <!---生产企业资质----->
        <el-card class="el-card">
            <el-form :inline="true" :model="filterQuery" class="query-form" size="mini" style="margin-top: 5px">
                <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-select v-model="filterQuery.auditStatus" placeholder="处理状态">
                            <el-option label="全部" value=""></el-option>
                            <el-option label="草稿" value="0"></el-option>
                            <el-option label="未审核" value="6"></el-option>
                            <el-option label="已审核" value="1"></el-option>
                            <el-option label="已拒绝" value="2"></el-option>
                        </el-select>
                    </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 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%" highlight-current-row
                      @current-change="supManufacturerClick">
                <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="placeArea"></el-table-column>
                <el-table-column label="所在地址" prop="placeAddress" :show-overflow-tooltip="true"></el-table-column>
                <el-table-column label="所属供应商" prop="supName" show-overflow-tooltip></el-table-column>
                <el-table-column label="审核状态" prop="auditStatus" width="120">
                    <template slot-scope="scope">
                        <el-tag :type="(scope.row.auditStatus) | statusFilterType">
                            {{ checkFlag[scope.row.auditStatus] }}
                        </el-tag>
                    </template>
                </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"
                            :disabled="scope.row.auditStatus === 1"
                            @click.native.stop="deleteDialog(scope.row)"
                        >删除
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            v-if="scope.row.auditStatus === 2"
                            @click.native.stop="rejectInfo(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-dialog
                :title="formMap[formName]"
                :visible.sync="addInfoVisible"
                width="80%"
                :close-on-click-modal="false"
                :close-on-press-escape="false"
                v-if="addInfoVisible"
                @close='closeDialog'
            >
                <suppliersEnterpriseBasicAdd
                    :addCloseDialog="closeDialog"
                    :inputQuery="inputQuery"
                    :editType="editType"
                    :fromType="fromType"
                ></suppliersEnterpriseBasicAdd>
            </el-dialog>


        </el-card>


        <!---配送企业资质----->
        <el-card class="el-card">
            <el-form :inline="true" :model="registrationQuery" class="query-form" size="mini" style="margin-top: 5px">
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordProductName" clearable
                            placeholder="产品名称"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordCode" clearable
                            placeholder="注册备案号"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordPeopleName" clearable
                            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 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" show-overflow-tooltip></el-table-column>
                <el-table-column label="注册/备案号" prop="recordCode" show-overflow-tooltip></el-table-column>
                <el-table-column label="注册/备案人名称" prop="recordPeopleName" show-overflow-tooltip></el-table-column>
                <el-table-column label="生产企业" prop="manufacturerName" show-overflow-tooltip></el-table-column>
                <el-table-column label="所属供应商" prop="supName" show-overflow-tooltip></el-table-column>
                <el-table-column label="审核状态" prop="auditStatus" width="120">
                    <template slot-scope="scope">
                        <el-tag :type="(scope.row.auditStatus) | statusFilterType">
                            {{ checkFlag[scope.row.auditStatus] }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="120">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="small"
                            @click.native.stop="registrationDialog(scope.row)"
                        >编辑
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            :disabled="scope.row.auditStatus === 1"
                            @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-dialog
                :title="formProductMap[formName]"
                :visible.sync="registrationVisible"
                width="80%"
                :close-on-click-modal="false"
                :close-on-press-escape="false"
                v-if="registrationVisible"
                @close='closeProductDialog'
            >
                <suppliersRegistrationBasic
                    :closeRegistrationDialog="closeProductDialog"
                    :inputQuery="curProductQuery"
                    :editType="editProductType"
                    :currentManufacturer="currentManufacturer"
                ></suppliersRegistrationBasic>
            </el-dialog>
        </el-card>

    </div>
</template>

<script>
import {deleteCompany, getCompanyList, getCompanyBySup} from "../../api/purchase/supManufacturer"
import suppliersEnterpriseBasicAdd from "./supManufacturerEditDialog";
import {BASE_URL} from "../../config/app";
import {getSupComapnys} from "@/api/purchase/supCompany";
import {getRegistrationList, deleteRegistration} from "../../api/purchase/suppliersRegistrationBasic";
import suppliersRegistrationBasic from "./supProductEditDialog";

export default {
    name: "supCertificationAdd",
    data() {
        return {

            /**------------------生产企业资质--------------------*/
            filterQuery: {
                customerId: null,
                companyName: "",
                auditStatus: null,
                creditCode: "",
                placeArea: "",
                page: 1,
                limit: 20,
            },
            total: 0,
            companyTypeMap: {
                1: "境内企业",
                2: "境外企业"
            },
            productManageTypeMap: {
                1: "Ⅰ类",
                2: "Ⅱ类",
                3: "Ⅲ类",
            },
            checkFlag: {
                0: "草稿",
                1: "已通过",
                2: "已拒绝",
                3: "变更未审核",
                6: "未审核",
            },
            loading: false,
            list: [],
            addInfoVisible: false,
            fileUrl: "",
            inputQuery: {

                auditStatus: 6
            },
            editType: 1,
            fromType: 2,//1:供应商，2:医院
            formMap: {
                add: "新增生产企业资质信息",
                update: "编辑生产企业资质信息",
            },
            currentManufacturer: null,


            /**------------------配送企业资质--------------------*/
            registrationVisible: false,
            enterpriseId: "",
            registrationQuery: {
                auditStatus: null,
                customerId: null,
                manufacturerIdFk: null,
                recordProductName: "",
                recordCode: "",
                recordPeopleName: "",
                page: 1,
                limit: 20
            },
            registrationList: [],
            certTotal: 0,
            registrationLoading: false,
            registrationId: "",

            pmQuery: {
                customerId: null,
                companyName: null,
                page: 1,
                limit: 10,
            },
            curProductQuery: {
                auditStatus: 6
            },
            editProductType: 0,
            formName: "add",
            formProductMap: {
                add: "新增配送产品资质信息",
                update: "编辑配送产品资质信息",
            },
        };
    },
    filters: {
        statusFilterType(status) {
            const statusMap = {
                0: "warning",
                1: "success",
                2: "danger",
                3: "warning",
                6: "warning",
            };
            return statusMap[status];
        },
        statusFilterName(status) {
            const statusMap = {
                0: "禁用",
                1: "正常",
                2: "未验证",
            };
            return statusMap[status];
        },
    },
    methods: {
        /**-------------------------生产企业资质---------------------*/
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                customerId: null,
                companyName: "",
                creditCode: "",
                placeArea: "",
                auditStatus: "",
                page: 1,
                limit: 20,
            };
            this.getList();
            this.registrationList = [];
        },
        onSubmit() {
            this.getList();
            this.registrationList = [];
        },
        toViewRegistrationCert(row, num) {
            let path = num === 1 ? row.filePath : row.instructions;
            window.open(this.fileUrl + path);
        },
        getList() {
            this.loading = true;
            getCompanyBySup(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.editType = 1;
                this.inputQuery = row;
            } else {
                this.enterpriseId = null;
                this.editType = 0;
                this.inputQuery = {
                    auditStatus: 0
                };
            }

            this.addInfoVisible = true;
        },
        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);
        },

        rejectInfo(row) {
            this.$confirm(row.auditComment, "驳回说明", {
                confirmButtonText: "确定",
                type: "warning",
                showCancelButton: false,
            })
                .then(() => {
                });

        },

        deleteDialog(row) {
            this.$confirm("删除后将清空该生产企业以及所有关联信息?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    let tQuery = {
                        id: row.id,
                    };
                    deleteCompany(tQuery).then(() => {
                        this.getList();
                    });
                })
                .catch(() => {
                });
        },

        supManufacturerClick(row) {
            this.registrationQuery.customerId = row.customerId;
            this.registrationQuery.manufacturerIdFk = row.manufacturerId;
            this.getRegistrationList();
            this.currentManufacturer = row;
        },

        /**---------------------------配送产品资质----------------------*/

        onReset2() {
            this.registrationQuery = {
                customerId: null,
                manufacturerIdFk: null,
                recordProductName: "",
                recordCode: "",
                recordPeopleName: "",
                auditStatus: null,
                page: 1,
                limit: 20
            };
            this.getRegistrationList();
        },
        onSubmit2() {
            if(this.currentManufacturer==null){
                this.$message.error("请先选中生产企业！");
                return
            }
            this.getRegistrationList();
        },
        registrationDialog(row) {
            if(this.currentManufacturer==null){
                this.$message.error("请先选中生产企业！");
                return
            }

            if (this.$isNotBlank(row)) {
                this.registrationId = row.id;
                this.curProductQuery = row;
                this.formName = "update";
                this.editProductType = 1;
            } else {
                this.editProductType = 0;
                this.formName = "add";
                this.curProductQuery = {
                    auditStatus: 0
                };
            }
            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(() => {
            });
        },
        closeProductDialog() {
            this.registrationVisible = false;
            this.enterpriseId = null;
            this.registrationId = null;
            this.getList();
            this.registrationList = [];
        },
        getRegistrationList() {
            this.registrationLoading = true;
            getRegistrationList(this.registrationQuery)
                .then((response) => {
                    this.registrationLoading = false;
                    this.registrationList = response.data.list || [];
                })
                .catch(() => {
                    this.registrationLoading = false;
                    this.registrationList = [];
                });
        },
        supChange(val) {
            this.pmQuery.customerId = val;
            this.findPMMethod();
        },
        certHandleCurrentChange(val) {
            this.registrationQuery.page = val;
            this.registrationList();
        }

    },
    components: {
        suppliersEnterpriseBasicAdd, suppliersRegistrationBasic
    },
    mounted() {
    },
    created() {
        this.fileUrl = BASE_URL + "/udiwms/image/register/file/getImage?type=image4&name=";
        this.getList();
        // this.getRegistrationList();
    },
}
</script>

<style scoped>

</style>
