<template>
  <div class="module_content">
    <el-card shadow="never" class="commont-cart">
      <div class="project-name">
        <p>{{projectName}} - 资源文件 - sql构建器</p>
      </div>
      <el-form ref="searchForm" :model="searchForm" @submit.native.prevent>
        <div class="">

          <div class="search-header">
            <div class="tool-common-wrap">
              <div class="search-wrap">
                <quick-query ref="quick" :placeholder="placeholder" :is-date="false" @quickSearch="quickSearch" />
              </div>
              <div class="btn-wrap">
                <el-button type="primary" @click="addSQLinfo"  >添加</el-button>
                <el-button type="primary" @click="rest()" > 刷新 </el-button>
              </div>
            </div>
          </div>
          <div class="tableWrap">
            <el-table
              :data="tableList"
              v-loading="tableLoading"
              highlight-current-row

              style="width: 100%; margin: 15px 0">
              <el-table-column
                type="index"
                label="序号"
                width="50"
                align="center">
              </el-table-column>
              <el-table-column
                prop="name"
                label="名称"
                align="left"
                show-overflow-tooltip>
                <template slot-scope="scope">
                  <!--<el-link class="table-link" :type="currentRow && currentRow.id && scope.row.id && currentRow.id === scope.row.id ? 'primary' : ''">{{scope.row.name}}</el-link>-->
                  <span >{{scope.row.name}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="description"
                label="描述"
                align="left"
                show-overflow-tooltip>
              </el-table-column>
              <el-table-column
                prop="projectName"
                label="项目名称"
                align="left"
                show-overflow-tooltip>
              </el-table-column>
              <el-table-column
                prop="operate"
                label="数据操作"
                align="left"
                show-overflow-tooltip>
              </el-table-column>
              <el-table-column
                label="操作"
                width="100"
                align="center"
                show-overflow-tooltip>
                <template slot-scope="scope">
                    <el-tooltip effect="dark" content="编辑" :enterable="false" placement="top">
                       <el-button class="icon-button" type="text" icon="el-icon-edit" @click.native.prevent="editSQLinfo('edit', scope.row)">
                       </el-button>
                   </el-tooltip>
                    <el-tooltip effect="dark" content="删除" :enterable="false" placement="top">
                      <el-button class="icon-button" type="text" icon="el-icon-delete" @click.native.prevent="handleDelete(scope.row)">
                      </el-button>
                   </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :page-size="page.pageSize"
              :current-page.sync="page.pageNum"
              background
              @current-change="getDataCurrent()"
              :total="page.total"
            />
          </div>
        </div>
      </el-form>
    </el-card>
    <add-sql-builder-dialog v-model='addSqlBuilderVisible' @closeAddDialog="closeAddDialog" @confirmAdd="confirmAdd"></add-sql-builder-dialog>
    <edit-sql-builder-dialog v-model='editSqlBuilderVisible' :rowData="checkedRow" @closeEditDialog="closeEditDialog" @confirmEdit="confirmEdit"></edit-sql-builder-dialog>
  </div>
</template>
<script>
// import AddSqlBuilderDialog from './AddSqlBuilderDialog'
import AddSqlBuilderDialog from './add'
import EditSqlBuilderDialog from './editSqlBuilderDialog'
import QuickQuery from '@/components/quickquery/QuickQuery'
import { querySqlBuilderList, deleteSqlBuilder } from '@/api/esb_global_management'

import EsbServiceListMixins from '@/mixins/esb_service_list_mixins'
import { createNamespacedHelpers } from 'vuex'
const { mapGetters, mapMutations } = createNamespacedHelpers('app')
export default {
  mixins: [EsbServiceListMixins],
  components: {
    AddSqlBuilderDialog,
    EditSqlBuilderDialog,
    QuickQuery
  },
  data() {
    return {
      dialogVisible: false,
      addSqlBuilderVisible: false,
      editSqlBuilderVisible: false,
      checkedRow: {},
      SQLid: '',
      quickQueryKeys: ['name'],
      sort: {
        orderby: 'createon',
        sort: 'desc'
      },
      placeholder: '名称'
    }
  },
  computed: {
    ...mapGetters(['projectID', 'projectName'])
  },
  watch: {
  },
  created() {
    this.setProjectID(localStorage.getItem('projectID'))
    this.setProjectName(localStorage.getItem('projectName'))
  },
  methods: {
    ...mapMutations(['setProjectID', 'setProjectName']),
    // 新增
    addSQLinfo() {
      this.addSqlBuilderVisible = true
    },
    closeAddDialog(val) {
      this.addSqlBuilderVisible = val
    },
    confirmAdd() {
      this.addSqlBuilderVisible = false
      this.getData()
    },
    // 编辑

    editSQLinfo(type, data) {
      this.editSqlBuilderVisible = true
      this.checkedRow = data
    },
    closeEditDialog(val) {
      this.editSqlBuilderVisible = val
    },
    confirmEdit() {
      this.editSqlBuilderVisible = false
      this.getData()
    },

    // 删除
    handleDelete(item) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableLoading = true
        deleteSqlBuilder(item.id).then((rt) => {
          this.tableLoading = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getData()
        }).catch(e => {
          this.tableLoading = false

          if (e.status === '500') {
            // const errData = e.data
            this.$refs.flowInfoBridge.showMsg(false)
            this.$message({
              message: e.message,
              type: 'error'
            })
          }
        })
      }).catch(() => {})
    },
    // 列表
    async getData() {
      this.tableLoading = true
      const filterName = {
        key: 'projectId',
        opt: 'EQ',
        value: this.projectID
      }
      this.filters.push(filterName)
      querySqlBuilderList({
        logic: this.logic,
        filters: this.filters,
        ...this.sort,
        ...this.page
      })
        .then((rt) => {
          if (rt.status === '200') {
            this.tableLoading = false
            this.tableList = rt.data.list || []
            this.page.total = rt.data.total
          } else {
            this.tableLoading = false
          }
        })
        .catch(e => {
          this.tableLoading = false
          return Promise.reject(e)
        })
    },
    editSqlBuilder(item) {
      this.SQLid = item.id
      this.dialogVisible = true
    },
    // quickQueryData() {
    //   this.tableLoading = true
    //   querySqlBuilderList({
    //     logic: this.logic,
    //     filters: this.filters,
    //     ...this.sort,
    //     ...this.page
    //   }).then(rt => {
    //     if (rt.status === '200') {
    //       this.tableLoading = false
    //       this.tableList = rt.data.list || []
    //       this.page.total = rt.data.total
    //     } else {
    //       this.tableLoading = false
    //     }
    //   }).catch(e => {
    //     this.tableLoading = false
    //     return Promise.reject(e)
    //   })
    // }

  }
}
</script>

<style lang="scss" scoped>
.module_content{
  padding: 10px 10px;
}
</style>

<style type="text/scss" lang="scss" scoped>
</style>
