<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="dataSource">
      <div class="d-header clearfix">
        <div class="fl">
          <i class="el-icon-info"/>
          {{ dataId ? $t1('查看数据源') : $t1('新增数据源') }}
        </div>
        <div class="fr">
          <el-button type="primary" plain class="button-sty" @click="$baseReload()" icon="el-icon-refresh-right">
            {{ $t1('重置') }}
          </el-button>
          <el-button type="primary" class="button-sty" icon="el-icon-check" @click="saveData">{{ $t1('保存') }}
          </el-button>
          <el-button type="primary" class="button-sty" icon="el-icon-video-play" @click="dblinktest">
            {{ $t1('测试数据库链接') }}
          </el-button>
        </div>
      </div>
      <div class="d-cont">
        <div class="d-item">
          <div class="title first"><b>{{ $t1('基本信息') }}</b></div>
          <table class="table-detail">
            <tbody>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('数据源名称') }}
              </th>
              <td>
                <el-form-item prop="dsName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="dataSource.dsName" clearable/>
                </el-form-item>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('数据源编码') }}
              </th>
              <td>
                <el-form-item prop="dsCode" :rules="[{ required: true, trigger: 'blur' }]" v-if="!dataId">
                  <el-input type="text" autocomplete="off" v-model="dataSource.dsCode" clearable/>
                </el-form-item>
                <template v-else>
                  {{ dataSource.dsCode }}
                </template>
              </td>
              <th>
                {{ $t1('是否启用') }}
              </th>
              <td>
                <el-form-item prop="enabled" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-radio-group v-model="dataSource.enabled">
                    <el-radio :label="true">{{ $t1('启用') }}</el-radio>
                    <el-radio :label="false">{{ $t1('禁用') }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('数据源类型') }}
              </th>
              <td colspan="3">
                <el-form-item prop="dsType" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-radio-group v-model="dataSource.dsType">
                    <el-radio :label="0">mysql</el-radio>
                    <el-radio :label="1">oracle</el-radio>
                    <el-radio :label="2">doris</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('数据源驱动类') }}
              </th>
              <td colspan="5">
                <el-form-item prop="driverClassName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="dataSource.driverClassName" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('链接数据源Url') }}
              </th>
              <td colspan="5">
                <el-form-item prop="dsUrl" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="dataSource.dsUrl" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('数据源用户名') }}
              </th>
              <td>
                <el-form-item prop="dsUserName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" auto-complete="new-password" v-model="dataSource.dsUserName"
                            clearable/>
                </el-form-item>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('数据源密码') }}
              </th>
              <td>
                <el-form-item prop="dsPassWord" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="password" autocomplete="off" v-model="dataSource.dsPassWord" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('备注') }}</th>
              <td colspan="5">
                <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                          v-model="dataSource.remark"
                          clearable></el-input>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ dataSource.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ dataSource.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ dataSource.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ dataSource.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'report_datasourceEdit',
  props: {
    _dataId: [String, Number]
  },
  components: {},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      dataSource: {
        enabled: null,
        dsType: null
      }
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$http({
          aes: true,
          url: USER_PREFIX + `/extend_datasource/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.dataSource = res.objx || {};
          }
        });
      } else {
        this.dataSource.enabled = true;
        this.dataSource.dsType = 0;
      }
    },
    dblinktest() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要测试数据库链接吗？')).then(() => {
            this.$http({
              aes: true,
              url: USER_PREFIX + '/extend_datasource/dblinktest',
              method: `post`,
              data: this.dataSource,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.objx,
                  type: 'success',
                  duration: 2000
                });
              }
            });
          });
        }
      });
    },
    saveData() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/extend_datasource/update` : `/extend_datasource/save`);
            this.$http({
              aes: true,
              url: url,
              method: `post`,
              data: this.dataSource,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.content,
                  type: 'success',
                  duration: 500,
                  onClose: t => {
                    if (this.isEdit) {
                      this.$baseReload();
                    } else {
                      this.$baseReload({
                        updateParam: {
                          _dataId: res.objx
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    }
  }
};
</script>
