<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="dataSourceName">
        <el-input
          v-model="queryParams.dataSourceName"
          placeholder="请输入链接名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据库来源系统名称" prop="dataSourceSystemName">
        <el-input
          v-model="queryParams.dataSourceSystemName"
          placeholder="请输入数据库来源系统名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="ip地址" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入ip地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="端口号" prop="port">
        <el-input
          v-model="queryParams.port"
          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:source: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:source: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:source: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:source:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="链接名" align="center" prop="dataSourceName" />
      <el-table-column label="数据库来源系统名称" align="center" prop="dataSourceSystemName" />
      <el-table-column label="数据库类型" align="center" prop="databaseType" />
      <el-table-column label="ip地址" align="center" prop="ip" />
      <el-table-column label="端口号" align="center" prop="port" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="类型 '查询'/'存储'" align="center" prop="type" />
      <el-table-column label="拼接的连接参数" align="center" prop="createBy" />
      <el-table-column label="拼接的连接配置" align="center" prop="updateBy" />
      <el-table-column label="状态 'Y'/'N'" align="center" prop="status" />
      <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:source:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:source: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="80px">
        <el-card>
          <div clot="header" class="clearfix">
            <span>基础信息</span>
          </div>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="链接名" prop="dataSourceName">
                <el-input v-model="form.dataSourceName" placeholder="请输入链接名" />
              </el-form-item>
              <el-form-item label="数据库来源系统名称" prop="dataSourceSystemName">
                <el-input v-model="form.dataSourceSystemName" placeholder="请输入数据库来源系统名称" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="数据库类型" prop="dataSourceSystemName">
                <el-select v-model="form.databaseType" clearable placeholder="请选择">
                  <el-option key="1" label="mysql" value="1"></el-option>
                  <el-option key="2" label="redis" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="ip地址" prop="ip">
                <el-input v-model="form.ip" placeholder="请输入ip地址" />
              </el-form-item>
              <el-form-item label="端口号" prop="port">
                <el-input v-model="form.port" placeholder="请输入端口号" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <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="请输入密码" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :md="12" :sm="24">
              <el-form-item label="类型" prop="connectionParameter">
                <el-select v-model="form.type" clearable placeholder="请选择">
                  <el-option key="查询" label="查询" value="查询"></el-option>
                  <el-option key="存储" label="存储" value="存储"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="状态" prop="connectionConfig">
                <el-radio v-model="form.status" label="Y">Y</el-radio>
                <el-radio v-model="form.status" label="N">N</el-radio>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <el-card>
          <div slot="header" class="clearfix">
          <span>数据连接参数</span>
          </div>
          <el-col :sm="24">
            <el-form-item label="数据连接参数" prop="connectionParam">
              <el-input v-model="form.createBy" 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-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 { listSource, getSource, delSource, addSource, updateSource } from "@/api/system/source";

export default {
  name: "Source",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 获取信息表格数据
      sourceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dataSourceName: null,
        dataSourceSystemName: null,
        databaseType: null,
        ip: null,
        port: null,
        username: null,
        password: null,
        type: null,
        connectionParameter: null,
        connectionConfig: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dataSourceName: [
          { required: true, message: "链接名不能为空", trigger: "blur" }
        ],
        dataSourceSystemName: [
          { required: true, message: "数据库来源系统名称不能为空", trigger: "blur" }
        ],
        databaseType: [
          { required: true, message: "数据库类型不能为空", trigger: "change" }
        ],
        ip: [
          { required: true, message: "ip地址不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "端口号不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型 '查询'/'存储'不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态 'Y'/'N'不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "连接数据参数不能为空", trigger: "change" }
        ]
      },
      //条件参数内容
      dataSourceParamList: [
        {
          name: null,
          val: null
        }
      ]
    };
  },
  created() {
    this.getList();
  },
  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.createBy = this.dataSourceParamList
        .filter(item => item.name != null || item.val != null)
        .map(item => {
          return item.name + "=" + item.val;
        }).join("&")
      console.log(this.form.connectionParam)
    },
    /** 查询获取信息列表 */
    getList() {
      this.loading = true;
      listSource(this.queryParams).then(response => {
        this.sourceList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dataSourceName: null,
        dataSourceSystemName: null,
        databaseType: null,
        ip: null,
        port: null,
        username: null,
        password: null,
        type: null,
        connectionParameter: null,
        connectionConfig: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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();
      const id = row.id || this.ids
      getSource(id).then(response => {
        this.form = response.data;
        this.dataSourceParamList = [];
        this.form.createBy.split("&").forEach(param => {
          let paramArr = param.split("=");
          this.dataSourceParamList.push({
            name: paramArr[0],
            val: paramArr[1]
          })
        })
        this.open = true;
        this.title = "修改获取信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSource(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSource(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除获取信息编号为"' + ids + '"的数据项？').then(function() {
        return delSource(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/source/export', {
        ...this.queryParams
      }, `source_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
