<template>
  <div class="pa-3 white">
    <m-search class="mb-3" :items="searchItems" v-model="searchValues" @search="onSearch"></m-search>
    <m-table
      v-loading="loading"
      :items="items"
      :headers="headers"
      :page-size="pageInfo.size"
      :page-current="pageInfo.current"
      :total="total"
      @page-change="onPageChange"
    >
      <!-- <template #card-tools>
        <m-button type="orange" icon="el-icon-plus" @click="onAdd">新增</m-button>
      </template> -->
      <template #dictCode="{ row }">
        <el-tag>{{ row.dictCode === 'systemCode' ? '系统级' : '用户级'}}</el-tag>
      </template>
      <template #actions="{ row }">
        <!-- <m-button type="primary" text @click="onEdit(row)">编辑</m-button> -->
        <m-button type="primary" text @click="onEdit(row)">字典配置</m-button>
        <!-- <m-button type="danger" text @click="onDelete(row)">删除</m-button> -->
      </template>
    </m-table>
    <DictionariesEdit ref="dictionariesEditRefs" @success="onInit"></DictionariesEdit>
  </div>
</template>

<script>
import {
  getDictionariesList,
  deleteDictionaries
} from '@/api/system'
import DictionariesEdit from './dictionariesEdit'
export default {
  name: 'SystemDictionaries',
  components: {
    DictionariesEdit
  },
  data () {
    return {
      searchValues: {
        dictTitle: null,
        dictCode: 'systemCode'
      },
      headers: [
        { label: '名称', prop: 'dictTitle' },
        { label: '类型', prop: 'dictCode', align: 'center' },
        { label: '标识', prop: 'dictValue', align: 'center' },
        { label: '创建时间', prop: 'createDate' },
        { label: '操作', prop: 'actions', fixed: 'right', width: 300 }
      ],
      items: [],
      total: 0,
      pageInfo: {
        current: 1,
        size: 10
      },
      orders: [],
      loading: false
    }
  },
  computed: {
    searchItems () {
      return [
        {
          label: '名称',
          prop: 'dictTitle',
          type: 'input',
          options: {
            placeholder: '请输入名称'
          }
        },
        {
          label: '类型',
          prop: 'dictCode',
          type: 'select',
          options: {
            placeholder: '请输入名称',
            clearable: false,
            items: [
              { label: '系统级', value: 'systemCode' },
              { label: '用户级', value: 'userCode' }
            ]
          }
        }
      ]
    }
  },
  created () {
    this.onInit()
  },
  methods: {
    async onInit () {
      this.loading = true
      try {
        const { data } = await getDictionariesList({
          page: {
            ...this.pageInfo,
            orders: this.orders
          },
          ...this.searchValues
        })
        this.items = data.records
        this.total = data.total
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    onAdd () {
      this.$refs.dictionariesEditRefs.open()
    },
    onEdit (item) {
      this.$refs.dictionariesEditRefs.open(item)
    },
    onDelete (row) {
      this.$confirm('确定删除吗？', '提示')
        .then(async () => {
          try {
            await deleteDictionaries({ id: row.id })
            this.$message.success('删除成功')
            this.onInit()
          } catch (error) {
            this.$message.error(error)
          }
        })
        .catch(_ => {})
    },
    onSearch () {
      this.pageInfo.current = 1
      this.onInit()
    },
    onPageChange (index) {
      this.pageInfo.current = index
      this.onInit()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
