<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <SearchCom
        :form="queryParam"
        :list="queryOptions"
        @search="handleRefresh"
        @reset="() => {queryParam = {}, handleRefresh()}" >
      </SearchCom>

      <div class="table-operator">
        <a-button v-if="hasPerm('sys:dictionary:edit')" type="primary" @click="handleCreate">新增</a-button>
      </div>
    </div>
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
    >
      <template slot="action" slot-scope="text, record">
        <a href="javascript:;" @click="handleEdit(record)" v-if="hasPerm('sys:dictionary:edit')">修改</a>
        <a-divider type="vertical" />
        <a-popconfirm title="是否删除?" v-if="hasPerm('sys:dictionary:del')"
          @confirm="() => handleDelete(record)">
          <a href="javascript:;">删除</a>
        </a-popconfirm>
        <a-divider type="vertical" />
        <a href="javascript:;" v-if="hasPerm('sys:dictionary:get')" @click="handleDetail(record)">详情</a>
        <a-divider type="vertical" />
        <router-link :to="'/dictionaryItem/list/' + record.dictionaryCode"
          v-if="hasPerm('sys:dictionary:item:list')">
          词典项
        </router-link>
      </template>
    </s-table>
    <dictionary-form ref="modal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { dictionaryPage, dictionaryDel } from '@/api/sys/dictionary'
import { STable, SearchCom } from '@/components'
import DictionaryForm from './DictionaryForm'

export default {
  name: 'DictionaryList',
  components: {
    STable,
    SearchCom,
    DictionaryForm
  },
  data () {
    return {
      queryParam: { dictionaryName: null, dictionaryCode: null },
      selectedRowKeys: [], // 选中行的key 出选择框时需要配置
      selectedRows: [], // 选中行的数据
      columns: [
        { title: '词典名称', width: 30, dataIndex: 'dictionaryName', key: 'dictionaryName' },
        { title: '词典编码', width: 30, dataIndex: 'dictionaryCode', key: 'dictionaryCode' }
      ],
      loadData: parameter => {
        return dictionaryPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res
          })
      }
    }
  },
  computed: {
    queryOptions: function () {
      return [
        { type: 'input', placeholder: '词典名称', key: 'dictionaryName' }
      ]
    },
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  created () {
    // 根据权限加载
    if (this.hasPerm('sys:dictionary:edit') || this.hasPerm('sys:dictionary:get') ||
        this.hasPerm('sys:dictionary:del') || this.hasPerm('sys:dictionary:item:list') ) {
      this.columns.push({
        title: '操作',
        dataIndex: 'action',
        width: 15,
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    // 增
    handleCreate () {
      this.$refs.modal.add()
    },
    // 改
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    // 详情
    handleDetail (record) {
      this.$refs.modal.detail(record)
    },
    // 删
    handleDelete (record) {
      dictionaryDel({ ids: record.id, deleteReason: '' }).then(() => {
        this.$refs.table.refresh()
      })
    },
    // 编辑后回调
    handleOk () {
      this.$refs.table.refresh()
    },
    handleRefresh () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
