<!--
 * @Author: your name
 * @Date: 2021-05-07 14:27:30
 * @LastEditTime: 2021-05-08 16:25:34
 * @LastEditors: Please set LastEditors
 * @Description: 项目分类
 * @FilePath: \ESB\src\views\esb_service\agency_service\sqlList.vue
-->
<template>
  <el-dialog :close-on-click-modal="false" title="选择资源文件" :visible.sync="dialogVisible" width="910px" top="5vh" append-to-body @close="closeDialog">
    <el-row :gutter="10">
      <el-col :span="6">
        <project-nav @itemClick="projectClick"></project-nav>
      </el-col>
      <el-col :span="18">
        <el-form :model="formData" label-width="50px" @submit.native.prevent>
          <div class="search-header">
            <div class="tool-common-wrap">
              <div class="search-wrap">
                <quick-query ref="quick" :placeholder="placeholder" :is-date="false" @quickSearch="powerSearch" />
              </div>
              <div class="btn-wrap">
                <el-button type="primary" @click="rest()"> 刷新</el-button>
              </div>
            </div>
          </div>
        </el-form>
        <div class="tableWrap">
          <el-table
            v-if="!isMore"
            :data="tableList"
            v-loading="tableLoading"
            highlight-current-row
            style="width: 100%; margin-bottom: 10px"
            size="mini"
            @current-change="handleCurrentChange">
            <el-table-column type="index" label="序号" width="50" align="center">
            </el-table-column>
            <el-table-column prop="name" label="名称" align="left" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="path" label="路径" align="left" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="type" label="类型" align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="选择" width="60" align="center">
              <template slot-scope="scope">
                <el-radio v-model="selectIndex" :label="scope.$index" class="radioEmpty">{{''}}</el-radio>
              </template>
            </el-table-column>
          </el-table>
          <el-table
            v-else
            :data="tableList"
            v-loading="tableLoading"
            highlight-current-row
            style="width: 100%; margin-bottom: 10px"
            size="mini"
            ref="multipleTable"
            tooltip-effect="dark"
            row-key="id"
            :reserve-selection="true"
            @row-click="clickRow"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" :reserve-selection="true">
            </el-table-column>
            <el-table-column type="index" label="序号" width="50" align="center">
            </el-table-column>
            <el-table-column prop="name" label="名称" align="left" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="path" label="路径" align="left" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="type" label="类型" align="center" show-overflow-tooltip>
            </el-table-column>
          </el-table>
          <el-pagination
            :page-size="page.pageSize"
            :current-page.sync="page.pageNum"
            background
            @current-change="getData()"
            :total="page.total"></el-pagination>
        </div>
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import QuickQuery from '@/components/quickquery/QuickQuery'
import { listFiles } from '@/api/esb_service'
import ProjectNav from '@/components/projectNav'

export default {
  name: 'ChooseGoods',
  components: {
    ProjectNav,
    QuickQuery
  },
  data() {
    return {
      dialogVisible: false,
      tableLoading: false,
      formData: {},
      tableList: [],
      page: {
        pageSize: 5,
        pageNum: 1,
        total: 0
      },
      selectIndex: -1,
      multipleSelection: [],
      defineFileTypeList: [
        { name: 'wsdl', value: 'wsdl' },
        { name: 'XML Schema', value: 'XML Schema' },
        { name: 'JSON Schema', value: 'JSON Schema' }
      ],
      projectId: localStorage.getItem('projectID'),
      filters: [
        {
          key: 'project_id',
          opt: 'LIKE',
          type: 'S',
          value: this.projectId
        }
      ],
      currentId: '',
      quickQueryKeys: ['name'],
      sort: {
        orderby: 'createon',
        sort: 'desc'
      },
      placeholder: '名称、描述',
      logic: 'and'
    }
  },
  props: {
    value: {
      type: Boolean,
      default: false
    },
    isMore: {
      type: Boolean,
      default: false
    },
    connectorId: {
      type: String,
      default: ''
    },
    defineFileType: {
      type: String,
      default: 'wsdl'
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      if (val) {
        this.currentId = this.connectorId
        this.rest()
      } else if (this.isMore) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.setCurrentRow()
      }
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  methods: {
    // 项目列表点击时
    projectClick(item) {
      this.projectId = item.id
      this.filters.forEach(fil => {
        if (fil.key === 'project_id') {
          fil.value = this.projectId
        }
      })
      this.getData()
    },
    getData() {
      this.tableLoading = true
      this.selectIndex = -1
      listFiles({
        logic: this.logic,
        filters: this.filters,
        ...this.sort,
        ...this.page
      }).then((rt) => {
        this.tableLoading = false
        if (rt.status === '200') {
          this.tableList = rt.data.list || []
          this.tableList.forEach((item, index) => {
            if (this.currentId && this.currentId === item.id) {
              this.selectIndex = index
            }
          })
          this.page.total = rt.data.total
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 搜索
    powerSearch(data) {
      this.filters = [
        {
          key: 'project_id',
          opt: 'LIKE',
          type: 'S',
          value: this.projectId
        }
      ]
      this.page.pageNum = 1
      this.quickQueryKeys.forEach(key => {
        const filter = {
          key,
          opt: 'LIKE',
          type: 'S',
          value: data.searchData
        }
        if (filter.value) {
          this.filters.push(filter)
        }
      })
      this.filters.push({ 'key': 'type', 'value': this.defineFileType })
      this.getData()
    },
    // 重置
    rest() {
      if (this.$refs.quick) {
        this.$refs.quick.quickReset() // 清除快速查询
      }
      this.powerSearch({})
    },
    // 搜索过滤
    filterData(obj) {
      this.filters = [{
        key: 'project_id',
        opt: 'LIKE',
        type: 'S',
        value: this.projectId
      }]
      Object.keys(obj).forEach(key => {
        const filter = {
          key,
          opt: 'LIKE',
          type: 'S',
          value: obj[key]
        }
        if (filter.value) {
          this.filters.push(filter)
        }
      })
    },
    // 单选
    handleCurrentChange(val) {
      if (val) {
        this.currentId = ''
        this.selectIndex = this.tableList.findIndex(item => item.id === val.id)
      }
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    clickRow(row) {
      this.$refs.multipleTable.toggleRowSelection(row)
    },
    submit() {
      if (this.isMore) {
        const data = JSON.parse(JSON.stringify(this.multipleSelection))
        this.$emit('confirm', data)
        this.dialogVisible = false
      } else {
        if (this.selectIndex >= 0) {
          const data = JSON.parse(JSON.stringify(this.tableList[this.selectIndex]))
          this.$emit('confirm', data)
          this.dialogVisible = false
        } else {
          this.$message({
            message: '请选择',
            type: 'error'
          })
        }
      }
    },
    // 关闭dialog
    closeDialog() {
      this.formData = {}
      this.dialogVisible = false
    },
    cancel() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
</style>
