<template>
  <div class="divBox">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="字典编码">
        <el-input v-model="dataForm.dictCode" placeholder="请输入字典编码" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典排序">
        <el-input v-model="dataForm.dictSort" placeholder="请输入字典排序" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典标签">
        <el-input v-model="dataForm.dictLabel" placeholder="请输入字典标签" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典键值">
        <el-input v-model="dataForm.dictValue" placeholder="请输入字典键值" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典类型">
        <el-input v-model="dataForm.dictType" placeholder="请输入字典类型" clearable></el-input>
      </el-form-item>
      <el-form-item label="样式属性（其他样式扩展）">
        <el-input v-model="dataForm.cssClass" placeholder="请输入样式属性（其他样式扩展）" clearable></el-input>
      </el-form-item>
      <el-form-item label="表格回显样式">
        <el-input v-model="dataForm.listClass" placeholder="请输入表格回显样式" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否默认（Y是 N否）">
        <el-input v-model="dataForm.isDefault" placeholder="请输入是否默认（Y是 N否）" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态（0正常 1停用）">
        <el-input v-model="dataForm.status" placeholder="请输入状态（0正常 1停用）" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="dataForm.remark" placeholder="请输入备注" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典代码">
        <el-input v-model="dataForm.dataCode" placeholder="请输入字典代码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="resetForm()">重置</el-button>
        <el-button v-hasPermi="['autogencode:sysdictdata:save']" type="primary" @click="addOrUpdateHandle()">新增数据</el-button>
        <el-button v-hasPermi="['autogencode:sysdictdata:delete']" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="dictCode"
        header-align="center"
        align="center"
        label="字典编码">
      </el-table-column>
      <el-table-column
        prop="dictSort"
        header-align="center"
        align="center"
        label="字典排序">
      </el-table-column>
      <el-table-column
        prop="dictLabel"
        header-align="center"
        align="center"
        label="字典标签">
      </el-table-column>
      <el-table-column
        prop="dictValue"
        header-align="center"
        align="center"
        label="字典键值">
      </el-table-column>
      <el-table-column
        prop="dictType"
        header-align="center"
        align="center"
        label="字典类型">
      </el-table-column>
      <el-table-column
        prop="cssClass"
        header-align="center"
        align="center"
        label="样式属性（其他样式扩展）">
      </el-table-column>
      <el-table-column
        prop="listClass"
        header-align="center"
        align="center"
        label="表格回显样式">
      </el-table-column>
      <el-table-column
        prop="isDefault"
        header-align="center"
        align="center"
        label="是否默认（Y是 N否）">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态（0正常 1停用）">
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="center"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateBy"
        header-align="center"
        align="center"
        label="更新者">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="dataCode"
        header-align="center"
        align="center"
        label="字典代码">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.dictCode)">{{ '修改' }}</el-button>
          <el-button v-hasPermi="['autogencode:sysdictdata:delete']" type="text" size="small" @click="deleteHandle(scope.row.dictCode)" style="color: #f56c6c;">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 表单弹窗, 新增数据和修改数据 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './sysdictdata-add-and-update'
  import * as api from '@/api/sysdictdata.js'
  export default {
    data () {
      return {
        dataForm: {
          dictCode: '',
          dictSort: '',
          dictLabel: '',
          dictValue: '',
          dictType: '',
          cssClass: '',
          listClass: '',
          isDefault: '',
          status: '',
          remark: '',
          dataCode: '',
        },
      // 重置表单
      resetForm() {
        this.dataForm = {
          dictCode: '',
          dictSort: '',
          dictLabel: '',
          dictValue: '',
          dictType: '',
          cssClass: '',
          listClass: '',
          isDefault: '',
          status: '',
          remark: '',
          dataCode: '',
        }
      },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        // 根据后端SysDictDataController和PageParamRequest的期望格式构建参数
        const params = {
          page: this.pageIndex,
          limit: this.pageSize
        }

        // 将查询条件添加到参数中
        if (this.dataForm.dictCode) params.dictCode = this.dataForm.dictCode;
        if (this.dataForm.dictSort) params.dictSort = this.dataForm.dictSort;
        if (this.dataForm.dictLabel) params.dictLabel = this.dataForm.dictLabel;
        if (this.dataForm.dictValue) params.dictValue = this.dataForm.dictValue;
        if (this.dataForm.dictType) params.dictType = this.dataForm.dictType;
        if (this.dataForm.cssClass) params.cssClass = this.dataForm.cssClass;
        if (this.dataForm.listClass) params.listClass = this.dataForm.listClass;
        if (this.dataForm.isDefault) params.isDefault = this.dataForm.isDefault;
        if (this.dataForm.status) params.status = this.dataForm.status;
        if (this.dataForm.remark) params.remark = this.dataForm.remark;
        if (this.dataForm.dataCode) params.dataCode = this.dataForm.dataCode;

        api.sysdictdataListApi(params).then(res => {
          this.dataListLoading = false
          this.dataList = res.list || [];
          this.totalPage = res.total;
        }).catch(e => {
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除处理（支持单个删除和批量删除）
      deleteHandle (id) {
        let ids = []
        let idText = ''

        // 如果传入了单个id参数，则为单个删除
        if (id) {
          ids = [id]
          idText = id
        } else {
          // 否则为批量删除
          ids = this.dataListSelections.map(item => {
            return item.dictCode
          })
          idText = ids.join(',')
        }

        this.$confirm(`确定进行删除操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 调用批量删除API
          api.sysdictdataDeleteApi(ids).then(res => {
            this.$message.success('删除成功')
            this.getDataList()
          })
        })
      },
    }
  }
</script>
