<template>
  <div id="containt">
    <el-tabs v-model="activeName" class="tab-box">
      <el-tab-pane :label="$t1('常规')" name="first">
        <editView v-if="showEdit" visible-key="showEdit" :_dataId.sync="dataId" :parent-target="_self"
                  @reload="$reloadHandle"></editView>
      </el-tab-pane>
      <el-tab-pane :label="$t1('列表')" name="second">
        <div class="grid-height">
          <vxe-grid ref="table-m1" v-bind="vxeOption" @resizable-change="$vxeTableUtil.onColumnWitchChange"
                    @custom="$vxeTableUtil.customHandle">
            <template #form>
              <div class="clearfix screen-btns">
                <div class="fl">
                  <vxe-button status="primary" class="button-sty" icon="el-icon-plus" @click="openEditDialog">
                    {{ $t1('新增') }}
                  </vxe-button>
                  <vxe-button status="primary" class="button-sty" icon="el-icon-upload2"
                              @click="$excelExport({ title: $t1('数据源导出'), targetRef: 'table-m1' })">{{
                      $t1('导出')
                    }}
                  </vxe-button>
                  <!-- <vxe-button status="primary" class="button-sty" icon="el-icon-download" @click="importExcel">导入</vxe-button> -->
                </div>
                <div class="fr">
                  <vxe-button icon="el-icon-brush" class="button-sty" @click="resetEvent" type="text" status="primary"
                              plain>{{ $t1('重置') }}
                  </vxe-button>
                  <vxe-button status="warning" icon="el-icon-search" class="button-sty" @click="searchEvent">
                    {{ $t1('搜索') }}
                  </vxe-button>
                </div>
              </div>
              <vxe-form ref="form" class="screen-box" title-width="92px" title-align="right" :data="formData"
                        @submit="searchEvent" @reset="searchEvent">
                <vxe-form-item :title="$t1('数据源名称')+':'" field="dsName">
                  <template v-slot>
                    <el-input v-model="formData.dsName" size="small" clearable/>
                  </template>
                </vxe-form-item>
                <vxe-form-item :title="$t1('数据源Url')+':'" field="dsUrl">
                  <template v-slot>
                    <el-input v-model="formData.dsUrl" size="small" clearable/>
                  </template>
                </vxe-form-item>
                <vxe-form-item :title="$t1('数据源类型')+':'" field="dsType">
                  <template v-slot>
                    <el-select v-model="formData.dsType" clearable>
                      <el-option :value="0" label="mysql"></el-option>
                      <el-option :value="1" label="oracle"></el-option>
                      <el-option :value="2" label="doris"></el-option>
                    </el-select>
                  </template>
                </vxe-form-item>
                <vxe-form-item :title="$t1('是否启用')+':'" field="enable">
                  <template v-slot>
                    <el-select v-model="formData.enable" clearable>
                      <el-option :value="true" :label="$t1('启用')"></el-option>
                      <el-option :value="false" :label="$t1('禁用')"></el-option>
                    </el-select>
                  </template>
                </vxe-form-item>
              </vxe-form>
            </template>
          </vxe-grid>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import editView from './edit.vue';

export default {
  name: 'extend_datasource:list',
  components: {editView},
  data() {
    return {
      activeName: 'second',
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      formData: {}
    };
  },
  mounted() {
    this.initTableList();
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.$openEditView('showEdit');
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'user_report_datasource_list-m1',
        path: USER_PREFIX + '/extend_datasource/listPage',
        aes: true,
        param: () => {
          return this.formData;
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('数据源名称'),
            field: 'dsName',
            width: 150,
            fixed: 'left'
          },
          {
            title: this.$t1('数据源编码'),
            field: 'dsCode',
            width: 150
          },
          {
            title: this.$t1('数据源类型'),
            field: 'dsType',
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.dsType === 0) {
                  return 'mysql';
                } else if (row.dsType === 1) {
                  return 'oracle';
                } else if (row.dsType === 2) {
                  return 'doris';
                }
              }
            }
          },
          {
            title: this.$t1('数据源对应驱动ClassName'),
            field: 'driverClassName',
            width: 150
          },
          {
            title: this.$t1('链接数据源Url'),
            field: 'dsUrl',
            width: 150
          },
          {
            title: this.$t1('数据源用户名'),
            field: 'dsUserName',
            width: 150
          },
          {
            field: 'enabled',
            title: this.$t1('是否启用'),
            width: 150,
            slots: {
              default: ({row}) => {
                if (row.enabled) {
                  return [
                    <div class="txt-status">
                      <span>{this.$t1('启用')}</span>
                    </div>
                  ];
                } else {
                  return [
                    <div class="txt-status disable">
                      <span>{this.$t1('禁用')}</span>
                    </div>
                  ];
                }
              }
            }
          },
          {
            field: 'remark',
            title: this.$t1('备注'),
            width: 250
          },
          {
            field: 'createDate',
            title: this.$t1('创建时间'),
            width: 150
          },
          {
            width: 47,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row}) => {
                return [
                  <div>
                    <a
                      href="javascript:void(0);"
                      class="a-link"
                      onclick={() => {
                        this.openEditDialog(row.id);
                      }}
                    >
                      <el-tooltip enterable={false} effect="dark" content={this.$t1('查看')} placement="top"
                                  popper-class="tooltip-skin">
                        <i class="el-icon-edit"/>
                      </el-tooltip>
                    </a>
                  </div>
                ];
              }
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
      });
    }
  }
};
</script>
