<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="词典项名称">
              <a-input v-model="queryParam.name" placeholder="词典项名称" @pressEnter="handleRefresh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="词典项值">
              <a-input v-model="queryParam.value" placeholder="请输入词典项值" @pressEnter="handleRefresh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="handleRefresh">查询</a-button>
            <a-button @click="() => {queryParam = { dictionaryCode: this.$route.params.id }, handleRefresh()}">重置</a-button>
          </a-col>
          <a-col :md="6" :sm="24" align="right" >
            <a-button type="primary" @click="handleCreate">新增</a-button>
          </a-col>
        </a-row>
      </a-form>
    </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>
      </template>
    </s-table>
    <dictionaryItem-form ref="modal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { dictionaryItemPage, dictionaryItemDel } from '@/api/sys/dictionaryItem'
import { STable } from '@/components'
import DictionaryItemForm from './DictionaryItemForm'

export default {
  name: 'DictionaryItemList',
  components: {
    STable,
    DictionaryItemForm
  },
  data () {
    return {
      queryParam: { name: '', value: '', dictionaryCode: this.$route.params.id },
      selectedRowKeys: [], // 选中行的key 出选择框时需要配置
      selectedRows: [], // 选中行的数据
      columns: [
        { title: '词典项名称', width: 30, dataIndex: 'name', key: 'name' },
        { title: '词典项值', width: 30, dataIndex: 'value', key: 'value' },
        { title: '词典标识', width: 30, dataIndex: 'dictionaryCode', key: 'dictionaryCode' },
        { title: '上级', width: 30, dataIndex: 'parentid', key: 'parentid' },
        { title: '排序', width: 30, dataIndex: 'sortid', key: 'sortid' },
        {
          title: '操作',
          key: 'operation',
          width: 10,
          align: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      loadData: parameter => {
        return dictionaryItemPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res
          })
      }
    }
  },
  computed: {
    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) {
      dictionaryItemDel({ ids: record.id, deleteReason: '' }).then(() => {
        this.$refs.table.refresh()
      })
    },
    // 编辑后回调
    handleOk () {
      this.$refs.table.refresh()
    },
    handleRefresh () {
      this.$refs.table.refresh(true)
    },
    // 选择行
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    }
  }
}
</script>
