<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addInstitution">新增部门</el-button>
                <!-- v-hasPermi="['caseManagement:list:add']" -->
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="dataList" style="width: 100%">
            <el-table-column label="序号" type="index" align="center">
                <template slot-scope="scope">
                    <span>{{
                        (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column label="调解机构名称" align="center" prop="identifyName" :show-overflow-tooltip="true" />
            <el-table-column label="经办人姓名" align="center" prop="operName" :show-overflow-tooltip="true" />
            <el-table-column label="经办人手机号" align="center" prop="operPhone" :show-overflow-tooltip="true" />
            <el-table-column label="认证时间" align="center" prop="identifyDate" />
            <el-table-column label="认证状态" align="center" prop="identifyStatus" :show-overflow-tooltip="true" />
            <el-table-column label="是否启用" align="center" prop="isUse"></el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" @click="DeptIndefiUrl(scope.row)" type="text" icon="el-icon-thumb"
                        v-if="scope.row.identifyStatus == '未认证'">认证</el-button>
                    <el-button size="mini" @click="changeStatus(scope.row)" type="text" icon="el-icon-thumb"
                        v-if="scope.row.isUse == '未启用' && scope.row.identifyStatus == '已认证'">启用</el-button>
                    <el-button size="mini" @click="uploadSeal(scope.row)" type="text"
                        v-if="scope.row.identifyStatus == '已认证'" icon="el-icon-upload2">上传公章</el-button>
                    <el-button size="mini" @click="sealManage(scope.row)" type="text"
                        v-if="scope.row.identifyStatus == '已认证'" icon="el-icon-upload2">公章管理</el-button>
                    <el-button size="mini" @click="sealDelete(scope.row)" v-if="scope.row.identifyStatus == '未认证'"
                        type="text" icon="el-icon-delete">删除</el-button>
                    <el-button size="mini" @click="eidtSeal(scope.row)" type="text" v-if="scope.row.identifyStatus == '未认证'"
                        icon="el-icon-edit">修改</el-button>
                    <el-button size="mini" @click="selectUser(scope.row)" type="text"
                        v-if="scope.row.identifyStatus == '未认证'" icon="el-icon-edit">选择经办人</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList(queryParams)" />
        <addInstitution :operateVisable="operateVisable" @cancelFilingreview="cancelFilingreview" :queryParams="queryParams"
            @getList="getList">
        </addInstitution>
        <uploadSeal :uploadVisable="uploadVisable" @cancelUpload="cancelUpload" :queryParams="queryParams"
            :uploadData="uploadData" @getList="getList">
        </uploadSeal>
        <sealManage :sealVisable="sealVisable" @cancelSeal="cancelSeal" :queryParams="queryParams" :sealData="sealData">
        </sealManage>
        <eidtInstitution :editVisable="editVisable" @cancelEdit="cancelEdit" :queryParams="queryParams" @getList="getList"
            :editData="editData"></eidtInstitution>
        <el-dialog title="选择经办人" :visible.sync="selectVisible" width="600px" @close="selectClose">
            <el-form :model="slectFrom" :rules="rules" ref="slectFrom" label-width="80px">
                <el-form-item label="经办人" prop="id">
                    <el-select v-model="slectFrom.id" clearable placeholder="请选择">
                        <el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="selectClose" class="endbutton"><span>取 消</span></el-button>
                <el-button type="primary" @click="submitUser" class="endbutton"><span>确认</span></el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import {
    deptIdentifyList,
    enableDept,
    selectDeptIndefiUrl,
    deleteSeal,
    selectPostUserByDeptId,
    bindHandler
} from "@/api/officialSeal/officialSeal.js";

import addInstitution from "./components/addInstitution";
import uploadSeal from "./components/uploadSeal";
import sealManage from "./components/sealManage";
import eidtInstitution from "./components/eidtInstitution";
export default {
    name: "paymentList",
    components: {
        addInstitution,
        uploadSeal,
        sealManage,
        eidtInstitution
    },
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 遮罩层
            loading: false,
            // 总条数
            total: 0,
            // 表格数据
            form: {},
            // 校验表单
            rules: {},
            dataList: [1],
            operateVisable: false,//新增弹窗
            uploadVisable: false,//上传弹窗
            sealVisable: false,//公章列表弹窗
            uploadData: {},
            sealData: {},
            editVisable: false,
            editData: {},
            selectVisible: false,
            slectFrom: {},
            userList: [],
            templateId: null,
            rules: {
                id: [
                    { required: true, message: '请选择经办人', trigger: 'blur' },
                ]
            }
        };
    },
    created() {
        this.getList(this.queryParams)
    },
    methods: {
        // 选择经办人
        selectUser(row) {
            this.selectVisible = true;
            this.templateId = row.id;
            selectPostUserByDeptId({ id: row.id }).then(res => {
                this.userList = res.data;
                if (row.userId) {
                    this.slectFrom.id = row.userId;
                } else {
                    this.slectFrom = {};
                }
            })
        },
        // 确认经办人
        submitUser() {
            this.$refs['slectFrom'].validate((valid) => {
                if (valid) {
                    bindHandler({ id: this.templateId, userId: this.slectFrom.id }).then(res => {
                        this.$modal.msgSuccess("选择成功");
                        this.selectVisible = false;
                        this.getList(this.queryParams);
                    })
                }
            })
        },
        selectClose() {
            this.selectVisible = false;
        },
        // 删除
        sealDelete(row) {
            this.$modal
                .confirm("是否更改状态")
                .then((res) => {
                    this.deleteSealFn({ id: row.id })
                })
        },
        // 删除接口
        deleteSealFn(data) {
            deleteSeal(data).then(res => {
                this.$modal.msgSuccess("删除成功！");
                this.getList(this.queryParams);
            })
        },
        // 公章管理
        sealManage(row) {
            this.sealVisable = true;
            this.sealData = row;
        },
        cancelSeal() {
            this.sealVisable = false;
        },
        // 上传公章
        uploadSeal(row) {
            this.uploadVisable = true;
            this.uploadData = row;
        },
        closeUploadSeal(){
            this.uploadVisable = false
        },
        cancelFilingreview() {
            this.operateVisable = false;
        },
        cancelUpload() {
            this.uploadVisable = false;
        },
        // 新增部门
        addInstitution() {
            this.operateVisable = true;
        },
        // 编辑
        eidtSeal(row) {
            this.editVisable = true;
            this.editData = row;
        },
        cancelEdit() {
            this.editVisable = false;
        },
        // 启用认证按钮
        changeStatus(row) {
            this.$modal.confirm('是否进行启用？')
                .then(function () {

                })
                .then(() => {
                    enableDept({ id: row.id }).then(res => {
                        this.$modal.msgSuccess("启用成功！");
                        this.getList(this.queryParams);
                    })

                })
                .catch(function () {
                });
        },
        // 认证按钮
        DeptIndefiUrl(row) {
            if (!row.operName || !row.legalPerPhone) {
                this.$modal.msgError("当前没有经办人 ，无法认证！！");
                return
            }
            this.$modal.confirm('是否认证？')
                .then(function () {
                    selectDeptIndefiUrl({ id: row.id }).then(res => {
                        let url = res.data.identifyUrl;
                        window.open(url)
                    })
                })
                .then(() => {
                    // this.$modal.msgSuccess("认证成功！");
                    // this.getList(this.queryParams);
                })
                .catch(function () {
                });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList(this.queryParams);
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 查询列表数据
        getList(parms) {
            this.loading = true;
            deptIdentifyList(parms).then((response) => {
                this.dataList = response.rows;
                this.total = response.total;
                this.loading = false;
                this.dataList.forEach(item => {
                    if (item.identifyStatus == 0 || item.identifyStatus == null) {
                        item.identifyStatus = '未认证'
                    } else if (item.identifyStatus == 2) {
                        item.identifyStatus = '认证中'
                    } else if (item.identifyStatus == 1) {
                        item.identifyStatus = '已认证'
                    }
                    if (item.isUse == 0 || item.isUse == null) {
                        item.isUse = '未启用'
                    } else {
                        item.isUse = '已启用'
                    }
                })
            });
        },
    },
};
</script>
  
<style lang="scss" scoped></style>