<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="数据库名称" prop="configName">
        <el-input
          v-model="queryParams.dbName"
          clearable
          placeholder=""
          style="width: 240px"
          @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-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>

      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="configList" @selection-change="handleSelectionChange">
      <el-table-column  type="selection" width="55"/>
      <el-table-column  label="主键" prop="id"/>
      <el-table-column  label="数据库名称" prop="dbName"/>
      <el-table-column  label="连接地址" prop="dbUrl"/>
      <el-table-column  label="账号" prop="dbUsername"/>
      <el-table-column  label="密码" prop="dbPassword"/>
      <el-table-column  label="版本" prop="dbDriver"/>
      <el-table-column  label="是否使用" prop="dbStates">
        <template slot-scope="scope">
          <span v-if="scope.row.dbStates==1">是</span>
          <span v-if="scope.row.dbStates==0">否</span>
        </template>
      </el-table-column>
      <el-table-column  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="80px">
        <el-form-item label="数据库名称" prop="dbName">
          <el-input v-model="form.dbName" placeholder="请输入参数数据库名称"/>
        </el-form-item>
        <el-form-item label="连接地址" prop="dbUrl">
          <el-input v-model="form.dbUrl" placeholder="请输入连接地址"/>
        </el-form-item>
        <el-form-item label="账号" prop="dbUsername">
          <el-input v-model="form.dbUsername" placeholder="请输入账号"/>
        </el-form-item>
        <el-form-item label="密码" prop="dbPassword">
          <el-input v-model="form.dbPassword" placeholder="请输入密码"/>
        </el-form-item>
        <el-form-item label="版本" prop="dbDriver">
          <el-input v-model="form.dbDriver" placeholder="请输入版本"/>
        </el-form-item>
      </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 {addConfig, delConfig, getConfig, refreshCache, updateConfig} from "@/api/system/config";
import {addDB, delDB, findList, getId, updateDB} from "@/api/databaseCredentials/databaseCredentials";

export default {
  name: "Config",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dbName: undefined,
        dbUrl: undefined,
        dbUsername: undefined,
        dbPassword: undefined,
        dbDriver: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dbName: [
          {required: true, message: "参数名称不能为空", trigger: "blur"}
        ],
        dbUrl: [
          {required: true, message: "参数键名不能为空", trigger: "blur"}
        ],
        dbUsername: [
          {required: true, message: "参数键值不能为空", trigger: "blur"}
        ],
        dbPassword: [
          {required: true, message: "参数键值不能为空", trigger: "blur"}
        ],
        dbDriver: [
          {required: true, message: "参数键值不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      findList({dbName:this.queryParams.dbName}).then(response => {
          this.configList = response.data;
          this.total = response.data.total;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dbName: undefined,
        dbUrl: undefined,
        dbUsername: undefined,
        dbPassword: undefined,
        dbDriver: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getId(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != undefined) {
            updateDB(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDB(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + id + '"的数据项？').then(function () {
        return delDB(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    }
  }
};
</script>
