<template>
    <div>
        <el-form
            :model="inputQuery"
            :rules="rules"
            ref="inputQuery"
            label-width="100px"
        >
            <el-card class="el-card" style="margin-top: -25px">

                <div style="display: flex;" >
                    <el-button
                        v-if="editType==2"
                        type="primary"
                        size="mini"
                        icon="search"
                        @click="onPassSubmit"
                    >审核通过
                    </el-button
                    >
                    <el-button
                        v-if="editType==2"
                        type="primary"
                        size="mini"
                        icon="search"
                        @click="onRejectSubmit"
                    >审核拒绝
                    </el-button
                    >
                    <el-button
                        v-if="editType==1"
                        type="primary"
                        size="mini"
                        icon="search"
                        @click="printSupCertAll"
                    >打印
                    </el-button>
                </div>
                <el-row :gutter="20" class="el-row" type="flex" style="margin-top: 20px">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="企业名称:" label-width="150px" prop="companyName">
                            <el-input
                                size="small"
                                splaceholder="请输入内容"
                                disabled
                                v-model="inputQuery.companyName"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="统一社会信用代码:" label-width="150px" prop="creditNum">
                            <el-input
                                size="small"
                                splaceholder="请输入内容" disabled
                                v-model="inputQuery.creditNum"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex" style="margin-top: 0px">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="企业类别:" label-width="150px" prop="classes">
                            <el-select
                                size="small"
                                v-model="inputQuery.classes" disabled
                                placeholder="企业类别"
                                style="width: 100%;"
                            >
                                <el-option v-if="classesDisplay" label="医院" value="1"></el-option>
                                <el-option label="经营企业" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="所属地区:" label-width="150px" prop="area">
                            <el-cascader
                                size="small"
                                :options="options"
                                v-model="selectedOptions" disabled
                                placeholder="请选择所属地区"
                                style="width: 100%"
                                @change="handleChange"
                            >
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex" style="margin-top: 0px">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="联系人:" label-width="150px" prop="contacts">
                            <el-input
                                size="small"
                                placeholder="请输入内容" disabled
                                v-model="inputQuery.contacts"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="详细地址:" label-width="150px" prop="detailAddr">
                            <el-input
                                size="small"
                                placeholder="请输入内容" disabled
                                v-model="inputQuery.detailAddr"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex" style="margin-top: 0px">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="联系电话:" label-width="150px" prop="mobile">
                            <el-input
                                size="small"
                                placeholder="请输入内容" disabled
                                v-model="inputQuery.mobile"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="邮箱:" label-width="150px" prop="email">
                            <el-input
                                size="small"
                                placeholder="请输入内容" disabled
                                v-model="inputQuery.email"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-card>
            <el-card>

                <div style=" float: right;
                        text-align: right;
                        margin-bottom: 8px;"
                >
                    <!--                    <el-button type="primary" size="mini" icon="search" @click="addCert"-->
                    <!--                               style="text-align:right"-->
                    <!--                    >添加电子档-->
                    <!--                    </el-button-->
                    <!--                    >-->

                </div>

                <el-table v-loading="certLoading"
                          :data="certList"
                          ref="certTable"
                          style="width: 100%">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column type="index" label="序号" width="50"></el-table-column>
                    <el-table-column label="证书名称" prop="name"></el-table-column>
                    <el-table-column label="证书编号" prop="code"></el-table-column>
                    <el-table-column label="有效期" prop="vailDate"></el-table-column>
                    <el-table-column label="失效期" prop="expireDate"></el-table-column>
                    <el-table-column label="备注" prop="remark"></el-table-column>
                    <el-table-column label="证书状态" prop="status">
                    <template slot-scope="scope">
                                {{ supStatus[scope.row.status] }}
                        </template>
                    </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">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="small"
                                @click.native="toViewCompanyCert(scope.row)"
                            >预览
                            </el-button
                            >
                                <el-button
                                    v-if="editType==1"
                                    type="text"
                                    size="small"
                                    @click.native="printSupCert(scope.row)"
                                >打印
                                </el-button
                                >
                            <el-button
                                type="text"
                                size="small"
                                v-if="editType==2"
                                @click.native="auditCert(scope.row)"
                            >审核
                            </el-button
                            >
                            <el-button
                                type="text"
                                size="small"
                                @click.native="rejectInfo(scope.row)"
                            >说明
                            </el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    :page-size="certQuery.limit"
                    @current-change="certHandleCurrentChange"
                    layout="prev, pager, next"
                    :total="certTotal"
                >
                </el-pagination>
            </el-card>
        </el-form>


        <el-dialog width="30%" :visible.sync="certAuditVisible" append-to-body>
            <el-form :model="currentAudit">
                <label></label>
                <el-form-item label="审核备注：" :label-width="120">
                    <el-input type="textarea" v-model="currentAudit.auditComment" autocomplete="off"
                              autosize></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="rejectAuditCert">审核拒绝</el-button>
                <el-button type="primary" @click="passAuditCert">审核通过</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {modifyCompany, auditCompany,printSupCert, verifyTemplateFile} from "../../api/purchase/supCompany";
import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode,
} from "element-china-area-data";
import {filterCompanyCert, deleteCompanyCert, auditSupCert} from "../../api/purchase/companyCert";
import draggable from "vuedraggable";
import {BASE_URL} from "@/config/app";

export default {

    name: "supCompanyEdit",
    props: {
        closeDialog: {
            type: Function,
            required: true,
        },
        inputQuery: {
            type: Object,
            required: true,
        },
        editType: {//1：查询页面，2:审核页面，
            type: Object,
            required: true,
        }
    },


    data() {
        return {
            headers: {},
            classesDisplay: false,
            selectedOptions: [],
            options: regionDataPlus,
            rules: {
                companyName: [
                    {
                        required: true,
                        message: "请输入企业名称",
                        trigger: "blur"
                    }
                ],
                creditNum: [
                    {
                        required: true,
                        message: "请输入统一社会信用代码",
                        trigger: "blur"
                    }
                ],
                classes: [
                    {
                        required: true,
                        message: "请选择企业类别",
                        trigger: "change"
                    }
                ],
                area: [
                    {
                        required: true,
                        message: "请选择所属地区",
                        trigger: "change"
                    }
                ],
                detailAddr: [
                    {
                        required: true,
                        message: "请输入详细地址",
                        trigger: "change"
                    }
                ],
                contacts: [
                    {
                        required: true,
                        message: "请输入联系人",
                        trigger: "change"
                    }
                ],
                mobile: [
                    {
                        required: true,
                        message: "请输入联系电话",
                        trigger: "change"
                    }
                ],
                email: [
                    {
                        required: true,
                        message: "请输入邮箱",
                        trigger: "change"
                    }
                ],
                updateCause: [
                    {
                        required: true,
                        message: "请输入变更原因",
                        trigger: "change"
                    }
                ],
            },
            uploadUrl: "",
            fileUrl: "",
            certFileUrl: "",
            url: 'http://127.0.0.1:9996/udiwms/image/register/file/getImage?type=image1&name=t11111.jpg',
            certQuery: {
                page: 1,
                limit: 20
            },
            certList: [],
            certTotal: 0,
            certLoading: false,
            addCertVisible: false,
            salesmanQuery: {
                name: "",
                page: 1,
                limit: 20
            },
            salesmanList: [],
            salesmanTotal: 0,
            salesmanLoading: false,
            addSalesmanVisible: false,
            certTypeMap: {
                1: '身份证'
            },
            selectLocalVisible: false,
            productQuery: {
                ylqxzcrbarmc: "",
                cpmctymc: "",
                ggxh: "",
                page: 1,
                limit: 20
            },
            productList: [],
            productTotal: 0,
            productLoading: false,
            uuid: null,
            companyApplyUpdateVisible: false,
            companyEditDisabled: true,
            companyEditButtonStr: "编辑",
            checkFlag: {
                0: "草稿",
                1: "已通过",
                2: "已拒绝",
                3: "变更未审核",
                6: "未审核",
            },
            supStatus: {
                0: "有效",
                1: "失效",
            },
            certAuditVisible: false,
            currentAudit: {},
        };
    },
    created() {
        this.getCompanyCertList();
        this.selectedOptions = this.inputQuery.areaCode.split(",");

    },
    components: {
        draggable
    },
    methods: {

        onPassSubmit(val) {
            this.inputQuery.auditStatus = 1;
            this.$confirm("是否审核通过?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.loading = true;
                auditCompany(this.inputQuery).then(response => {
                    this.loading = false;
                    if (response.code === 20000) {
                        this.$message.success("提交成功");
                        this.closeDialog();
                    } else {
                        this.$message.error(response.message);
                    }
                });
            }).catch(() => {
            });
        },

        onRejectSubmit() {
            this.$prompt('请输入驳回意见', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
            }).then(({value}) => {
                this.inputQuery.auditComment = value;
                this.inputQuery.auditStatus = 2;
                auditCompany(this.inputQuery).then(response => {
                    this.loading = false;
                    if (response.code === 20000) {
                        this.$message.success("提交成功");
                        this.closeDialog();
                    } else {
                        this.$message.error(response.message);
                    }
                });


            }).catch(() => {
            });
        }
        ,


        cancelDialog() {
            this.closeDialog();
        }
        ,

        closeLocalDialog() {
            this.addCertVisible = false;
            this.getCompanyCertList();
        }
        ,

        handleChange(value) {
            this.inputQuery.area =
                CodeToText[value[0]] + CodeToText[value[1]] + CodeToText[value[2]];
            this.inputQuery.areaCode = value.toString();
        }
        ,

        addCert(index, row) {
            this.inputQuery.id = '';
            if (this.$isNotBlank(row) && this.$isNotBlank(row.id)) {
                this.inputQuery.id = row.id;
                this.inputQuery.formData = row;
            }
            this.addCertVisible = true;
        }
        ,

        toViewCompanyCert(row) {
            this.certFileUrl = BASE_URL + "/udiwms/image/register/file/getImage?type=image2&name=";
            window.open(this.certFileUrl + row.filePath);
        },


        auditCert(row) {
            this.certAuditVisible = true;
            this.currentAudit = row;
        },

        passAuditCert() {
            this.currentAudit.auditStatus = 1;
            this.auditSupCert(this.currentAudit);
        },
        rejectAuditCert() {
            this.currentAudit.auditStatus = 2;
            this.auditSupCert(this.currentAudit);
        },


        auditSupCert(inputQuery) {
            auditSupCert(inputQuery).then(response => {
                this.loading = false;
                if (response.code === 20000) {
                    this.getCompanyCertList();
                    this.certAuditVisible = false;
                } else {
                    this.$message.error(response.message);
                }
            });
        },

        rejectInfo(row) {
            this.$confirm(row.auditComment, "审核备注", {
                confirmButtonText: "确定",
                type: "warning",
                showCancelButton: false,
            })
                .then(() => {
                });

        },

        certHandleCurrentChange(val) {
            this.certQuery.page = val;
            this.getCompanyCertList();
        }
        ,

        getCompanyCertList() {
            this.certLoading = true;
            this.certQuery.customerId = this.inputQuery.customerId;
            this.certQuery.type = 1;
            this.certQuery.auditStatus=25;

            filterCompanyCert(this.certQuery)
                .then((response) => {
                    this.certLoading = false;
                    this.certList = response.data.list || [];
                    this.certTotal=response.data.total
                })
                .catch(() => {
                    this.certLoading = false;
                    this.certList = [];
                });
        }
        ,
        printSupCert(row) {
            var selectIds=[row.id];
            let query = {moduleId: 8};
            this.loading = true;
            verifyTemplateFile(query).then((res) => {
                if (res.code === 20000) {
                    let printParams = {
                        templateId: res.data,
                        ids: selectIds,
                        customerId: row.customerId
                    };
                    printSupCert(printParams).then((response) => {
                        //将pdf文件转换为url。
                        const binaryData = [];
                        binaryData.push(response);
                        //获取blob链接。
                        let url = window.URL.createObjectURL(
                            new Blob(binaryData, {type: "application/pdf"})
                        );
                        this.loading = false;
                        window.open(url);//打开新标签页，预览pdf。
                    })
                } else {
                    this.loading = false;
                    this.$message.error(res.message);
                }
            }).catch((error) => {
                this.loading = false;
                this.$message.error(error.message);
            })
        },
        printSupCertAll() {
            var certLists=this.$refs.certTable.selection;
            var  selectIds=[];
            certLists.forEach(item => {
                selectIds.push(item.id);
            })
            let query = {moduleId: 8};
            this.loading = true;
            verifyTemplateFile(query).then((res) => {
                if (res.code === 20000) {
                    let printParams = {
                        templateId: res.data,
                        ids: selectIds,
                        customerId: this.inputQuery.customerId
                    };
                    printSupCert(printParams).then((response) => {
                        //将pdf文件转换为url。
                        const binaryData = [];
                        binaryData.push(response);
                        //获取blob链接。
                        let url = window.URL.createObjectURL(
                            new Blob(binaryData, {type: "application/pdf"})
                        );
                        this.loading = false;
                        window.open(url);//打开新标签页，预览pdf。
                    })
                } else {
                    this.loading = false;
                    this.$message.error(res.message);
                }
            }).catch((error) => {
                this.loading = false;
                this.$message.error(error.message);
            })
        },
        deleteCompanyCert(row) {
            this.$confirm("是否删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.certLoading = true;
                let tquery = {
                    id: row.id,
                    filePath: row.filePath,
                };
                deleteCompanyCert(tquery)
                    .then((response) => {
                        this.certLoading = false;
                        if (response.code === 20000) {
                            this.getCompanyCertList();
                            this.$message({
                                type: "success",
                                message: "删除成功！",
                            });
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.certLoading = false;
                    });
            }).catch(() => {
            });
        }
        ,

    },
    filters: {
        statusFilterType(status) {
            const statusMap = {
                0: "warning",
                1: "success",
                2: "danger",
                6: "warning",
            };
            return statusMap[status];
        },
    },
}
</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>
