<template>
    <div class="app-container">
        <!--    三种查询-->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="接入源名称" prop="paramsName">
                <el-input
                        v-model="queryParams.paramsName"
                        placeholder="请输入接入源名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="数据来源系统名称" prop="dataSourcesSystemName">
                <el-input
                        v-model="queryParams.dataSourcesSystemName"
                        placeholder="请输入数据来源系统名称"
                        clearable
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="数据库名称" prop="databaseName">
                <el-input
                        v-model="queryParams.databaseName"
                        placeholder="请输入数据库名称"
                        clearable
                        @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"
                        v-hasPermi="['etl:info:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['etl:info:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['etl:info:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['etl:info:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!--列表-->
        <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="接入源名称" align="center" prop="dataResourceName" />
            <el-table-column label="数据来源系统名称" align="center" prop="dataSourcesSystemName" />
            <el-table-column label="主机ip" align="center" prop="host" />
            <el-table-column label="主机端口" align="center" prop="port" />
            <el-table-column label="数据接入类型" align="center" prop="databaseType" />
            <el-table-column label="数据库名称" align="center" prop="databaseName" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">

                    <el-button size="mini" type="text" icon="el-icon-s-help" @click="connectionTest(scope.row)" v-hasPermi="['etl:info:edit']"
                    >测试链接</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['etl:info:edit']"
                    >修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['etl:info:remove']"
                    >删除</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"
        />
        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="150px">
                <el-card>
                    <div slot="header" class="clearfix">
                        <span>基础配置信息</span>
                    </div>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="接入源名称" prop="dataResourceName">
                                <el-input v-model="form.dataResourceName" placeholder="请输入接入源名称" />
                            </el-form-item>
                            <el-form-item label="主机ip地址" prop="host">
                                <el-input v-model="form.host" placeholder="请输入主机ip地址" />
                            </el-form-item>
                            <el-form-item label="接入数据库类型" prop="databaseType">
                                <el-select v-model="form.databaseType" placeholder="请选择">
                                    <el-option
                                            v-for="type in databaseTypeList"
                                            :key="type.value"
                                            :label="type.label"
                                            :value="type.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="用户名" prop="host">
                                <el-input v-model="form.username" placeholder="请输入用户名" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="数据来源系统名称" prop="dataSourcesSystemName">
                                <el-input v-model="form.dataSourcesSystemName" placeholder="请输入数据来源系统名称" />
                            </el-form-item>
                            <el-form-item label="端口" prop="port">
                                <el-input v-model="form.port" placeholder="请输入端口" />
                            </el-form-item>
                            <el-form-item label="数据库名称" prop="databaseName">
                                <el-input v-model="form.databaseName" placeholder="请输入数据库名称" />
                            </el-form-item>
                            <el-form-item label="密码" prop="databaseName">
                                <el-input v-model="form.password" placeholder="请输入密码" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-card>
                        <div slot="header" class="clearfix">
                            <span>数据连接参数</span>
                        </div>
                        <el-col :sm="24">
                            <el-form-item label="数据连接参数" prop="connectionParams">
                                <el-input v-model="form.connectionParams" placeholder="请输入内容" />
                            </el-form-item>
                        </el-col>
                        <el-row :gutter="10" style="margin: 10px 0">
                            <el-col :span="2">
                                <el-button type="primary" @click="addParam()">新增参数</el-button>
                            </el-col>
                            <el-col :span="2">
                                <el-button type="danger" @click="deleteParam(null)">清空参数</el-button>
                            </el-col>
                        </el-row>
                        <el-row :gutter="10">
                            <el-col :md="8" :xs="12" style="margin: 10px 0" v-for="(basicParam, basicParamIndex) in basicParamList">
                                <el-col :span="10">
                                    <el-input v-model="basicParam.paramsName" placeholder="请输入参数键" @keyup.native="processParam"/>
                                </el-col>
                                <el-col :span="1"><span style="font-size: 28px; margin: 0 3px 0 0;">:</span></el-col>
                                <el-col :span="10">
                                    <el-input v-model="basicParam.paramsValue" placeholder="请输入参数值" @keyup.native="processParam"/>
                                </el-col>
                                <el-col :span="3">
                                    <el-button type="danger" @click="deleteParam(basicParamIndex)">删除</el-button>
                                </el-col>
                            </el-col>
                        </el-row>
                    </el-card>
                </el-card>

                <el-card>
                    <div slot="header" class="clearfix">
                        <span>连接池配置</span>
                    </div>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="初始化连接数量" prop="initLinkNum">
                                <el-input v-model="form.initLinkNum" placeholder="请输入初始化连接数量" />
                            </el-form-item>
                            <el-form-item label="最大等待时间" prop="maxWaitTime">
                                <el-input v-model="form.maxWaitTime" placeholder="请输入最大等待时间" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="最大连接数量" prop="maxLinkNum">
                                <el-input v-model="form.maxLinkNum" placeholder="请输入最大连接数量" />
                            </el-form-item>
                            <el-form-item label="最大等待次数" prop="maxWaitTimes">
                                <el-input v-model="form.maxWaitTimes" placeholder="请输入最大等待次数" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-card>


                <el-card >
                    <div slot="header" class="clearfix">
                        <span>卡片名称</span>
                    </div>
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" placeholder="请输入备注" />
                    </el-form-item>
                </el-card>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {addInfo, connectionTest, delInfo, getInfo, listInfo, updateInfo} from "@/api/etl/etl";

export default {
    name: "info",
    data() {
        return {


            basicParamList:[
                {
                    paramsName: null,
                    paramsValue: null
                }
            ],
            databaseTypeList:[{
                value:'MySql',
                label:'MySql'
            },{
                value:'PostSql',
                label:'PostSql'
            },{
                value:'Oracle',
                label:'Oracle'
            }],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            infoList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                paramsName: null,
                dataSourcesSystemName: null,
                databaseTypeList: null,
                databaseName: null,
                initLinkNum :null,
                maxWaitTime :null,
                maxLinkNum :null,
                maxWaitTimes :null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                paramsName: [
                    {required: true, message: "接入源名称不能为空", trigger: "blur"}
                ],
                host: [ {required: true, message: "主机ip地址不能为空", trigger: "blur"}
                ],
                databaseType: [ {required: true, message: "接入数据库类型不能为空", trigger: "blur"}
                ],
                dataSourcesSystemName: [
                    {required: true, message: "数据来源系统名称不能为空", trigger: "blur"}
                ],
                port: [
                    {required: true, message: "端口不能为空", trigger: "blur"}
                ],
                databaseName: [
                    {required: true, message: "数据库名称不能为空", trigger: "blur"}
                ],
                initLinkNum: [
                    {required: true, message: "初始化连接池数量不能为空", trigger: "blur"}
                ],
                maxWaitTime: [
                    {required: true, message: "最大等待连接时长不能为空", trigger: "blur"}
                ],
                maxLinkNum: [
                    {required: true, message: "最大连接池数量为空", trigger: "blur"}
                ],
                maxWaitTimes: [
                    {required: true, message: "最大连接次数不能为空", trigger: "blur"}
                ],
                connectionParams: [
                    {required: true, message: "数据连接参数不能为空", trigger: "blur"}
                ],

            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        // 添加参数
        addParam(){
            this.basicParamList.push({
                paramsName: null,
                paramsValue: null
            })
        },
        // 删除参数
        deleteParam(index){
            if (index != null){
                this.basicParamList.splice(index, 1);
            }else {
                this.basicParamList = []
            }
            this.processParam();
        },
        // 处理参数
        processParam(){
            this.form.connectionParams = this.basicParamList
                .filter(item => item.paramsName != null || item.paramsValue != null)
                .map(item => {
                    return item.paramsName + "=" + item.paramsValue;
                }).join("&")
            console.log(this.form.connectionParams)
        },
        /** 查询列表 */
        getList() {
            this.loading = true;
            // let response = {
            //   "code": 200,
            //   "msg": "查询成功",
            //   "data": {
            //     "total": 1,
            //     "rows": [
            //       {
            //         id: null,
            //         paramsName: "测试",
            //         databaseType: "MySql",
            //         dataSourcesSystemName: "云计算业务系统",
            //         host: "125.36.25.36",
            //         port: 6536,
            //         databaseName: "yunjisuan-server",
            //         connectionParams: "useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8",
            //         initLinkNum: 5,
            //         maxLinkNum: 20,
            //         maxWaitTime: 3000,
            //         maxWaitTimes: 3,
            //         remark: "云计算业务系统，开展云计算详细业务，教学，作业等工作"
            //       }
            //     ]
            //   }
            // };
            listInfo(this.queryParams).then(response =>{
                console.log(response)
                this.infoList = response.data.rows;
                this.total = response.data.total;
                this.loading = false;
            })

        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                paramsName: null,
                dataSourcesSystemName: null,
                host: null,
                port: null,
                username: null,
                password: null,
                databaseTypeList: null,
                databaseName: null,
                connectionParams: null,
                initLinkNum: 5,
                maxLinkNum: 20,
                maxWaitTime: 3000,
                maxWaitTimes: 3,
                remark: null
            };
            this.basicParamList= [
                {
                    paramsName: null,
                    paramsValue: null
                }
            ],
                this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加数据接入";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.form = row;
            this.basicParamList = [];
            this.form.connectionParams.split("&").forEach(param => {
                let paramArr = param.split("=");
                this.basicParamList.push({
                    paramsName: paramArr[0],
                    paramsValue: paramArr[1]
                })
                console.log(this.basicParamList)
            })
            this.open = true;
            this.title = "修改数据接入";
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateInfo(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addInfo(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },

        /** 测试连接 */
        connectionTest(basicConfigInfo){
            connectionTest(basicConfigInfo).then( response => {
                if (response.code === 200){
                    this.$message.success("连接成功")
                }else {
                    this.$message.error("连接失败")
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
                return delInfo(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('etl/info/export', {
                ...this.queryParams
            }, `info_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
