<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-row mb-10">
        <div class="filter-group ">
          <div class="filter-lable">
            关键字：
          </div>
          <div class="filter-width">
            <el-input v-model="KEYWORDS" placeholder="搜索" />
          </div>
        </div>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left:10px" @click="getQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
          重置
        </el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" ref="multipleTable" :data="varList" :row-key="getRowKey" :header-cell-style="{'font-weight': 'bold','color': '#000'}" tooltip-effect="dark" border fit highlight-current-row>
      <el-table-column :reserve-selection="true" type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="CORP_NAME" label="企业名称" />
      <el-table-column prop="COMPANY_AREA" label="属地" />
      <el-table-column prop="CORP_TYPE_NAME" label="行业类型" />
      <el-table-column prop="STATUS" label="资源库使用状态" >
        <template slot-scope="{row}">
          <span v-if="row.STATUS==1">启用</span>
          <span v-if="row.STATUS==2">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="{row}">
          <el-button v-if="row.STATUS==2" type="success" size="mini" @click="editStatus(row.RESOURCECORP_ID,1)">启用</el-button>
          <el-button v-if="row.STATUS==1" type="primary" size="mini" @click="editStatus(row.RESOURCECORP_ID,2)">禁用</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.RESOURCECORP_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormAdd" title="新增" width="1000px">
      <CorpInfo v-if="dialogFormAdd" ref="corpInfo" @onClose="onClose"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormAdd = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import SelectTree from '@/components/SelectTree'
import waves from '@/directive/waves' // waves directive
import CorpInfo from './corpInfo'
export default {
  components: { Pagination, SelectTree, CorpInfo },
  directives: { waves },
  data() {
    return {
      config: config,
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      dialogFormAdd: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.RESOURCECORP_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getList()
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.getQuery()
    },
    handleAdd() {
      this.dialogFormAdd = true
    },
    confirm() {
      this.$refs.corpInfo.confirm()
    },
    onClose() {
      this.dialogFormAdd = false
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/resourcecorp/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 启用/禁用
    editStatus(id, status) {
      var str = ''
      if (status == '1') {
        str = '启用'
      } else if (status == '2') {
        str = '禁用'
      }

      this.$confirm('确定要' + str + '吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/resourcecorp/editStatus',
          {
            RESOURCECORP_ID: id,
            STATUS: status
          }
        ).then(() => {
          this.$message({
            message: str + '成功',
            type: 'success'
          })
          this.getQuery()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 获取列表
    handleDelete(ID) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        requestFN(
          '/resourcecorp/delete?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
          {
            RESOURCECORP_ID: ID
          }
        ).then((data) => {
          this.$message({
            message: '数据已删除',
            type: 'success'
          })
          this.getQuery()
        }).catch((e) => {
        })
      }).catch(() => {
      })
    },
    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.RESOURCECORP_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/resourcecorp/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getQuery()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
