<template>
    <div class="app-container">
        <el-form
            v-show="showSearch"
            ref="queryForm"
            :inline="true"
            :model="queryParams"
            label-width="68px"
        >
            <el-form-item label="名称" prop="datasourceName">
                <el-input
                    v-model="queryParams.datasourceName"
                    clearable
                    placeholder="请输入名称"
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button
                    icon="el-icon-search"
                    size="mini"
                    type="primary"
                    @click="handleQuery"
                >搜索</el-button
                >
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
                >重置</el-button
                >
                <el-button
                    icon="el-icon-plus"
                    size="mini"
                    type="primary"
                    @click="handleAdd"
                >新增</el-button
                >
                <el-button
                    :disabled="single"
                    icon="el-icon-edit"
                    size="mini"
                    type="success"
                    @click="handleUpdate"
                >修改</el-button
                >
                <el-button
                    :disabled="multiple"
                    icon="el-icon-delete"
                    size="mini"
                    type="danger"
                    @click="handleDelete"
                >删除</el-button
                >
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8" style="display: none">
            <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
            ></right-toolbar>
        </el-row>

        <el-table
            v-loading="loading"
            :data="datasourceList"
            border
            stripe
            @selection-change="handleSelectionChange"
        >
            <el-table-column align="center" type="selection" width="55" />
            <el-table-column align="center" label="名称" prop="datasourceName" />
            <el-table-column
                align="center"
                label="驱动类"
                min-width="180px"
                prop="driverClass"
            >
                <template slot-scope="scope">
                    <dict-tag
                        :options="dict.type.DriverType"
                        :value="scope.row.driverClass"
                    />
                </template>
            </el-table-column>
            <el-table-column
                align="center"
                label="连接地址"
                min-width="230px"
                prop="url"
                show-overflow-tooltip="true"
            />
            <el-table-column v-if="false" align="center" label="适配器标识" prop="adapterFlag">
                <template slot-scope="scope">
                    <dict-tag
                        :options="dict.type.sys_datasource_adapter_flag"
                        :value="scope.row.adapterFlag"
                    />
                </template>
            </el-table-column>
            <!--<el-table-column label="用户名" align="center" prop="username" />-->
            <!--<el-table-column label="密码" align="center" prop="password" />-->
            <el-table-column align="center" v-if="false" label="机构代码" prop="orgCode" />
            <el-table-column
            v-if="false"
                align="center"
                label="数据源组"
                min-width="100px"
                prop="datasourceGroup"
            >
                <template slot-scope="scope">
                    <dict-tag
                        :options="dict.type.datasource_group"
                        :value="scope.row.datasourceGroup"
                    />
                </template>
            </el-table-column>
            <el-table-column align="center" label="备注" prop="remark" />
            <el-table-column
                align="center"
                class-name="small-padding fixed-width"
                label="操作"
            >
                <template slot-scope="scope">
                    <el-button
                        icon="el-icon-edit"
                        size="mini"
                        type="text"
                        @click="handleUpdate(scope.row)"
                    >修改</el-button
                    >
                    <el-button
                        icon="el-icon-delete"
                        size="mini"
                        type="text"
                        @click="handleDelete(scope.row)"
                    >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>

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

        <!-- 添加或修改数据源配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="名称" prop="datasourceName">
                    <el-input v-model="form.datasourceName" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="驱动类" prop="driverClass">
                    <el-select v-model="form.driverClass" placeholder="请选择驱动类">
                        <el-option
                            v-for="dict in dict.type.DriverType"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="连接地址" prop="url">
                    <el-input
                        v-model="form.url"
                        autosize
                        placeholder="请输入内容"
                        type="textarea"
                    />
                </el-form-item>
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" placeholder="请输入密码" show-password />
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleTest">连接测试</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addDatasource,
    delDatasource,
    getDatasource,
    listDatasource,
    testDatasourceConect,
    updateDatasource
} from '@/api/emport/datasource'

export default {
    name: 'DatsSource',
    dicts: ["DriverType", "datasource_group","sys_datasource_adapter_flag"],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 数据源配置表格数据
            datasourceList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                datasourceName: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                datasourceName: [
                    { required: true, message: "名称不能为空", trigger: "blur" },
                ],
                driverClass: [
                    { required: true, message: "驱动类不能为空", trigger: "change" },
                ],
                url: [{ required: true, message: "连接地址不能为空", trigger: "blur" }],
                username: [
                    { required: true, message: "用户名不能为空", trigger: "blur" },
                ],
                password: [
                    { required: true, message: "密码不能为空", trigger: "blur" },
                ]
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询数据源配置列表 */
        getList() {
            this.loading = true;
            listDatasource(this.queryParams).then((response) => {
                this.datasourceList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                orgCode: null,
                datasourceGroup: null,
                datasourceId: null,
                datasourceName: null,
                driverClass: null,
                url: null,
                username: null,
                password: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: 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.datasourceId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加数据源配置";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const datasourceId = row.datasourceId || this.ids;
            getDatasource(datasourceId).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改数据源配置";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.datasourceId != null) {
                        updateDatasource(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addDatasource(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const datasourceIds = row.datasourceId || this.ids;
            this.$modal
                .confirm(
                    '是否确认删除数据源配置编号为"' + datasourceIds + '"的数据项？'
                )
                .then(function () {
                    return delDatasource(datasourceIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 测试连通按钮操作 */
        handleTest() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    testDatasourceConect(this.form).then(res=>{
                            this.$message.success("连接成功")
                        }
                    )
                }
            });

        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "system/datasource/export",
                {
                    ...this.queryParams,
                },
                `datasource_${new Date().getTime()}.xlsx`
            );
        },
    },
}
</script>

<style lang="scss" scoped>
.app-container{
    box-shadow: none;
}
</style>
