<template>
  <div>
    <base-layout
      ref="baseLayout"
      :buttonList="buttonList"
      @onFuncBtn="onFuncBtn"
      :querySwitch="true"
      :searchList="requirementList"
      @search="handleSearchEvent"
      :isPage="true"
      @pageChange="handlePageChange"
    >
      <div slot="main" slot-scope="{ tableHeight }">
        <base-table
          ref="baseTable"
          :showIndex="true"
          :funWidth="180"
          :funData="funData"
          @onFunc="onFunc"
          :tabLoading.sync="tabLoading"
          :tableHeight="tableHeight"
          :tableData="tableData"
          :tableColumn="tableColumnData"
        >
          <!-- 数据源类型 -->
          <template v-slot:sourceType="{ row }">
            <div style="width: 100%">
              <span v-if="row.sourceType == 1">oracle</span>
              <span v-else-if="row.sourceType == 2">sqlsever</span>
              <span v-else-if="row.sourceType == 3">mysql</span>
              <span v-else></span>
            </div>
          </template>
          <!-- 使用状态 -->
          <template v-slot:useState="{ row }">
            <div style="width: 100%">
              <span v-if="row.useState == 1">启用</span>
              <span v-else-if="row.useState == 0">锁定</span>
              <span v-else></span>
            </div>
          </template>
        </base-table>
      </div>
    </base-layout>
    <right-dialog ref="rightDialog" @resetTable="resetTable"></right-dialog>
  </div>
</template>

<script>
import baseLayout from "@/components/base/baseLayout";
import baseTable from "@/components/base/baseTable";
import rightDialog from "./rightDialog";
import configData from "./configData";
import { authApi } from "@/api/apis/auth";
export default {
  components: {
    baseLayout,
    baseTable,
    rightDialog,
  },
  data() {
    return {
      buttonList: [
        {
          menuName: "新增",
          icon: "el-icon-plus",
          btnFunction: "add",
        },
        {
          menuName: "刷新",
          icon: "el-icon-refresh",
          btnFunction: "resetLoad",
        },
      ], //按钮组
      requirementList: [
        {
          placeholder: "数据源编码",
          prop: "sourceCode",
          tag: "elInput",
        },
        {
          placeholder: "数据源名称",
          prop: "sourceName",
          tag: "elInput",
        },
      ], //查询模板list
      tabLoading: false,
      tableColumnData: configData.tableColumnData, //表头数据
      funData: [
        {
          color: "#6a9af1",
          text: "查看",
        },
        {
          color: "#6a9af1",
          text: "编辑",
        },
        {
          color: "#d67a74",
          text: "删除",
        },
      ],
      tableData: [], //表格数据
      pageModel: {
        pageNum: 1,
        pageSize: 10,
      },
      queryModel: {
        sourceCode: "", //数据源编码
        sourceName: "", //数据源名称
      },
    };
  },
  mounted() {
    this.GetMessageTableData();
  },
  methods: {
    // 表格数据
    async GetMessageTableData() {
      this.tabLoading = true;
      let param = {
        ...this.pageModel,
        ...this.queryModel,
      };
      let res = await authApi(
        "sysDataSourceService",
        "dataSource",
        "queryPage",
        "",
        param
      );
      this.tabLoading = false;
      if (res.status == "200") {
        this.tableData = res.attribute.list;
        this.$refs.baseLayout.setPageTotal(res.attribute.total);
      }
    },
    // 分页变化
    handlePageChange(val) {
      this.pageModel.pageNum = val.pageIndex;
      this.pageModel.pageSize = val.pageSize;
      this.GetMessageTableData();
    },
    // 按钮组
    onFuncBtn(btn) {
      this[btn.btnFunction]();
    },
    // 新增
    add() {
      this.$refs.rightDialog.openDialog("add");
    },
    // 表格操作事件 查看 编辑 删除
    onFunc(index, row) {
      // 查看
      if (index == 0) {
        this.openLoading("detail");
        this.$refs.rightDialog.openDialog("show", row);
      }
      // 编辑
      if (index == 1) {
        this.openLoading("detail");
        this.$refs.rightDialog.openDialog("edit", row);
      }
      // 删除
      if (index == 2) {
        this.$delConfirm().then(() => {
          this.openLoading("del");
          this.messageDeleteById(row.id);
        });
      }
    },
    async messageDeleteById(id) {
      let param = {
        id: id,
      };
      let res = await authApi(
        "sysDataSourceService",
        "dataSource",
        "deleteEntity",
        "",
        param
      );
      if (res.status == "200") {
        this.$vmNews("删除成功", "success");
        this.resetTable();
      }
    },
    // 重置表格
    resetTable() {
      this.pageModel.pageNum = 1;
      this.$refs.baseLayout.pageClear();
      this.GetMessageTableData();
    },
    // 搜索
    handleSearchEvent() {
      let data = this.$refs.baseLayout.ruleForm;
      this.queryModel.sourceCode = data.sourceCode;
      this.queryModel.sourceName = data.sourceName;
      this.resetTable();
    },
  },
};
</script>

<style scoped>
.clickTitle {
  color: #409eff;
  cursor: pointer;
}
</style>