<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="5">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)">
          添加
        </el-button>
        <el-button
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete($event)">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="19">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item label="字典名称" prop="dictName">
            <el-input
              v-model.trim="queryForm.dictName"
              clearable
              placeholder="请输入字典名称" />
          </el-form-item>
          <el-form-item label="字典标签" prop="dictLabel">
            <el-input
              v-model.trim="queryForm.dictLabel"
              clearable
              placeholder="请输入字典标签" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryForm.status"
              clearable
              placeholder="数据状态">
              <el-option label="正常" value="10" />
              <el-option label="停用" value="20" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="height"
      @selection-change="setSelectRows">
      <el-table-column align="center" type="selection" />
      <el-table-column align="center" label="字典编码" prop="dictCode" />
      <el-table-column align="center" label="字典标签" prop="dictLabel">
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.listClass == '' || scope.row.listClass == 'default'
            ">
            {{ scope.row.dictLabel }}
          </span>
          <el-tag
            v-else
            :type="scope.row.listClass == 'primary' ? '' : scope.row.listClass">
            {{ scope.row.dictLabel }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="字典键值" prop="dictValue" />
      <el-table-column align="center" label="字典排序" prop="dictSort" />
      <el-table-column align="center" label="状态" prop="status">
        <template #default="{ row }">
          {{ row.status === '10' ? '正常' : '停用' }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="备注"
        prop="remark"
        show-overflow-tooltip />
      <el-table-column align="center" label="创建时间" prop="createdTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import dictApi from '@/api/dict'
  import Edit from './components/DataEdit'

  export default {
    name: 'DataList',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          dictName: undefined,
          dictType: undefined,
          status: undefined,
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight(1)
      },
    },
    created() {
      const dictId = this.$route.params && this.$route.params.dictId
      this.getDictTypeInfo(dictId)
    },
    methods: {
      async getDictTypeInfo(dictId) {
        const { data: data } = await dictApi.getDictTypeEntity({
          id: Number(dictId),
        })
        this.queryForm.dictType = data.dictType
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.dictCode) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit({ dictType: this.queryForm.dictType })
        }
      },
      handleDelete(row) {
        if (row.dictCode) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await dictApi.deleteDictDataByIds({
              ids: [row.dictCode],
            })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.dictCode)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await dictApi.deleteDictDataByIds({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await dictApi.getDictDataList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
