<template>
    <!-- 会签配置管理 列表页面 -->
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item prop="keyWord">
                <el-input
                    clearable
                    maxlength="20"
                    placeholder="请输入关键词"
                    v-model="queryParams.keyWord"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="configList" ref="multipleTable">
            <el-table-column
                width="80"
                label="序号"
                type="index"
                align="center"
                :index="indexContinuation(queryParams.pageNum, queryParams.pageSize)"
            />

            <!-- 点击跳转到二级页面配置审核流程 -->
            <el-table-column label="流程名称" align="center">
                <template slot-scope="{ row }">
                    <router-link
                        class="link-type"
                        :to="`/countersign/config-data/index/${encodeURIComponent(row.typeName)}/${row.id}`"
                    >
                        <span>{{ row.typeName }}</span>
                    </router-link>
                </template>
            </el-table-column>
            <el-table-column label="会签类型" align="center" prop="taskName" show-overflow-tooltip />
            <el-table-column label="操作" align="center" width="180">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="primary"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['signConfig:info:edit']"
                    >
                        编辑
                    </el-button>
                    <el-button
                        size="mini"
                        type="danger"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['signConfig:info:remove']"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            :total="total"
            v-show="total > 0"
            @pagination="getList"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
        />

        <!-- 新增或修改弹窗 -->
        <el-dialog :title="title" :visible.sync="showConfig" width="40%" append-to-body @close="onDialogClose">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row :gutter="24">
                    <el-col :span="20">
                        <el-form-item label="流程名称" prop="typeName">
                            <el-input v-model="form.typeName" placeholder="请输入流程名称" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="24">
                    <el-col :span="20">
                        <el-form-item label="会签类型" prop="taskType">
                            <el-select
                                filterable
                                clearable
                                style="width: 100%"
                                v-model="form.taskType"
                                placeholder="请选择会签类型"
                            >
                                <el-option
                                    :key="dict.value"
                                    :label="dict.label"
                                    :value="dict.value"
                                    v-for="dict in dict.type.tm_task_type"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getConfigListApi, addConfigApi, editConfigApi, deleteConfigApi } from '@/api/countersign/countersign'
export default {
    name: 'Config',
    dicts: ['countersign_process_name', 'countersign_type_name', 'tm_task_type'],
    data() {
        return {
            // 遮罩层
            loading: false,
            // 显示搜索条件
            showSearch: true,
            showConfig: false,
            // 总条数
            total: 0,
            // 会签配置表格数据
            configList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                keyWord: undefined
            },
            // 表单参数
            form: {
                typeName: '',
                taskType: ''
            },
            // 表单校验
            rules: {
                typeName: [
                    {
                        required: true,
                        message: '请输入流程名称',
                        trigger: 'blur'
                    },
                    { max: 30, message: '长度不能超过30个字符', trigger: 'blur' }
                ],
                taskType: [
                    {
                        required: true,
                        message: '请选择会签类型',
                        trigger: 'blur'
                    }
                ]
            }
        }
    },
    created() {
        this.getList()
    },

    methods: {
        // 取消按钮
        cancel() {
            this.showConfig = false
        },

        // 搜索
        handleQuery() {
            this.getList()
        },

        // 弹框关闭时触发
        onDialogClose() {
            this.$refs.form.resetFields()
        },

        // 表单重置
        reset() {
            this.form = {
                typeName: '',
                taskType: ''
            }
            this.resetForm('form')
        },

        // 新增
        handleAdd() {
            this.reset()
            this.showConfig = true
            this.title = '新增'
        },

        // 重置
        resetQuery() {
            this.resetForm('queryForm')
            this.queryParams.keyWord = null
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.handleQuery()
        },

        // 编辑
        handleUpdate(row) {
            const { typeName, taskType, id } = row
            this.form.typeName = typeName
            this.form.taskType = taskType + ''
            this.form.id = id
            this.title = '编辑'
            this.showConfig = true
        },

        // 查询列表
        async getList() {
            this.loading = true
            const res = await getConfigListApi(this.queryParams)
            this.configList = res.rows
            this.total = res.total
            this.loading = false
        },

        // 删除
        handleDelete(row) {
            const id = row.id
            this.$modal
                .confirm('是否确认删除数据项？')
                .then(function () {
                    return deleteConfigApi({ id })
                })
                .then(() => {
                    this.$modal.msgSuccess('删除成功')
                    this.getList()
                })
                .catch(() => {})
        },

        // 提交
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        editConfigApi(this.form).then(res => {
                            this.$modal.msgSuccess('修改成功')
                            this.showConfig = false
                            this.getList()
                        })
                    } else {
                        addConfigApi(this.form).then(res => {
                            this.$modal.msgSuccess('新增成功')
                            this.showConfig = false
                            this.getList()
                        })
                    }
                }
            })
        }
    }
}
</script>
