<template>
  <div>
    <base-right-dialog
      ref="baseRightDialog"
      :footerShow="true"
      :dialogVisible.sync="dialogVisible"
      :title="dialogTitle + ' 数据源'"
      @handleClose="handleDialogClose"
      :type="dialogType"
      :submitShow="submitShow"
      :size="'65%'"
      @handleConfirmClick="handleConfirmClick"
    >
      <base-form
        ref="basicsForm"
        :formRow="formRow"
        :isFunBtn="false"
        :rules="basicsRules"
        class="dialog_form"
        :spanWidth="`120px`"
        :loading="vLoading"
        @onSelect="onSelect"
      ></base-form>
    </base-right-dialog>
  </div>
</template>

<script>
import baseRightDialog from "@/components/base/baseRightDialog";
import baseForm from "@/components/base/baseNewForm";
import baseTable from "@/components/base/baseTable";
import configData from "./configData";
import { authApi } from "@/api/apis/auth";
export default {
  components: {
    baseRightDialog,
    baseForm,
    baseTable,
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "",
      dialogType: "",
      formRow: configData.formRow,
      basicsRules: configData.basicsRules,
      vLoading: false,
      newMarryOptions: [],
      submitShow: true,
    };
  },
  mounted() {},
  methods: {
    openDialog(type, row) {
      this.formRow = configData.formRow;
      this.submitShow = true;
      this.formRow[5].elCol[0].show = false;
      // 新增
      if (type == "add") {
        this.dialogTitle = "新增";
        this.dialogType = "add";
        this.formRow[0].elCol[0].disabled = false;
        this.formRow[1].elCol[0].disabled = false;
        this.$nextTick(() => {
          this.$refs.basicsForm.getField("useState", 1);
        });
      }
      // 编辑
      if (type == "edit") {
        this.formRow[0].elCol[0].disabled = true;
        this.formRow[1].elCol[0].disabled = true;
        this.dialogTitle = "编辑";
        this.dialogType = "edit";
        this.messageGetById(row.id);
      }
      // 查看
      if (type == "show") {
        this.formRow[0].elCol[0].disabled = true;
        this.formRow[1].elCol[0].disabled = true;
        this.submitShow = false;
        this.formRow = configData.formRowShow;
        this.dialogTitle = "查看";
        this.dialogType = "show";
        this.messageGetById(row.id);
      }
      this.dialogVisible = true;
    },
    // 主表
    onSelect(val, index, indexRow, obj) {
      // 当前数据源类型为 oracle
      if (index == 3 && indexRow == 1 && val == 1) {
        this.formRow[5].elCol[0].show = true;
      } else {
        this.formRow[5].elCol[0].show = false;
      }
    },
    // 编辑详情
    async messageGetById(id) {
      let params = {
        id: id,
      };
      let res = await authApi(
        "sysDataSourceService",
        "dataSource",
        "queryEntity",
        "",
        params
      );
      if (res.status == "200") {
        this.$nextTick(() => {
          if(res.attribute.sourceType == "1"){
            this.formRow[5].elCol[0].show = true;
          }else{
            this.formRow[5].elCol[0].show = false;
          }
          this.$refs.basicsForm.incomingParameters(res.attribute);
        });
      }
    },
    // 弹窗关闭
    handleDialogClose() {
      this.$refs.basicsForm.resetFields();
      this.dialogVisible = false;
    },
    // 弹窗确认按钮
    handleConfirmClick() {
      this.$refs.basicsForm.$refs["ruleForm"].validate((valid) => {
        if (!valid) {
          return;
        } else {
          let params = {
            ...this.$refs.basicsForm.ruleForm,
          };
          if (this.dialogType == "add") {
            this.openLoading("submit");
            this.messageSaveDto(params);
          }
          if (this.dialogType == "edit") {
            this.openLoading("submit");
            this.messageUpdateDto(params);
          }
        }
      });
    },
    // 新增保存
    async messageSaveDto(params) {
      let res = await authApi(
        "sysDataSourceService",
        "dataSource",
        "addEntity",
        "",
        params
      );
      if (res.status == "200") {
        this.handleDialogClose();
        this.$vmNews("新增成功", "success");
        this.$emit("resetTable");
      }
    },
    // 编辑保存
    async messageUpdateDto(params) {
      let res = await authApi(
        "sysDataSourceService",
        "dataSource",
        "editEntity",
        "",
        params
      );
      if (res.status == "200") {
        this.handleDialogClose();
        this.$vmNews("更新成功", "success");
        this.$emit("resetTable");
      }
    },
  },
};
</script>

<style scoped lang="scss">
.dialogList {
  padding: 16px 0;
  border-top: 1px solid #dcdfe6;
  display: flex;
  flex-direction: column;
}

.updateBtn {
  border: 1px solid #ebedf1;
  padding: 5px 0;
  border-radius: 3px;
  text-align: center;
  font-size: 14px;
  cursor: pointer;
  width: 100px;
}

.updateBtn:hover {
  color: #1890ff;
  border-color: #badeff;
  background-color: #e8f4ff;
}

::v-deep .el-table__body-wrapper.is-scrolling-none {
  height: auto !important;
}

::v-deep .app-container {
  height: auto !important;
}
</style>