<template>
    <div>

        <!--        配送企业资质       -->
        <el-card>
            <el-form :inline="true" :model="filterQuery" class="query-form" size="mini">
                <el-row>
                    <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.creditNum"
                            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-row>
            </el-form>
            <el-table v-loading="loading" :data="list"
                      style="width: 100%"
                      highlight-current-row
                      @row-click="supCompanyClick">
                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column
                    label="配送企业名称"
                    prop="companyName"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    label="统一社会信用号"
                    prop="creditNum"
                ></el-table-column>
                <el-table-column label="所属地区" prop="area">
                </el-table-column>
                <el-table-column
                    label="详细地址"
                    prop="detailAddr"
                    :show-overflow-tooltip="true"
                >
                </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"
                            v-if="scope.row.auditStatus ==6 || scope.row.auditStatus ==3|| scope.row.auditStatus ==4"
                            @click.native.stop="editCompany(scope.row)"
                        >审核
                        </el-button>

                        <el-button
                            type="text"
                            size="small"
                            v-if="scope.row.auditStatus !=6 && scope.row.auditStatus !=3 && scope.row.auditStatus !=4"
                            @click.native.stop="editCompany(scope.row)"
                        >详情
                        </el-button>

                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                :page-size="filterQuery.limit"
                :current-page="filterQuery.page"
                :current-page.sync="filterQuery.page"
                @current-change="handleCurrentChange"
                layout="prev, pager, next"
                :total="total"
            ></el-pagination>
        </el-card>
        <el-dialog
            :title="formMap[formName]"
            :visible.sync="supCompanyVisible"
            width="80%"
            v-if="supCompanyVisible"
            @close='closeDialog'
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <supCompanyEditAdudit
                :closeDialog="closeDialog"
                :inputQuery="inputQuery"
                :editType="editType"
            ></supCompanyEditAdudit>
        </el-dialog>


        <!--        生产企业资质       -->
        <el-card class="el-card">
            <el-form :inline="true" :model="filterManufacturerQuery" class="query-form" size="mini">
                <el-row style="width: 100%">
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="filterManufacturerQuery.companyName"
                            placeholder="生产企业名称"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="filterManufacturerQuery.creditCode"
                            placeholder="统一社会信用号"
                        ></el-input>
                    </el-form-item>

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

                </el-row>
            </el-form>
            <el-table v-loading="loadManufacturerLoading" :data="manufacturerList" style="width: 100%"
                      highlight-current-row
                      @row-click="supManufacturerClick">
                <el-table-column label="序号" type="index"></el-table-column>
                <el-table-column label="生产企业名称" prop="companyName" width="200" show-overflow-tooltip></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="true"></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"
                            v-if="scope.row.auditStatus ==6 || scope.row.auditStatus ==3|| scope.row.auditStatus ==4"
                            @click.native.stop="addInfoDialog(scope.row)"
                        >审核
                        </el-button>


                        <el-button
                            type="text"
                            size="small"
                            v-if="scope.row.auditStatus !=6 && scope.row.auditStatus !=3 && scope.row.auditStatus !=4"
                            @click.native.stop="addInfoDialog(scope.row)"
                        >详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                :page-size="filterManufacturerQuery.limit"
                :current-page="filterManufacturerQuery.page"
                :current-page.sync="filterManufacturerQuery.page"
                @current-change="handleManuChange"
                layout="prev, pager, next"
                :total="manufacturerTotal"
            ></el-pagination>
        </el-card>
        <el-dialog
            :title="formManufacturerMap[formManufacturerName]"
            :visible.sync="addInfoVisible"
            width="80%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="addInfoVisible"
            @close='closeDialog'
        >
            <supManufacturerAuditDialog
                :addCloseDialog="closeManufacturerDialog"
                :enterpriseId="enterpriseId"
                :inputQuery="inputManufacturerQuery"
                :editType="editManufacturerType"
            ></supManufacturerAuditDialog>
        </el-dialog>


        <!--        配送产品资质       -->
        <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" clearable="true"
                            placeholder="产品名称"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordCode" clearable="true"
                            placeholder="注册备案号"
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="registrationQuery.recordPeopleName" clearable="true"
                            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-row>
            </el-form>
            <el-table v-loading="registrationLoading"
                      :data="registrationList"
                      highlight-current-row
                      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"
                            v-if="scope.row.auditStatus ==6 || scope.row.auditStatus ==3|| scope.row.auditStatus ==4"
                            @click.native.stop="registrationDialog(scope.row)"
                        >审核
                        </el-button>


                        <el-button
                            type="text"
                            size="small"
                            v-if="scope.row.auditStatus !=6 && scope.row.auditStatus !=3 && scope.row.auditStatus !=4"
                            @click.native.stop="registrationDialog(scope.row)"
                        >详情
                        </el-button>

                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                :page-size="registrationQuery.limit"
                :current-page="registrationQuery.page"
                :current-page.sync="registrationQuery.page"
                @current-change="certHandleCurrentChange"
                layout="prev, pager, next"
                :total="certTotal"
            >
            </el-pagination>
        </el-card>
        <el-dialog
            :title="formProductMap[formProductName]"
            :visible.sync="registrationVisible"
            width="80%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            v-if="registrationVisible"
            @close='closeProductDialog'
        >
            <suppliersRegistrationBasic
                :closeRegistrationDialog="closeProductDialog"
                :inputQuery="inputProudtQuery"
                :editType="editProductType"
            ></suppliersRegistrationBasic>
        </el-dialog>

    </div>
</template>

<script>
import {getSupComapnys, delSupComapnys} from "@/api/purchase/supCompany";
import supCompanyEditAdudit from "./supCompanyAduditDialog";
import supManufacturerAuditDialog from "@/views/purchase/supManufacturerAuditDialog";
import {deleteCompany, getCompanyList} from "@/api/purchase/supManufacturer";
import suppliersRegistrationBasic from "@/views/purchase/supProductAuditDialog";
import {deleteRegistration, getRegistrationList} from "@/api/purchase/suppliersRegistrationBasic";

export default {
    data() {
        return {
            /**-----------------配送企业资质----------------------------*/
            filterQuery: {
                companyName: "",
                checkType: "",
                creditNum: "",
                auditStatus: "21",
                page: 1,
                limit: 20,
            },
            loading: false,
            list: [],
            total: 0,
            formName: 1,
            formMap: {
                1: "配送企业资质详情",
                2: "配送企业资质审核",
            },
            checkFlag: {
                0: "草稿",
                1: "已通过",
                2: "已拒绝",
                3: "变更未审核",
                6: "未审核",
            },
            supCompanyVisible: false,
            inputQuery: {},
            editType: 0,            //0:新增，1：编辑

            /**-----------------生产企业资质----------------------------*/
            filterManufacturerQuery: {
                customerId: null,
                companyName: "",
                creditCode: "",
                placeArea: "",
                auditStatus: "21",
                page: 1,
                limit: 10,
            },
            manufacturerTotal: 0,
            companyTypeMap: {
                1: "境内企业",
                2: "境外企业"
            },
            productManageTypeMap: {
                1: "Ⅰ类",
                2: "Ⅱ类",
                3: "Ⅲ类",
            },
            formManufacturerName: 1,
            formManufacturerMap: {
                1: "生产企业资质详情",
                2: "生产企业资质审核",
            },

            manufacturerLoading: false,
            manufacturerList: [],
            fromOptions: [],
            addInfoVisible: false,
            enterpriseId: "",
            inputManufacturerQuery: {},
            editManufacturerType: 1,
            loadManufacturerLoading: false,


            /**----------------------------配送产品资质--------------------------------*/
            productLoading: false,
            value: "",
            registrationVisible: false,
            registrationQuery: {
                customerId: null,
                manufacturerIdFk: null,
                auditStatus: "21",
                recordProductName: "",
                recordCode: "",
                recordPeopleName: "",
                page: 1,
                limit: 20
            },
            registrationList: [],
            registrationLoading: false,

            pmQuery: {
                customerId: null,
                companyName: null,
                page: 1,
                limit: 10,
            },
            formProductName: 1,
            formProductMap: {
                1: "配送产品资质详情",
                2: "配送产品资质审核",
            },

            inputProudtQuery: {},
            editProductType: 0,
            certTotal: 0,

        };
    },
    filters: {


        statusFilterType(status) {
            const statusMap = {
                0: "warning",
                1: "success",
                2: "danger",
                6: "warning",
                3: "warning",
            };
            return statusMap[status];
        },
        statusFilterName(status) {
            const statusMap = {
                0: "禁用",
                1: "正常",
                2: "未验证",
            };
            return statusMap[status];
        },
    },
    methods: {

        /**-------------------------配送企业资质--------------------------*/
        onReset() {
            this.$router.push({
                path: "",
            });
            this.filterQuery = {
                companyName: "",
                creditNum: "",
                auditStatus: "21",
                page: 1,
                limit: 20,
            };
            this.getList();
            this.productList = [];
            this.salesmanList = [];
        },
        getList() {
            this.loading = true;
            getSupComapnys(this.filterQuery)
                .then((response) => {
                    console.log(response)
                    this.loading = false;
                    this.list = response.data.list || [];
                    this.total = response.data.total || 0;
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                    this.total = 0;
                });
        },
        onSubmit() {
            this.getList();
        },
        editCompany(row) {
            if (row.auditStatus == 6 || row.auditStatus == 3 || row.auditStatus == 4) {
                this.formName = 2;
                this.editType = 2;
            } else {
                this.editType = 1;
                this.formName = 1;
            }
            this.supCompanyVisible = true;
            this.inputQuery = row;
        },

        handleCurrentChange(val) {
            this.filterQuery.page = val;
            this.getList();
        },
        deleteDialog(row) {
            this.$confirm("删除后将清空该供应商以及所有关联信息?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    let tQuery = {
                        id: row.customerId,
                    };
                    delSupComapnys(tQuery).then(() => {
                        this.getList();
                    });
                })
                .catch(() => {
                });
        },
        closeDialog() {
            this.supCompanyVisible = false;
        },

        supCompanyClick(row) {
            this.filterManufacturerQuery.customerId = row.customerId;
            this.onManufacturerReset();
        },


        /**-------------------------生产企业资质--------------------------*/
        onManufacturerReset() {
            let customerId = this.filterManufacturerQuery.customerId;
            this.filterManufacturerQuery = {
                customerId: customerId,
                companyName: "",
                creditCode: "",
                placeArea: "",
                page: 1,
                auditStatus: "21",
                limit: 10,
            };
            this.manufacturerList = [];
            this.getManufacturerList();
        },
        onManufacturerSubmit() {
            this.manufacturerList = [];
            this.getManufacturerList();
        },
        getManufacturerList() {
            this.manufacturerLoading = true;
            getCompanyList(this.filterManufacturerQuery)
                .then((response) => {
                    this.manufacturerLoading = false;
                    this.manufacturerList = response.data.list || [];
                    this.manufacturerTotal = response.data.total || 0;
                })
                .catch(() => {
                    this.manufacturerLoading = false;
                    this.manufacturerList = [];
                    this.total = 0;
                });
        },
        addInfoDialog(row) {

            if (row.auditStatus == 6 || row.auditStatus == 3 || row.auditStatus == 4) {
                this.editManufacturerType = 2;
                this.formManufacturerName = 2;
            } else {
                this.editManufacturerType = 1;
                this.formManufacturerName = 1;
            }
            this.enterpriseId = row.id;
            this.inputManufacturerQuery = row;
            this.addInfoVisible = true;
        },
        closeManufacturerDialog(type) {

           if(type==true){
            this.addInfoVisible = false;
            this.enterpriseId = null;
            this.manufacturerList = [];
            this.getManufacturerList();
           }else{
            this.getManufacturerList();
           }

        },
        handleManuChange(val) {
            this.filterManufacturerQuery.page = val;
            this.getManufacturerList();
        },

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


        /**-------------------------配送产品资质--------------------------*/
        onReset2() {

            let customerId = this.registrationQuery.customerId;
            let manufacturerIdFk = this.registrationQuery.manufacturerIdFk;

            this.registrationQuery = {
                customerId: customerId,
                manufacturerIdFk: manufacturerIdFk,
                recordProductName: "",
                recordCode: "",
                recordPeopleName: "",
                auditStatus: "21",
                page: 1,
                limit: 10
            };
            this.getRegistrationList();
        },
        onSubmit2() {
            this.getRegistrationList();
        },
        registrationDialog(row) {
            if (row.auditStatus == 6 || row.auditStatus == 3 || row.auditStatus == 4) {
                this.formProductName = 2;
                this.editProductType = 2;
            } else {
                this.editProductType = 1;
                this.formProductName = 1;
            }
            this.registrationId = row.id;
            this.inputProudtQuery = row;
            this.registrationVisible = true;
        },
        closeProductDialog() {
            this.registrationVisible = false;
            this.enterpriseId = null;
            this.registrationId = null;
            this.registrationList = [];
            this.getRegistrationList();
        },
        getRegistrationList() {
            this.registrationLoading = true;
            getRegistrationList(this.registrationQuery)
                .then((response) => {
                    this.registrationLoading = false;
                    this.registrationList = response.data.list || [];
                    this.certTotal= response.data.total

                })
                .catch(() => {
                    this.registrationLoading = false;
                    this.registrationList = [];
                });
        },
        certHandleCurrentChange(val) {
            this.registrationQuery.page = val;
            this.getRegistrationList();
        }

    },
    components: {
        supCompanyEditAdudit, supManufacturerAuditDialog, suppliersRegistrationBasic
    }
    ,

    mounted() {
    },
    created() {
        this.getList();
    },
};
</script>
