<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.name"
          clearable
          placeholder=""
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人名称" prop="configName">
        <el-input
            v-model="queryParams.legalName"
            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="name"/>
      <el-table-column  label="负责人" prop="legalName"/>
      <el-table-column  label="手机号" prop="phoneNumber"/>
      <el-table-column  label="电子邮箱" prop="email"/>
      <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>
          <el-button
              icon="el-icon-plus"
              size="mini"
              type="text"
              v-if="scope.row.dbId ==null || scope.row.dbId == undefined"
              @click="openDBForm(scope.row)">
            选择数据库
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="选择数据库" :visible.sync="openDB" append-to-body width="600px">
      <el-select v-model="db.dbId" placeholder="请选择">
        <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.dbName"
            :value="item.id">
        </el-option>
      </el-select>
      <el-button @click="addEnterpriseDB()">确定</el-button>
    </el-dialog>
    <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="公司名称">
          <el-input v-model="form.name" placeholder="请输入公司名称"/>
        </el-form-item>
        <el-form-item label="负责人" prop="负责人">
          <el-input v-model="form.legalName" placeholder="请输入负责人"/>
        </el-form-item>
        <el-form-item label="手机号" prop="手机号">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="电子邮箱">
          <el-input v-model="form.email" placeholder="请输入电子邮箱"/>
        </el-form-item>
        <el-form-item label="企业登录名" prop="企业登录名">
          <el-input v-model="form.firmUser" placeholder="企业登录名"/>
        </el-form-item>
        <el-form-item label="企业登录密码" prop="企业登录密码">
          <el-input v-model="form.firmPassword" 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 {
  addEnterprise,
  delEnterprise,
  findEnterprise,
  findList,
  getId,
  updateEnterprise, updEnterpriseBydbId
} from "@/api/enterprise/enterprise";
import {findStatesList} from "@/api/databaseCredentials/databaseCredentials";


export default {
  name: "Config",
  dicts: ['sys_yes_no'],
  data() {
    return {
   //等待加载
      fullscreenLoading: false,

      options:[],
      openDB:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        legalName: undefined,
        phoneNumber: undefined,
        dbPassword: undefined
      },
      db:{},
      // 表单参数
      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: {
    addEnterpriseDB(){
      console.log(this.db)
      updEnterpriseBydbId(this.db).then(response => {
      })
    },
    openDBForm(row){
      this.openDB=true;
      this.db.id=row.id;
      findStatesList().then(response => {
        this.options=response.data;
      })
    },
    /** 查询参数列表 */
    getList() {
      findList(this.queryParams).then(response => {
        console.log(response)
        this.configList= response.data.records
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        legalName: undefined,
        phoneNumber: undefined,
        dbPassword: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    openFullScreen2() {

    },


    /** 新增按钮操作 */
    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.id != undefined) {
            updateEnterprise(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const loading = this.$loading({
              lock: true,
              text: '正在为您入驻企业',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            addEnterprise(this.form).then(response => {
                loading.close();
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal.confirm('是否确认删除参数编号为"' + id + '"的数据项？').then(function () {
        return delEnterprise(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>
