<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-select
                            v-model="filterQuery.customerId"
                            filterable
                            remote
                            clearable="true"
                            reserve-keyword
                            placeholder="请输入供应商"
                            :remote-method="findMethod"
                            :loading="loading"
                            style="width: 100%;"
                        >
                            <el-option
                                v-for="item in fromOptions"
                                :key="item.customerId"
                                :label="item.companyName"
                                :value="item.customerId"
                            >
                                <span style="float: left">{{ item.companyName }}</span>
                            </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%">
                <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="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"
                            @click.native.stop="addInfoDialog(scope.row)"
                        >编辑
                        </el-button
                        >
                        <el-button
                            type="text"
                            size="small"
                            @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-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"
                :inputQuery="inputQuery"
                :editType="editType"
            ></suppliersEnterpriseBasicAdd>
        </el-dialog>

    </div>
</template>

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

export default {
    data() {
        return {
            filterQuery: {
                customerId: null,
                companyName: "",
                auditStatus: "20",
                creditCode: "",
                placeArea: "",
                page: 1,
                limit: 20,
            },
            value: "",
            total: 0,
            companyTypeMap: {
                1: "境内企业",
                2: "境外企业"
            },
            productManageTypeMap: {
                1: "Ⅰ类",
                2: "Ⅱ类",
                3: "Ⅲ类",
            },
            checkFlag: {
                0: "草稿",
                1: "已通过",
                2: "已拒绝",
                3: "变更未审核",
                6: "未审核",
            },
            loading: false,
            list: [],
            fromOptions: [],
            addInfoVisible: false,
            registrationVisible: false,
            enterpriseId: "",
            registrationQuery: {
                recordProductName: "",
                recordCode: "",
                recordPeopleName: "",
                page: 1,
                limit: 20
            },
            registrationList: [],
            certTotal: 0,
            registrationLoading: false,
            registrationId: "",
            fileUrl: "",
            inputQuery: {},
            editType: 1,
        };
    },
    filters: {
        statusFilterType(status) {
            const statusMap = {
                0: "warning",
                1: "success",
                2: "danger",
                6: "success",
            };
            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: "20",
                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;
            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;
                });
        },

        findMethod(query) {
            this.fromOptions = [];
            let cQuery = {
                companyName: query,
            };
            getSupComapnys(cQuery)
                .then((response) => {
                    this.loading = false;
                    this.fromOptions = response.data.list || [];
                })
                .catch(() => {
                    this.loading = false;
                });
        }
        ,

        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 = {};
            }

            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;
        },
        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(() => {
                });
        },

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