<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="接入源名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入接入源名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据来源系统名称" prop="fromSystem">
        <el-input
          v-model="queryParams.fromSystem"
          placeholder="请输入数据来源系统名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据库名称" prop="dataSourceDatabaseName">
        <el-input
          v-model="queryParams.dataSourceDatabaseName"
          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="['system:access: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="['system:access: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="['system:access: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="['system:access:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="accessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="接入源名称" align="center" prop="name" />
      <el-table-column label="数据来源系统名称" align="center" prop="fromSystem" />
      <el-table-column label="主机地址" align="center" prop="dataSourceIp" />
      <el-table-column label="端口号" align="center" prop="dataSourcePort" />
      <el-table-column label="数据接入类型" align="center" prop="dataType" />
      <el-table-column label="数据库名称" align="center" prop="dataSourceDatabaseName" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:access:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:access:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="testConnect(scope.row)"
            v-hasPermi="['system:access:remove']"
          >测试连接</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="dataSynchronization(scope.row)"
            v-hasPermi="['system:access: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="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-card>
          <div slot="header" class="clearfix">
            <span>基础配置信息</span>
          </div>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="接入源名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入接入源名称" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="数据来源系统名称" prop="systemName">
                <el-input v-model="form.fromSystem" placeholder="请输入数据来源系统名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="主机地址" prop="host">
                <el-input v-model="form.dataSourceIp" placeholder="请输入主机地址" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="端口号" prop="port">
                <el-input v-model="form.dataSourcePort" placeholder="请输入主机地址" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="用户名" prop="dataSourceUsername">
                <el-input v-model="form.dataSourceUsername" placeholder="请输入用户名" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="密码" prop="dataSourcePassword">
                <el-input v-model="form.dataSourcePassword" placeholder="请输入密码" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="数据接入类型" prop="type">
                <el-select style="width: 100%" v-model="form.typeId">
                    <el-option v-for="type in typeList" :label="type.dataType" :value="type.id" ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="数据库名称" prop="databaseName">
                <el-input v-model="form.dataSourceDatabaseName" placeholder="请输入数据库名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-card>
              <div slot="header" class="clearfix">
                <span>数据连接参数</span>
              </div>
              <el-col :sm="24">
                <el-form-item label="数据连接参数" prop="additionalConfiguration">
                  <el-input v-model="form.additionalConfiguration" 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="(dataSourceParam, dataSourceParamIndex) in dataSourceParamList">
                  <el-col :span="10">
                    <el-input v-model="dataSourceParam.name" 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="dataSourceParam.val" placeholder="请输入参数值" @keyup.native="processParam"/>
                  </el-col>
                  <el-col :span="3">
                    <el-button type="danger" @click="deleteParam(dataSourceParamIndex)">删除</el-button>
                  </el-col>
                </el-col>
              </el-row>
            </el-card>
          </el-row>
        </el-card>
        <el-card style="margin-top: 20px">
          <div slot="header" class="clearfix">
            <span>连接池配置</span>
          </div>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="初始连接数量" prop="initialNumberOfConnections">
                <el-input v-model="form.initialNumberOfConnections" placeholder="请输入初始连接数量" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="最大连接数量" prop="maximumNumberOfConnections">
                <el-input v-model="form.maximumNumberOfConnections" placeholder="请输入最大连接数量" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="最大等待时间" prop="maximumWaitingTime">
                <el-input v-model="form.maximumWaitingTime" placeholder="请输入最大等待时间" />
              </el-form-item>
            </el-col>
            <el-col :md="12" :sm="24">
              <el-form-item label="最大等待次数" prop="maximumWaitingTimes">
                <el-input v-model="form.maximumWaitingTimes" placeholder="请输入最大等待次数" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>

        <el-card style="margin-top: 20px">
          <div slot="header" class="clearfix">
            <span>其他信息</span>
          </div>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" 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 {
  dataSynchronization,
  getAllDataType,
  getDataSourceList,
  insertDataSource,
  queryStructure,
  testConnect
} from "@/api/dataSource/data";

export default {
  name: "Access",
  data() {
    return {
      selectTableName: "",
      tableList:[],
      dialogTableVisible: false,
      dataSourceQueryReq: {
        typeId: null,
        name: "",
        dataSourceDatabaseName: ""
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据接入表格数据
      accessList: [],
      //数据库类型
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: null,
        fromSystem: null,
        dataSourceDatabaseName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "接入源名称不能为空", trigger: "blur"}
        ],
        fromSystem: [
          {required: true, message: "数据来源系统名称不能为空", trigger: "blur"}
        ],
        typeId: [
          {required: true, message: "数据接入类型不能为空", trigger: "blur"}
        ],
        dataSourceIp: [
          {required: true, message: "主机地址不能为空", trigger: "blur"}
        ],
        dataSourcePort: [
          {required: true, message: "主机地址不能为空", trigger: "blur"}
        ],
        dataSourceDatabaseName: [
          {required: true, message: "数据库名称不能为空", trigger: "blur"}
        ],
        additionalConfiguration: [
          {required: true, message: "数据连接参数不能为空", trigger: "blur"}
        ],
        initialNumberOfConnections: [
          {required: true, message: "初始连接数量不能为空", trigger: "blur"}
        ],
        maximumNumberOfConnections: [
          {required: true, message: "最大连接数量不能为空", trigger: "blur"}
        ],
        maximumWaitingTime: [
          {required: true, message: "最大等待时间不能为空", trigger: "blur"}
        ],
        maximumWaitingTimes: [
          {required: true, message: "最大等待次数不能为空", trigger: "blur"}
        ]
      },
      // 条件参数内容
      dataSourceParamList: [
        {
          name: null,
          val: null
        }
      ]
    };
  },
  created() {
    this.getList();
    this.getDataTypeList()
  },
  methods: {
    // 添加参数
    addParam(){
      this.dataSourceParamList.push({
        name: null,
        val: null
      })
    },
    // 删除参数
    deleteParam(index){
      if (index != null){
        this.dataSourceParamList.splice(index, 1);
      }else {
        this.dataSourceParamList = []
      }
      this.processParam();
    },
    // 处理参数
    processParam(){
      this.form.additionalConfiguration = this.dataSourceParamList
        .filter(item => item.name != null || item.val != null)
        .map(item => {
          return item.name + "=" + item.val;
        }).join("&")
      console.log(this.form.additionalConfiguration)
    },
    /** 查询数据接入列表 */
    getList() {
      this.loading = true;
      getDataSourceList(this.queryParams).then(res => {
        this.accessList = res.data;
        console.log(res.data)
      })
      this.loading = false;
    },
    getDataTypeList() {
      getAllDataType().then(
        res => {
          this.typeList = res.data
          console.log("typeList:",this.typeList)
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        typeId: null,
        fromSystem: null,
        dataSourceIp: null,
        dataSourcePort: null,
        dataSourceDatabaseName: null,
        additionalConfiguration: null,
        initialNumberOfConnections: null,
        maximumNumberOfConnections: null,
        maximumWaitingTime: null,
        maximumWaitingTimes: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      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.dataSourceParamList = []
      this.reset();
      this.open = true;
      this.title = "添加数据接入";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.dataSourceParamList = [];
      if (this.form.additionalConfiguration!=undefined){
        this.form.additionalConfiguration.split("&").forEach(param => {
          let paramArr = param.split("=");
          this.dataSourceParamList.push({
            name: paramArr[0],
            val: paramArr[1]
          })
        })
      }
      this.open = true;
      this.title = "修改数据接入";
    },
    /**
     * 查询表结构
     */
    queryTableStructure(){

    },
    /** 提交按钮 */
    submitForm() {
      console.log('from:',this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
          } else {
            insertDataSource(this.form).then(
              res => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
              }
            )

          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据接入编号为"' + ids + '"的数据项？').then(() => {
        this.$modal.msgSuccess("删除成功");
      });
    },
    queryDataBase(row) {
      queryStructure(row).then(
        res => {
          console.log(res.data)
          this.tableList = res.data
          this.dialogTableVisible = true
        }
      )
    },
    dataSynchronization(row) {
      dataSynchronization(row).then(
        res => {
          if (res.code ===200){
            this.$message.success(res.msg)
          }else {
            this.$message.error(res.msg)
          }
        }
      )
    },
    testConnect(row) {
      testConnect(row).then(
        res => {
          if (res.code ===200){
            this.$message.success(res.msg)
          }else {
            this.$message.error(res.msg)
          }
        }
      )
      console.log("test:",row)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.msgSuccess("导出成功");
    }
  }
};
</script>
