<!--
 * @Author: your name
 * @Date: 2021-05-07 14:34:59
 * @LastEditTime: 2021-05-19 10:45:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ESB\src\views\esb_service\sql_builder\sqlList.vue
-->
<template>
  <div class="module_content">
    <el-col :span="6" >
        <project-nav @itemClick="projectClick"></project-nav>
      </el-col>
    <el-card shadow="never" class="commont-cart">
      <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>
                </template>
              </el-table-column>
              <el-table-column
                prop="description"
                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.description}}</el-link>
                </template>
              </el-table-column>
              <el-table-column
                prop="operate"
                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.operate}}</el-link>
                </template>
              </el-table-column>
              <el-table-column
                prop="projectName"
                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.projectName}}</el-link>
                </template>
              </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-circle-check" @click.native.prevent="rely(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-column label="选择" width="60" align="center">
                <template slot-scope="scope">
                  <el-radio v-model="selectIndex" :label="scope.row.id" class="radioEmpty" @click.native.prevent="rely(scope.row)">{{''}}</el-radio>
                </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" :projectId="projectId"></add-sql-builder-dialog>
    <edit-sql-builder-dialog v-model='editSqlBuilderVisible' :rowData="checkedRow" @closeEditDialog="closeEditDialog" @confirmEdit="confirmEdit" :projectId="projectId"></edit-sql-builder-dialog>
  </div>
</template>
<script>
import AddSqlBuilderDialog from './add'
import EditSqlBuilderDialog from './editSqlBuilderDialog'
import QuickQuery from '@/components/quickquery/QuickQuery'
import ProjectNav from '@/components/projectNav'
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,
    ProjectNav
  },
  data() {
    return {
      perjectName: '',
      selectIndex: '',
      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.projectId = localStorage.getItem('projectID')
    this.setProjectName(localStorage.getItem('projectName'))
    this.perjectName = localStorage.getItem('projectName')
  },
  methods: {
    ...mapMutations(['setProjectID', 'setProjectName']),
    // 项目列表点击时
    projectClick(item) {
      console.log(item, '11111')
      this.projectId = item.id
      this.perjectName = item.name
      this.filters.forEach(fil => {
        if (fil.key === 'project_id') {
          fil.value = this.projectId
        }
      })
      this.getData()
    },
    // 新增
    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()
    },
    //    依赖检测
    rely(data, index) {
      data.perjectName = this.perjectName
      data.perjectId = this.projectId
      this.$emit('getSqlData', data)
      this.tableList.forEach((item, index) => {
        if (data.id === item.id) {
          this.selectIndex = item.id
        }
      })
    },
    // 删除
    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
      querySqlBuilderList({
        attr: {
          projectId: this.projectId
        },
        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 type="text/scss" lang="scss" scoped>
.module_content{
  overflow: hidden;
}
</style>
