<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row >
            <a-col :md="8" :sm="24" >
              <a-form-item
                label="字典类型"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.dictType"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" >
              <a-form-item
                label="字典key"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.dictKey"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24" >
              <a-form-item
                label="字典值"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.dictValue"/>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row v-if="advanced">
            <a-col :md="8" :sm="24" >
              <a-form-item
                label="字段"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 18, offset: 1}">
                <a-input v-model="queryParams.fieldName" />
              </a-form-item>
            </a-col>
          </a-row>
        </div>
        <span style="float: right; margin-top: 3px;">
          <a-button type="primary" @click="search">查询</a-button>
          <a-button style="margin-left: 8px" @click="reset">重置</a-button>
          <a @click="toggleAdvanced" style="margin-left: 8px">
            {{advanced ? '收起' : '展开'}}
            <a-icon :type="advanced ? 'up' : 'down'" />
          </a>
        </span>
      </a-form>
    </div>
    <div>
      <div class="operator">
        <a-button v-hasPermission="['platformDict:addItem']" type="primary" ghost @click="add">新增</a-button>
        <a-button v-hasPermission="['platformDict:batchDelete']" @click="batchDelete">删除</a-button>
        <!--<a-dropdown v-hasPermission="['dict:export']">
          <a-menu slot="overlay">
            <a-menu-item key="export-data" @click="exportExcel">导出Excel</a-menu-item>
          </a-menu>
          <a-button>
            更多操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>-->
      </div>
      <!-- 表格区域 -->
      <a-table
              ref="TableInfo"
              :bordered="true"
              :columns="columns"
              :dataSource="dataSource"
              :pagination="pagination"
              :loading="loading"
              :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
              @change="handleTableChange"
              :scroll="{ x: 1000 }"
              size="small">
        <template slot="remark" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div style="max-width: 200px">{{text}}</div>
            </template>
            <p style="width: 200px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="operation" slot-scope="text, record">
          <a-icon style="font-size: 20px" v-hasPermission="['platformDict:updateItem']" type="setting" theme="twoTone" twoToneColor="#4a9ff5" @click="edit(record)" title="修改系统业务字典"></a-icon>
          <a-badge style="font-size: 20px" v-hasNoPermission="['platformDict:updateItem']" status="warning" text="无权限"></a-badge>
        </template>
      </a-table>
    </div>
    <!-- 新增系统业务字典 -->
    <ItemAdd
      @close="handleDictAddClose"
      @success="handleDictAddSuccess"
      :dictAddVisible="dictAddVisible">
    </ItemAdd>
    <!-- 修改系统业务字典 -->
    <ItemEdit
      ref="ItemEdit"
      @close="handleDictEditClose"
      @success="handleDictEditSuccess"
      :dictEditVisible="dictEditVisible">
    </ItemEdit>
  </a-card>
</template>
<script>
  import ItemAdd from './ItemAdd'
  import ItemEdit from './ItemEdit'
  import baseUtils from '@/utils/baseUtils'
  export default {
    name: 'BusinessDictList',
    components: {
      ItemAdd:ItemAdd,
      ItemEdit:ItemEdit
    },
    data () {
      return {
        advanced: false,
        dataSource: [],
        selectedRowKeys: [],
        paginationInfo: null,
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        queryParams: {},
        dictAddVisible: false,
        dictEditVisible: false,
        loading: false
      }
    },
    computed: {
      columns () {
        return [
        {
          title: '字典类型',
          dataIndex: 'dictType',
          align:'center',
        },
        {
          title: '字典键',
          dataIndex: 'dictKey',
          align:'center',
        },
        {
          title: '字典值',
          dataIndex: 'dictValue',
          align:'center',
        },
        {
          title: '排序',
          dataIndex: 'sortIndex',
          align:'center',
        },
        {
          title: '备注',
          dataIndex: 'dictRemark',
          align:'center',
        },
          {
          title: '操作',
          dataIndex: 'operation',
          align:'center',
          scopedSlots: { customRender: 'operation' },
          fixed: 'right',
          width: 100
        }
        ]
      }
    },
    mounted () {
      this.fetch()
    },
    methods: {
      onSelectChange (selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
        if (!this.advanced) {
          this.queryParams.fieldName = ''
        }
      },
      handleDictAddSuccess () {
        this.dictAddVisible = false
        this.$message.success('新增系统业务字典成功')
        this.search()
      },
      handleDictAddClose () {
        this.dictAddVisible = false
      },
      add () {
        this.dictAddVisible = true
      },
      handleDictEditSuccess () {
        this.dictEditVisible = false
        this.$message.success('修改系统业务字典成功')
        this.search()
      },
      handleDictEditClose () {
        this.dictEditVisible = false
      },
      edit (record) {
        this.$refs.ItemEdit.setFormValues(record)
        this.dictEditVisible = true
      },
      batchDelete () {
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要删除的记录')
          return
        }
        let that = this
        this.$confirm({
          title: '确定删除所选中的记录?',
          content: '当您点击确定按钮后，这些记录将会被彻底删除',
          centered: true,
          onOk () {
            let deleteByMainId = []
            for (let key of that.selectedRowKeys) {
              deleteByMainId.push(that.dataSource[key].platformDictId)
            }
            that.$delete('api/v1/platformDict/batchDeleteItem/' + deleteByMainId.join(',')).then(() => {
              that.$message.success('删除成功')
              that.selectedRowKeys = []
              that.search()
            })
          },
          onCancel () {
            that.selectedRowKeys = []
          }
        })
      },
      exportExcel () {
        this.$exportData('dict/excel', {
          ...this.queryParams
        })
      },
      search () {
        this.fetch({
          ...this.queryParams
        })
      },
      reset () {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        this.paginationInfo = null
        // 重置查询参数
        this.queryParams = {}
        this.fetch()
      },
      handleTableChange (pagination, filters, sorter) {
        this.paginationInfo = pagination
        this.fetch({
          ...this.queryParams
        })
      },
      fetch (params = {}) {
        this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$postBodyRequest('api/v1/platformDict/queryByPage', {
          ...params
        }).then((r) => {
          let data = r.data.data
          const pagination = { ...this.pagination }
          pagination.total = baseUtils.isNull(data.total) ? 0 : data.total
          this.loading = false
          this.dataSource = data.rows
          this.pagination = pagination
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  /*webpack 会以~符号作为前缀的路径视为依赖模块去解析*/
  @import "~@static/less/Common";
</style>
