<template>
    <div>
        <div style="width:600px;float: left; margin: 10px;">
            <el-input v-model="query" placeholder="请输入查询条件" style="width :300px;float: left;"></el-input>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button type="primary" @click="handleAdd">新增</el-button>
        </div>
        <el-table v-loading="loading" :data="tableData" stripe border style="width: 99%;">
            <el-table-column v-for="item in tableProperty" :key="item.id" :prop="item.tableProp" :label="item.tableLabel" :width="item.tableWidth" v-if="item.attr1==='0'">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                       :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalRows">
        </el-pagination>

        <el-dialog title="操作" :visible.sync="dialogVisible" width="500px">
            <el-form :model="sysDic" label-position="right" label-width="80px">
                <el-form-item label="ID">
                    <el-input v-model="sysDic.id" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="编码">
                    <el-input v-model="sysDic.dicCode" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="名称">
                    <el-input v-model="sysDic.dicName" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-input v-model="sysDic.dicType" placeholder=""></el-input>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="sysDic.orderBy" placeholder=""></el-input>
                </el-form-item>
    <el-form-item>
        <el-button type="success" @click="handleSave">保存</el-button>
        <el-button @click="handleCancelSave">取消</el-button>
    </el-form-item>
            </el-form>
        </el-dialog>


    </div>
</template>
<script>
    import axios from '@/axios';
    export default {
        data() {
            return {
                tableData: [],
                tableProperty: [],
                currentPage: 1,
                totalRows: 0,
                pageSizes: [10, 50, 100, 200],
                pageSize: 10,
                query:'',
        sysDic: {
                id: '',
                dicCode: '',
                dicName: '',
                dicType: '',
                orderBy: '',
                },
                loading: true,
                dialogVisible: false,
            }
        },
        methods: {
            //查询table数据
            getTableData() {
                var url = 'SysDic/query.do'
                var param = {
                    page: this.currentPage,
                    rows: this.pageSize,
                    dicType: this.query,
                }
                axios.get(url, param).then(response => {
                    if (response.data.code == '0') {
                    this.tableData = response.data.data.list
                    this.totalRows = response.data.data.total
                } else {
                    this.$message({
                        type: 'error',
                        message: '操作失败!' + response.data.msg
                    });
                }
                this.loading = false
            });
            },
            //查询table列
            getTableProperty() {
                //console.log("获取table数据getTableData")
                var url = 'CodeMachine/queryTableProperty.do'
                var param = {
                    bussinessName:'SYS_DIC'
                }
                axios.get(url, param).then(response => {
                    this.tableProperty = response.data
            });
            },

            // 修改每页行数
            handleSizeChange(val) {
                this.pageSize = val
                this.getTableData()
            },
            // 修改当前页事件
            handleCurrentChange(val) {
                this.currentPage = val
                this.getTableData()
            },
            //新增按钮事件
            handleAdd() {
                this.dialogVisible = true
                this.sysDic = {
            id: '',
            dicCode: '',
            dicName: '',
            dicType: '',
            orderBy: '',
                }
            },
            // 编辑按钮事件
            handleEdit(row) {
                this.dialogVisible = true
                this.sysDic = row

            },
            // 删除按钮事件
            handleDelete(id) {
                this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var url = 'SysDic/remove.do'
                    var param = {
                        id: id
                    }
             axios.post(url, param).then(response => {
                    if (response.data.code == '0') {
                    this.$message({
                        type: 'success',
                        message: '删除成功!',
                    });
                    this.getTableData()
                } else {
                    this.$message({
                        type: 'error',
                        message: '删除失败!' + response.data.msg
                    });

                }
            });
            }).catch(() => {
                    this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
            },
            //保存取消按钮事件   关闭编辑对话框
            handleCancelSave(row) {
                this.dialogVisible = false
            },
            //保存按钮事件
            handleSave(row) {
                var url = 'SysDic/save.do'
        var json = JSON.stringify(this.sysDic)
                var param = {
                    json: json
                }
                axios.post(url, param).then(response => {
                    if (response.data.code == '0') {
            this.$message({
                type: 'success',
                message: '操作成功!',
            });
            this.getTableData()
            this.dialogVisible = false
        } else {
            this.$message({
                type: 'error',
                message: '操作失败!' + response.data.msg
            });
        }
            });
            },

            // 查询按钮事件
            search() {
                this.getTableData()
            }
        },
        mounted() {
            this.getTableData()
            this.getTableProperty()
        }
    }
</script>
