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

      <a-button type="primary" @click="handleCreate">新增</a-button>
    </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)">修改</a>
        <a-divider type="vertical" />
        <a-popconfirm title="是否删除?" @confirm="() => handleDelete(record)">
          <a href="javascript:;">删除</a>
        </a-popconfirm>
        <a-divider type="vertical" />
        <a href="javascript:;" @click="handleDetail(record)">详情</a>
        <a-divider type="vertical" />
        <router-link :to="'/dictionary/dictionaryItem/list/' + record.dictionaryCode">词典项</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, status: null, createTime: null },
      selectedRowKeys: [], // 选中行的key 出选择框时需要配置
      selectedRows: [], // 选中行的数据
      columns: [
        { title: '词典名称', width: 30, dataIndex: 'dictionaryName', key: 'dictionaryName' },
        { title: '词典编码', width: 30, dataIndex: 'dictionaryCode', key: 'dictionaryCode' },
        {
          title: '操作',
          key: 'operation',
          width: 10,
          align: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: parameter => {
        return dictionaryPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res
          })
      }
    }
  },
  computed: {
    queryOptions: function () {
      const list = [{ 'value': 1, 'name': '类型1' }, { 'value': 2, 'name': '类型2' }]
      return [
        { type: 'input', placeholder: '词典名称', key: 'dictionaryName' },
        { type: 'date', placeholder: '请选择开始时间', key: 'createTime', format: 'YYYY-MM-DD HH:mm:ss' },
        { type: 'select', placeholder: '请选择类型', key: 'status', options: [{ name: '类型', id: undefined }, ...list] }
      ]
    },
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  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>
