<template>
    <a-card class="j-address-list-right-card-box" :loading="cardLoading" :bordered="false">
        <div class="table-page-search-wrapper">
            <a-form-model layout="inline" :model="queryParam">
                <a-row :gutter="10">

                    <a-col :md="6" :sm="12">
                        <a-form-model-item label="名称" prop="itemText" style="margin-left:8px">
                            <j-input placeholder="请输入名称查询" v-model="queryParam.itemText"></j-input>
                        </a-form-model-item>
                    </a-col>

                    <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                        <a-col :md="6" :sm="24">
                         <a-button type="primary" @click="searchQuery" icon="search" class="margin-left-8">查询</a-button>
                          <a-button type="primary" @click="searchReset" icon="reload" class="margin-left-8">重置</a-button>
                            <a-button @click="handleAdd" type="primary" icon="plus" class="margin-left-8">新增</a-button>
                        </a-col>
                      </span>
                </a-row>
            </a-form-model>
        </div>

        <a-table
                ref="table"
                size="middle"
                bordered
                rowKey="userId"
                :pagination="ipagination"
                :columns="columns"
                :dataSource="dataSource"
                :loading="loading"
                @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        </a-table>
        <dict-item-modal ref="modalForm" @ok="modalFormOk"></dict-item-modal>
    </a-card>
</template>

<script>
    import { getAction } from '@/api/manage'
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    // import DictItemModal from "../../../system/modules/DictItemModal";
    import DictItemModal from "./dictItemModal";

    export default {
        name: 'dictionaryInfoRight',
        mixins: [JeecgListMixin],
        components: {DictItemModal},
        props: ['value'],
        data() {
            return {
                description: '用户信息',
                cardLoading: true,
                positionInfo: {},
                columns: [
                    {
                        title:'显示',
                        align:"center",
                        dataIndex: 'itemText'
                    },
                    {
                        title:'编码',
                        align:"center",
                        dataIndex: 'itemValue'
                    },
                    {
                        title:'排序',
                        align:"center",
                        dataIndex: 'sortOrder'
                    },
                    {
                        title:'备注',
                        align:"center",
                        dataIndex: 'description'
                    },
                    {
                        title: '操作',
                        dataIndex: 'action',
                        align:"center",
                        fixed:"right",
                        width:147,
                        scopedSlots: { customRender: 'action' }
                    }
                ],
                url: {
                    list: '/business/busDictItem/list',
                    delete: "/business/busDictItem/delete",
                    deleteBatch: "/business/busDictItem/deleteBatch",
                    exportXlsUrl: "/business/busDictItem/exportXls",
                    importExcelUrl: "business/busDictItem/importExcel",
                    // listByPosition: '/sys/position/list'
                },
                parentId:'',
            }
        },
        watch: {
            value: {
                immediate: true,
                handler(id) {
                    this.dataSource = []
                    this.loadData(1, id)
                }
            },
        },
        created() {
            // this.queryPositionInfo()
        },
        methods: {

            loadData(pageNum, dictId) {
                this.loading = true
                if (pageNum === 1) {
                    this.ipagination.current = 1
                }
                // console.log(dictId);
                // console.log(this.getQueryParams());
                var _dictId = localStorage.getItem("dictId")
                if (_dictId != undefined && _dictId != '' && (dictId == undefined || dictId == '')){
                    dictId = _dictId;
                }
                // update-begin- --- author:wangshuai ------ date:20200102 ---- for:传过来的部门编码为空全查
                if (!dictId) {
                    getAction(this.url.list, {
                        ...this.getQueryParams()
                    }).then((res) => {
                        if (res.success) {
                            this.dataSource = res.result.records
                            this.ipagination.total = res.result.total
                        }
                    }).finally(() => {
                        this.loading = false
                        this.cardLoading = false
                    })
                    // update-end- --- author:wangshuai ------ date:20200102 ---- for:传过来的部门编码为空全查
                }else{
                    //加载数据 若传入参数1则加载第一页的内容
                    getAction(this.url.list, {
                        dictId,
                        ...this.getQueryParams()
                    }).then((res) => {
                        if (res.success) {
                            this.dataSource = res.result.records
                            this.ipagination.total = res.result.total
                        }
                    }).finally(() => {
                        this.loading = false
                        this.cardLoading = false
                    })
                }
            },

            searchQuery() {
                this.loadData(1, this.value)
            },
            searchReset() {
                this.queryParam = {}
                this.loadData(1, this.value)
            },

            handleTableChange(pagination, filters, sorter) {
                if (Object.keys(sorter).length > 0) {
                    this.isorter.column = sorter.field
                    this.isorter.order = 'ascend' === sorter.order ? 'asc' : 'desc'
                }
                this.ipagination = pagination
                this.loadData(null, this.value)
            },

            // 查询职务信息
            queryPositionInfo() {
                getAction(this.url.listByPosition, { pageSize: 99999 }).then(res => {
                    if (res.success) {
                        let positionInfo = {}
                        res.result.records.forEach(record => {
                            positionInfo[record['code']] = record['name']
                        })
                        this.positionInfo = positionInfo
                    }
                })
            },
            // 添加字典数据
            handleAdd() {
                var dictId = localStorage.getItem("dictId")
                this.$refs.modalForm.add(dictId);
                this.$refs.modalForm.title = "新增";
            },
        }
    }
</script>
<style>
    .j-address-list-right-card-box .ant-table-placeholder {
        min-height: 46px;
    }
</style>
<style scoped>
    .j-address-list-right-card-box {
        height: 100%;
        min-height: 300px;
    }
    .margin-left-8{
        margin-left: 8px
    }
</style>
